diff options
| author | Ophestra <cat@gensokyo.uk> | 2025-08-26 03:15:32 +0900 |
|---|---|---|
| committer | Ophestra <cat@gensokyo.uk> | 2025-08-26 03:27:07 +0900 |
| commit | 9bc8532d56fc5c1f727919d21f464dff09316913 (patch) | |
| tree | 1c933c8c4fec2c0516b3f70fe8d5bb75cc2259dc /container | |
| parent | 07194c74cb464cb073cabda33779c1782eaf0d74 (diff) | |
container/initdev: mount tmpfs on shm for ro dev
Programs expect /dev/shm to be a writable tmpfs.
Signed-off-by: Ophestra <cat@gensokyo.uk>
Diffstat (limited to 'container')
| -rw-r--r-- | container/container_test.go | 2 | ||||
| -rw-r--r-- | container/initdev.go | 11 | ||||
| -rw-r--r-- | container/initdev_test.go | 2 | ||||
| -rw-r--r-- | container/mount.go | 2 |
4 files changed, 14 insertions, 3 deletions
diff --git a/container/container_test.go b/container/container_test.go index 5dd510e6..90fba871 100644 --- a/container/container_test.go +++ b/container/container_test.go @@ -100,6 +100,7 @@ var containerTestCases = []struct { ent("/tty", "/dev/tty", "rw,nosuid", "devtmpfs", "devtmpfs", ignore), ent("/", "/dev/pts", "rw,nosuid,noexec,relatime", "devpts", "devpts", "rw,mode=620,ptmxmode=666"), ent("/", "/dev/mqueue", "rw,nosuid,nodev,noexec,relatime", "mqueue", "mqueue", "rw"), + ent("/", "/dev/shm", "rw,nosuid,nodev,relatime", "tmpfs", "tmpfs", ignore), ), 1971, 100, nil, 0, seccomp.PresetStrict}, @@ -116,6 +117,7 @@ var containerTestCases = []struct { ent("/urandom", "/dev/urandom", "rw,nosuid", "devtmpfs", "devtmpfs", ignore), ent("/tty", "/dev/tty", "rw,nosuid", "devtmpfs", "devtmpfs", ignore), ent("/", "/dev/pts", "rw,nosuid,noexec,relatime", "devpts", "devpts", "rw,mode=620,ptmxmode=666"), + ent("/", "/dev/shm", "rw,nosuid,nodev,relatime", "tmpfs", "tmpfs", ignore), ), 1971, 100, nil, 0, seccomp.PresetStrict}, diff --git a/container/initdev.go b/container/initdev.go index b3192912..43ef5247 100644 --- a/container/initdev.go +++ b/container/initdev.go @@ -72,8 +72,9 @@ func (d *MountDevOp) apply(state *setupState, k syscallDispatcher) error { } } + devShmPath := path.Join(target, "shm") devPtsPath := path.Join(target, "pts") - for _, name := range []string{path.Join(target, "shm"), devPtsPath} { + for _, name := range []string{devShmPath, devPtsPath} { if err := k.mkdir(name, state.ParentPerm); err != nil { return wrapErrSelf(err) } @@ -117,8 +118,12 @@ func (d *MountDevOp) apply(state *setupState, k syscallDispatcher) error { if d.Write { return nil } - return wrapErrSuffix(k.remount(target, MS_RDONLY), - fmt.Sprintf("cannot remount %q:", target)) + + if err := k.remount(target, MS_RDONLY); err != nil { + return wrapErrSuffix(k.remount(target, MS_RDONLY), + fmt.Sprintf("cannot remount %q:", target)) + } + return k.mountTmpfs(SourceTmpfs, devShmPath, MS_NOSUID|MS_NODEV, 0, 01777) } func (d *MountDevOp) Is(op Op) bool { diff --git a/container/initdev_test.go b/container/initdev_test.go index 9d2529c9..de756d0b 100644 --- a/container/initdev_test.go +++ b/container/initdev_test.go @@ -645,6 +645,7 @@ func TestMountDevOp(t *testing.T) { {"readlink", expectArgs{"/host/proc/self/fd/1"}, "/dev/pts/2", nil}, {"bindMount", expectArgs{"/host/dev/pts/2", "/sysroot/dev/console", uintptr(0), false}, nil, nil}, {"remount", expectArgs{"/sysroot/dev", uintptr(1)}, nil, nil}, + {"mountTmpfs", expectArgs{"tmpfs", "/sysroot/dev/shm", uintptr(0x6), 0, os.FileMode(01777)}, nil, nil}, }, nil}, {"success rw", &Params{ParentPerm: 0750, RetainSession: true}, &MountDevOp{ @@ -715,6 +716,7 @@ func TestMountDevOp(t *testing.T) { {"mkdir", expectArgs{"/sysroot/dev/mqueue", os.FileMode(0750)}, nil, nil}, {"mount", expectArgs{"mqueue", "/sysroot/dev/mqueue", "mqueue", uintptr(0xe), ""}, nil, nil}, {"remount", expectArgs{"/sysroot/dev", uintptr(1)}, nil, nil}, + {"mountTmpfs", expectArgs{"tmpfs", "/sysroot/dev/shm", uintptr(0x6), 0, os.FileMode(01777)}, nil, nil}, }, nil}, }) diff --git a/container/mount.go b/container/mount.go index 68011c7c..f7d4af31 100644 --- a/container/mount.go +++ b/container/mount.go @@ -43,6 +43,8 @@ const ( // Note that any source value is allowed when fstype is [FstypeOverlay]. SourceOverlay = "overlay" + // SourceTmpfs is used when mounting tmpfs. + SourceTmpfs = "tmpfs" // SourceTmpfsRootfs is used when mounting the tmpfs instance backing the intermediate root. SourceTmpfsRootfs = "rootfs" // SourceTmpfsDevtmpfs is used when mounting tmpfs representing a subset of host devtmpfs. |
