Skip to content

Add daemon option to disable AppArmor integration #41553

Closed

Description

@ericchiang

FEATURE REQUEST

Description

Currently, the dockerd binary always runs containers as docker-default if AppArmor is enabled on the host.

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
}

https://github.com/opencontainers/runc/blob/44f221e2fce70dd6a75e7d67e9bdde36706aef79/libcontainer/apparmor/apparmor.go#L15-L21

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

  1. AkihiroSuda commented on Oct 16, 2020

    @AkihiroSuda
    Member

    SGTM

  2. cyphar commented on Oct 16, 2020

    @cyphar
    Contributor

    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 word insecure in it -- so --insecure-apparmor or 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 using change_profile -- in fact that is what the profile does:

    {{if ge .Version 209000}}
      # Transitions
      change_profile -> docker-*,
      change_profile -> unconfined,
    {{end}}
    

    You could restrict this to not allow unconfined containers -- this isn't done in the example profile because disabling this makes --privileged containers not work. Being able to write to /proc/self/attr/exec is not really a security issue if you have change_profile restrictions (as far as I can tell) because:

    1. You can only ever write to your own /proc/$pid/attr/exec -- AppArmor doesn't allow you to change other programs' profiles.
    2. exec will 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.

  3. thaJeztah commented on Oct 16, 2020

    @thaJeztah
    Member

    this is kind of a problem with our default security profiles in general -- you also can't disable seccomp entirely on a daemon-level

    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=disabled don't manage, disable seccomp handling, do not allow seccomp profiles to be used
    • --seccomp-profile=unconfined use "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=myprofile use custom profile "myprofile" as default
  4. ericchiang commented on Oct 16, 2020

    @ericchiang
    Author

    Thanks @cyphar for the information here. I was able to get the change_profile restrictions working. Couple of notes on that:

    • runc is actually the process that needs change_profile restrictions, not dockerd.
    • We're running all processes under a custom apparmor profile, so change_profile breaks the --privileged flag. That's kinda working as intended, but ideally users that supply --privileged would just get our default profile, not have docker attempt to run it as unconfined and fail.
    • We'd have to ship a custom docker-default since we don't want docker to be able to load profiles (not a huge deal).

    So the options that would work for me are:

    • Implement @thaJeztah's suggestion to add a --seccomp-profile flag.
    • Allow some way of making --privileged mean "don't attempt to change the containers profile" rather than "run as unconfined".

    The --seccomp-profile seems like the least special-casing. Does that work for you @cyphar? Should I send a PR?

  5. thaJeztah commented on Oct 16, 2020

    @thaJeztah
    Member

    Allow some way of making --privileged mean "don't attempt to change the containers profile" rather than "run as unconfined".

    I'd rather not change what --privileged does. --privileged is 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 --privileged altogether (and thus require the user to (albeit cumbersome) specify what's needed) could be an option.

  6. ericchiang commented on Oct 16, 2020

    @ericchiang
    Author

    Sorry, I meant to say that I'd prefer adding --seccomp-profile instead of changing the way --privileged behaves.

    Disabling --privileged through 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.

  7. cyphar commented on Oct 17, 2020

    @cyphar
    Contributor

    @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 contrib configurations.

    runc is actually the process that needs change_profile restrictions, not dockerd.

    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 the contrib setup hence why it's omitted).

    We're running all processes under a custom apparmor profile, so change_profile breaks the --privileged flag. [...] Disabling --privileged through 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.

    It seems (at least to me) a little bit dodgy to permit users to pass the --privileged flag but not actually running privileged containers when they're requested. Often --privileged containers 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 how change_profile overrides interact with /proc/self/attr/exec changes off the top of my head). You can also force a profile change at pivot_root (with the pivot_root directive) and that might also override the /proc/self/attr/exec override -- 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 --privileged is 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...

  8. ericchiang commented on Oct 19, 2020

    @ericchiang
    Author

    The profile transition at pivot_root sounds 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 --privileged flag or disable AppArmor integration to move forward. We can disable AppArmor integration with AppArmor itself:

    profile dockerd /usr/bin/dockerd {
        // ...
        deny /sys/kernel/security/apparmor/**,
    }
    

    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?

  9. cpuguy83 commented on Oct 20, 2020

    @cpuguy83
    Member

    You can also build dockerd without the apparmor build tag.
    I realize this is painful if you aren't already building it.

  10. ericchiang commented on Oct 22, 2020

    @ericchiang
    Author

    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.

  11. adamnovak commented on Dec 1, 2022

    @adamnovak

    #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:

    VERBOSE [U=0,P=532]        user_namespace_init()         Create user namespace
    VERBOSE [U=0,P=532]        create_namespace()            Create user namespace
    VERBOSE [U=65534,P=532]    init()                        Spawn master process
    DEBUG   [U=65534,P=532]    setup_userns_mappings()       Write deny to setgroups file
    DEBUG   [U=65534,P=532]    setup_userns_mappings()       Write to GID map
    DEBUG   [U=65534,P=532]    setup_userns_mappings()       Write to UID map
    DEBUG   [U=0,P=551]        set_parent_death_signal()     Set parent death signal to 9
    VERBOSE [U=0,P=551]        create_namespace()            Create mount namespace
    ERROR   [U=0,P=551]        shared_mount_namespace_init() Failed to set mount propagation: Permission denied
    

    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 to singularity exec -u even without making the container privileged.

    If I go onto the host after starting the container and sudo aa-teardown to disconnect all the profiles from all the running processes, then in the container I get back my ability to singularity exec -u. But when the next container starts, Docker applies its docker-default profile 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.

  12. cyphar commented on Dec 8, 2022

    @cyphar
    Contributor

    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.

    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.

  13. ericchiang commented on Dec 8, 2022

    @ericchiang
    Author

    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.

  14. Gunni commented on Apr 15, 2024

    @Gunni

    I found a way to disable apparmor on docker.

    Just set the environment variable container to any value.

    $ systemctl edit docker

    [Service]
    Environment=container="disable apparmor"
    

    https://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.

    AVC apparmor="DENIED" operation="signal" class="signal" profile="docker-default" pid=2965 comm="runc" requested_mask="receive" denied_mask="receive" signal=kill peer="/usr/sbin/runc"
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions