title: AppArmor child profile not applied in Docker container
body: <p>I am trying to build a Docker image that will serve as a Python jailed sandbox. For that, I'd like to apply a certain AppArmor profile to my container. My problem is that I do not manage to apply the child &quot;python&quot; profile to the actual container.</p>
  <p>Here is how to reproduce the issue: first, write a &quot;docker-python-sandbox&quot; AppArmor profile with the following contents:</p>
  <pre><code>#include &lt;tunables/global&gt;
  
  
  profile docker-python-sandbox flags=(attach_disconnected,mediate_deleted) {
      ################# copy-pasted from apparmor.d/docker
      #include &lt;abstractions/base&gt;
      network,
      capability,
      file,
      umount,
  
      deny @{PROC}/* w,   # deny write for all files directly in /proc (not in a subdir)
      # deny write to files not in /proc/&lt;number&gt;/** or /proc/sys/**
      deny @{PROC}/{[^1-9],[^1-9][^0-9],[^1-9s][^0-9y][^0-9s],[^1-9][^0-9][^0-9][^0-9]*}/** w,
      deny @{PROC}/sys/[^k]** w,  # deny /proc/sys except /proc/sys/k* (effectively /proc/sys/kernel)
      deny @{PROC}/sys/kernel/{?,??,[^s][^h][^m]**} w,  # deny everything except shm* in /proc/sys/kernel/
      deny @{PROC}/sysrq-trigger rwklx,
      deny @{PROC}/mem rwklx,
      deny @{PROC}/kmem rwklx,
      deny @{PROC}/kcore rwklx,
  
      deny mount,
  
      deny /sys/[^f]*/** wklx,
      deny /sys/f[^s]*/** wklx,
      deny /sys/fs/[^c]*/** wklx,
      deny /sys/fs/c[^g]*/** wklx,
      deny /sys/fs/cg[^r]*/** wklx,
      deny /sys/firmware/efi/efivars/** rwklx,
      deny /sys/kernel/security/** rwklx,
  
      # suppress ptrace denials when using 'docker ps' or using 'ps' inside a container
      ptrace (trace,read) peer=docker-default,
      ################# end of copy-paste
  
      # deny access to all
      deny /tmp/forbidden-all w,
  
      # create python child profile
      /usr/bin/python cx,
      profile /usr/bin/python {
          deny /tmp/forbidden w,
      }
  }
  </code></pre>
  <p>The first part is copy-pasted from the boilerplate <a href="https://github.com/moby/moby/blob/master/profiles/apparmor/template.go" rel="nofollow noreferrer">docker-default</a> profile. In the second part, we deny write access to &quot;/tmp/forbidden-all&quot;. In addition, we (attempt to) deny write access to &quot;/tmp/forbidden&quot; to the python executable.</p>
  <p>Here's my attempt at applying this profile:</p>
  <pre><code>$ sudo apparmor_parser --replace ./docker-python-sandbox
  $ docker run --rm -it --security-opt apparmor=docker-python-sandbox python:3.8 /usr/bin/python -c 'open(&quot;/tmp/forbidden&quot;, &quot;w&quot;).write(&quot;h4cked&quot;); print(&quot;managed to write to forbidden file&quot;); open(&quot;/tmp/forbidden-all&quot;, &quot;w&quot;).write(&quot;h4cked&quot;)'
  managed to write to forbidden file
  Traceback (most recent call last):
    File &quot;&lt;string&gt;&quot;, line 1, in &lt;module&gt;
  IOError: [Errno 13] Permission denied: '/tmp/forbidden-all'
  </code></pre>
  <p>As we can see, we successfully prevented writing to &quot;/tmp/forbidden-all&quot;, but failed at preventing Python access to &quot;/tmp/forbidden&quot;.</p>
  <p>On the host, I can successfully create a profile that applies both these restrictions. This leads me to think that child profiles are ignored by the Docker container. Is this conclusion correct? Is it by design? If not, is there a way to address this?</p>