Commit Graph

7823 Commits

Author SHA1 Message Date
lifubang 0d9be8021f libct: close rootFd ASAP in maskPaths
Close the root file descriptor immediately after use in maskPaths to
reduce the window during which an attacker could potentially exploit
an open fd to access or manipulate the root filesystem. This follows
the principle of least privilege and mitigates risks in compromised
or malicious container scenarios.

Co-authored-by: Kir Kolyshkin <kolyshkin@gmail.com>
Signed-off-by: lifubang <lifubang@acmcoder.com>
(cherry picked from commit b88635e57e)
Signed-off-by: lifubang <lifubang@acmcoder.com>
2026-05-20 10:21:49 +00:00
lifubang 81d66c66f9 libct: optimize maskPaths for single-directory case
This is a follow-up to #5275. That change reused a single tmpfs mount
to mask multiple directories, which is efficient when masking more than
one path. However, it introduced unnecessary overhead when only one
directory is masked. This commit restores the original behavior for the
single-path case while preserving shared tmpfs logic for multiple paths.

Signed-off-by: lifubang <lifubang@acmcoder.com>
(cherry picked from commit e7e2f00248)
Signed-off-by: lifubang <lifubang@acmcoder.com>
2026-05-20 10:21:42 +00:00
lifubang e454170167 integration: reuse tmpfs for directory masks
Co-authored-by: Davanum Srinivas <davanum@gmail.com>
Signed-off-by: lifubang <lifubang@acmcoder.com>
(cherry picked from commit 124772f354)
Signed-off-by: lifubang <lifubang@acmcoder.com>
2026-05-20 10:21:37 +00:00
lifubang 8dbcab66d1 libct: reuse tmpfs for directory masks
Kubernetes may add one sysfs thermal_throttle entry per CPU to
maskedPaths. On large Intel systems this can produce many directory
masks for a single container. runc currently handles each directory
mask with a separate read-only tmpfs mount, and therefore a separate
tmpfs superblock.

On Linux 4.18/RHEL 8 kernels, creating and tearing down many tmpfs
superblocks can contend on the global shrinker_rwsem when containers
start or stop concurrently.

Use one read-only tmpfs for directory masks and bind-mount it over the
remaining directory targets. The first non-procfs-fd directory mount is
reopened through the container root fd before it is reused. File masks
still bind /dev/null, and procfs fd targets keep the existing
one-tmpfs-per-target behaviour because they are fd aliases rather than
stable rootfs paths.

If the bind-mount of the shared source fails (e.g. due to kernel
restrictions), fall back to individual tmpfs mounts for all remaining
directories. Tmpfs mounts use nr_blocks=1,nr_inodes=1 to minimise
kernel resource usage.

The bind mounts do not create additional tmpfs superblocks. They also
retain the read-only mount flag inherited from the source vfsmount, so
the masking semantics remain unchanged.

xref: kubernetes/kubernetes#138512
xref: kubernetes/kubernetes#138388
xref: kubernetes/kubernetes#131018

Co-authored-by: Davanum Srinivas <davanum@gmail.com>
Refactored-by: lifubang <lifubang@acmcoder.com>
Signed-off-by: lifubang <lifubang@acmcoder.com>
(cherry picked from commit c046c9b973)
Signed-off-by: lifubang <lifubang@acmcoder.com>
2026-05-20 10:21:37 +00:00
lifubang abc399150b libct: enforce strict tmpfs limits for masked paths
Previously, masked directories (e.g., /proc/acpi, /proc/scsi) were
mounted as read-only tmpfs without explicit size or inode limits.
Although these mounts are meant to be empty and unwritable, the lack
of resource constraints means that—should an attacker bypass the
read-only protection (e.g., via container escape, mount namespace
manipulation, or a kernel vulnerability)—the tmpfs could consume up
to 50% of system memory by default (the kernel's default tmpfs limit).

To mitigate this risk in high-density container environments and
adhere to the principle of least privilege, we now explicitly set:
  - nr_blocks=1 (sufficient for at most one block size)
  - nr_inodes=1 (sufficient for at most one inode)
Ref: https://man7.org/linux/man-pages/man5/tmpfs.5.html

These limits ensure that even if compromised, kernel memory usage
remains strictly bounded and negligible.

This change aligns with best practices used by other container
runtimes and strengthens defense-in-depth for sensitive masked paths.

Co-authored-by: Davanum Srinivas <davanum@gmail.com>
Refactored-by: lifubang <lifubang@acmcoder.com>
Signed-off-by: lifubang <lifubang@acmcoder.com>
(cherry picked from commit e57a7a4c8f)
Signed-off-by: lifubang <lifubang@acmcoder.com>
2026-05-20 10:21:37 +00:00
lifubang b9e7a27a01 libct: skip mount for duplicate masked paths
Co-authored-by: Davanum Srinivas <davanum@gmail.com>
Refactored-by: lifubang <lifubang@acmcoder.com>
Signed-off-by: lifubang <lifubang@acmcoder.com>
(cherry picked from commit abf70bab63)
Signed-off-by: lifubang <lifubang@acmcoder.com>
2026-05-20 10:21:37 +00:00
lfbzhm 522b4c804e libct: use preopened rootfs more
This uses preopened rootfs in Chdir and pivotRoot.

While at it, add O_PATH when opening oldroot in pivotRoot.

Co-authored-by: Kir Kolyshkin <kolyshkin@gmail.com>
Signed-off-by: lfbzhm <lifubang@acmcoder.com>
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
(cherry picked from commit 5b094ed1ac)
Signed-off-by: lifubang <lifubang@acmcoder.com>
2026-05-20 10:21:37 +00:00
Kir Kolyshkin aa769e7dcc Pre-open container root directory
A lot of filesystem-related stuff happens inside the container root
directory, and we have used its name before. It makes sense to pre-open
it and use a *os.File handle instead.

Function names in internal/pathrs are kept as is for simplicity (and it
is an internal package), but they now accept root as *os.File.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
(cherry picked from commit 28cb321887)
Signed-off-by: lifubang <lifubang@acmcoder.com>
2026-05-20 10:21:37 +00:00
Kir Kolyshkin 63857e5d20 libct: minor refactor in mountToRootfs
No change in functionality, just a preparation for the next patch.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
(cherry picked from commit 78b80677f6)
Signed-off-by: lifubang <lifubang@acmcoder.com>
2026-05-20 10:21:37 +00:00
Kir Kolyshkin a4afb37ba7 libct: mountCgroupV1: address TODO
Indeed, it does not make sense to prepend c.root once we started using
MkdirAllInRoot in commit 63c29081.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
(cherry picked from commit 60352524d3)
Signed-off-by: lifubang <lifubang@acmcoder.com>
2026-05-20 10:21:37 +00:00
lfbzhm 4d5dc51e5d Merge pull request #5249 from AkihiroSuda/cherrypick-5239-1.4
[release-1.4] Complete migration from Cirrus CI to GHA (Lima)
2026-04-19 08:51:49 +08:00
Akihiro Suda 830d041dee CI: lima: add template name to cache key
The cache created for almalinux-8 could be overwritten for almalinux-9

Signed-off-by: Akihiro Suda <akihiro.suda.cz@hco.ntt.co.jp>
(cherry picked from commit ff4470156e)
Signed-off-by: Akihiro Suda <akihiro.suda.cz@hco.ntt.co.jp>
2026-04-17 17:39:14 +09:00
Akihiro Suda fc8b6a01df Complete migration from Cirrus CI to GHA (Lima)
Fix issue 5238

Signed-off-by: Akihiro Suda <akihiro.suda.cz@hco.ntt.co.jp>
(cherry picked from commit 9d697a9222)
(cherry-pick was not clean)
Signed-off-by: Akihiro Suda <akihiro.suda.cz@hco.ntt.co.jp>
2026-04-16 22:22:50 +09:00
Rodrigo Campos Catelin f6d51d52e1 Merge pull request #5237 from kolyshkin/1.4-5222
[1.4] tests/int/checkpoint: drop unneeded tests
2026-04-13 11:15:36 +02:00
Kir Kolyshkin d3a0ee7c5b tests/int/checkpoint: drop unneeded tests
Those tests were added by commit 8d180e96 ("Add support for Linux
Network Devices"), apparently by copy-pasting the test cases which
call simple_cr (all four of them).

While different simple_cr tests make sense as they cover different
code paths in runc and/or check for various regression, the same
variations with netdevice do not make sense, as having a net device
is orthogonal to e.g. bind mount, --debug, or cgroupns.

Remove those.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
(cherry picked from commit 2cd4782b70)
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2026-04-10 14:43:55 -07:00
Kir Kolyshkin d40e1ad2cf tests/int/checkpoint: fix using run twice
In our bats tests, runc itself is a wrapper which calls bats run helper,
so using "run runc" is wrong as it results in calling run helper twice.

Fixes: 8d180e965
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
(cherry picked from commit ad72eab6c7)
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2026-04-10 14:43:20 -07:00
Kir Kolyshkin a5c14847f4 tests/int: remove useless/obvious comments
This is a bit opinionated, but some comments in integration tests do not
really help to understand the nature of the tests being performed by
stating something very obvious, like

	# run busybox detached
	runc run -d busybox

To make things worse, these not-so-helpful messages are being
copy/pasted over and over, and that is the main reason to remove them.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
(cherry picked from commit 0eb03ef86f)
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2026-04-10 14:42:11 -07:00
lfbzhm 49e6be637f Merge pull request #5229 from kolyshkin/1.4-5226
[1.4] tests/rootless.sh: fix skipping idmap tests for systemd
2026-04-09 09:34:54 +08:00
Kir Kolyshkin ceb8e42641 tests/rootless.sh: fix skipping idmap tests for systemd
When RUNC_USE_SYSTEMD is set, tests/rootless.sh is using

	ssh -tt rootless@localhost

to run tests as rootless user. In this case, local environment is not
passed to the user's ssh session (unless explicitly specified), and so
the tests do not get ROOTLESS_FEATURES.

As a result, idmap-related tests are skipped when running as rootless
using systemd cgroup driver:

	integration test (systemd driver)
	...
	[02] run rootless tests ... (idmap)
	...
	ok 286 runc run detached ({u,g}id != 0) # skip test requires rootless_idmap
	...

Fix this by creating a list of environment variables needed by the
tests, and adding those to ssh command line (in case of ssh) or
exporting (in case of sudo) so both cases work similarly.

Also, modify disable_idmap to unset variables set in enable_idmap so
they are not exported at all if idmap is not in features.

Fixes: bf15cc99 ("cgroup v2: support rootless systemd")
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
(cherry picked from commit 3e0829d195)
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2026-04-08 10:59:44 -07:00
Kir Kolyshkin 59dd4cdee9 tests: rename AUX_{DIR,UID} to ROOTLESS_AUX_*
Also, fix the typo (AUX_DIX) in cleanup.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
(cherry picked from commit ac2a53be8e)
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2026-04-08 10:59:44 -07:00
Kir Kolyshkin a9669107ff Merge pull request #5217 from kolyshkin/1.4.2
Release 1.4.2
2026-04-02 17:02:19 -07:00
Kir Kolyshkin 814f276bb0 VERSION: back to development
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2026-04-02 15:52:51 -07:00
Kir Kolyshkin c241c0bb5e VERSION: release v1.4.2
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
v1.4.2
2026-04-02 15:52:45 -07:00
Rodrigo Campos Catelin 95f27e8053 Merge pull request #5216 from lifubang/backport-5210-1.4
[1.4] Fix SIGCHLD race in signal handler setup
2026-04-02 12:20:05 +02:00
lifubang 39791aeab6 Fix SIGCHLD race in signal handler setup
When signal installation was moved to a goroutine for performance,
containers that exited quickly could complete before SIGCHLD was
registered, causing runc to hang waiting for the signal.

This fix ensures SIGCHLD is registered immediately in the main thread
before other signals are handled in the goroutine, maintaining performance
while guaranteeing no missed SIGCHLD notifications for fast-exiting
containers.

Reported-by: Ayato Tokubi <atokubi@redhat.com>
Signed-off-by: lifubang <lifubang@acmcoder.com>
(cherry picked from commit 404181e4cc)
Signed-off-by: lifubang <lifubang@acmcoder.com>
2026-04-02 01:11:45 +00:00
Aleksa Sarai 226ff030b4 Merge pull request #5201 from lifubang/backport-5177-1.4
[1.4] libct: close the mount source fd ASAP!
2026-03-29 19:56:11 +11:00
lifubang 9de77a986c test: check mount source fds are cleaned up with idmapped mounts
Signed-off-by: lifubang <lifubang@acmcoder.com>
(cherry picked from commit 7fdab1cb69)
Signed-off-by: lifubang <lifubang@acmcoder.com>
2026-03-28 15:16:13 +00:00
lifubang e4a82fc2d8 libct: close mount source fd as soon as possible
This commit factors out setupAndMountToRootfs without changing any
logic. Use "Hide whitespace changes" during review to focus on the
actual changes.

The refactor ensures the mount source file descriptor is closed via
defer in each loop iteration, reducing the total number of open FDs
in runc. This helps avoid hitting the file descriptor limit under
high concurrency or when handling many mounts.

Signed-off-by: lifubang <lifubang@acmcoder.com>
(cherry picked from commit c77e71a3e7)
Signed-off-by: lifubang <lifubang@acmcoder.com>
2026-03-28 15:16:06 +00:00
lifubang 87db63422d libct: add a nil check for mountError
Signed-off-by: lifubang <lifubang@acmcoder.com>
(cherry picked from commit 0d0fd95731)
Signed-off-by: lifubang <lifubang@acmcoder.com>
2026-03-28 15:15:56 +00:00
Rodrigo Campos Catelin d4305dc5dd Merge pull request #5187 from kolyshkin/1.4-5159
[1.4] ci: add conmon tests run
2026-03-20 13:56:48 +01:00
Kir Kolyshkin 63605fc49f ci: add conmon tests run
This adds a CI job to run conmon tests with runc.

Related to issue 5151, PR 5153.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
(cherry picked from commit 192e3d416f)
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2026-03-19 16:55:08 -07:00
Aleksa Sarai 0daa0038d2 Merge pull request #5178 from kolyshkin/1.4-5175
[1.4] CHANGELOG: add "better errors from runc init" to 1.4.0
2026-03-17 10:25:02 +09:00
Kir Kolyshkin c10ee7d4b6 CHANGELOG: add "better errors from runc init" to 1.4.0
Found out that these changes were backported to release-1.4 (PR 5040)
and made its way into runc v1.4.0, but were missing from its CHANGELOG.

Add the item to v1.4.0 changelog.

Same as commit e232a54 in main branch.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2026-03-16 10:42:29 -07:00
Kir Kolyshkin d13808e9d2 Merge pull request #5163 from kolyshkin/v141
VERSION: release 1.4.1
2026-03-12 21:56:37 -07:00
Kir Kolyshkin 6096163226 VERSION: back to development
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2026-03-12 13:27:50 -07:00
Kir Kolyshkin c671325303 VERSION: release v1.4.1
Note a few minor PRs were not mentioned: #5134, #5094, #5074.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
v1.4.1
2026-03-12 13:23:25 -07:00
Kir Kolyshkin 1a6e2e695f Merge pull request #5167 from cyphar/1.4-libpathrs-cherry-pick
[1.4] *: libpathrs cherry-picks
2026-03-12 12:42:18 -07:00
Kir Kolyshkin 485f8f6b8f Merge pull request #5168 from cyphar/1.4-5009-close-fd-on-error
[1.4] Close fds on error
2026-03-12 12:38:05 -07:00
Kir Kolyshkin 5dac7379e7 [1.4] notify_socket: close fds on error
Reported in issue 5008.

Reported-by: Arina Cherednik <arinacherednik034@gmail.com>
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
(cherry picked from commit 93792e6c13)
Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
2026-03-12 19:15:37 +09:00
Kir Kolyshkin d934c6dd9e [1.4] libct: mountFd: close mountFile on error
Reported in issue 5008.

Reported-by: Arina Cherednik <arinacherednik034@gmail.com>
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
(cherry picked from commit 8a9b4dcda6)
Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
2026-03-12 19:15:36 +09:00
Kir Kolyshkin 0af85cbca8 [1.4] libct: newProcessComm: close fds on error
Reported in issue 5008.

Reported-by: Arina Cherednik <arinacherednik034@gmail.com>
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
(cherry picked from commit c24965b742)
Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
2026-03-12 19:15:35 +09:00
Kir Kolyshkin ffc6092b8d [1.4] libct: startInitialization: add defer close
This function calls Init what normally never returns, so the defer only
works if there is an error and we can safely use it to close those fds
we opened. This was done for most but not all fds.

Reported in issue 5008.

Reported-by: Arina Cherednik <arinacherednik034@gmail.com>
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
(cherry picked from commit 88f897160c)
Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
2026-03-12 19:15:28 +09:00
Aleksa Sarai 4fb2e1ceba [1.4] deps: update to cyphar.com/go-pathrs@v0.2.4
This includes a few fixes for 32-bit builds.

Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
(cherry picked from commit b58e342758)
Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
2026-03-12 19:00:19 +09:00
Aleksa Sarai 97f79dbf59 [1.4] README: document libpathrs build tag
Ever since v0.6.0 of github.com/cyphar/filepath-securejoin, pathrs-lite
has been able to transparently switch to using libpathrs as the backend
for safe path resolution (at compile-time, using a build tag). Note that
because build-tags apply globally, this allows for us to easily opt
pure-Go dependencies into all using libpathrs as well for our binaries.

In a future patch this will likely be enabled by default, but document
that this is an option for downstreams that want to opt-in to using
libpathrs.

Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
(cherry picked from commit ab6f75dd25)
Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
2026-03-12 19:00:17 +09:00
Aleksa Sarai 5424ac4ddb [1.4] script: seccomp.sh -> build-seccomp.sh
This name is far more descriptive.

Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
(cherry picked from commit b1a9047c7a)
Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
2026-03-12 19:00:16 +09:00
Aleksa Sarai ac7909922e [1.4] build: rename /opt/libseccomp cdylib directory
In a future patch this will contain other cdylibs so it deserves a
slightly more general name.

Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
(cherry picked from commit 8292574e7a)
Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
2026-03-12 19:00:14 +09:00
Aleksa Sarai 8cdb38cd4e [1.4] build: treat armhf as ARMv7
The intention of commit 531e29e192 ("script/lib.sh: set GOARM=5 for
armel, GOARM=6 for armhf") was to properly support older ARM platforms
with our release builds.

However, we have never been able to support ARMv6 for our builds because
we use the Debian compiler to build the libseccomp we statically compile
into our binaries and (as per the now-deleted comment itself) Debian
treats armhf as being ARMv7 so the final binaries we produced were
always only ever compatible with ARMv7+.

This was a bit of an oddity before but when building libpathrs for
releases we will need to use Rust which makes the target more explicit
(and while it does support armhf, we are using the Debian-packaged Rust
cross-compiler and thus are in the same dilemma with what Debian
considers "armhf" to be).

All-in-all, it's better to just bite the bullet and just follow Debian
here properly.

Fixes: 531e29e192 ("script/lib.sh: set GOARM=5 for armel, GOARM=6 for armhf")
Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
(cherry picked from commit 51ae8de054)
Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
2026-03-12 19:00:13 +09:00
Aleksa Sarai ba78fffbe5 [1.4] dockerfile: switch to Debian 13
Debian 13 (trixie) was released a few months ago and it's probably
prudent to just upgrade. This is also necessary to get access to riscv64
repositories when we build libpathrs for inclusion in our runc binaries.

Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
(cherry picked from commit 6b757b6aa0)
Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
2026-03-12 19:00:05 +09:00
Curd Becker 6b36291fd5 Add linter rule to guard against use of os.Is* error functions
Signed-off-by: Curd Becker <me@curd-becker.de>
(cherry picked from commit 58d24d2dfb)
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2026-03-12 12:01:15 +09:00
Curd Becker 323a54ef0d Replace os.Is* error checking functions with their errors.Is counterpart
Signed-off-by: Curd Becker <me@curd-becker.de>
(cherry picked from commit 536e183451)
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2026-03-12 12:01:15 +09:00