Skip to content

Commit

Permalink
Fix tmpfs mode opts when dir already exists
Browse files Browse the repository at this point in the history
When a directory already exists (or after a container is restarted) the
perms of the directory being mounted to were being used even when a
different permission is set on the tmpfs mount options.

This prepends the original directory perms to the mount options.
If the perms were already set in the mount opts then those perms will
win.
This eliminates the need to perform a chmod after mount entirely.

Signed-off-by: Brian Goff <cpuguy83@gmail.com>
  • Loading branch information
cpuguy83 committed Jun 26, 2023
1 parent bcaee38 commit bec7e1a
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 13 deletions.
25 changes: 12 additions & 13 deletions libcontainer/rootfs_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -443,29 +443,28 @@ func mountToRootfs(c *mountConfig, m mountEntry) error {
}
return label.SetFileLabel(dest, mountLabel)
case "tmpfs":
stat, err := os.Stat(dest)
if err != nil {
if err := os.MkdirAll(dest, 0o755); err != nil {
var mode os.FileMode = 0o755
if stat, err := os.Stat(dest); err != nil {
if err := os.MkdirAll(dest, mode); err != nil {
return err
}
} else {
mode = stat.Mode()
}

dt := fmt.Sprintf("mode=%04o", mode)
if m.Data != "" {
dt = dt + "," + m.Data
}
m.Data = dt

if m.Extensions&configs.EXT_COPYUP == configs.EXT_COPYUP {
err = doTmpfsCopyUp(m, rootfs, mountLabel)
} else {
err = mountPropagate(m, rootfs, mountLabel)
}

if err != nil {
return err
}

if stat != nil {
if err = os.Chmod(dest, stat.Mode()); err != nil {
return err
}
}
return nil
return err
case "bind":
if err := prepareBindMount(m, rootfs); err != nil {
return err
Expand Down
34 changes: 34 additions & 0 deletions tests/integration/run.bats
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,37 @@ function teardown() {
[ "$status" -eq 0 ]
[[ "${lines[0]}" == *'mydomainname'* ]]
}

@test "runc run with tmpfs perms" {
# shellcheck disable=SC2016
update_config '.process.args = ["sh", "-c", "stat -c %a /tmp/test"]'
update_config '.mounts += [{"destination": "/tmp/test", "type": "tmpfs", "source": "tmpfs", "options": ["mode=0444"]}]'

# Directory is to be created by runc.
runc run test_tmpfs
[ "$status" -eq 0 ]
[ "$output" = "444" ]

# Run a 2nd time with the pre-existing directory.
# Ref: https://github.com/opencontainers/runc/issues/3911
runc run test_tmpfs
[ "$status" -eq 0 ]
[ "$output" = "444" ]

# Existing directory, custom perms, no mode on the mount,
# so it should use the directory's perms.
update_config '.mounts[-1].options = []'
chmod 0710 rootfs/tmp/test
# shellcheck disable=SC2016
runc run test_tmpfs
[ "$status" -eq 0 ]
[ "$output" = "710" ]

# Add back the mode on the mount, and it should use that instead.
# Just for fun, use different perms than was used earlier.
# shellcheck disable=SC2016
update_config '.mounts[-1].options = ["mode=0410"]'
runc run test_tmpfs
[ "$status" -eq 0 ]
[ "$output" = "410" ]
}

0 comments on commit bec7e1a

Please sign in to comment.