Commit Graph

2220 Commits

Author SHA1 Message Date
Adrian Reber 43b36dc4ac Support changing of lsm mount context on restore
Wire through CRIU's support to change the mount context on restore.

This is especially useful if restoring a container in a different pod.

Single container restore uses the same SELinux process label and
same mount context as during checkpointing. If a container is being
restored into an existing pod the process label and the mount context
needs to be changed to the context of the pod.

Changing process label on restore is already supported by runc. This
patch adds the possibility to change the mount context.

Signed-off-by: Adrian Reber <areber@redhat.com>
2021-09-20 10:01:16 +02:00
Shengjing Zhu 163e2523d7 libct/cg: replace bitset with std math/big library
Cut down third party dependency.

Signed-off-by: Shengjing Zhu <zhsj@debian.org>
2021-09-19 23:38:15 +08:00
Kir Kolyshkin f1b703fc45 libct/nsenter/nsexec.c: honor _LIBCONTAINER_LOGLEVEL
Currently, if the log level is not set to e.g. "debug", runc init sends
some debug logs to the parent, which parses and discards it.

It is better to not send those in the first place.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2021-09-09 15:01:26 -07:00
Kir Kolyshkin d5ffe83f94 libct/nsenter/nsexec.c: factor out getenv_int
The code already parses an environment variable into an integer twice,
and we're about to add a third one.

Factor it out to getenv_int().

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2021-09-09 14:57:20 -07:00
Kir Kolyshkin d2f49d4563 libct/nsenter/nsexec.c: improve bail
This makes it possible to use bail() even if logging is not set up
(yet), so we don't have to think whether it's OK to use it or not.
In addition, this might help some unit tests that do not set log
forwarding.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2021-09-09 14:57:20 -07:00
Kir Kolyshkin 9e22bca54a Merge pull request #3204 from cyphar/seccomp-kill-thread-process
Add support for seccomp actions ActKillThread and ActKillProcess
2021-09-09 08:50:39 -07:00
Sascha Grunert 4a4d4f109b Add support for seccomp actions ActKillThread and ActKillProcess
Two new seccomp actions have been added to the libseccomp-golang
dependency, which can be now supported by runc, too.

ActKillThread kills the thread that violated the rule. It is the same as
ActKill. All other threads from the same thread group will continue to
execute.

ActKillProcess kills the process that violated the rule. All threads in
the thread group are also terminated. This action is only usable when
libseccomp API level 3 or higher is supported.

Signed-off-by: Sascha Grunert <sgrunert@redhat.com>
Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
2021-09-09 17:47:00 +10:00
Aleksa Sarai 4a751b05a9 seccomp: drop unnecessary const SCMP_ACT_* defines
These are just boilerplate and are only really useful for the two
actions which require us to set a default errno/aux value (ActErrno and
ActTrace).

Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
2021-09-09 17:46:34 +10:00
Aleksa Sarai 76c1583413 merge branch 'pr-3186'
Akihiro Suda (1):
  improve error message when dbus-user-session is not installed

LGTMs: kolyshkin cyphar
2021-09-09 14:57:03 +10:00
Aleksa Sarai 3eccbe995e merge branch 'pr-3157'
Kir Kolyshkin (6):
  runc --debug: shorter caller info
  libct/logs: do not show caller in nsexec logs
  libct/logs: parse log level implicitly
  libct/logs: test: make more robust
  libct/logs: remove ConfigureLogging
  init.go, main.go: don't use logs.ConfigureLogging

LGTMs: thaJeztah cyphar
2021-09-09 14:54:53 +10:00
Aleksa Sarai 1e5fe26f4e merge branch 'pr-2696'
Kir Kolyshkin (3):
  libct/system: add I and P process states
  libct/system.Stat: fix/improve/speedup
  libct/system/proc_test: fix, improve, add benchmark

LGTMs: thaJeztah cyphar
2021-09-09 14:37:19 +10:00
Aleksa Sarai 8bf032602a merge branch 'pr-3047'
Liu Hua (1):
  checkpoint: resolve symlink for external bind mount(fix ci broken)

LGTMs: kolyshkin cyphar
2021-09-09 14:24:26 +10:00
Mauricio Vásquez c64aaf0e0b libcontainer/specconv: extend SetupSeccomp tests
Extend the SetupSeccomp tests by adding the following cases:
- Test nil config
- Test empty config
- Test bad action and architecture
- Test all possible actions

Signed-off-by: Mauricio Vásquez <mauricio@kinvolk.io>
2021-09-07 13:04:24 +02:00
Alban Crequy 2b025c0173 Implement Seccomp Notify
This commit implements support for the SCMP_ACT_NOTIFY action. It
requires libseccomp-2.5.0 to work but runc still works with older
libseccomp if the seccomp policy does not use the SCMP_ACT_NOTIFY
action.

A new synchronization step between runc[INIT] and runc run is introduced
to pass the seccomp fd. runc run fetches the seccomp fd with pidfd_get
from the runc[INIT] process and sends it to the seccomp agent using
SCM_RIGHTS.

As suggested by @kolyshkin, we also make writeSync() a wrapper of
writeSyncWithFd() and wrap the error there. To avoid pointless errors,
we made some existing code paths just return the error instead of
re-wrapping it. If we don't do it, error will look like:

	writing syncT <act>: writing syncT: <err>

By adjusting the code path, now they just look like this
	writing syncT <act>: <err>

Signed-off-by: Alban Crequy <alban@kinvolk.io>
Signed-off-by: Rodrigo Campos <rodrigo@kinvolk.io>
Co-authored-by: Rodrigo Campos <rodrigo@kinvolk.io>
2021-09-07 13:04:24 +02:00
Mauricio Vásquez 4e7aeff610 libcontainer/utils: introduce SendFds
SendFds is a helper function for sending a set of file descriptors and a message
over a unix domain socket.

Signed-off-by: Mauricio Vásquez <mauricio@kinvolk.io>
2021-09-07 12:38:12 +02:00
Alban Crequy c55530bedc vendoring: Use libseccomp with notify support
The notify support has been merged in libseccomp-golang in this PR:
	https://github.com/seccomp/libseccomp-golang/pull/59

Also, we update to new API of libseccomp-golang so code doesn't break.

Signed-off-by: Alban Crequy <alban@kinvolk.io>
Signed-off-by: Rodrigo Campos <rodrigo@kinvolk.io>
Co-authored-by: Rodrigo Campos <rodrigo@kinvolk.io>
2021-09-07 12:38:12 +02:00
xiadanni 64358c4de9 optimize log: move WriteJSON defer as early as possible
if function returns error before WriteJSON defer, error will not be
printed out, so move this defer as early as possible and use logrus to
print out error if returns before it.

Signed-off-by: xiadanni <xiadanni1@huawei.com>
2021-09-07 07:00:57 +08:00
Kir Kolyshkin b55b308143 libct/logs: do not show caller in nsexec logs
Commit 9f3d7534ea enabled logrus to show information about log
caller, if --debug is set. It is helpful in many scenarios, but does
not work very well when we are debugging runc init, for example:

	# runc --debug run -d xx4557
	DEBU[0000]libcontainer/logs/logs.go:45 github.com/opencontainers/runc/libcontainer/logs.processEntry() nsexec[279687]: logging set up
	DEBU[0000]libcontainer/logs/logs.go:45 github.com/opencontainers/runc/libcontainer/logs.processEntry() nsexec[279687]: logging set up
	DEBU[0000]libcontainer/logs/logs.go:45 github.com/opencontainers/runc/libcontainer/logs.processEntry() nsexec[279687]: => nsexec container setup
	DEBU[0000]libcontainer/logs/logs.go:45 github.com/opencontainers/runc/libcontainer/logs.processEntry() nsexec[279687]: update /proc/self/oom_score_adj to '30'

As we're merely forwarding the logs here, printing out filename:line and
function is useless and clutters the logs a log.

To fix, create and use a copy of the standard logger with caller info
turned off.

With this in place, nsexec logs are sane again:

	# runc --debug --log-format=text run -d xe34
	DEBU[0000] nsexec[293595]: logging set up
	DEBU[0000] nsexec[293595]: logging set up
	DEBU[0000] nsexec[293595]: => nsexec container setup
	DEBU[0000] nsexec[293595]: update /proc/self/oom_score_adj to '30'

This patch also changes Logf to Log in processEntry, as this is what it
should be.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2021-09-04 11:47:31 -07:00
Kir Kolyshkin c3910e7331 libct/logs: parse log level implicitly
There's no need to call logrus.ParseLevel as logrus.Level already
implements encoding.TextUnmarshaler interface.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2021-09-04 11:47:31 -07:00
Kir Kolyshkin c4826905f1 libct/logs: test: make more robust
When playing with the log forwarder, I broke it, but all the units tests
were still passing. This happened because test cases were merely looking
for a word (like "kitten") in the log output, which also happened to be
there in case of an error (as a part of an error message produced by log
forwarder).

Make the test a bit more robust by
 - looking for a complete log message, not just part of it;
 - also checking that log file does NOT contain errors.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2021-09-04 11:47:09 -07:00
Sebastiaan van Stijn 963e0146a4 Merge pull request #3158 from kolyshkin/nsenter-tests
libct/nsenter/nsenter_test.go: fix and improve
2021-09-03 16:55:58 +02:00
Sebastiaan van Stijn 7fcfb3fad6 Merge pull request #3030 from kolyshkin/openat2-improve
libct/cg/OpenFile: fix/improve openat2 handling
2021-09-03 16:54:39 +02:00
Kir Kolyshkin 33dcb994f4 libct/nsenter/nsenter_test.go: logging nits
- add missing colons before error message;
 - unify error messages after cmd.Start and cmd.Wait, so that they show
   context and the error itself.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2021-09-02 10:43:54 -07:00
Kir Kolyshkin 78b271555e libct/nsenter: test: rm misleading comments
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2021-09-02 10:37:05 -07:00
Kir Kolyshkin 2c46455c3f libct/nsenter: test: improve TestNsenterChildLogging
Instead of reading a single message, do read all the logs from the init,
and use DisallowUnknownFields for stricter checking.

While at it, use reapChildren to reap zombies (and add an extra check).

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2021-09-02 10:37:05 -07:00
Kir Kolyshkin feb1fe11a1 libct/nsenter: test: fix TestNsenterValidPaths
The test was not working since at least commit 64bb59f592
renamed pid to stage2_pid (or maybe even earlier), so the pid
was never received (i.e. pid.Pid was 0).

The problem was not caught because os.FindProcess never return an error
on Unix.

Factor out and fix pid decode function:
 - use DisallowUnknownInput to get error if JSON will be changed;
 - check pids to make sure they are valid
 - and use unix.Wait4 to reap zombies.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2021-09-02 10:37:05 -07:00
Kir Kolyshkin 3df6a02f5d libct/nsenter: test: improve newPipe
1. Make sure we close all file descriptors at the end of the test.

2. Make sure we close child fds after the start.

3. Use newPipe for logs as well, for simplicity and uniformity.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2021-09-02 10:37:02 -07:00
Akihiro Suda bd75bc2dc6 Merge pull request #3176 from kolyshkin/rm-config-error-alt
libct/error.go: rm ConfigError (alt)
2021-09-02 14:34:32 +09:00
Akihiro Suda 5fb9b2a006 Merge pull request #3185 from kolyshkin/go117-build-tags
Add go:build tags
2021-09-02 13:35:33 +09:00
Akihiro Suda 0d193edbee Merge pull request #3190 from presztak/nsexec_bail_message_typo_fix
Fix typo in bail message
2021-08-31 14:29:02 +09:00
Kir Kolyshkin d8da00355e *: add go-1.17+ go:build tags
Go 1.17 introduce this new (and better) way to specify build tags.
For more info, see https://golang.org/design/draft-gobuild.

As a way to seamlessly switch from old to new build tags, gofmt (and
gopls) from go 1.17 adds the new tags along with the old ones.

Later, when go < 1.17 is no longer supported, the old build tags
can be removed.

Now, as I started to use latest gopls (v0.7.1), it adds these tags
while I edit. Rather than to randomly add new build tags, I guess
it is better to do it once for all files.

Mind that previous commits removed some tags that were useless,
so this one only touches packages that can at least be built
on non-linux.

Brought to you by

        go1.17 fmt ./...

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2021-08-30 20:58:22 -07:00
Kir Kolyshkin 1b17ec95af libct/cg: rm "unsupported.go" files
These are not needed as these packages (libcontainer/cgroups,
libcontainer/cgroups/fs, and libcontainer/cgroups/systemd) can
not be built under non-linux anyway (for various reasons).

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2021-08-30 20:56:35 -07:00
Kir Kolyshkin dbb9fc03ae libct/*: remove linux build tag from some pkgs
Only some libcontainer packages can be built on non-linux platforms
(not that it make sense, but at least go build succeeds). Let's call
these "good" packages.

For all other packages (i.e. ones that fail to build with GOOS other
than linux), it does not make sense to have linux build tag (as they
are broken already, and thus are not and can not be used on anything
other than Linux).

Remove linux build tag for all non-"good" packages.

This was mostly done by the following script, with just a few manual
fixes on top.

function list_good_pkgs() {
	for pkg in $(find . -type d -print); do
		GOOS=freebsd go build $pkg 2>/dev/null \
		&& GOOS=solaris go build $pkg 2>/dev/null \
		&& echo $pkg
	done | sed -e 's|^./||' | tr '\n' '|' | sed -e 's/|$//'
}

function remove_tag() {
	sed -i -e '\|^// +build linux$|d' $1
	go fmt $1
}

SKIP="^("$(list_good_pkgs)")"
for f in $(git ls-files . | grep .go$); do
	if echo $f | grep -qE "$SKIP"; then
		echo skip $f
		continue
	fi
	echo proc $f
	remove_tag $f
done

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2021-08-30 20:52:07 -07:00
Kir Kolyshkin 9ff64c3d97 *: rm redundant linux build tag
For files that end with _linux.go or _linux_test.go, there is no need to
specify linux build tag, as it is assumed from the file name.

In addition, rename libcontainer/notify_linux_v2.go -> libcontainer/notify_v2_linux.go
for the file name to make sense.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2021-08-30 20:15:00 -07:00
Kir Kolyshkin 3c7db3827c Merge pull request #2883 from flouthoc/master
Add support for rdma cgroup introduced in Linux Kernel 4.11
2021-08-30 20:02:04 -07:00
Kir Kolyshkin 62ec6dc973 Merge pull request #2920 from marquiz/devel/rdt
libcontainer/intelrdt: support ClosID parameter
2021-08-30 19:36:03 -07:00
Piotr Resztak 895e0a5cb3 nsenter: fix typo in bail message
Signed-off-by: Piotr Resztak <piotr.resztak@gmail.com>
2021-08-30 23:24:31 +02:00
Akihiro Suda 1f5798f784 improve error message when dbus-user-session is not installed
Before:

```console
$ docker --context=rootless run -it --rm alpine
docker: Error response from daemon: OCI runtime create failed: unable to start
container process: unable to apply cgroup configuration: unable to start unit
"docker-7ef2c29ccafc1ed9c7fd9859337e5b79870d8ccb282f560e43060a847a6c5310.scope"
(properties [{Name:Description Value:"libcontainer container
7ef2c29ccafc1ed9c7fd9859337e5b79870d8ccb282f560e43060a847a6c5310"} {Name:Slice
Value:"user.slice"} {Name:PIDs Value:@au [6286]} {Name:Delegate Value:true}
{Name:MemoryAccounting Value:true} {Name:CPUAccounting Value:true}
{Name:IOAccounting Value:true} {Name:TasksAccounting Value:true}
{Name:DefaultDependencies Value:false}]): read unix @->/run/systemd/private:
read: connection reset by peer: unknown.
```

After:

```console
$ docker --context=rootless run -it --rm alpine
docker: Error response from daemon: OCI runtime create failed: unable to start
container process: unable to apply cgroup configuration: unable to start unit
"docker-8527d83e046da46d1b56b1c6a89324e687da1c365e044b8dde52cfbf1c461c5a.scope"
(properties [{Name:Description Value:"libcontainer container
8527d83e046da46d1b56b1c6a89324e687da1c365e044b8dde52cfbf1c461c5a"} {Name:Slice
Value:"user.slice"} {Name:PIDs Value:@au [10012]} {Name:Delegate Value:true}
{Name:MemoryAccounting Value:true} {Name:CPUAccounting Value:true}
{Name:IOAccounting Value:true} {Name:TasksAccounting Value:true}
{Name:DefaultDependencies Value:false}]): failed to connect to dbus (hint: for
rootless containers, maybe you need to install dbus-user-session package, see
https://github.com/opencontainers/runc/blob/master/docs/cgroup-v2.md): read
unix @->/run/systemd/private: read: connection reset by peer: unknown.
```

For moby/moby issue 42793

Signed-off-by: Akihiro Suda <akihiro.suda.cz@hco.ntt.co.jp>
2021-08-27 17:19:02 +09:00
Kir Kolyshkin 1b2adcfe56 libct/cg/v1: workaround CPU quota period set failure
As reported in issue 3084, sometimes setting CPU quota period fails
when a new period is lower and a parent cgroup has CPU quota limit set.

This happens as in cgroup v1 the quota and the period can not be set
together (this is fixed in v2), and since the period is being set first,
new_limit = old_quota/new_period may be higher than the parent cgroup
limit.

The fix is to retry setting the period after the quota, to cover all
possible scenarios.

Add a test case to cover a regression caused by an earlier version of
this patch (ignoring a failure of setting invalid period when quota is
not set).

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2021-08-26 13:11:22 -07:00
Sebastiaan van Stijn 8b59b768a9 Merge pull request #3182 from cyphar/revert-3159
Revert "libct/devices: change devices.Type to be a string"
2021-08-25 10:55:04 +02:00
Qiang Huang b4b797200e Merge pull request #3136 from kolyshkin/cg-d-c
libct/cg: rm dead code to improve clarity
2021-08-25 14:46:27 +08:00
Aleksa Sarai 09b80811f6 Revert "libct/devices: change devices.Type to be a string"
This reverts commit 814f3ae1d9. This
changed the on-disk state which breaks runc when it has to operate on
containers started with an older runc version. Working around this is
far more complicated than just reverting it.

Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
2021-08-25 14:11:32 +10:00
Kir Kolyshkin 538ba846dd libct/error.go: rm ConfigError
ConfigError was added by commit e918d02139, while removing runc own
error system, to preserve a way for a libcontainer user to distinguish
between a configuration error and something else.

The way ConfigError is implemented requires a different type of check
(compared to all other errors defined by error.go). An attempt was made
to rectify this, but the resulting code became even more complicated.

As no one is using this functionality (of differentiating a "bad config"
type of error from other errors), let's just drop the ConfigError type.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2021-08-23 18:56:08 -07:00
Kir Kolyshkin 6145628fff configs/validate: audit all returned errors
All the errors returned from Validate should tell about a configuration
error. Some were lacking a context, so add it.

While at it, fix abusing fmt.Errorf and logrus.Warnf where the argument
do not contain %-style formatting.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2021-08-23 18:54:47 -07:00
Kir Kolyshkin bbcf96f91f libct/cg/devices: stop using regex
Looking into data generated by setting

	GODEBUG="inittrace=1"

I have noticed this line:

init github.com/opencontainers/runc/libcontainer/cgroups/devices @1.2 ms, 0.020 ms clock, 10512 bytes, 133 allocs

This is the leader for both bytes and allocs among the packages from
this repo, and all of it is caused by a single regex:

> var devicesListRegexp = regexp.MustCompile(`^([abc])\s+(\d+|\*):(\d+|\*)\s+([rwm]+)$`)

It seems that the same parsing can be done without relying on
a regular expression, no decrease in readability, and 2x faster
(according to the benchmark added), and also makes runc start
slightly faster and leaner.

Before:
    BenchmarkParseLine-4   	  176240	      6768 ns/op	    6576 B/op	      64 allocs/op

After:
    BenchmarkParseLine-4   	  322441	      3535 ns/op	    5520 B/op	      53 allocs/op

[v2: single split with SplitFunc; fix a typo in error message]
[v3: rebase after 3159 merge; re-ran benchmarks (results are similar)]

Co-authored-by: Sebastiaan van Stijn <github@gone.nl>
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2021-08-23 17:04:09 -07:00
Kir Kolyshkin 34df203d13 Merge pull request #3159 from thaJeztah/norunes
libct/devices: change devices.Type to be a string
2021-08-23 16:58:34 -07:00
flouthoc b3d14488b5 Add support for rdma cgroup introduced in Linux Kernel 4.11
Signed-off-by: Aditya Rajan <flouthoc.git@gmail.com>
2021-08-23 12:25:33 +05:30
Kir Kolyshkin 8d8415ee46 libct/logs: remove ConfigureLogging
Previous commits removed all its users -- the only one left is package's
own unit tests.

Modify those unit tests to configure logrus directly, and remove
ConfigureLogging for good. The world is better without it.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2021-08-20 11:02:09 -07:00
Markus Lehtonen 9393700003 libcontainer/intelrdt: update code comments
Use the term "clos group" instead of "container_id group" as the group
that a container belongs to is not necessarily tied to its container id.

Signed-off-by: Markus Lehtonen <markus.lehtonen@intel.com>
2021-08-20 07:47:07 +03:00
Kir Kolyshkin a37a89f4db libct/system: add I and P process states
Those states are available since Linux 4.14 (kernel commits
8ef9925b02c23e3838d5 and 06eb61844d841d003). Before this
patch, they were shown as unknown.

This is mostly cosmetical.

Note that I is described in /proc/pid/status as just "idle", although
elsewhere it says it's an idle kernel thread. Let's have it as "idle"
for brevity.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2021-08-19 19:49:18 -07:00