Skip to content

Commit 874953d

Browse files
author
Michael Crosby
committed
Ignore isnotexist errors for restrict paths
Handle the error when remounting certain files and paths as readonly if they do not exist. Docker-DCO-1.1-Signed-off-by: Michael Crosby <[email protected]> (github: crosbymichael)
1 parent 4145356 commit 874953d

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

security/restrict/restrict.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@ const defaultMountFlags = syscall.MS_NOEXEC | syscall.MS_NOSUID | syscall.MS_NOD
1515

1616
func mountReadonly(path string) error {
1717
for i := 0; i < 5; i++ {
18-
if err := system.Mount("", path, "", syscall.MS_REMOUNT|syscall.MS_RDONLY, ""); err != nil {
18+
if err := system.Mount("", path, "", syscall.MS_REMOUNT|syscall.MS_RDONLY, ""); err != nil && !os.IsNotExist(err) {
1919
switch err {
2020
case syscall.EINVAL:
2121
// Probably not a mountpoint, use bind-mount
2222
if err := system.Mount(path, path, "", syscall.MS_BIND, ""); err != nil {
2323
return err
2424
}
25+
2526
return system.Mount(path, path, "", syscall.MS_BIND|syscall.MS_REMOUNT|syscall.MS_RDONLY|syscall.MS_REC|defaultMountFlags, "")
2627
case syscall.EBUSY:
2728
time.Sleep(100 * time.Millisecond)
@@ -30,15 +31,16 @@ func mountReadonly(path string) error {
3031
return err
3132
}
3233
}
34+
3335
return nil
3436
}
37+
3538
return fmt.Errorf("unable to mount %s as readonly max retries reached", path)
3639
}
3740

3841
// This has to be called while the container still has CAP_SYS_ADMIN (to be able to perform mounts).
3942
// However, afterwards, CAP_SYS_ADMIN should be dropped (otherwise the user will be able to revert those changes).
4043
func Restrict(mounts ...string) error {
41-
// remount proc and sys as readonly
4244
for _, dest := range mounts {
4345
if err := mountReadonly(dest); err != nil {
4446
return fmt.Errorf("unable to remount %s readonly: %s", dest, err)
@@ -48,5 +50,6 @@ func Restrict(mounts ...string) error {
4850
if err := system.Mount("/dev/null", "/proc/kcore", "", syscall.MS_BIND, ""); err != nil && !os.IsNotExist(err) {
4951
return fmt.Errorf("unable to bind-mount /dev/null over /proc/kcore: %s", err)
5052
}
53+
5154
return nil
5255
}

0 commit comments

Comments
 (0)