From ac1a703731cf517ae87dde1d02d642bb3057af8e Mon Sep 17 00:00:00 2001 From: Lars Seipel Date: Thu, 23 Dec 2021 17:37:27 +0100 Subject: [PATCH] Fix possible nil pointer derefence in fs_util.go (#1813) When os.Stat returns an error different from ErrNotExist, mkdirAllWithPermissions may panic with a nil pointer derefence due to insufficient error checking. Avoid the panic by bailing out, returning the error to the caller. --- pkg/util/fs_util.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pkg/util/fs_util.go b/pkg/util/fs_util.go index 7b3110b3d..2158c6daa 100644 --- a/pkg/util/fs_util.go +++ b/pkg/util/fs_util.go @@ -777,12 +777,15 @@ func Volumes() []string { func mkdirAllWithPermissions(path string, mode os.FileMode, uid, gid int64) error { // Check if a file already exists on the path, if yes then delete it info, err := os.Stat(path) - if !os.IsNotExist(err) && !info.IsDir() { + if err == nil && !info.IsDir() { logrus.Tracef("removing file because it needs to be a directory %s", path) if err := os.Remove(path); err != nil { return errors.Wrapf(err, "error removing %s to make way for new directory.", path) } } + if err != nil && !os.IsNotExist(err) { + return errors.Wrapf(err, "error calling stat on %s.", path) + } if err := os.MkdirAll(path, mode); err != nil { return err