Commit Graph

1710 Commits

Author SHA1 Message Date
Mrunal Patel 7ba005bbd3 Merge pull request #2647 from cyphar/procfs-masking-nested
rootfs: handle nested procfs mounts for MS_MOVE
2020-10-19 14:56:34 -07:00
Aleksa Sarai b8bf572812 rootfs: handle nested procfs mounts for MS_MOVE
In a case where the host /proc mount has already been overmounted, the
MS_MOVE handling would get ENOENT when trying to hide (for instance)
"/proc/bus" because it had already hidden away "/proc". This revealed
two issues in the previous implementation of this hardening feaure:

1. No checks were done to make sure the mount was a "full" mount (it is
   a mount of the root of the filesystem), but the kernel doesn't permit
   a non-full mount to be converted to a full mount (for reference, see
   mnt_already_visible). This just removes extra busy-work during setup.

2. ENOENT was treated as a critical error, even though it actually
   indicates the mount doesn't exist and thus isn't a problem. A more
   theoretically pure solution would be to store the set of mountpoints
   to be hidden and only ignore the error if an ancestor directory of
   the current mountpoint was already hidden, but that would just add
   complexity with little justification.

In addition, better document the reasoning behind this logic so that
folks aren't confused when looking at it.

Fixes: 28a697cce3 ("rootfs: umount all procfs and sysfs with --no-pivot")
Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
2020-10-13 20:54:56 +11:00
Xiaochen Shen 5903b0cec3 libcontainer/intelrdt: remove 'omitempty' property from CMT and MBM counters
If the values of CMT and MBM counters are zero, they will be omitted as
empty items when getting Intel RDT stats.

Remove 'omitempty' property from CMT and MBM counters to display zero
values.

Signed-off-by: Xiaochen Shen <xiaochen.shen@intel.com>
2020-10-12 22:04:37 +08:00
Kir Kolyshkin 44f221e2fc Merge pull request #2633 from chaitanyabandi/2632-fix
Kill processes in cgroup even if process Wait fails
2020-10-08 10:51:13 -07:00
Mrunal Patel 10825f70d3 Merge pull request #2631 from knabben/fix-some-lints
Fixing some lint issues
2020-10-07 21:24:40 -07:00
Chaitanya Bandi 0aa0fae393 Kill all processes in cgroup even if init process Wait fails
If the cgroup's init process doesn't complete successfully, Wait returns a
non-nil error. We should still kill all the process in the cgroup if process
namespace is shared. Otherwise, it may result in process leak.

Fixes #2632

Signed-off-by: Chaitanya Bandi <kbandi@cs.stonybrook.edu>
2020-10-08 01:26:34 +00:00
Kir Kolyshkin de77b5b39f Merge pull request #2630 from aos/2582-rootless-systemd-dbus-friendly-error
Rootless systemd dbus friendlier error
2020-10-06 12:43:17 -07:00
Amim Knabben 978fa6e906 Fixing some lint issues
Signed-off-by: Amim Knabben <amim.knabben@gmail.com>
2020-10-06 14:44:14 -04:00
Akihiro Suda bb539a9965 Merge pull request #2628 from thaJeztah/linting_foo
fix some linting issues
2020-10-06 20:10:40 +09:00
Aos Dabbagh b448330514 Add error message
* Updated cgroups-v2 documentation to mention dbus-user-session requirement
* Added friendlier error message

Signed-off-by: Aos Dabbagh <aosdab@gmail.com>
2020-10-05 17:08:07 -04:00
Kir Kolyshkin e25b8cfcd5 libct/cg/utils: use fscommon.ReadFile
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2020-10-05 14:07:57 -07:00
Kir Kolyshkin 6bae53f589 libct/cg/fs2: use fscommon.ReadFile
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2020-10-05 14:07:15 -07:00
Kir Kolyshkin 2588e6f1b8 libct/cg/fs/cpuset: use fscommon.ReadFile
While at it,

 - change some functions to not be methods of CpusetCgroup as
   they don't use any members;
 - simplify isEmpty.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2020-10-05 14:07:15 -07:00
Kir Kolyshkin 1d20cf49f1 libct/cg/fs/cpuacct: use fscommon.ReadFile
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2020-10-05 14:07:15 -07:00
Kir Kolyshkin 9e78b66e88 libct/cg/systemd/v1.enableKmem: use fscommon.ReadFile
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2020-10-05 14:07:15 -07:00
Kir Kolyshkin 316344364c libct/cg/fs2.CreateCgroupPath: use fscommon.*File
fscommon's ReadFile and WriteFile are tailored to cgroupfs,
so let's use them here.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2020-10-05 14:07:15 -07:00
Kir Kolyshkin b7092d84ee libct/cg/fs.setKernelMemory: use fscommon.WriteFile
fscommon.WriteFile is added specifically to work with cgroup files,
and the error it returns does not need to be wrapped.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2020-10-05 14:07:15 -07:00
Kir Kolyshkin 619de9773e libct/cg/fscommon_test: rm cgroups dependency
This removes package dependency on cgroup, as following commits
make cgroup use fscommon, which would result in dependency cycle.

The code to find out memory cgroup root is not really needed,
as 99% of test envrionments will have it at /sys/fs/cgroup/memory.
If not, that means we're either on cgroupv2 or on some very custom
system, so just skip the test.

The code that checks if we're on cgroupv2 is replaced by the check
of the particular v1 control file.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2020-10-05 14:07:15 -07:00
Akihiro Suda c23c05eac0 Merge pull request #2528 from zvier/master
cleancode: adjust and make it more readability
2020-10-05 03:56:56 +09:00
Mrunal Patel f671f6bf3d Merge pull request #2597 from kolyshkin/hugepages
Fix/improve GetHugePageSize
2020-10-02 19:23:23 -07:00
Kir Kolyshkin dc775c138e Merge pull request #2625 from KentaTada/modify-rdt-test-perm
libcontainer/intelrdt: modify the incorrect file mode
2020-10-02 18:59:33 -07:00
Mrunal Patel 0d9b0dfc46 Merge pull request #2626 from kolyshkin/mountinfo-0.3.1
vendor: bump mountinfo v0.3.1
2020-10-02 15:11:45 -07:00
Sebastiaan van Stijn e8eb8000f1 fix some linting issues
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2020-10-02 10:21:54 +02:00
Akihiro Suda 750036e41d Merge pull request #2609 from thaJeztah/byte_trim
libcontainer: prefer bytes.TrimSpace() over strings.TrimSpace()
2020-10-02 14:22:39 +09:00
Ashok Pon Kumar fcf210d631 Fix goreport warnings of ineffassign and misspell
Signed-off-by: Ashok Pon Kumar <ashokponkumar@gmail.com>
2020-10-02 09:03:45 +05:30
Kenta Tada 644c107ecb libcontainer/intelrdt: modify the incorrect file mode
Signed-off-by: Kenta Tada <Kenta.Tada@sony.com>
2020-10-02 11:08:06 +09:00
Kir Kolyshkin 87412ee435 vendor: bump mountinfo v0.3.1
It contains some breaking changes, so fix the code.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2020-10-01 18:51:25 -07:00
Sebastiaan van Stijn 28b452bf65 libcontainer: unconvert
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2020-10-01 18:36:56 +02:00
Sebastiaan van Stijn b3a8b0742c libcontainer: prefer bytes.TrimSpace() over strings.TrimSpace()
Perform trimming before converting to a string, which should be
somewhat more performant.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2020-10-01 18:36:53 +02:00
Kir Kolyshkin c82737665a Merge pull request #2621 from KentaTada/refactor-config-test
libcontainer: remove loadConfig which is the unused function
2020-10-01 08:17:21 -07:00
Kir Kolyshkin 2854950e5f Merge pull request #2622 from KentaTada/remove-unused-removepath
libcontainer: remove `removePath` from cgroups
2020-10-01 08:15:20 -07:00
Kenta Tada 3d5dec2f44 libcontainer: remove the unused variable from spec
Signed-off-by: Kenta Tada <Kenta.Tada@sony.com>
2020-10-01 12:29:48 +09:00
Kenta Tada b76652fbeb libcontainer: remove removePath from cgroups
`removePath` is unused as below
https://github.com/opencontainers/runc/commit/11fb94965cab5b2a54da9dca50d0e36d0ee36ef0

Signed-off-by: Kenta Tada <Kenta.Tada@sony.com>
2020-10-01 12:22:12 +09:00
Kenta Tada faaecac77d libcontainer: remove loadConfig which is the unused function
Signed-off-by: Kenta Tada <Kenta.Tada@sony.com>
2020-10-01 12:07:49 +09:00
Akihiro Suda a220b9c6cc Merge pull request #2608 from thaJeztah/no_join
libcontainer: remove redundant strings.Join()
2020-10-01 10:20:23 +09:00
Kir Kolyshkin 8258fae775 Merge pull request #2619 from thaJeztah/nil_return
setFreezer: explicitly return nil
2020-09-30 18:07:10 -07:00
Sebastiaan van Stijn c6ac3c4bde libcontainer/system: remove deprecated GetProcessStartTime
GetProcessStartTime was deprecated over three Years ago in
439eaa3584, so we may as well remove
it now.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2020-10-01 00:06:47 +02:00
Sebastiaan van Stijn 3eb469b029 libcontainer: remove redundant strings.Join()
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2020-10-01 00:00:47 +02:00
Sebastiaan van Stijn bc9a7bda58 setFreezer: explicitly return nil
errors.Wrap(err, "some error") returns nil if err is nil, so it's
slightly clearer to just return early than to set the error to nil
and call errors.Wrap(). This is also somewhat defensive in case
we decide to replace `errors.Wrap()` for golang's native `%w`
wrapping.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2020-09-30 23:41:07 +02:00
Mrunal Patel ffc30bb00d Merge pull request #2606 from kolyshkin/intel-rdt
libcontainer/intelrdt: use moby/sys/mountinfo
2020-09-30 13:14:10 -07:00
Mrunal Patel d5098eaae4 Merge pull request #2604 from kolyshkin/fscommon-I
libct/cgroups/fscommon: nits
2020-09-30 13:12:16 -07:00
Kir Kolyshkin 360981ae1d libct/cgroups: rewrite getHugePageSizeFromFilenames
This is a function to convert huge page sizes (obtained by reading
/sys/kernel/mm/hugepages directory entries) to strings user for hugetlb
cgroup controller resource files. Those strings are when used to get the
hugetlb resource statistics.

This function used external library, floating point numbers, and can
(theoretically) produce invalid values, since the kernel only uses KB,
MB, and GB suffixes.

Rewrite it to produce the same strings as used in the kernel (see [1]).
As a result, it's also faster, more future-proof (entries that do not
start with "hugepages-" and/or incorrect suffix are skipped), and does
more input sanity checks. As a side effect, libcontainer no longer
depends on docker/go-units.

While at it, add more test cases.

Before:
	BenchmarkGetHugePageSize-8       	  187452	      6265 ns/op
	BenchmarkGetHugePageSizeImpl-8   	  396769	      2998 ns/op

After:
	BenchmarkGetHugePageSize-8       	  222898	      4554 ns/op
	BenchmarkGetHugePageSizeImpl-8   	 4738924	       241 ns/op

NOTE on removing HugePageSizeUnitList -- this was added by commit
6f77e35da and was used by kubernetes code in [2], which was later
superceded by [3], so there are (hopefully) no external users.
If there are any, they should not be doing that.

[1] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/mm/hugetlb_cgroup.c?id=eff48ddeab782e35e58ccc8853f7386bbae9dec4#n574
[2] https://github.com/kubernetes/kubernetes/pull/78495
[3] https://github.com/kubernetes/kubernetes/pull/84154

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2020-09-30 10:58:31 -07:00
Akihiro Suda 890cc2aa60 Merge pull request #2612 from thaJeztah/concat
use string-concatenation instead of sprintf for simple cases
2020-10-01 01:56:28 +09:00
Kir Kolyshkin 22f11bafc3 Merge pull request #2611 from thaJeztah/const
libcontainer/cgroups: use const for templates
2020-09-30 09:01:42 -07:00
Mrunal Patel fdd00a693e Merge pull request #2614 from kolyshkin/systemd-race
libct/cgroups/systemd: eliminate runc/systemd race
2020-09-30 08:20:11 -07:00
Sebastiaan van Stijn 8bf216728c use string-concatenation instead of sprintf for simple cases
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2020-09-30 10:51:59 +02:00
Kir Kolyshkin a4d5e8a27b libcontainer/ignoreTerminateError: ignore SIGKILL
When we call terminate(), we kill the process, and wait
returns the error indicating the process was killed.
This is exactly what we expect here, so there is no reason
to treat it as an error.

Before this patch, when a container with invalid cgroup parameters is
started:

> WARN[0000] unable to terminate initProcess               error="signal: killed"
> ERRO[0000] container_linux.go:366: starting container process caused: process_linux.go:495: container init caused: process_linux.go:458: setting cgroup config for procHooks process caused: failed to write "555": open /sys/fs/cgroup/blkio/user.slice/xx33/blkio.weight: permission denied

After:

> ERRO[0000] container_linux.go:366: starting container process caused: process_linux.go:495: container init caused: process_linux.go:458: setting cgroup config for procHooks process caused: failed to write "555": open /sys/fs/cgroup/blkio/user.slice/xx33/blkio.weight: permission denied

I.e. the useless warning is gone.

NOTE this breaks a couple of integration test cases, since they were
expecting a particular message in the second line, and now due to
"signal: killed" removed it's in the first line. Fix those, too.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2020-09-29 23:55:02 -07:00
Kir Kolyshkin dc42459130 libct/(*initProcess).start: fix removing cgroups on error
In case cgroup configuration is invalid (some parameters can't be set
etc.), p.manager.Set fails, the error is returned, and then we try to
remove cgroups (by calling p.manager.Destroy) in a defer.

The problem is, the container init is not yet killed (as it is killed in
the caller, i.e. (*linuxContainer).start), so cgroup removal fails like
this:

> time="2020-09-26T07:46:25Z" level=warning msg="Failed to remove cgroup (will retry)" error="rmdir /sys/fs/cgroup/net_cls,net_prio/kubepods.slice/kubepods-burstable.slice/kubepods-burstable-pod28ce6e74_694c_4b77_a953_dc01e182ac76.slice/crio-f6984c5eeb6c6b49ff3f036bdcb9ded317b3d0b2469ebbb35705442a2afd98c2.scope: device or resource busy"
> ...
> time="2020-09-26T07:46:27Z" level=error msg="Failed to remove cgroup" error="rmdir /sys/fs/cgroup/net_cls,net_prio/kubepods.slice/kubepods-burstable.slice/kubepods-burstable-pod28ce6e74_694c_4b77_a953_dc01e182ac76.slice/crio-f6984c5eeb6c6b49ff3f036bdcb9ded317b3d0b2469ebbb35705442a2afd98c2.scope: device or resource busy"

The above is repeated for every controller, and looks quite scary.

To fix, move the init termination to the abovementioned defer.

Do the same for (*setnsProcess).start() for uniformity.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2020-09-29 18:49:29 -07:00
Kir Kolyshkin 8699596dce libct/(*setnsProcess).Start: use retErr
It is not a good practice to have the name `err` for the error returned
by a function.  Switch to `retErr`.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2020-09-29 18:38:31 -07:00
Kir Kolyshkin 38447895a5 libct/cgroups/systemd: eliminate runc/systemd race
In case it takes more than 1 second for systemd to create a unit,
startUnit() times out with a warning and then runc proceeds
(to create cgroups using fs manager and so on).

Now runc and systemd are racing, and multiple scenarios are possible.

In one such scenario, by the time runc calls systemd manager's Apply()
the unit is not yet created, the dbusConnection.SetUnitProperties()
call fails with "unit xxx.scope not found", and the whole container
start also fails.

To eliminate the race, we need to return an error in case the timeout is
hit.

To reduce the chance to fail, increase the timeout from 1 to 30 seconds,
to not error out too early on a busy/slow system (and times like 3-5
seconds are not unrealistic).

While at it, as the timeout is quite long now, make sure to not leave
a stray timer.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2020-09-29 17:43:15 -07:00