Merge pull request #24 from crosbymichael/is-not-exist-errors
Ignore isnotexist errors for restrict paths
This commit is contained in:
commit
2a9ff02bee
|
@ -15,13 +15,14 @@ const defaultMountFlags = syscall.MS_NOEXEC | syscall.MS_NOSUID | syscall.MS_NOD
|
||||||
|
|
||||||
func mountReadonly(path string) error {
|
func mountReadonly(path string) error {
|
||||||
for i := 0; i < 5; i++ {
|
for i := 0; i < 5; i++ {
|
||||||
if err := system.Mount("", path, "", syscall.MS_REMOUNT|syscall.MS_RDONLY, ""); err != nil {
|
if err := system.Mount("", path, "", syscall.MS_REMOUNT|syscall.MS_RDONLY, ""); err != nil && !os.IsNotExist(err) {
|
||||||
switch err {
|
switch err {
|
||||||
case syscall.EINVAL:
|
case syscall.EINVAL:
|
||||||
// Probably not a mountpoint, use bind-mount
|
// Probably not a mountpoint, use bind-mount
|
||||||
if err := system.Mount(path, path, "", syscall.MS_BIND, ""); err != nil {
|
if err := system.Mount(path, path, "", syscall.MS_BIND, ""); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return system.Mount(path, path, "", syscall.MS_BIND|syscall.MS_REMOUNT|syscall.MS_RDONLY|syscall.MS_REC|defaultMountFlags, "")
|
return system.Mount(path, path, "", syscall.MS_BIND|syscall.MS_REMOUNT|syscall.MS_RDONLY|syscall.MS_REC|defaultMountFlags, "")
|
||||||
case syscall.EBUSY:
|
case syscall.EBUSY:
|
||||||
time.Sleep(100 * time.Millisecond)
|
time.Sleep(100 * time.Millisecond)
|
||||||
|
@ -30,15 +31,16 @@ func mountReadonly(path string) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return fmt.Errorf("unable to mount %s as readonly max retries reached", path)
|
return fmt.Errorf("unable to mount %s as readonly max retries reached", path)
|
||||||
}
|
}
|
||||||
|
|
||||||
// This has to be called while the container still has CAP_SYS_ADMIN (to be able to perform mounts).
|
// This has to be called while the container still has CAP_SYS_ADMIN (to be able to perform mounts).
|
||||||
// However, afterwards, CAP_SYS_ADMIN should be dropped (otherwise the user will be able to revert those changes).
|
// However, afterwards, CAP_SYS_ADMIN should be dropped (otherwise the user will be able to revert those changes).
|
||||||
func Restrict(mounts ...string) error {
|
func Restrict(mounts ...string) error {
|
||||||
// remount proc and sys as readonly
|
|
||||||
for _, dest := range mounts {
|
for _, dest := range mounts {
|
||||||
if err := mountReadonly(dest); err != nil {
|
if err := mountReadonly(dest); err != nil {
|
||||||
return fmt.Errorf("unable to remount %s readonly: %s", dest, err)
|
return fmt.Errorf("unable to remount %s readonly: %s", dest, err)
|
||||||
|
@ -48,5 +50,6 @@ func Restrict(mounts ...string) error {
|
||||||
if err := system.Mount("/dev/null", "/proc/kcore", "", syscall.MS_BIND, ""); err != nil && !os.IsNotExist(err) {
|
if err := system.Mount("/dev/null", "/proc/kcore", "", syscall.MS_BIND, ""); err != nil && !os.IsNotExist(err) {
|
||||||
return fmt.Errorf("unable to bind-mount /dev/null over /proc/kcore: %s", err)
|
return fmt.Errorf("unable to bind-mount /dev/null over /proc/kcore: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue