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 mount propagation #4295

Merged
merged 1 commit into from
Aug 17, 2023
Merged

Conversation

mihalicyn
Copy link
Member

@mihalicyn mihalicyn commented Mar 31, 2023

Long story behind this. Many years ago, Stéphane Graber
discovered an issue with apparmor mount rules.

Since
7f2b132
commit ("apparmor: Update mount states handling") it was prohibited
to change mount propagation flags, just because adding rules which
allow mount propagation user inside the container gets an ability
to mount everything [1].

Now with modern systemd versions this problem become more critical than
before. For instance, ArchLinux containers fail to start without
nesting apparmor profile enabled (because nesting profile effectively
just allow all mounts). Of course, that's a security issue.

We've also enabled sharing on the container rootfs:
#4229

Now for many workloads it's needed to change propagation flag to
private (see canonical/craft-parts#400).

Issue:
$ lxc-start -F archlinux-test

systemd 253-1-arch running in system mode (+PAM +AUDIT -SELINUX -APPARMOR -IMA +SMACK +SECCOMP +GCRYPT +GNUTLS +OPENSSL +ACL +BLKID +CURL +ELFUTILS +FIDO2 +IDN2 -IDN +IPTC +KMOD +LIBCRYPTSETUP +LIBFDISK +PCRE2 -PWQUALITY +P11KIT -QRENCODE +TPM2 +BZIP2 +LZ4 +XZ +ZLIB +ZSTD +BPF_FRAMEWORK +XKBCOMMON +UTMP -SYSVINIT default-hierarchy=unified)
Detected virtualization lxc.
Detected architecture x86-64.

Welcome to Arch Linux!

bpf-lsm: BPF LSM hook not enabled in the kernel, BPF LSM not supported
Failed to remount root directory as MS_SLAVE: Permission denied
(sd-gens) failed with exit status 1.
[!!!!!!] Failed to start up manager.
Exiting PID 1...

Workaround (unsafe):
$ lxc-start -s lxc.apparmor.allow_nesting=1 -s lxc.apparmor.profile=generated -F arch-test

John Johansen (Apparmor maintainer) and LXD team worked on fix [2].
It was merged to stable AppArmor 3.0 and 3.1 branches already.
There is no stable AppArmor version tag for that, but I think it will
be in the AppArmor version 3.0.10.

See also:
[1] https://bugs.launchpad.net/apparmor/+bug/1597017
[2] https://gitlab.com/apparmor/apparmor/-/merge_requests/333

Fixes: #4280

@lxc-jenkins
Copy link

Testsuite passed

@lxc-jenkins
Copy link

Testsuite passed

@mihalicyn
Copy link
Member Author

mihalicyn commented Mar 31, 2023

We need to be careful with merging this, because this change will cause wider privileges on the old AppArmor versions...
Currently we have no stable AppArmor version with required fix.

Only privileged LXC containers are affected because their security model relies on the AppArmor heavily.

Copy link
Member

@brauner brauner left a comment

Choose a reason for hiding this comment

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

Over 4 or 5 years later.

Quoting from the commit message:

Now with modern systemd versions this problem become more critical than
before. For instance, ArchLinux containers fail to start without
nesting apparmor profile enabled (because nesting profile effectively
just allow all mounts). Of course, that's a security issue.

This claims that this becomes more critical but doesn't explain why e.g., Archlinux containers failing to start are caused by this. I'd like to have that. What exactly is failing suddenly?

config/apparmor/abstractions/container-base.in Outdated Show resolved Hide resolved
config/apparmor/abstractions/container-base Outdated Show resolved Hide resolved
@Blub
Copy link
Member

Blub commented Mar 31, 2023

Should we wait for a release and check the version via apparmor_parser --version?
We should also remember that once we can assume a new-enough apparmor we should be able to replace append_all_remount_rules with using options in (...)

@mihalicyn
Copy link
Member Author

Should we wait for a release and check the version via apparmor_parser --version? We should also remember that once we can assume a new-enough apparmor we should be able to replace append_all_remount_rules with using options in (...)

That's a good point. I thought about that in the context of LXD. Because in LXD we have a template for AppArmor profile (https://github.com/lxc/lxd/blob/bb09d3a6c071ac8c38861e3fc0595b7a5550bf09/lxd/apparmor/instance_lxc.go#L7), it's easy to check AppArmor version in runtime and generate an appropriate AppArmor profile for that.

@mihalicyn
Copy link
Member Author

Over 4 or 5 years later.

Quoting from the commit message:

Now with modern systemd versions this problem become more critical than
before. For instance, ArchLinux containers fail to start without
nesting apparmor profile enabled (because nesting profile effectively
just allow all mounts). Of course, that's a security issue.

This claims that this becomes more critical but doesn't explain why e.g., Archlinux containers failing to start are caused by this. I'd like to have that. What exactly is failing suddenly?

Fixed.

Long story behind this. Many years ago, Stéphane Graber
discovered an issue with apparmor mount rules.

Since
lxc@7f2b132
commit ("apparmor: Update mount states handling") it was prohibited
to change mount propagation flags, just because adding rules which
allow mount propagation user inside the container gets an ability
to mount everything [1].

Now with modern systemd versions this problem become more critical than
before. For instance, ArchLinux containers fail to start without
nesting apparmor profile enabled (because nesting profile effectively
just allow all mounts). Of course, that's a security issue.

We've also enabled sharing on the container rootfs:
lxc#4229

Now for many workloads it's needed to change propagation flag to
private (see canonical/craft-parts#400).

Issue:
$ lxc-start -F archlinux-test

systemd 253-1-arch running in system mode (+PAM +AUDIT -SELINUX -APPARMOR -IMA +SMACK +SECCOMP +GCRYPT +GNUTLS +OPENSSL +ACL +BLKID +CURL +ELFUTILS +FIDO2 +IDN2 -IDN +IPTC +KMOD +LIBCRYPTSETUP +LIBFDISK +PCRE2 -PWQUALITY +P11KIT -QRENCODE +TPM2 +BZIP2 +LZ4 +XZ +ZLIB +ZSTD +BPF_FRAMEWORK +XKBCOMMON +UTMP -SYSVINIT default-hierarchy=unified)
Detected virtualization lxc.
Detected architecture x86-64.

Welcome to Arch Linux!

bpf-lsm: BPF LSM hook not enabled in the kernel, BPF LSM not supported
Failed to remount root directory as MS_SLAVE: Permission denied
(sd-gens) failed with exit status 1.
[!!!!!!] Failed to start up manager.
Exiting PID 1...

Workaround (unsafe):
$ lxc-start -s lxc.apparmor.allow_nesting=1 -s lxc.apparmor.profile=generated -F arch-test

John Johansen (Apparmor maintainer) and LXD team worked on fix [2].
It was merged to stable AppArmor 3.0 and 3.1 branches already.
There is no stable AppArmor version tag for that, but I think it will
be in the AppArmor version 3.0.10.

See also:
[1] https://bugs.launchpad.net/apparmor/+bug/1597017
[2] https://gitlab.com/apparmor/apparmor/-/merge_requests/333

Fixes: lxc#4280

Signed-off-by: Alexander Mikhalitsyn <aleksandr.mikhalitsyn@canonical.com>
@lxc-jenkins
Copy link

Testsuite passed

@lxc-jenkins
Copy link

Testsuite passed

@brauner
Copy link
Member

brauner commented Mar 31, 2023

Over 4 or 5 years later.
Quoting from the commit message:

Now with modern systemd versions this problem become more critical than
before. For instance, ArchLinux containers fail to start without
nesting apparmor profile enabled (because nesting profile effectively
just allow all mounts). Of course, that's a security issue.

This claims that this becomes more critical but doesn't explain why e.g., Archlinux containers failing to start are caused by this. I'd like to have that. What exactly is failing suddenly?

Fixed.

Funny:

Failed to remount root directory as MS_SLAVE: Permission denied.

And that is why I asked. :)

Because AppArmor is the proximal cause but not the distal cause. The distal cause is that I made the rootfs MS_SHARED a little while ago. So when systemd forks of new services in a new mount namespace it sets MS_SLAVE and has done so since forever. Now it starts failing because it actually does need to change to MS_SLAVE whereas before it would've been a nop.

@mihalicyn
Copy link
Member Author

The distal cause is that I made the rootfs MS_SHARED a little while ago. So when systemd forks of new services in a new mount namespace it sets MS_SLAVE and has done so since forever. Now it starts failing because it actually does need to change to MS_SLAVE whereas before it would've been a nop.

agree!

@hallyn
Copy link
Member

hallyn commented Mar 31, 2023

Should we wait for a release and check the version via apparmor_parser --version? We should also remember that once we can assume a new-enough apparmor we should be able to replace append_all_remount_rules with using options in (...)

That's a good point. I thought about that in the context of LXD. Because in LXD we have a template for AppArmor profile (https://github.com/lxc/lxd/blob/bb09d3a6c071ac8c38861e3fc0595b7a5550bf09/lxd/apparmor/instance_lxc.go#L7), it's easy to check AppArmor version in runtime and generate an appropriate AppArmor profile for that.

That's good for the run-time generated profiles. Is there a sort of #ifdef we can put in config/apparmor/abstractions/container-base{,.in} to gate policy on the apparmor version?

@mihalicyn
Copy link
Member Author

Should we wait for a release and check the version via apparmor_parser --version? We should also remember that once we can assume a new-enough apparmor we should be able to replace append_all_remount_rules with using options in (...)

That's a good point. I thought about that in the context of LXD. Because in LXD we have a template for AppArmor profile (https://github.com/lxc/lxd/blob/bb09d3a6c071ac8c38861e3fc0595b7a5550bf09/lxd/apparmor/instance_lxc.go#L7), it's easy to check AppArmor version in runtime and generate an appropriate AppArmor profile for that.

That's good for the run-time generated profiles. Is there a sort of #ifdef we can put in config/apparmor/abstractions/container-base{,.in} to gate policy on the apparmor version?

As far as I know AppArmor doesn't support that. The only thing that we can do for non-runtime-generated profile is to put some warning to container logs if the AppArmor version is affected and privileged container is used.

@stgraber
Copy link
Member

Can we hold off merging this until we get a CVE filed against AppArmor?

I'd like to have some assurance that distros that care about security have the parser bug resolved before anyone gets LXC with the new rule enabled.

@mihalicyn
Copy link
Member Author

Sure, friends, I'm not insisting on merging this right now. But just want to be prepared and discuss everything in advance.

(we can convert this to a "draft" PR)

@brauner
Copy link
Member

brauner commented Mar 31, 2023

Sure, friends, I'm not insisting on merging this right now. But just want to be prepared and discuss everything in advance.

(we can convert this to a "draft" PR)

@stgraber can also just set the Blocked label.

@brauner
Copy link
Member

brauner commented Mar 31, 2023

Can we hold off merging this until we get a CVE filed against AppArmor?

I have to say that I'm puzzled why that never happened before. I mean that bug is older than 4 years and @Blub has even proposed patches to fix this before. :)

@mihalicyn
Copy link
Member Author

Looks like I have no permission to convert PR to a draft.

@mihalicyn
Copy link
Member Author

mihalicyn commented Mar 31, 2023

@brauner brauner marked this pull request as draft March 31, 2023 15:48
@mihalicyn mihalicyn marked this pull request as ready for review August 17, 2023 12:34
@stgraber stgraber merged commit c1d7302 into lxc:main Aug 17, 2023
@mihalicyn
Copy link
Member Author

cc @alexmurray @jrjohansen

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

Unable to start archlinux container
6 participants