Commit Graph

26 Commits

Author SHA1 Message Date
lifubang 3661a9d4b3 integration: add some tests for bind mount through dangling symlinks
We intentionally broke this in commit d40b3439a9 ("rootfs: switch to
fd-based handling of mountpoint targets") under the assumption that most
users do not need this feature. Sadly it turns out they do, and so
commit 3f925525b4 ("rootfs: re-allow dangling symlinks in mount
targets") added a hotfix to re-add this functionality.

This patch adds some much-needed tests for this behaviour, since it
seems we are going to need to keep this for compatibility reasons (at
least until runc v2...).

Co-developed-by: lifubang <lifubang@acmcoder.com>
Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
(cherry picked from commit 15d7c214cd)
Signed-off-by: Aleksa Sarai <aleksa@amutable.com>
2026-06-12 23:52:47 +02:00
Aleksa Sarai daf9664eb4 rootfs: only set mode= for tmpfs mount if target already existed
This was always the intended behaviour but commit 72fbb34f50 ("rootfs:
switch to fd-based handling of mountpoint targets") regressed it when
adding a mechanism to create a file handle to the target if it didn't
already exist (causing the later stat to always succeed).

A lot of people depend on this functionality, so add some tests to make
sure we don't break it in the future.

Fixes: 72fbb34f50 ("rootfs: switch to fd-based handling of mountpoint targets")
Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
(cherry picked from commit 9a9719eeb4)
Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
2025-11-11 03:11:53 +11:00
Aleksa Sarai d8844e2939 tests: integration: add setgid mkdirall test
Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
2024-09-13 23:34:33 +10:00
Aleksa Sarai 457e1ffa4c tests: add regression test for CVE-2019-19921 / CVE-2023-27561
We reintroduced this once already because it is quite easy to miss this
subtle aspect of proc mounting. The recent migration to
securejoin.MkdirAllInRoot could have also inadvertently reintroduced
this (though it didn't).

Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
2024-09-13 17:57:42 +10:00
Aleksa Sarai fa93c8b05b tests: mounts: add some tests to check mount ordering
Our previous implementation of idmapped mounts and bind-mount sources
would open all of the source paths before we did any mounts, meaning
that mounts using sources from inside the container rootfs would not be
correct.

This has been fixed with the new on-demand system, and so add some
regression tests.

Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
2023-12-14 11:36:43 +11:00
Aleksa Sarai 884117471c tests: integration: fix spurious SC203[01] shellcheck errors
We had ignore lines for these warnings, but it turns out this is most
likely a bug in shellcheck and we can work around it by moving the
helper function definition before any of the functions that use the
helper function. Hopefully it'll be fixed soon.

Ref: https://github.com/koalaman/shellcheck/issues/2873
Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
2023-12-05 17:39:37 +11:00
Kir Kolyshkin 730bc84418 Fix directory perms vs umask for tmpcopyup
Bump fileutils to v0.5.1, which fixes permissions of newly created directories
to not depend on the value of umask.

Add a test case which fails like this before the fix:

	mounts.bats
	 ✗ runc run [tmpcopyup]
	   (in test file tests/integration/mounts.bats, line 28)
	     `[[ "${lines[0]}" == *'drwxrwxrwx'* ]]' failed
	   runc spec (status=0):

	   runc run test_busybox (status=0):
	   drwxr-xr-x    2 root     root            40 Oct  4 22:35 /dir1/dir2

Fixes 3991.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2023-10-04 15:35:44 -07:00
Kir Kolyshkin 370e3be202 tests/int/mounts: only check non-shadowed mounts
This fixes a bogus failure in "ro cgroup" test cases when running as
rootless.

The test finds the following mount that is not read-only:

> cgroup2 /sys/fs/cgroup/unified cgroup2 rw,nosuid,nodev,noexec,relatime,nsdelegate 0 0

This happens because:

1. runc spec --rootless adds an rbind /sys mounts, so we have all the
   /sys/fs/cgroup/XXX mounts inside the container;

2. Those /sys/fs/cgroup/XXX mounts are shadowed by the /sys/fs/cgroup
   tmpfs mount created by mountCgroupV1().

This means that this mount is shadowed, inaccessible, and it can not be
unshadowed, thus it should not be checked.

The fix is to check whether the directory exists, to exclude such
shadowed mounts.

NOTE that item 2 comes from commit ff692f289b60e19b3079cb; before it, we
had the whole hierarchy of host /sys/fs/cgroup visible (though not
writable -- because rootless) from inside of any rootless container.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2023-03-30 14:45:14 -07:00
Kir Kolyshkin a37109ce02 tests/int/mount: fix issues with ro cgroup test
Fix the following issues with the "ro /sys/fs/cgroup" test:

1. Disable bogus SC2016 warning from shellcheck.

2. Split the test into two -- with and without cgroupns. This is done
   because not all systems have cgroupns available (so the "+cgroupns"
   test will be skipped).

3. This splitting resulted in a few more bogus shellcheck warnings that
   we have to suppress -- due to a known bug in shellcheck (see [1]).

4. s/mount/mounts/ in the test name, because in case of cgroup v1 there
   are multiple mounts.

[1] https://github.com/koalaman/shellcheck/issues/2431

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2023-03-30 14:44:03 -07:00
Akihiro Suda df4eae457b rootless: fix /sys/fs/cgroup mounts
It was found that rootless runc makes `/sys/fs/cgroup` writable in following conditons:

1. when runc is executed inside the user namespace, and the config.json does not specify the cgroup namespace to be unshared
   (e.g.., `(docker|podman|nerdctl) run --cgroupns=host`, with Rootless Docker/Podman/nerdctl)
2. or, when runc is executed outside the user namespace, and `/sys` is mounted with `rbind, ro`
   (e.g., `runc spec --rootless`; this condition is very rare)

A container may gain the write access to user-owned cgroup hierarchy `/sys/fs/cgroup/user.slice/...` on the host.
Other users's cgroup hierarchies are not affected.

To fix the issue, this commit does:
1. Remount `/sys/fs/cgroup` to apply `MS_RDONLY` when it is being bind-mounted
2. Mask `/sys/fs/cgroup` when the bind source is unavailable

Fix CVE-2023-25809 (GHSA-m8cg-xc2p-r3fc)

Co-authored-by: Kir Kolyshkin <kolyshkin@gmail.com>
Signed-off-by: Akihiro Suda <akihiro.suda.cz@hco.ntt.co.jp>
2023-03-14 14:16:25 +09:00
Kir Kolyshkin b247cd392a runc run: fix ro /dev
Commit fb4c27c4b7 (went into v1.0.0-rc93) fixed a bug with
read-only tmpfs, but introduced a bug with read-only /dev.

This happens because /dev is a tmpfs mount and is therefore remounted
read-only a bit earlier than before.

To fix,

1. Revert the part of the above commit which remounts all tmpfs mounts
   as read-only in mountToRootfs.

2. Reuse finalizeRootfs (which is already used to remount /dev
   read-only) to also remount all ro tmpfs mounts that were previously
   mounted rw in mountPropagate.

3. Remove the break in finalizeRootfs, as now we have more than one
   mount to care about.

4. Reorder the if statements in finalizeRootfs to perform the fast check
   (for ro flag) first, and compare the strings second. Since /dev is
   most probably also a tmpfs mount, do the m.Device check first.

Add a test case to validate the fix and prevent future regressions;
make sure it fails before the fix:

 ✗ runc run [ro /dev mount]
   (in test file tests/integration/mounts.bats, line 45)
     `[ "$status" -eq 0 ]' failed
   runc spec (status=0):

   runc run test_busybox (status=1):
   time="2021-11-12T12:19:48-08:00" level=error msg="runc run failed: unable to start container process: error during container init: error mounting \"devpts\" to rootfs at \"/dev/pts\": mkdir /tmp/bats-run-VJXQk7/runc.0Fj70w/bundle/rootfs/dev/pts: read-only file system"

Fixes: fb4c27c4b7
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2021-11-15 10:37:16 -08:00
Kir Kolyshkin f252eb5436 test/int/mount.bats: refer to github issue
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2021-11-12 12:10:46 -08:00
Kir Kolyshkin 41670e21f0 tests/int: rework/simplify setup and teardown
1. Get rid of fixed ROOT, *_BUNDLE, and CONSOLE_SOCKET dirs.
   Now they are temporary directories created in setup_bundle.

2. Automate containers cleanup: instead of having to specify all
   containers to be removed, list and destroy everything (which is
   now possible since every test case has its own unique root).

3. Randomize cgroup paths so two tests running in parallel won't
   use the same cgroup.

Now it's theoretically possible to execute tests in parallel.
Practically it's not possible yet because bats uses GNU parallel,
which do not provide a terminal for whatever it executes, and
many runc tests (all those that run containers with terminal:
true) needs a tty. This may possibly be addressed later.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2021-02-09 20:05:54 -08:00
Kir Kolyshkin 637f82d6b8 runc run: resolve tmpfs mount dest in container scope
In case a tmpfs mount path contains absolute symlinks, runc errors out
because those symlinks are resolved in the host (rather than container)
filesystem scope.

The fix is similar to that for bind mounts -- resolve the destination
in container rootfs scope using securejoin, and use the resolved path.

A simple integration test case is added to prevent future regressions.

Fixes https://github.com/opencontainers/runc/issues/2683.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2021-01-06 14:57:02 -08:00
Kir Kolyshkin d64c3afe1b tests/int/mount.bats: reformat
Easier to read mounts. No functional change.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2021-01-06 14:57:02 -08:00
Kir Kolyshkin a2c9866e54 tests/int/mounts.bats: cleanup
1. Make sure that containers created are stopped and removed.
This is done by using a predefined test_busybox container name,
which is getting removed in teardown_busybox.

2. Fix space at EOL.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2021-01-06 14:57:02 -08:00
Akihiro Suda 7b8c4e980a shfmt mounts.bats to pass make validate
Signed-off-by: Akihiro Suda <akihiro.suda.cz@hco.ntt.co.jp>
2020-10-29 15:56:21 +09:00
Akihiro Suda 9d4c02cf29 Merge pull request #2570 from EduardoVega/2246-fix-chmod-ro-tmpfs-mount
Fix mount error when chmod RO tmpfs
2020-10-26 18:19:16 +09:00
Kir Kolyshkin fc8c779704 tests/integration/*.bats: reformat with shfmt
Various bats tests use various types of indentation (about half
is tabs, the rest is 2 spaces, 4 spaces, etc.).

Let's bring it to one style (tabs) using recently added
shfmt support for bats files (see [1]).

This commit is brought to you by

	shfmt -ln bats -w tests/integration/*.bats

[1] https://github.com/mvdan/sh/issues/600

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2020-10-11 19:15:36 -07:00
Eduardo Vega fb4c27c4b7 Fix mount error when chmod RO tmpfs
Signed-off-by: Eduardo Vega <edvegavalerio@gmail.com>
2020-10-05 21:23:30 -06:00
Kir Kolyshkin 186a38ba73 tests/int: whitespace cleanup
Fix
 - whitespace at EOL
 - empty lines at EOF

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2020-09-21 02:47:23 -07:00
Kir Kolyshkin 908b7076e9 tests/int/*.bats: make sure to delete containers
With some grep|awk help, found a few places where the containers
supposed to be removed in teardown() weren't.

To find container names:

	grep -E '^[[:space:]]*\<_*_*runc\>.*\<(create|run|restore)\>'

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2020-09-21 02:47:23 -07:00
Kir Kolyshkin 699fdf8952 tests/int/mount.bats: fix a check
It's not a regex but a substring, so use a substring match.

Fixes the following warning by shellcheck:

> In mounts.bats line 20:
> 	[[ "${lines[0]}" =~ '/tmp/bind/config.json' ]]
>                           ^---------------------^ SC2076: Don't quote right-hand side of =~, it'll match literally rather than as a regex.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2020-08-10 07:41:49 -07:00
John Hwang 79fe41d3c1 Replace sed with jq for more readable json manipulation in tests
Signed-off-by: John Hwang <John.F.Hwang@gmail.com>
2020-06-03 18:35:47 -07:00
Odin Ugedal df583b4c51 Fix file permissions for mounts.bats
Signed-off-by: Odin Ugedal <odin@ugedal.com>
2020-03-07 09:29:33 +01:00
W. Trevor King 0aa6e4e5d3 libcontainer/specconv/spec_linux: Support empty 'type' for bind mounts
From the "Creating a bind mount" section of mount(2) [1]:

> If mountflags includes MS_BIND (available since Linux 2.4), then
> perform a bind mount...
>
> The filesystemtype and data arguments are ignored.

This commit adds support for configurations that leave the OPTIONAL
type [2] unset for bind mounts.  There's a related spec-example change
in flight with [3], although my personal preference would be a more
explicit spec for the whole mount structure [4].

[1]: http://man7.org/linux/man-pages/man2/mount.2.html
[2]: https://github.com/opencontainers/runtime-spec/blame/v1.0.1/config.md#L102
[3]: https://github.com/opencontainers/runtime-spec/pull/954
[4]: https://github.com/opencontainers/runtime-spec/pull/771

Signed-off-by: W. Trevor King <wking@tremily.us>
2018-03-07 10:23:42 -08:00