Repository navigation
Description
FEATURE REQUEST
Description
Currently, the dockerd binary always runs containers as docker-default if AppArmor is enabled on the host.
Lines 30 to 54 in b4cb377
| if apparmor.IsEnabled() { | |
| var appArmorProfile string | |
| if c.AppArmorProfile != "" { | |
| appArmorProfile = c.AppArmorProfile | |
| } else if c.HostConfig.Privileged { | |
| // `docker exec --privileged` does not currently disable AppArmor | |
| // profiles. Privileged configuration of the container is inherited | |
| appArmorProfile = unconfinedAppArmorProfile | |
| } else { | |
| appArmorProfile = defaultAppArmorProfile | |
| } | |
| if appArmorProfile == defaultAppArmorProfile { | |
| // Unattended upgrades and other fun services can unload AppArmor | |
| // profiles inadvertently. Since we cannot store our profile in | |
| // /etc/apparmor.d, nor can we practically add other ways of | |
| // telling the system to keep our profile loaded, in order to make | |
| // sure that we keep the default profile enabled we dynamically | |
| // reload it if necessary. | |
| if err := ensureDefaultAppArmorProfile(); err != nil { | |
| return err | |
| } | |
| } | |
| p.ApparmorProfile = appArmorProfile | |
| } |
As an administrator, I'd like to use AppArmor to restrict dockerd from performing certain operations on a portion of my fleet (developer machines). However, since dockerd always attempts to apply a custom profile (by writing to /proc/self/attr/exec), runc can always get around my restrictions by running containers as unconstrained. If I restrict writes to /proc/self/attr/exec, then runc will always fail to start containers.
Currently, I have two options:
- Compile dockerd myself with build tags and distributing different packages to different hosts (different docker packages for developer machines vs. servers)
- Hide /sys/kernel/security/apparmor from dockerd (nice and hacky).
As an alternative, I'd like to send a PR that adds a dockerd flag to disable AppArmor integration (similar to --selinux-enabled). Does this sound reasonable?
https://docs.docker.com/engine/reference/commandline/dockerd/
/cc @cyphar
Activity
AkihiroSuda commentedon Oct 16, 2020
SGTM
cyphar commentedon Oct 16, 2020
This could be implemented as an extension of #41442 though this is kind of a problem with our default security profiles in general -- you also can't disable seccomp entirely on a daemon-level. Maybe we need some kind of
--insecure-disable-profiles=...flag? Something like--selinux-enabled(which I forgot existed) would work too, though I think it'd be ideal to make the flag seem scary (it should probably have the wordinsecurein it -- so--insecure-apparmoror something like that).However, regarding your original issue with restricting dockerd -- in case you were not aware, there is a dockerd profile in
contrib. It is more lax than what you appear to require, but AppArmor should allow you to only permit certain transitions usingchange_profile-- in fact that is what the profile does:You could restrict this to not allow unconfined containers -- this isn't done in the example profile because disabling this makes
--privilegedcontainers not work. Being able to write to/proc/self/attr/execis not really a security issue if you havechange_profilerestrictions (as far as I can tell) because:/proc/$pid/attr/exec-- AppArmor doesn't allow you to change other programs' profiles.execwill fail with permission denied if the profile transition is not permitted (AFAICS).But please double-check that this is actually the case (AppArmor also supports profile nesting which would eliminate this issue as well but I'm not entirely sure whether you could convince Docker to use it).
I also hasten to point out that running containers without AppArmor profiles at all (but with the daemon restricted) makes those containers less secure because the containers are running with less restricted profiles by necessity.
thaJeztah commentedon Oct 16, 2020
You mean "disabled by default" for containers, versus: do not allow containers to be started with seccomp/selinux/apparmor ?
Should we have options like;
--seccomp-profile=disableddon't manage, disable seccomp handling, do not allow seccomp profiles to be used--seccomp-profile=unconfineduse "unconfined" as a default, but allow custom profiles for a container, if specified--seccomp-profile=default(or not set): use the default profile as default--seccomp-profile=myprofileuse custom profile "myprofile" as defaultericchiang commentedon Oct 16, 2020
Thanks @cyphar for the information here. I was able to get the change_profile restrictions working. Couple of notes on that:
--privilegedflag. That's kinda working as intended, but ideally users that supply--privilegedwould just get our default profile, not have docker attempt to run it as unconfined and fail.So the options that would work for me are:
--seccomp-profileflag.--privilegedmean "don't attempt to change the containers profile" rather than "run as unconfined".The
--seccomp-profileseems like the least special-casing. Does that work for you @cyphar? Should I send a PR?thaJeztah commentedon Oct 16, 2020
I'd rather not change what
--privilegeddoes.--privilegedis a footgun, and unfortunately we're kinda stuck with it being there. Unfortunately the alternatives (manually specifying which capabilities to grant, syscalls, mounts, etc to allow) is too difficult for most users. A better options for that would be something like "entitlements" (which was proposed in #32801, but never lifted off).Perhaps in the meantime, having a daemon option that would disable
--privilegedaltogether (and thus require the user to (albeit cumbersome) specify what's needed) could be an option.ericchiang commentedon Oct 16, 2020
Sorry, I meant to say that I'd prefer adding
--seccomp-profileinstead of changing the way--privilegedbehaves.Disabling
--privilegedthrough a daemon option isn't something we'd enable. We've got a very large fleet, and users expect docker to "just work" :) I'm mostly interested in preventing dockerd and runc from being able to modify AppArmor state.cyphar commentedon Oct 17, 2020
@ericchiang
I would be interested to see what profiles you end up with at the end of this, it might be useful to integrate some of them into the
contribconfigurations.Right but that distinction only matters if you have a separate profile for
/usr/bin/runc(which is a totally reasonable thing to do, but is not done by thecontribsetup hence why it's omitted).It seems (at least to me) a little bit dodgy to permit users to pass the
--privilegedflag but not actually running privileged containers when they're requested. Often--privilegedcontainers do things like load kernel modules, manage hardware, or any number of other privileged operations -- these operations are going to fail anyway when the user tries them later (so the container will fail regardless, the question is whether it will fail upfront or later on in the middle of some operation). But that's just my two cents based on what you've said -- just thought it was worth mentioning.I do wonder if you can force AppArmor to override the profile change by doing something like
change_profile /var/lib/docker/** -> docker-default(I don't know howchange_profileoverrides interact with/proc/self/attr/execchanges off the top of my head). You can also force a profile change atpivot_root(with thepivot_rootdirective) and that might also override the/proc/self/attr/execoverride -- again, I'm not sure what the interaction is. And as I said above, the worst case is you can try to force Docker to create nested profiles but I'm not sure how well that works in practice.While I think
--privilegedis quite insidious in many respects, it's a necessary evil and I do wonder whether having a way to make it "no-so---privileged" is going to cause more trouble than it's worth...ericchiang commentedon Oct 19, 2020
The profile transition at
pivot_rootsounds really interesting, I'd have to think a bit about how we'd use that.Long-term, our goal is to prevent dockerd from being able to modify AppArmor state or trivially get around our controls. That's something we'll have to experiment with and work with our users to achieve. If we end up with something that works, I think that'd be valuable to contribute back to
contrib!Short-term, I think we'd either need to break the
--privilegedflag or disable AppArmor integration to move forward. We can disable AppArmor integration with AppArmor itself:So to circle back to the original ask, would others find it useful for us to add a proper flag to the daemon to manage the level of LSM integration?
cpuguy83 commentedon Oct 20, 2020
You can also build dockerd without the
apparmorbuild tag.I realize this is painful if you aren't already building it.
ericchiang commentedon Oct 22, 2020
Okay, so it sounds like there are workarounds for this issue and corresponding effort in #41442. I'm going to go ahead and close this out.
cyphar@ feel free to CC me on any AppArmor related changes if you'd like feedback. And as we get further in our experiments, I'll look to see if we can add anything to
contrib.adamnovak commentedon Dec 1, 2022
#41442 seems to have stalled.
I'm running into a problem related to this where, after moving a Kubernetes setup from CentOS 7 hosts (which as far as I can tell don't have AppArmor), to Ubuntu hosts (which do), I'm running into apptainer/singularity#5857 and I can no longer start a Singularity container using user-mode namespaces from within an unprivileged, default-confined Kubernetes container (running via Docker). I get a log from Singularity like:
So something Singularity needs to do to set up its user-mode namespaces isn't being allowed. (It might be something cgroups v2 related.)
Many of the workloads on my cluster need to use user-mode namespaces, and the point of user-mode namespaces is that you can do them without having root or setuid root, let alone unconfined access to the kernel, so it should be safe to allow the container to do what it is trying to do here.
If I set an annotation in the Kubernetes pod metadata of
container.apparmor.security.beta.kubernetes.io/<whatever-my-container-name-is>: unconfined, I get back the ability tosingularity exec -ueven without making the container privileged.If I go onto the host after starting the container and
sudo aa-teardownto disconnect all the profiles from all the running processes, then in the container I get back my ability tosingularity exec -u. But when the next container starts, Docker applies itsdocker-defaultprofile to it, so this isn't a real solution.What I want to do is turn off Docker's control of AppArmor altogether, and maybe eventually swap the system over to SELinux instead of AppArmor, since that worked on CentOS. But there doesn't seem to be a way to tell Docker not to use AppArmor at all, or even to change the default profile applied.
cyphar commentedon Dec 8, 2022
At runtime, the only way to disable it is to disable AppArmor entirely on the system (or uninstalling
apparmor_parser). The only other way to disable it is at compile-time by removing the "apparmor" build tag.As for #41442, I can revive the PR.
ericchiang commentedon Dec 8, 2022
For what it's worth, the workaround where we disable Docker's view of the AppArmor filesystem described in #41553 (comment) worked for us to disable Docker's AppArmor integration.
Gunni commentedon Apr 15, 2024
I found a way to disable apparmor on docker.
Just set the environment variable
containerto any value.$ systemctl edit dockerhttps://github.com/containerd/containerd/blob/8317959018015f6a1756ec8cd08be1093fd630a2/pkg/apparmor/apparmor_linux.go#L37
It entirely disabled apparmor for docker and does not even try to load the docker-default apparmor profile.
Good thing I found that, i was going to have to diable apparmor on the host if this wasn't a thing.
Because when I try to stop docker containers, i get this.