Skip to content
This repository has been archived by the owner on Dec 13, 2018. It is now read-only.

Commit

Permalink
Handle EBUSY on remount
Browse files Browse the repository at this point in the history
Docker-DCO-1.1-Signed-off-by: Michael Crosby <michael@crosbymichael.com> (github: crosbymichael)
  • Loading branch information
crosbymichael committed May 29, 2014
1 parent d660c31 commit d5d4b2b
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions security/restrict/restrict.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,33 @@ import (
"fmt"
"os"
"syscall"
"time"

"github.com/dotcloud/docker/pkg/system"
)

const defaultMountFlags = syscall.MS_NOEXEC | syscall.MS_NOSUID | syscall.MS_NODEV

func mountReadonly(path string) error {
if err := system.Mount("", path, "", syscall.MS_REMOUNT|syscall.MS_RDONLY, ""); err != nil {
if err == syscall.EINVAL {
// Probably not a mountpoint, use bind-mount
if err := system.Mount(path, path, "", syscall.MS_BIND, ""); err != nil {
for i := 0; i < 5; i++ {
if err := system.Mount("", path, "", syscall.MS_REMOUNT|syscall.MS_RDONLY, ""); err != nil {
switch err {
case syscall.EINVAL:
// Probably not a mountpoint, use bind-mount
if err := system.Mount(path, path, "", syscall.MS_BIND, ""); err != nil {
return err
}
return system.Mount(path, path, "", syscall.MS_BIND|syscall.MS_REMOUNT|syscall.MS_RDONLY|syscall.MS_REC|defaultMountFlags, "")
case syscall.EBUSY:
time.Sleep(100 * time.Millisecond)
continue
default:
return err
}
if err := system.Mount(path, path, "", syscall.MS_BIND|syscall.MS_REMOUNT|syscall.MS_RDONLY|syscall.MS_REC|defaultMountFlags, ""); err != nil {
return err
}
} else {
return err
}
return nil
}
return nil
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).
Expand All @@ -38,6 +44,7 @@ func Restrict(mounts ...string) error {
return fmt.Errorf("unable to remount %s readonly: %s", dest, 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)
}
Expand Down

0 comments on commit d5d4b2b

Please sign in to comment.