|
| 1 | +# AppArmor profile for running codejail-service in devstack. |
| 2 | +# |
| 3 | +# #=========# |
| 4 | +# # WARNING # |
| 5 | +# #=========# |
| 6 | +# |
| 7 | +# This is not a complete and secure apparmor profile! Do not use this |
| 8 | +# in any deployed environment (even a staging environment) without |
| 9 | +# careful inspection and modification to fit your needs. |
| 10 | +# |
| 11 | +# See https://manpages.ubuntu.com/manpages/noble/man5/apparmor.d.5.html |
| 12 | +# or `man apparmor.d` for documentation of syntax and options. |
| 13 | +# |
| 14 | +# Failure to apply a secure apparmor profile *will* likely result in a |
| 15 | +# compromise of your environment by an attacker. |
| 16 | +# |
| 17 | +# We may at some point make this file good enough for confinement in |
| 18 | +# production, but for now it is only intended to be used in devstack. |
| 19 | + |
| 20 | + |
| 21 | + |
| 22 | +# Sets standard variables used by abstractions/base, later. Controlled |
| 23 | +# by OS, see /etc/apparmor.d/tunables/global for contents. |
| 24 | +include <tunables/global> |
| 25 | + |
| 26 | +# Require that the system understands the feature set that this policy was written |
| 27 | +# for. If we didn't include this, then on Ubuntu >= 22.04, AppArmor might assume |
| 28 | +# the wrong feature set was requested, and some rules might become too permissive. |
| 29 | +# See https://github.com/netblue30/firejail/issues/3659#issuecomment-711074899 |
| 30 | +abi <abi/3.0>, |
| 31 | + |
| 32 | +# This outer profile applies to the entire container, and isn't as |
| 33 | +# important as the inner (codejail_sandbox) profile. If the inner profile doesn't work, it's not likely that |
| 34 | +# the outer one is going to help. But there may be some small value in |
| 35 | +# defense-in-depth, as it's possible that a bug in the codejail_sandbox (inner) |
| 36 | +# profile isn't present in the outer one. |
| 37 | +profile codejail_service flags=(mediate_deleted) { |
| 38 | + |
| 39 | + # Allow access to a variety of commonly needed, generally safe things |
| 40 | + # (such as reading /dev/random, free memory, etc.) |
| 41 | + # |
| 42 | + # Manpage: "Includes files that should be readable and writable in all profiles." |
| 43 | + include <abstractions/base> |
| 44 | + |
| 45 | + # Filesystem access -- self-explanatory |
| 46 | + file, |
| 47 | + |
| 48 | + # netlink is needed for sudo's interprocess communication |
| 49 | + network netlink raw, |
| 50 | + |
| 51 | + # Allow all of the various network operations required to listen, accept connection, etc. |
| 52 | + network tcp, |
| 53 | + # But then deny making a new *outbound* connection. |
| 54 | + deny network (connect) tcp, |
| 55 | + |
| 56 | + # Required for sudoing to sandbox |
| 57 | + capability setuid setgid audit_write, |
| 58 | + # Allow sending a kill signal |
| 59 | + capability kill, |
| 60 | + |
| 61 | + # Allow sending a kill signal to the codejail_sandbox subprofile when the execution |
| 62 | + # runs beyond time limits. |
| 63 | + signal (send) set=(kill) peer=codejail_service//codejail_sandbox, |
| 64 | + |
| 65 | + # The core of the confinement: When the sandbox Python is executed, switch to |
| 66 | + # the (extremely constrained) codejail_sandbox profile. |
| 67 | + # |
| 68 | + # This path needs to be coordinated with the Dockerfile and Django settings. |
| 69 | + # |
| 70 | + # Manpage: "Cx: transition to subprofile on execute -- scrub the environment" |
| 71 | + /sandbox/venv/bin/python Cx -> codejail_sandbox, |
| 72 | + |
| 73 | + # This is the important apparmor profile -- the one that actually |
| 74 | + # constrains the sandbox Python process. |
| 75 | + # |
| 76 | + # mediate_deleted is not well documented, but it seems to indicate that |
| 77 | + # apparmor will continue to make policy decisions in cases where a confined |
| 78 | + # executable has a handle to a file's inode even after the file is removed |
| 79 | + # from the filesystem. |
| 80 | + profile codejail_sandbox flags=(mediate_deleted) { |
| 81 | + |
| 82 | + # This inner profile also gets general access to "safe" |
| 83 | + # actions; we could list those explicitly out of caution but |
| 84 | + # it could get pretty verbose. |
| 85 | + include <abstractions/base> |
| 86 | + |
| 87 | + # Read and run binaries and libraries in the virtualenv. This |
| 88 | + # includes the sandbox's copy of Python as well as any |
| 89 | + # dependencies that have been installed for inclusion in |
| 90 | + # sandboxes. |
| 91 | + # |
| 92 | + # m: executable mapping, required for shared libraries used by some |
| 93 | + # Python dependencies with C compontents, eg `nltk`. |
| 94 | + /sandbox/venv/** rm, |
| 95 | + |
| 96 | + # Allow access to the temporary directories that are set up by |
| 97 | + # codejail, one for each code-exec call. Each /tmp/code-XXXXX |
| 98 | + # contains one execution. |
| 99 | + # |
| 100 | + # Codejail has a hardcoded reference to this file path, although the |
| 101 | + # use of /tmp specifically may be controllable with environment variables: |
| 102 | + # https://github.com/openedx/codejail/blob/0165d9ca351/codejail/util.py#L15 |
| 103 | + /tmp/codejail-*/ r, |
| 104 | + /tmp/codejail-*/** rw, |
| 105 | + |
| 106 | + # Allow interactive terminal in devstack. |
| 107 | + /dev/pts/* rw, |
| 108 | + |
| 109 | + # Allow receiving a kill signal from the webapp when the execution |
| 110 | + # runs beyond time limits. |
| 111 | + signal (receive) set=(kill) peer=codejail_service, |
| 112 | + } |
| 113 | +} |
0 commit comments