Commit Graph

11006 Commits

Author SHA1 Message Date
Christian Brauner
0e3af26b2f
cgroups: split out unified cgroup helpers
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2021-02-20 03:17:47 +01:00
Christian Brauner
6407ddf4da
memory_utils: adapt to new pointer error macros
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2021-02-20 03:17:46 +01:00
Christian Brauner
844d929d2e
macro: add pointer error encoding support
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2021-02-20 01:36:00 +01:00
Christian Brauner
45069107aa
compiler: add likely() and unlikely() support
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2021-02-20 01:33:23 +01:00
Christian Brauner
0eb3b1ceed
cgroups: s/basecginfo/cgroup_info/g
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2021-02-20 01:10:53 +01:00
Christian Brauner
8033666ce2
cgroups: simplify current cgroup retrieval on pure unified cgroup layouts
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2021-02-20 01:07:43 +01:00
Christian Brauner
a96be3c335
cgroups: tweak return values
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2021-02-20 00:49:25 +01:00
Christian Brauner
0fbf99d600
cgroups: tweak lxc.cgroup.use handling in __cgroup_init()
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2021-02-20 00:43:43 +01:00
Christian Brauner
7a0c8ed32c
cgroups: s/must_copy_string()/strdup()/g
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2021-02-20 00:40:32 +01:00
Christian Brauner
0d83967fa3
cgroups: avoid additional variable for single access
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2021-02-20 00:38:57 +01:00
Christian Brauner
37ac0b481d
cgroups: fix prune_init_scope()
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2021-02-19 18:25:30 +01:00
Christian Brauner
d9be3d2694
string_utils: move lxc_iterate_parts()
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2021-02-19 18:09:13 +01:00
Stéphane Graber
c33840f6f9
Merge pull request #3684 from brauner/2021-02-18/fixes_2
bpf: device cgroup improvements
2021-02-19 12:04:56 -05:00
Stéphane Graber
02882d8358
Merge pull request #3687 from brauner/2021-02-19/fixes
lsm: fixes
2021-02-19 11:58:43 -05:00
Christian Brauner
54dbe498b9
doc: tweak cgroup headline
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2021-02-19 15:23:29 +01:00
Christian Brauner
5025f3a690
doc: epxlain eBPF-based device controller semantics
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2021-02-19 15:23:29 +01:00
Christian Brauner
e9b3d28df4
doc: add missing ".[controller file] suffix to lxc.cgroup{2}. key explanations
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2021-02-19 15:23:27 +01:00
Christian Brauner
0ede37250c
bpf: update device cgroup semantics
LXC has supported the bpf device controlller for a while now. A bpf device
program can be attached to the container's cgroup if this is a pure cgroup2
host.

The format for specifying device rules for the cgroup2 bpf device controller is
the same as for the legacy cgroup device controller; only the configuration key
prefix has to change. Specifically, device rules for the legacy cgroup device
controller are specified by via lxc.cgroup.devices.{allow,deny} whereas for the
cgroup2 bpf device controller lxc.cgroup2.devices.{allow,deny} must be used.

The following semantics apply:
1. The device rule "lxc.cgroup2.devices.deny = a" will cause LXC to instruct
   the kernel to block access to all devices by default. To grant access to
   devices "allow device rules" must be added via the
   "lxc.cgroup2.devices.allow" key. This is referred to as a "allowlist" device
   program.
2. The device rule "lxc.cgroup2.devices.allow = a" will cause LXC to instruct
   the kernel to allow access to all devices by default. To deny access to
   devices "deny device rules" must be added via "lxc.cgroup2.devices.deny"
   key. This is referred to as a "denylist" device program.
3. Specifying a rule as explained in 1. or 2. will cause all previous rules to
   be cleared, i.e. the device list will be reset.

For example the set of rules:

lxc.cgroup2.devices.deny = a
lxc.cgroup2.devices.allow = c *:* m
lxc.cgroup2.devices.allow = b *:* m
lxc.cgroup2.devices.allow = c 1:3 rwm

implements a "allowlist" device program, i.e. the kernel will block access to
all devices not specifically allowed in this list. This particular program
states that all character and block devices might be created but only /dev/null
might be read or written.

If we to switch to the set of rules to:

lxc.cgroup2.devices.allow = a
lxc.cgroup2.devices.deny = c *:* m
lxc.cgroup2.devices.deny = b *:* m
lxc.cgroup2.devices.deny = c 1:3 rwm

then LXC would instruct the kernel to implement a "denylist", i.e. the kernel
will allow access to all devices not specifically denied in this list. This
particular program states that no character devices or block devices might be
created and that /dev/null is not allow allowed to be read, written, or
created.

Consider the same program but followed by a rule as explained in 1. or 2.:

lxc.cgroup2.devices.allow = a
lxc.cgroup2.devices.deny = c *:* m
lxc.cgroup2.devices.deny = b *:* m
lxc.cgroup2.devices.deny = c 1:3 rwm
lxc.cgroup2.devices.allow = a

The last line will cause LXC to reset the device list without changing the type
of device program.

lxc.cgroup2.devices.allow = a
lxc.cgroup2.devices.deny = c *:* m
lxc.cgroup2.devices.deny = b *:* m
lxc.cgroup2.devices.deny = c 1:3 rwm
lxc.cgroup2.devices.deny = a

The last line will cause LXC to reset the device list and switch from a
"allowlist" program to a "denylist" program.

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2021-02-19 15:23:26 +01:00
Christian Brauner
30bfbd3ff1
cgroups: tweak bpf_device_cgroup_prepare()
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2021-02-19 15:23:26 +01:00
Christian Brauner
a7744f125e
conf: expose lxc_clear_cgroup2_devices()
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2021-02-19 15:23:26 +01:00
Christian Brauner
d6c06927df
conf: improve lxc_clear_cgroups()
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2021-02-19 15:23:26 +01:00
Christian Brauner
159702774c
bpf: fix typos
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2021-02-19 15:23:26 +01:00
Christian Brauner
a0f0e9df1d
bpf: comment bpf_cgroup_devices_update()
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2021-02-19 15:23:26 +01:00
Christian Brauner
60532b18f4
commands: only update bpf device program if really needed
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2021-02-19 15:23:26 +01:00
Christian Brauner
a134099de5
cgroups: make device cgroup handling smarter and simpler
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2021-02-19 15:23:26 +01:00
Christian Brauner
fc4612cb39
cgroups: ensure no garbage is returned
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2021-02-19 15:23:25 +01:00
Christian Brauner
ba9055c96b
lsm: twek apparmor_process_label_get()
Fixes: Coverity 1473189
Fixes: Coverity 1473190
Fixes: 47f4914d88 ("apparmor: prefer /proc/.../attr/apparmor/current over legacy interface")
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2021-02-19 14:40:58 +01:00
Christian Brauner
8b145a8778
file_utils: allow fd_to_buf() to fail for real
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2021-02-19 14:32:36 +01:00
Christian Brauner
f43ed6a0ab
Merge pull request #3686 from cyphar/apparmor-attr-subdir
apparmor: prefer /proc/.../attr/apparmor/current over legacy interface
2021-02-19 13:15:31 +01:00
Aleksa Sarai
47f4914d88
apparmor: prefer /proc/.../attr/apparmor/current over legacy interface
It turns out that since Linux 5.1 there are now per-LSM subdirectories
for major LSMs, which users are recommended to use over the "legacy"
top-level /proc/$pid/attr/... files[1]:

> Process attributes associated with “major” security modules should be
> accessed and maintained using the special files in /proc/.../attr. A
> security module may maintain a module specific subdirectory there,
> named after the module. /proc/.../attr/smack is provided by the Smack
> security module and contains all its special files. The files directly
> in /proc/.../attr remain as legacy interfaces for modules that provide
> subdirectories.

AppArmor has had such a directory since Linux 5.8[2], and it turns out
that with certain CONFIG_LSM configurations you can end up with AppArmor
files not being accessible from the legacy interface. Arch Linux
recently added BPF as one of the enabled LSM in their configuration, and
this broke runc[3] and LXC.

The solution is to first try to use /proc/$pid/attr/apparmor/current and
fall back to /proc/$pid/attr/current if the former is not available.

[1]: https://www.kernel.org/doc/html/latest/admin-guide/LSM/index.html
[2]: Linux 5.8 ; commit 6413f852ce08 ("apparmor: add proc subdir to attrs")
[3]: https://github.com/opencontainers/runc/issues/2801

Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
2021-02-19 20:53:50 +11:00
Aleksa Sarai
301a5f8e78
apparmor: clean up apparmor_process_label_get
Rather than open-coding file reading and retry semantics and
implementing the path generation logic separately to
apparmor_process_label_fd_get, refactor the logic so that it looks
closer to the pidfd version.

This will make it easier to implement the two-step handling for
/proc/self/attr/apparmor/current and makes this code slightly less
confusing.

Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
2021-02-19 20:46:01 +11:00
Stéphane Graber
35a68d6df2
Merge pull request #3681 from brauner/2021-02-18/cgroups
cgroups: fixes & bpf rework
2021-02-18 11:52:52 -05:00
Stéphane Graber
599a0c6c9c
Merge pull request #3682 from brauner/2021-02-18/fixes
console: fixes
2021-02-18 11:42:17 -05:00
Christian Brauner
f640c8187a
conf: don't log garbage
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2021-02-18 17:08:11 +01:00
Christian Brauner
f3dff08054
start: fix non-daemonized and application containers
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2021-02-18 17:01:59 +01:00
Christian Brauner
1dd71c90e8
conf: use saner mode for console
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2021-02-18 17:01:59 +01:00
Christian Brauner
ad755295f1
bpf: simplify bpf (device) program freeing
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2021-02-18 16:26:16 +01:00
Christian Brauner
25903ba9c0
bpf: make bpf_program_cgroup_attach() static
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2021-02-18 16:21:04 +01:00
Christian Brauner
da03dc28e1
bpf: prevent double-close
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2021-02-18 16:13:45 +01:00
Christian Brauner
8c49586f0d
cgroups: use close_equal() and free_equal()
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2021-02-18 15:55:24 +01:00
Christian Brauner
284868b249
memory_utils: add close_equal() and free_equal()
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2021-02-18 15:52:35 +01:00
Christian Brauner
3d01776c50
lxccontainer: fix reboot logging
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2021-02-18 15:39:27 +01:00
Christian Brauner
0a150695b4
bpf: rework live device cgroup update
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2021-02-18 14:56:01 +01:00
Christian Brauner
fd1cf1b1ab
compiler: fix fallthrough attribute
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2021-02-18 14:42:39 +01:00
Christian Brauner
d202c500d6
bpf: fix return values in bpf_program_cgroup_attach()
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2021-02-18 14:42:15 +01:00
Christian Brauner
354d21c491
bpf: let bpf_list_add_device() take the device list directly
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2021-02-18 13:35:03 +01:00
Christian Brauner
7da502de6a
bpf: add and use bpf_cgroup_devices_attach() helper
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2021-02-18 13:29:26 +01:00
Christian Brauner
928937b114
cgroups: remove compile-time bpf support detection
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2021-02-18 12:42:34 +01:00
Christian Brauner
7fc77b1ae0
bpf: vendor bpf headers
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2021-02-18 12:34:51 +01:00
Christian Brauner
34683e39b9
bpf: handling missing defines
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2021-02-18 12:02:54 +01:00