tutor-contrib-codejail 21.0.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,4 @@
1
+ ENABLE_CODEJAIL_REST_SERVICE: true
2
+ CODE_JAIL_REST_SERVICE_HOST: "http://{{ CODEJAIL_HOST }}:8550"
3
+ CODE_JAIL_REST_SERVICE_CONNECT_TIMEOUT: 0.5
4
+ CODE_JAIL_REST_SERVICE_READ_TIMEOUT: 3.5
@@ -0,0 +1,133 @@
1
+ ---
2
+ {% if CODEJAIL_USE_SERVICE_V2 %}
3
+ apiVersion: apps/v1
4
+ kind: Deployment
5
+ metadata:
6
+ name: codejailservice
7
+ labels:
8
+ app.kubernetes.io/name: codejailservice
9
+ spec:
10
+ selector:
11
+ matchLabels:
12
+ app.kubernetes.io/name: codejailservice
13
+ template:
14
+ metadata:
15
+ labels:
16
+ app.kubernetes.io/name: codejailservice
17
+ spec:
18
+ securityContext:
19
+ appArmorProfile:
20
+ type: Localhost
21
+ localhostProfile: openedx_codejail_service
22
+ containers:
23
+ - name: codejailservice
24
+ image: {{ CODEJAIL_DOCKER_IMAGE_V2 }}
25
+ ports:
26
+ - containerPort: 8550
27
+ env:
28
+ - name: DJANGO_SETTINGS_MODULE
29
+ value: codejail_service.settings.tutor
30
+ volumeMounts:
31
+ - mountPath: /app/codejail_service/settings/tutor.py
32
+ name: settings-codejail
33
+ subPath: tutor.py
34
+ volumes:
35
+ - name: settings-codejail
36
+ configMap:
37
+ name: settings-codejail
38
+ {% else %}
39
+ apiVersion: apps/v1
40
+ kind: Deployment
41
+ metadata:
42
+ name: codejailservice
43
+ labels:
44
+ app.kubernetes.io/name: codejailservice
45
+ spec:
46
+ selector:
47
+ matchLabels:
48
+ app.kubernetes.io/name: codejailservice
49
+ template:
50
+ metadata:
51
+ labels:
52
+ app.kubernetes.io/name: codejailservice
53
+ spec:
54
+ {% if CODEJAIL_ENFORCE_APPARMOR %}
55
+ securityContext:
56
+ appArmorProfile:
57
+ type: Localhost
58
+ localhostProfile: docker-edx-sandbox
59
+ {% endif %}
60
+ containers:
61
+ - name: codejailservice
62
+ image: {{ CODEJAIL_DOCKER_IMAGE }}
63
+ ports:
64
+ - containerPort: 8550
65
+ env:
66
+ - name: FLASK_APP_SETTINGS
67
+ value: codejailservice.tutor.ProductionConfig
68
+ volumeMounts:
69
+ - mountPath: /openedx/codejailservice/codejailservice/tutor.py
70
+ name: settings-codejail
71
+ subPath: tutor.py
72
+ volumes:
73
+ - name: settings-codejail
74
+ configMap:
75
+ name: settings-codejail
76
+ {% endif %}
77
+ {% if CODEJAIL_ENABLE_K8S_DAEMONSET %}
78
+ ---
79
+ apiVersion: apps/v1
80
+ kind: DaemonSet
81
+ metadata:
82
+ name: codejail-aa-loader
83
+ labels:
84
+ app.kubernetes.io/name: codejail-aa-loader
85
+ spec:
86
+ selector:
87
+ matchLabels:
88
+ app.kubernetes.io/name: codejail-aa-loader
89
+ template:
90
+ metadata:
91
+ name: codejail-aa-loader
92
+ labels:
93
+ app.kubernetes.io/name: codejail-aa-loader
94
+ spec:
95
+ containers:
96
+ - name: apparmor-loader
97
+ image: {{ CODEJAIL_APPARMOR_DOCKER_IMAGE }}
98
+ command:
99
+ - /usr/bin/loader
100
+ - -logtostderr
101
+ - -v=2
102
+ args:
103
+ # Tell the loader to pull the /profiles directory every 30 seconds.
104
+ - -poll
105
+ - 30s
106
+ - /profiles
107
+ securityContext:
108
+ # The loader requires root permissions to actually load the profiles.
109
+ privileged: true
110
+ volumeMounts:
111
+ - name: sys
112
+ mountPath: /sys
113
+ readOnly: true
114
+ - name: apparmor-includes
115
+ mountPath: /etc/apparmor.d
116
+ readOnly: true
117
+ - name: profiles
118
+ mountPath: /profiles
119
+ readOnly: true
120
+ volumes:
121
+ # The /sys directory must be mounted to interact with the AppArmor module.
122
+ - name: sys
123
+ hostPath:
124
+ path: /sys
125
+ # The /etc/apparmor.d directory is required for most apparmor include templates.
126
+ - name: apparmor-includes
127
+ hostPath:
128
+ path: /etc/apparmor.d
129
+ # Map in the profile data.
130
+ - name: profiles
131
+ configMap:
132
+ name: codejail-profile
133
+ {% endif %}
@@ -0,0 +1,14 @@
1
+ ---
2
+ apiVersion: v1
3
+ kind: Service
4
+ metadata:
5
+ name: codejailservice
6
+ labels:
7
+ app.kubernetes.io/name: codejailservice
8
+ spec:
9
+ type: ClusterIP
10
+ ports:
11
+ - port: 8550
12
+ protocol: TCP
13
+ selector:
14
+ app.kubernetes.io/name: codejailservice
@@ -0,0 +1,17 @@
1
+ - name: codejail-profile
2
+ files:
3
+ - plugins/codejail/apps/profiles/docker-edx-sandbox.profile
4
+ - plugins/codejail/apps/profiles/openedx-codejail-service.profile
5
+ options:
6
+ labels:
7
+ app.kubernetes.io/name: codejail-aa-loader
8
+ - name: settings-codejail
9
+ files:
10
+ {% if CODEJAIL_USE_SERVICE_V2 %}
11
+ - plugins/codejail/apps/codejail-service-v2/tutor.py
12
+ {% else %}
13
+ - plugins/codejail/apps/codejail/tutor.py
14
+ {% endif %}
15
+ options:
16
+ labels:
17
+ app.kubernetes.io/name: codejailservice
@@ -0,0 +1,4 @@
1
+ ENABLE_CODEJAIL_REST_SERVICE: true
2
+ CODE_JAIL_REST_SERVICE_HOST: "http://{{ CODEJAIL_HOST }}:8550"
3
+ CODE_JAIL_REST_SERVICE_CONNECT_TIMEOUT: 0.5
4
+ CODE_JAIL_REST_SERVICE_READ_TIMEOUT: 3.5
@@ -0,0 +1,8 @@
1
+ codejailservice:
2
+ command: flask run --host 0.0.0.0 --port 8550
3
+ environment:
4
+ FLASK_ENV: development
5
+ FLASK_APP_SETTINGS: codejailservice.tutor.DevelopmentConfig
6
+ ports:
7
+ - "8550:8550"
8
+ restart: unless-stopped
@@ -0,0 +1,45 @@
1
+ #############Codejail service
2
+ {% if CODEJAIL_USE_SERVICE_V2 %}
3
+ codejailservice:
4
+ image: {{ CODEJAIL_DOCKER_IMAGE_V2 }}
5
+ ports:
6
+ - 8550:8550
7
+ environment:
8
+ DJANGO_SETTINGS_MODULE: codejail_service.settings.tutor
9
+ security_opt:
10
+ - apparmor:openedx_codejail_service
11
+ volumes:
12
+ - ../plugins/codejail/apps/codejail-service-v2/tutor.py:/app/codejail_service/settings/tutor.py:ro
13
+ restart: unless-stopped
14
+ depends_on:
15
+ codejail-apparmor-loader:
16
+ condition: service_completed_successfully
17
+ {% else %}
18
+ codejailservice:
19
+ image: {{ CODEJAIL_DOCKER_IMAGE }}
20
+ environment:
21
+ FLASK_APP_SETTINGS: codejailservice.tutor.ProductionConfig
22
+ {% if CODEJAIL_ENFORCE_APPARMOR %}
23
+ security_opt:
24
+ - apparmor:docker-edx-sandbox
25
+ {% endif %}
26
+ volumes:
27
+ - ../plugins/codejail/apps/codejail/tutor.py:/openedx/codejailservice/codejailservice/tutor.py:ro
28
+ restart: unless-stopped
29
+ depends_on:
30
+ codejail-apparmor-loader:
31
+ condition: service_completed_successfully
32
+ {% endif %}
33
+
34
+ codejail-apparmor-loader:
35
+ image: {{ CODEJAIL_APPARMOR_DOCKER_IMAGE }}
36
+ privileged: true
37
+ command:
38
+ - /usr/bin/loader
39
+ - -logtostderr
40
+ - -v=2
41
+ - /profiles
42
+ volumes:
43
+ - ../plugins/codejail/apps/profiles/:/profiles/:ro
44
+ - /sys:/sys
45
+ - /etc/apparmor.d:/etc/apparmor.d
@@ -0,0 +1,150 @@
1
+ """Manage the plugin for the tutorcodejail."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import os
6
+ from glob import glob
7
+ from pathlib import Path
8
+
9
+ import importlib_resources
10
+ from tutor import hooks
11
+ from tutor.types import Config
12
+
13
+ from .__about__ import __version__
14
+
15
+ ABI_PATH = "/etc/apparmor.d/abi"
16
+
17
+ config = {
18
+ "unique": {
19
+ "SECRET_KEY": "{{ 24|random_string }}",
20
+ },
21
+ "defaults": {
22
+ "APPARMOR_DOCKER_IMAGE": "docker.io/ednxops/codejail_apparmor_loader:apparmor-4",
23
+ "DOCKER_IMAGE": f"docker.io/ednxops/codejailservice:{__version__}",
24
+ "DOCKER_IMAGE_V2": "{{ CODEJAIL_DOCKER_IMAGE }}-v2",
25
+ "ENABLE_K8S_DAEMONSET": False,
26
+ "ENFORCE_APPARMOR": True,
27
+ "EXTRA_PIP_REQUIREMENTS": [],
28
+ "HOST": "codejailservice",
29
+ "SANDBOX_PYTHON_VERSION": "3.11.14",
30
+ "SERVICE_REPOSITORY": "https://github.com/edunext/codejailservice.git",
31
+ "SERVICE_V2_REPOSITORY": "https://github.com/openedx/codejail-service.git",
32
+ "SERVICE_V2_VERSION": "{{ OPENEDX_COMMON_VERSION }}",
33
+ "SERVICE_VERSION": "{{ OPENEDX_COMMON_VERSION }}",
34
+ "SKIP_INIT": False,
35
+ "USE_SERVICE_V2": False,
36
+ "VERSION": __version__,
37
+ },
38
+ "overrides": {},
39
+ }
40
+
41
+
42
+ def get_apparmor_abi():
43
+ """
44
+ Return the latest default abi rule if available in the system.
45
+
46
+ AppArmor uses the Policy feature ABI to establish which rules it can
47
+ enforce based on the kernel capabilities. AppArmor profiles can include an
48
+ ABI rule to indicate the ABI they were developed under. If no rule is used
49
+ AppArmor will fallback to whichever rule is pinned in the
50
+ `/etc/apparmor/parser.conf` file.
51
+
52
+ We try to use at least the 3.0 abi whenever it's available at `/etc/apparmor.d/abi/`
53
+ to guarantee that network rules are correctly enforced on newer versions of
54
+ the kernel. If neither the 3.0 ABI nor the 4.0 ABI are present we don't set
55
+ the abi rule and instead rely on the default fallback.
56
+
57
+ See: https://github.com/netblue30/firejail/issues/3659#issuecomment-711074899
58
+ """
59
+ if Path(f"{ABI_PATH}/4.0").exists():
60
+ return "abi <abi/4.0>,"
61
+
62
+ if Path(f"{ABI_PATH}/3.0").exists():
63
+ return "abi <abi/3.0>,"
64
+
65
+ return ""
66
+
67
+
68
+ hooks.Filters.ENV_TEMPLATE_VARIABLES.add_items(
69
+ [
70
+ ("get_apparmor_abi", get_apparmor_abi()),
71
+ ]
72
+ )
73
+
74
+
75
+ @hooks.Filters.IMAGES_BUILD.add()
76
+ def _build_codejail_images(
77
+ images: list[tuple[str, str | tuple[str, ...], str, tuple[str, ...]]],
78
+ tutor_config: Config,
79
+ ):
80
+ """Choose the appropiate build context when using CODEJAIL_USE_SERVICE_V2."""
81
+ # TODO: Remove after the Verawood update
82
+ if tutor_config.get("CODEJAIL_USE_SERVICE_V2"):
83
+ codejail_img = (
84
+ "codejail",
85
+ "plugins/codejail/build/codejail-service",
86
+ "{{ CODEJAIL_DOCKER_IMAGE_V2 }}",
87
+ (),
88
+ )
89
+ else:
90
+ codejail_img = (
91
+ "codejail",
92
+ "plugins/codejail/build/codejail",
93
+ "{{ CODEJAIL_DOCKER_IMAGE }}",
94
+ (),
95
+ )
96
+ apparmor_img = (
97
+ "codejail_apparmor",
98
+ ("plugins", "codejail", "build", "codejail_apparmor"),
99
+ "{{CODEJAIL_APPARMOR_DOCKER_IMAGE}}",
100
+ (),
101
+ )
102
+
103
+ return images + [codejail_img, apparmor_img]
104
+
105
+
106
+ @hooks.Filters.IMAGES_PUSH.add()
107
+ def _push_codejail_images(
108
+ images: list[tuple[str, str]],
109
+ tutor_config: Config,
110
+ ):
111
+ """Choose the appropiate image tag when using CODEJAIL_USE_SERVICE_V2."""
112
+ # TODO: Remove after the Verawood update
113
+ if tutor_config.get("CODEJAIL_USE_SERVICE_V2"):
114
+ codejail_img = (
115
+ "codejail",
116
+ "{{ CODEJAIL_DOCKER_IMAGE_V2 }}",
117
+ )
118
+ else:
119
+ codejail_img = (
120
+ "codejail",
121
+ "{{ CODEJAIL_DOCKER_IMAGE }}",
122
+ )
123
+ apparmor_img = (
124
+ "codejail_apparmor",
125
+ "{{CODEJAIL_APPARMOR_DOCKER_IMAGE}}",
126
+ )
127
+ return images + [codejail_img, apparmor_img]
128
+
129
+
130
+ # Boilerplate code
131
+ # Add the "templates" folder as a template root
132
+ hooks.Filters.ENV_TEMPLATE_ROOTS.add_item(str(importlib_resources.files("tutorcodejail") / "templates"))
133
+ # Render the "build" and "apps" folders
134
+ hooks.Filters.ENV_TEMPLATE_TARGETS.add_items(
135
+ [
136
+ ("codejail/build", "plugins"),
137
+ ("codejail/apps", "plugins"),
138
+ ("codejail/k8s", "plugins"),
139
+ ],
140
+ )
141
+ # Load patches from files
142
+ for path in glob(str(importlib_resources.files("tutorcodejail") / "patches" / "*")):
143
+ with open(path, encoding="utf-8") as patch_file:
144
+ hooks.Filters.ENV_PATCHES.add_item((os.path.basename(path), patch_file.read()))
145
+ # Add configuration entries
146
+ hooks.Filters.CONFIG_DEFAULTS.add_items(
147
+ [(f"CODEJAIL_{key}", value) for key, value in config.get("defaults", {}).items()]
148
+ )
149
+ hooks.Filters.CONFIG_UNIQUE.add_items([(f"CODEJAIL_{key}", value) for key, value in config.get("unique", {}).items()])
150
+ hooks.Filters.CONFIG_OVERRIDES.add_items(list(config.get("overrides", {}).items()))
File without changes
@@ -0,0 +1,18 @@
1
+ """Module with the configuration of config classes."""
2
+ from codejailservice.config import DevelopmentConfig, ProductionConfig
3
+
4
+
5
+ class DevelopmentConfig(DevelopmentConfig):
6
+ """Class to use for development context that inherits from DevelopmentConfig."""
7
+
8
+ CODE_JAIL = DevelopmentConfig.CODE_JAIL
9
+ {{patch("codejail-common-settings") | indent(4)}}
10
+ {{patch("codejail-development-settings") | indent(4)}}
11
+
12
+
13
+ class ProductionConfig(ProductionConfig):
14
+ """Class to use for production context that inherits from ProductionConfig."""
15
+
16
+ CODE_JAIL = ProductionConfig.CODE_JAIL
17
+ {{patch("codejail-common-settings") | indent(4)}}
18
+ {{patch("codejail-production-settings") | indent(4)}}
@@ -0,0 +1,34 @@
1
+ from codejail_service.settings.local import * # pylint: disable=wildcard-import
2
+
3
+ ALLOWED_HOSTS = [
4
+ 'codejailservice',
5
+ 'localhost',
6
+ ]
7
+
8
+ CODEJAIL_ENABLED = True
9
+ SECRET_KEY = '{{ CODEJAIL_SECRET_KEY }}'
10
+
11
+ CODE_JAIL = {
12
+ 'python_bin': '/sandbox/venv/bin/python',
13
+ 'user': 'sandbox',
14
+
15
+ # Configurable limits.
16
+ 'limits': {
17
+ # CPU-seconds
18
+ 'CPU': 3,
19
+ # Clock seconds
20
+ 'REALTIME': 3,
21
+ # Need at least 300 MiB memory for matplotlib alone. 512 MiB should be
22
+ # enough headroom in general.
23
+ 'VMEM': 512 * 1024 * 1024,
24
+ # 10 MB file size limit
25
+ 'FSIZE': 10 * 1024 * 1024,
26
+ # 15 processes and threads (codejail default)
27
+ 'NPROC': 15,
28
+ # Don't use a proxy process to spawn subprocesses.
29
+ 'PROXY': 0,
30
+ },
31
+ }
32
+
33
+ {{ patch("codejail-common-settings") }}
34
+ {{ patch("codejail-production-settings") }}
@@ -0,0 +1,80 @@
1
+ #include <tunables/global>
2
+
3
+ {{ get_apparmor_abi }}
4
+
5
+ profile docker-edx-sandbox flags=(attach_disconnected,mediate_deleted) {
6
+ #include <abstractions/base>
7
+
8
+ network,
9
+ capability,
10
+ file,
11
+ umount,
12
+ signal (receive) peer=unconfined,
13
+ signal (receive) peer=cri-containerd.apparmor.d,
14
+ signal (send,receive) peer=docker-edx-sandbox,
15
+
16
+ deny @{PROC}/* w, # deny write for all files directly in /proc (not in a subdir)
17
+ # deny write to files not in /proc/<number>/** or /proc/sys/**
18
+ deny @{PROC}/{[^1-9],[^1-9][^0-9],[^1-9s][^0-9y][^0-9s],[^1-9][^0-9][^0-9][^0-9]*}/** w,
19
+ deny @{PROC}/sys/[^k]** w, # deny /proc/sys except /proc/sys/k* (effectively /proc/sys/kernel)
20
+ deny @{PROC}/sys/kernel/{?,??,[^s][^h][^m]**} w, # deny everything except shm* in /proc/sys/kernel/
21
+ deny @{PROC}/sysrq-trigger rwklx,
22
+ deny @{PROC}/mem rwklx,
23
+ deny @{PROC}/kmem rwklx,
24
+ deny @{PROC}/kcore rwklx,
25
+
26
+ deny mount,
27
+ deny /sys/[^f]*/** wklx,
28
+ deny /sys/f[^s]*/** wklx,
29
+ deny /sys/fs/[^c]*/** wklx,
30
+ deny /sys/fs/c[^g]*/** wklx,
31
+ deny /sys/fs/cg[^r]*/** wklx,
32
+ deny /sys/firmware/** rwklx,
33
+ deny /sys/kernel/security/** rwklx,
34
+
35
+ ptrace (trace,read) peer=docker-edx-sandbox,
36
+
37
+ /sandbox/venv/bin/python Cx -> child,
38
+ profile child flags=(attach_disconnected,mediate_deleted){
39
+ #include <abstractions/base>
40
+
41
+ #
42
+ # Python abstractions adapted from https://gitlab.com/apparmor/apparmor/-/raw/master/profiles/apparmor.d/abstractions/python
43
+ #
44
+ /opt/pyenv/versions/{3.[0-9],3.[1-9][0-9]}.*/lib/python{3.[0-9],3.[1-9][0-9]}/**.{pyc,so} mr,
45
+ /opt/pyenv/versions/{3.[0-9],3.[1-9][0-9]}.*/lib/python{3.[0-9],3.[1-9][0-9]}/**.{egg,py,pth} r,
46
+ /opt/pyenv/versions/{3.[0-9],3.[1-9][0-9]}.*/lib/python{3.[0-9],3.[1-9][0-9]}/site-packages/ r,
47
+ /opt/pyenv/versions/{3.[0-9],3.[1-9][0-9]}.*/lib/python{3.[0-9],3.[1-9][0-9]}/lib-dynload/*.so mr,
48
+ /opt/pyenv/versions/{3.[0-9],3.[1-9][0-9]}.*/include/python{3.[0-9],3.[1-9][0-9]}*/pyconfig.h r,
49
+
50
+ #
51
+ # Whitelist particiclar shared objects from the system
52
+ # python installation
53
+ #
54
+ /sandbox/venv/** mr,
55
+ /opt/pyenv/versions/{3.[0-9],3.[1-9][0-9]}.*_sandbox/** mr,
56
+ /tmp/codejail-*/ rix,
57
+ /tmp/codejail-*/** wrix,
58
+ /tmp/* wrix,
59
+
60
+ #
61
+ # Whitelist particular shared objects from the system
62
+ # python installation
63
+ #
64
+ /sandbox/venv/.config/ wrix,
65
+ /sandbox/venv/.cache/ wrix,
66
+ /sandbox/venv/.config/** wrix,
67
+ /sandbox/venv/.cache/** wrix,
68
+
69
+ # Matplot lib needs fonts to make graphs
70
+ /usr/share/fonts/ r,
71
+ /usr/share/fonts/** r,
72
+ /usr/local/share/fonts/ r,
73
+ /usr/local/share/fonts/** r,
74
+
75
+ #
76
+ # Allow access to selections from /proc
77
+ #
78
+ /proc/*/mounts r,
79
+ }
80
+ }
@@ -0,0 +1,132 @@
1
+ # AppArmor profile for running codejail-service.
2
+ #
3
+ # Changes to this profile must be coordinated carefully with changes to the
4
+ # Dockerfile. See README for additional cautions.
5
+ #
6
+ # #=========#
7
+ # # WARNING #
8
+ # #=========#
9
+ #
10
+ # Failure to apply a secure apparmor profile *will* likely result in a
11
+ # full compromise of the host by an attacker. AppArmor is *mandatory*
12
+ # for using codejail -- this is not just for hardening.
13
+ #
14
+ # This profile is written for AppArmor 4 or higher (Ubuntu 24.04 or higher).
15
+
16
+
17
+
18
+ # Sets standard variables used by abstractions/base, later. Controlled
19
+ # by OS, see /etc/apparmor.d/tunables/global for contents.
20
+ include <tunables/global>
21
+
22
+ # Require that the system understands the feature set that this policy was written
23
+ # for. If we didn't include this, then on Ubuntu >= 22.04, AppArmor might assume
24
+ # the wrong feature set was requested, and some rules might become too permissive.
25
+ # See https://github.com/netblue30/firejail/issues/3659#issuecomment-711074899
26
+ abi <abi/4.0>,
27
+
28
+ # This outer profile applies to the entire container, and isn't as
29
+ # important as the inner (codejail_sandbox) profile. If the inner profile doesn't work, it's not likely that
30
+ # the outer one is going to help. But there may be some small value in
31
+ # defense-in-depth, as it's possible that a bug in the codejail_sandbox (inner)
32
+ # profile isn't present in the outer one.
33
+ #
34
+ # The use of `attach_disconnected.path` is required in the outer profile to
35
+ # allow accessing the unix domain socket that Datadog creates at
36
+ # `/var/run/datadog/apm.socket`. This is a fd that is not mounted in the
37
+ # filesystem (AppArmor sees it as `var/run/datadog/apm.socket` with no leading
38
+ # slash) and so this is our only option for allowing AppArmor to mediate (and
39
+ # allow) access to it. With this options, we would be able to safely refer in
40
+ # the profile to `/disconnected/var/run/datadog/apm.socket`. Other disconnected
41
+ # objects could masquerade as this one, but not as paths in the wider
42
+ # filesystem. WARNING: Plain `attach_disconnected` (without the path) *would not
43
+ # be safe* in any profile that includes file paths, as it can create dangerous
44
+ # and hard-to-understand path aliasing for disconnected objects. However, the
45
+ # outer profile already broadly allows `file`, and the only path mentioned is
46
+ # for sandbox execution, which is a transition to a *more* restrictive
47
+ # profile. It might be acceptable, but the `.path` version (new in AppArmor 4)
48
+ # is much safer.
49
+ profile openedx_codejail_service flags=(mediate_deleted, attach_disconnected.path=/disconnected) {
50
+
51
+ # Allow access to a variety of commonly needed, generally safe things
52
+ # (such as reading /dev/random, free memory, etc.)
53
+ #
54
+ # Manpage: "Includes files that should be readable and writable in all profiles."
55
+ include <abstractions/base>
56
+
57
+ # Filesystem access -- self-explanatory
58
+ file,
59
+
60
+ # netlink is needed for sudo's interprocess communication
61
+ network netlink raw,
62
+
63
+ # Allow all of the various network operations required to listen, accept connection, etc.
64
+ network tcp,
65
+ # But then deny making a new *outbound* connection.
66
+ deny network (connect) tcp,
67
+
68
+ # Required for sudoing to sandbox
69
+ capability setuid setgid audit_write,
70
+ # Allow sending a kill signal
71
+ capability kill,
72
+
73
+ # Allow sending a kill signal to the codejail_sandbox subprofile when the execution
74
+ # runs beyond time limits.
75
+ signal (send) set=(kill) peer=openedx_codejail_service//codejail_sandbox,
76
+
77
+ # Allow receiving a kill signal (and other signals) from container orchestration.
78
+ signal (receive),
79
+
80
+ # The core of the confinement: When the sandbox Python is executed, switch to
81
+ # the (extremely constrained) codejail_sandbox profile.
82
+ #
83
+ # This path needs to be coordinated with the Dockerfile and Django settings.
84
+ #
85
+ # Manpage: "Cx: transition to subprofile on execute -- scrub the environment"
86
+ /sandbox/venv/bin/python Cx -> codejail_sandbox,
87
+
88
+ # This is the important apparmor profile -- the one that actually
89
+ # constrains the sandbox Python process.
90
+ #
91
+ # `mediate_deleted` instructs apparmor to continue to make policy decisions
92
+ # in cases where a confined executable has a file descriptor even after the
93
+ # file is removed from the filesystem. It's unclear if this is important for
94
+ # sandboxing, but it doesn't seem like it would loosen security or interfere
95
+ # with functionality to include it, so we have.
96
+ #
97
+ # `no_attach_disconnected` is default, but is explicitly indicated on the
98
+ # inner profile because `attach_disconnected` is very commonly used in
99
+ # example profiles despite being a security risk (due to allowing
100
+ # disconnected objects to masquerade as other, trusted paths in the
101
+ # filesystem).
102
+ profile codejail_sandbox flags=(mediate_deleted, no_attach_disconnected) {
103
+
104
+ # This inner profile also gets general access to "safe"
105
+ # actions; we could list those explicitly out of caution but
106
+ # it could get pretty verbose.
107
+ include <abstractions/base>
108
+
109
+ # Read and run binaries and libraries in the virtualenv. This
110
+ # includes the sandbox's copy of Python as well as any
111
+ # dependencies that have been installed for inclusion in
112
+ # sandboxes.
113
+ #
114
+ # m: executable mapping, required for shared libraries used by some
115
+ # Python dependencies with C compontents, eg `nltk`.
116
+ /sandbox/venv/** rm,
117
+
118
+ # Allow access to the temporary directories that are set up by
119
+ # codejail, one for each code-exec call. Each /tmp/code-XXXXX
120
+ # contains one execution.
121
+ #
122
+ # Codejail has a hardcoded reference to this file path, although the
123
+ # use of /tmp specifically may be controllable with environment variables:
124
+ # https://github.com/openedx/codejail/blob/0165d9ca351/codejail/util.py#L15
125
+ /tmp/codejail-*/ r,
126
+ /tmp/codejail-*/** rw,
127
+
128
+ # Allow receiving a kill signal from the webapp when the execution
129
+ # runs beyond time limits.
130
+ signal (receive) set=(kill) peer=openedx_codejail_service,
131
+ }
132
+ }
File without changes