Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apparmor unprivileged restrictions disable #327

Conversation

simondeziel
Copy link
Member

@simondeziel simondeziel commented Feb 13, 2024

At the distro level, Ubuntu 24.04 LTS should ship with additional restrictions preventing unconfined processes from creating userns by default.

The long term goal is for LXD to play nice with those additional restrictions but since a lots of moving parts are involved (kernel, kernel config, apparmor parser, core24 snap, snapd and LXD itself) and not fully ready yet, we decided on disabling those restrictions globally by default in the short term. This decision also took into consideration that LXD needs to work across many distros and versions (Debian w/ Apparmor, old Ubuntu version, old Ubuntu version with HWE kernel, etc).

This PR thus introduce a new snap config key (apparmor.unprivileged-restrictions-disable) that in its default value (true) causes LXD to set kernel.apparmor_restrict_unprivileged_userns=0 and kernel.apparmor_restrict_unprivileged_unconfined=0 thus disabling the new restrictions.

For additional information on those new restrictions, see https://ubuntu.com/blog/ubuntu-23-10-restricted-unprivileged-user-namespaces and https://discourse.ubuntu.com/t/spec-unprivileged-user-namespace-restrictions-via-apparmor-in-ubuntu-23-10/37626.

This PR reintroduced bits of #175.

@github-advanced-security
Copy link

This pull request sets up GitHub code scanning for this repository. Once the scans have completed and the checks have passed, the analysis results for this pull request branch will appear on this overview. Once you merge this pull request, the 'Security' tab will show more code scanning analysis results (for example, for the default branch). Depending on your configuration and choice of analysis tool, future pull requests will be annotated with code scanning analysis results. For more information about GitHub code scanning, check out the documentation.

@simondeziel
Copy link
Member Author

simondeziel commented Feb 13, 2024

This was tested in a local 24.04 VM like this:

$ lxc launch ubuntu-daily:24.04 nv1 --vm
$ lxc shell nv1
root@nv1:~# snap install lxd   # getting the assertion in place
lxd 5.20-f3dd836 from Canonical✓ installed
root@nv1:~# snap install --dangerous /dev/shm/lxd_0+git.170c8352_amd64.snap 
lxd 0+git.170c8352 installed
root@nv1:~# snap alias lxd.lxc lxc
Added:
  - lxd.lxc as lxc
root@nv1:~# cat /proc/sys/kernel/apparmor_restrict_unprivileged_userns
1
root@nv1:~# cat /proc/sys/kernel/apparmor_restrict_unprivileged_unconfined
0

So the keys have their default values before starting LXD's daemon.

root@nv1:~# systemctl start snap.lxd.daemon.service
root@nv1:~# cat /proc/sys/kernel/apparmor_restrict_unprivileged_userns
0
root@nv1:~# cat /proc/sys/kernel/apparmor_restrict_unprivileged_unconfined
0

So LXD noticed that apparmor_restrict_unprivileged_userns needed to be disabled which it did as we can also confirm in the logs:

root@nv1:~# journalctl -u snap.lxd.daemon.service --grep apparmor
Feb 13 21:47:24 nv1 lxd.daemon[2325]: ==> Disabling Apparmor unprivileged userns mediation

And LXD behaves fine:

root@nv1:~# lxd init --auto
root@nv1:~# lxc launch ubuntu-minimal-daily:22.04 c1 -c security.nesting=true
root@nv1:~# lxc exec c1 -- hostname
c1
root@nv1:~# lxc exec c1 -- systemctl --failed
  UNIT LOAD ACTIVE SUB DESCRIPTION
0 loaded units listed.
root@nv1:~# 

And now to prevent LXD from disabling the Apparmor restriction:

root@nv1:~# snap set lxd apparmor.unprivileged-restrictions-disable=false
root@nv1:~# snap stop lxd
2024-02-13T21:57:45Z INFO Waiting for "snap.lxd.daemon.unix.socket", "snap.lxd.daemon.service" to stop.
Stopped.
root@nv1:~# cat /proc/sys/kernel/apparmor_restrict_unprivileged_userns
0
root@nv1:~# echo 1 > /proc/sys/kernel/apparmor_restrict_unprivileged_userns
root@nv1:~# cat /proc/sys/kernel/apparmor_restrict_unprivileged_userns
1
root@nv1:~# snap start lxd
Started.
root@nv1:~# cat /proc/sys/kernel/apparmor_restrict_unprivileged_userns
1

@simondeziel simondeziel marked this pull request as ready for review February 13, 2024 21:22
@simondeziel simondeziel marked this pull request as draft February 13, 2024 21:27
@simondeziel
Copy link
Member Author

simondeziel commented Feb 13, 2024

I forgot to test setting the config key to the non-default value.

Update: snap set rejects keys with _ in their name :/

@simondeziel simondeziel marked this pull request as ready for review February 13, 2024 21:59
@simondeziel
Copy link
Member Author

@tomponline this time, setting the (new) key to its non-default value (false) worked and did the right thing.

snapcraft.yaml Outdated Show resolved Hide resolved
@@ -45,11 +45,11 @@ description: |-
- lxcfs.loadavg: Start tracking per-container load average [default=false]
- lxcfs.cfs: Consider CPU shares for CPU usage [default=false]
- lxcfs.debug: Increase logging to debug level [default=false]
- minio.path: Path to the minio binary to use with LXD [default=""]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@masnax in a separate PR please can you update description of this setting to make it clear its the path to the directory containing the minio and mc commands thanks

@simondeziel simondeziel force-pushed the apparmor-unprivileged-restrictions-disable branch from e84c668 to 170c835 Compare February 13, 2024 22:04
@tomponline tomponline merged commit b748fed into canonical:latest-edge Feb 13, 2024
5 checks passed
@simondeziel simondeziel deleted the apparmor-unprivileged-restrictions-disable branch February 13, 2024 22:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants