Skip to content

Commit a4e6955

Browse files
committed
linux: fix remount readonly in a user namespace
if we are remounting root read only when in a user namespace, make sure the existing flags (e.g. MS_NOEXEC, MS_NODEV) are maintained otherwise the mount fails with EPERM. Signed-off-by: Giuseppe Scrivano <[email protected]>
1 parent cf6c074 commit a4e6955

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

libcontainer/rootfs_linux.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -741,7 +741,19 @@ func prepareRoot(config *configs.Config) error {
741741
}
742742

743743
func setReadonly() error {
744-
return unix.Mount("/", "/", "bind", unix.MS_BIND|unix.MS_REMOUNT|unix.MS_RDONLY|unix.MS_REC, "")
744+
flags := uintptr(unix.MS_BIND | unix.MS_REMOUNT | unix.MS_RDONLY | unix.MS_REC)
745+
746+
err := unix.Mount("", "/", "", flags, "")
747+
if err == nil {
748+
return nil
749+
}
750+
var s unix.Statfs_t
751+
if err := unix.Statfs("/", &s); err != nil {
752+
return &os.PathError{Op: "statfs", Path: "/", Err: err}
753+
}
754+
flags |= uintptr(s.Flags)
755+
return unix.Mount("", "/", "", flags, "")
756+
745757
}
746758

747759
func setupPtmx(config *configs.Config) error {

0 commit comments

Comments
 (0)