Skip to content

Commit 2cda498

Browse files
cclergetdtrudg
authored andcommitted
Fix insecure permissions
1 parent 1f48d05 commit 2cda498

File tree

6 files changed

+27
-9
lines changed

6 files changed

+27
-9
lines changed

cmd/internal/cli/singularity.go

+13-1
Original file line numberDiff line numberDiff line change
@@ -297,9 +297,21 @@ func handleRemoteConf(remoteConfFile string) {
297297
// handleConfDir tries to create the user's configuration directory and handles
298298
// messages and/or errors.
299299
func handleConfDir(confDir string) {
300-
if err := fs.Mkdir(confDir, os.ModePerm); err != nil {
300+
if err := fs.Mkdir(confDir, 0700); err != nil {
301301
if os.IsExist(err) {
302302
sylog.Debugf("%s already exists. Not creating.", confDir)
303+
fi, err := os.Stat(confDir)
304+
if err != nil {
305+
sylog.Fatalf("Failed to retrieve information for %s: %s", confDir, err)
306+
}
307+
if fi.Mode().Perm() != 0700 {
308+
sylog.Debugf("Enforce permission 0700 on %s", confDir)
309+
// enforce permission on user configuration directory
310+
if err := os.Chmod(confDir, 0700); err != nil {
311+
// best effort as chmod could fail for various reasons (eg: readonly FS)
312+
sylog.Warningf("Couldn't enforce permission 0700 on %s: %s", confDir, err)
313+
}
314+
}
303315
} else {
304316
sylog.Debugf("Could not create %s: %s", confDir, err)
305317
}

internal/app/singularity/oci_run_linux.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func OciRun(ctx context.Context, containerID string, args *OciArgs) error {
2525
if err != nil {
2626
return err
2727
}
28-
if err := os.MkdirAll(dir, 0755); err != nil {
28+
if err := os.MkdirAll(dir, 0700); err != nil {
2929
return err
3030
}
3131
args.SyncSocketPath = filepath.Join(dir, "run.sock")

internal/app/singularity/registry.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ func (l *Library) Pull(ctx context.Context, from, to, arch string) error {
122122
if dst != to {
123123
os.Remove(to)
124124
sylog.Debugf("Copying %s to %s", dst, to)
125-
if err := fs.CopyFile(dst, to, 0777); err != nil {
125+
if err := fs.CopyFile(dst, to, 0755); err != nil {
126126
return fmt.Errorf("cannot copy cache element %s to final destination %s: %w", dst, to, err)
127127
}
128128
}

internal/pkg/client/cache/dir.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -269,13 +269,19 @@ func updateCacheSubdir(c *Handle, subdir string) (string, error) {
269269
}
270270

271271
func initCacheDir(dir string) error {
272-
if _, err := os.Stat(dir); os.IsNotExist(err) {
272+
if fi, err := os.Stat(dir); os.IsNotExist(err) {
273273
sylog.Debugf("Creating cache directory: %s", dir)
274-
if err := fs.MkdirAll(dir, 0755); err != nil {
274+
if err := fs.MkdirAll(dir, 0700); err != nil {
275275
return fmt.Errorf("couldn't create cache directory %v: %v", dir, err)
276276
}
277277
} else if err != nil {
278278
return fmt.Errorf("unable to stat %s: %s", dir, err)
279+
} else if fi.Mode().Perm() != 0700 {
280+
// enforce permission on cache directory to prevent
281+
// potential information leak
282+
if err := os.Chmod(dir, 0700); err != nil {
283+
return fmt.Errorf("couldn't enforce permission 0700 on %s: %s", dir, err)
284+
}
279285
}
280286

281287
return nil

internal/pkg/instance/instance_linux.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ func (i *File) Update() error {
238238
oldumask := syscall.Umask(0)
239239
defer syscall.Umask(oldumask)
240240

241-
if err := os.MkdirAll(path, 0755); err != nil {
241+
if err := os.MkdirAll(path, 0700); err != nil {
242242
return err
243243
}
244244
file, err := os.OpenFile(i.Path, os.O_CREATE|os.O_TRUNC|os.O_WRONLY|syscall.O_NOFOLLOW, 0644)
@@ -267,10 +267,10 @@ func SetLogFile(name string, uid int, subDir string) (*os.File, *os.File, error)
267267
oldumask := syscall.Umask(0)
268268
defer syscall.Umask(oldumask)
269269

270-
if err := os.MkdirAll(filepath.Dir(stderrPath), 0755); err != nil {
270+
if err := os.MkdirAll(filepath.Dir(stderrPath), 0700); err != nil {
271271
return nil, nil, err
272272
}
273-
if err := os.MkdirAll(filepath.Dir(stdoutPath), 0755); err != nil {
273+
if err := os.MkdirAll(filepath.Dir(stdoutPath), 0700); err != nil {
274274
return nil, nil, err
275275
}
276276

internal/pkg/plugin/meta.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ func (m *Meta) config() (*os.File, error) {
112112
// install installs the plugin represented by m into the destination
113113
// directory. This should normally only be called in InstallFromSIF.
114114
func (m *Meta) install(dstdir string) error {
115-
if err := os.MkdirAll(m.Path, 0777); err != nil {
115+
if err := os.MkdirAll(m.Path, 0755); err != nil {
116116
return err
117117
}
118118

0 commit comments

Comments
 (0)