From 0e88b92e4b59e2cf460fe1c9fa08a38a3a2acba0 Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Sat, 28 Dec 2019 13:31:06 -0800 Subject: Disable shared memory in Docker container --- docker-compose.yml | 1 + scripts/dev.sh | 1 + 2 files changed, 2 insertions(+) diff --git a/docker-compose.yml b/docker-compose.yml index d071a71..aeb2806 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -6,6 +6,7 @@ services: image: pythondiscord/snekbox:latest network_mode: "host" init: true + ipc: none build: context: . dockerfile: docker/Dockerfile diff --git a/scripts/dev.sh b/scripts/dev.sh index 6aeb1de..0275651 100755 --- a/scripts/dev.sh +++ b/scripts/dev.sh @@ -40,6 +40,7 @@ docker run \ --privileged \ --network host \ --hostname pdsnk-dev \ + --ipc="none" \ -e PYTHONDONTWRITEBYTECODE=1 \ -e PIPENV_PIPFILE="/snekbox/Pipfile" \ -e BASH_ENV="${PWD}/scripts/.profile" \ -- cgit v1.2.3 From b2fb654371a07a77ba4a39f11395836c6b593527 Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Sat, 28 Dec 2019 15:17:34 -0800 Subject: Mount only what's needed in the chroot jail devfs and sysfs were problematic since they were being mounted as tmpfs, which is r/w. For example, the Python process could write to cgroups. Now, only what is needed to run Python gets mounted. This boils down to the venv itself and some shared libraries Python needs. * Use a config file for NsJail instead of command-line options * Map 65534 (nobody) user & group inside the user namespace to 65534 outside the namespace rather than mapping to current uid/guid (which was 0 AKA root) --- .dockerignore | 2 +- README.md | 2 +- docker/venv.Dockerfile | 2 +- scripts/.profile | 18 +--------- snekbox.cfg | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++ snekbox/nsjail.py | 22 +++--------- 6 files changed, 99 insertions(+), 37 deletions(-) create mode 100644 snekbox.cfg diff --git a/.dockerignore b/.dockerignore index afc786a..4f43e08 100644 --- a/.dockerignore +++ b/.dockerignore @@ -2,8 +2,8 @@ * # Make exceptions for what's needed -!docker/.profile !snekbox +!snekbox.cfg !tests !Pipfile !Pipfile.lock diff --git a/README.md b/README.md index f1fcac5..d90609e 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ result <- | |<----------| | <----------+ The code is executed in a Python process that is launched through [NsJail](https://github.com/google/nsjail), which is responsible for sandboxing the Python process. NsJail is configured as follows: -* Root directory is mounted as read-only +* All mounts are read-only * Time limit of 2 seconds * Maximum of 1 PID * Maximum memory of 52428800 bytes diff --git a/docker/venv.Dockerfile b/docker/venv.Dockerfile index be15f08..b415430 100644 --- a/docker/venv.Dockerfile +++ b/docker/venv.Dockerfile @@ -7,7 +7,7 @@ ENV PIP_NO_CACHE_DIR=false \ PIPENV_NOSPIN=1 \ PIPENV_VENV_IN_PROJECT=1 -COPY Pipfile Pipfile.lock /snekbox/ +COPY Pipfile Pipfile.lock snekbox.cfg /snekbox/ WORKDIR /snekbox RUN if [ -n "${DEV}" ]; then pipenv sync --dev; else pipenv sync; fi diff --git a/scripts/.profile b/scripts/.profile index daaf1dd..47ee141 100644 --- a/scripts/.profile +++ b/scripts/.profile @@ -15,23 +15,7 @@ nsjpy() { echo "${MEM_MAX}" > /sys/fs/cgroup/memory/NSJAIL/memory.memsw.limit_in_bytes nsjail \ - -Mo \ - --rlimit_as 700 \ - --chroot / \ - -E LANG=en_US.UTF-8 \ - -E OMP_NUM_THREADS=1 \ - -E OPENBLAS_NUM_THREADS=1 \ - -E MKL_NUM_THREADS=1 \ - -E VECLIB_MAXIMUM_THREADS=1 \ - -E NUMEXPR_NUM_THREADS=1 \ - -R/usr -R/lib -R/lib64 \ - --user 65534 \ - --group 65534 \ - --time_limit 2 \ - --disable_proc \ - --iface_no_lo \ - --cgroup_pids_max=1 \ - --cgroup_mem_max="${MEM_MAX}" \ + --config "${NSJAIL_CFG:-/snekbox/snekbox.cfg}" \ $nsj_args -- \ /snekbox/.venv/bin/python3 -Iq -c "$@" } diff --git a/snekbox.cfg b/snekbox.cfg new file mode 100644 index 0000000..2f4a0e4 --- /dev/null +++ b/snekbox.cfg @@ -0,0 +1,90 @@ +name: "snekbox" +description: "Execute Python" + +mode: ONCE +hostname: "snekbox" +cwd: "/snekbox" + +time_limit: 2 + +keep_env: false +envar: "LANG=en_US.UTF-8" +envar: "OMP_NUM_THREADS=1" +envar: "OPENBLAS_NUM_THREADS=1" +envar: "MKL_NUM_THREADS=1" +envar: "VECLIB_MAXIMUM_THREADS=1" +envar: "NUMEXPR_NUM_THREADS=1" + +keep_caps: false + +rlimit_as: 700 + +clone_newnet: true +clone_newuser: true +clone_newns: true +clone_newpid: true +clone_newipc: true +clone_newuts: true +clone_newcgroup: true + +uidmap { + inside_id: "65534" + outside_id: "65534" +} + +gidmap { + inside_id: "65534" + outside_id: "65534" +} + +mount_proc: false + +mount { + src: "/etc/ld.so.cache" + dst: "/etc/ld.so.cache" + is_bind: true + rw: false +} + +mount { + src: "/lib" + dst: "/lib" + is_bind: true + rw: false +} + +mount { + src: "/lib64" + dst: "/lib64" + is_bind: true + rw: false +} + +mount { + src: "/snekbox" + dst: "/snekbox" + is_bind: true + rw: false +} + +mount { + src: "/usr/local/lib" + dst: "/usr/local/lib" + is_bind: true + rw: false +} + +cgroup_mem_max: 52428800 +cgroup_mem_mount: "/sys/fs/cgroup/memory" +cgroup_mem_parent: "NSJAIL" + +cgroup_pids_max: 1 +cgroup_pids_mount: "/sys/fs/cgroup/pids" +cgroup_pids_parent: "NSJAIL" + +iface_no_lo: true + +exec_bin { + path: "/snekbox/.venv/bin/python3" + arg: "-Iq" +} diff --git a/snekbox/nsjail.py b/snekbox/nsjail.py index f160aa8..83d3b8d 100644 --- a/snekbox/nsjail.py +++ b/snekbox/nsjail.py @@ -24,6 +24,7 @@ CGROUP_PIDS_PARENT = Path("/sys/fs/cgroup/pids/NSJAIL") CGROUP_MEMORY_PARENT = Path("/sys/fs/cgroup/memory/NSJAIL") NSJAIL_PATH = os.getenv("NSJAIL_PATH", "/usr/sbin/nsjail") +NSJAIL_CFG = os.getenv("NSJAIL_CFG", "./snekbox.cfg") MEM_MAX = 52428800 @@ -31,9 +32,9 @@ class NsJail: """ Core Snekbox functionality, providing safe execution of Python code. - NsJail configuration: + Default NsJail configuration (snekbox.cfg): - - Root directory is mounted as read-only + - All mounts are read-only - Time limit of 2 seconds - Maximum of 1 PID - Maximum memory of 52428800 bytes @@ -117,21 +118,8 @@ class NsJail: """Execute Python 3 code in an isolated environment and return the completed process.""" with NamedTemporaryFile() as nsj_log: args = ( - self.nsjail_binary, "-Mo", - "--rlimit_as", "700", - "--chroot", "/", - "-E", "LANG=en_US.UTF-8", - "-E", "OMP_NUM_THREADS=1", - "-E", "OPENBLAS_NUM_THREADS=1", - "-E", "MKL_NUM_THREADS=1", - "-E", "VECLIB_MAXIMUM_THREADS=1", - "-E", "NUMEXPR_NUM_THREADS=1", - "-R/usr", "-R/lib", "-R/lib64", - "--user", "65534", # nobody - "--group", "65534", # nobody/nogroup - "--time_limit", "2", - "--disable_proc", - "--iface_no_lo", + self.nsjail_binary, + "--config", NSJAIL_CFG, "--log", nsj_log.name, f"--cgroup_mem_max={MEM_MAX}", "--cgroup_mem_mount", str(CGROUP_MEMORY_PARENT.parent), -- cgit v1.2.3 From 4c27e7aec0f2b8e159eab12a148f5b4725163d07 Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Sat, 28 Dec 2019 21:14:20 -0800 Subject: Mount /usr/lib so ctypes can use libffi --- snekbox.cfg | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/snekbox.cfg b/snekbox.cfg index 2f4a0e4..4cb58de 100644 --- a/snekbox.cfg +++ b/snekbox.cfg @@ -67,6 +67,13 @@ mount { rw: false } +mount { + src: "/usr/lib" + dst: "/usr/lib" + is_bind: true + rw: false +} + mount { src: "/usr/local/lib" dst: "/usr/local/lib" -- cgit v1.2.3 From 25219587c0ac2239d42fb82ad32f6c86d2da6e27 Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Sat, 28 Dec 2019 21:27:45 -0800 Subject: Test shared memory is disabled Co-authored-by: 0xf0f <0x0meta@gmail.com> --- tests/test_nsjail.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/tests/test_nsjail.py b/tests/test_nsjail.py index bb176d9..00ca89c 100644 --- a/tests/test_nsjail.py +++ b/tests/test_nsjail.py @@ -122,3 +122,30 @@ class NsJailTests(unittest.TestCase): "INFO:snekbox.nsjail:pid=20 ([STANDALONE MODE]) exited with status: 2, (PIDs left: 0)", log.output ) + + def test_shm_and_tmp_not_mounted(self): + for path in ("/dev/shm", "/run/shm", "/tmp"): + with self.subTest(path=path): + code = dedent(f""" + with open('{path}/test', 'wb') as file: + file.write(bytes([255])) + """).strip() + + result = self.nsjail.python3(code) + self.assertEqual(result.returncode, 1) + self.assertIn("No such file or directory", result.stdout) + self.assertEqual(result.stderr, None) + + def test_multiprocessing_shared_memory_disabled(self): + code = dedent(""" + from multiprocessing.shared_memory import SharedMemory + try: + SharedMemory('test', create=True, size=16) + except FileExistsError: + pass + """).strip() + + result = self.nsjail.python3(code) + self.assertEqual(result.returncode, 1) + self.assertIn("Function not implemented", result.stdout) + self.assertEqual(result.stderr, None) -- cgit v1.2.3 From 58477b8a96773da0de428e45ed56a7b1b44c0ab6 Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Sat, 28 Dec 2019 21:45:40 -0800 Subject: Test root and direct children are read-only --- tests/test_nsjail.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/tests/test_nsjail.py b/tests/test_nsjail.py index 00ca89c..e439c15 100644 --- a/tests/test_nsjail.py +++ b/tests/test_nsjail.py @@ -56,14 +56,17 @@ class NsJailTests(unittest.TestCase): self.assertEqual(result.stderr, None) def test_read_only_file_system(self): - code = dedent(""" - open('hello', 'w').write('world') - """).strip() + for path in ("/", "/etc", "/lib", "/lib64", "/snekbox", "/usr"): + with self.subTest(path=path): + code = dedent(f""" + with open('{path}/hello', 'w') as f: + f.write('world') + """).strip() - result = self.nsjail.python3(code) - self.assertEqual(result.returncode, 1) - self.assertIn("Read-only file system", result.stdout) - self.assertEqual(result.stderr, None) + result = self.nsjail.python3(code) + self.assertEqual(result.returncode, 1) + self.assertIn("Read-only file system", result.stdout) + self.assertEqual(result.stderr, None) def test_forkbomb_resource_unavailable(self): code = dedent(""" -- cgit v1.2.3 From da8a1752dde8d8a8afb1a4f64d7678f80e802dab Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Sat, 28 Dec 2019 21:49:33 -0800 Subject: Add test for importing numpy This is a test for #53, which fixed numpy failing to import due to using multiple threads by default. --- tests/test_nsjail.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/test_nsjail.py b/tests/test_nsjail.py index e439c15..f04d317 100644 --- a/tests/test_nsjail.py +++ b/tests/test_nsjail.py @@ -152,3 +152,9 @@ class NsJailTests(unittest.TestCase): self.assertEqual(result.returncode, 1) self.assertIn("Function not implemented", result.stdout) self.assertEqual(result.stderr, None) + + def test_numpy_import(self): + result = self.nsjail.python3("import numpy") + self.assertEqual(result.returncode, 0) + self.assertEqual(result.stdout, "") + self.assertEqual(result.stderr, None) -- cgit v1.2.3 From 83f1c49ab6ed6ff0b04f32f5031e4838131302d1 Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Sat, 11 Jan 2020 13:50:47 -0800 Subject: Fix #56: stdout and stderr outputs in wrong order --- scripts/.profile | 2 +- snekbox.cfg | 2 +- snekbox/nsjail.py | 2 +- tests/test_nsjail.py | 16 ++++++++++++++++ 4 files changed, 19 insertions(+), 3 deletions(-) diff --git a/scripts/.profile b/scripts/.profile index 47ee141..73fbb28 100644 --- a/scripts/.profile +++ b/scripts/.profile @@ -17,5 +17,5 @@ nsjpy() { nsjail \ --config "${NSJAIL_CFG:-/snekbox/snekbox.cfg}" \ $nsj_args -- \ - /snekbox/.venv/bin/python3 -Iq -c "$@" + /snekbox/.venv/bin/python3 -Iqu -c "$@" } diff --git a/snekbox.cfg b/snekbox.cfg index 4cb58de..1d58ea5 100644 --- a/snekbox.cfg +++ b/snekbox.cfg @@ -93,5 +93,5 @@ iface_no_lo: true exec_bin { path: "/snekbox/.venv/bin/python3" - arg: "-Iq" + arg: "-Iqu" } diff --git a/snekbox/nsjail.py b/snekbox/nsjail.py index 83d3b8d..df69e7a 100644 --- a/snekbox/nsjail.py +++ b/snekbox/nsjail.py @@ -128,7 +128,7 @@ class NsJail: "--cgroup_pids_mount", str(CGROUP_PIDS_PARENT.parent), "--cgroup_pids_parent", CGROUP_PIDS_PARENT.name, "--", - self.python_binary, "-Iq", "-c", code + self.python_binary, "-Iqu", "-c", code ) msg = "Executing code..." diff --git a/tests/test_nsjail.py b/tests/test_nsjail.py index f04d317..0b755b2 100644 --- a/tests/test_nsjail.py +++ b/tests/test_nsjail.py @@ -158,3 +158,19 @@ class NsJailTests(unittest.TestCase): self.assertEqual(result.returncode, 0) self.assertEqual(result.stdout, "") self.assertEqual(result.stderr, None) + + def test_output_order(self): + stdout_msg = "greetings from stdout!" + stderr_msg = "hello from stderr!" + code = dedent(f""" + print({stdout_msg!r}) + raise ValueError({stderr_msg!r}) + """).strip() + + result = self.nsjail.python3(code) + self.assertLess( + result.stdout.find(stdout_msg), + result.stdout.find(stderr_msg), + msg="stdout does not come before stderr" + ) + self.assertEqual(result.stderr, None) -- cgit v1.2.3 From d794331360ec31c2e26bfb20a9ca6e9edd01dcbd Mon Sep 17 00:00:00 2001 From: Joseph Banks Date: Fri, 24 Jan 2020 00:59:48 +0000 Subject: Increase time limit from 2 to 5 seconds --- README.md | 2 +- snekbox.cfg | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index d90609e..bbb8d64 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ result <- | |<----------| | <----------+ The code is executed in a Python process that is launched through [NsJail](https://github.com/google/nsjail), which is responsible for sandboxing the Python process. NsJail is configured as follows: * All mounts are read-only -* Time limit of 2 seconds +* Time limit of 5 seconds * Maximum of 1 PID * Maximum memory of 52428800 bytes * Loopback interface is down diff --git a/snekbox.cfg b/snekbox.cfg index 1d58ea5..968271c 100644 --- a/snekbox.cfg +++ b/snekbox.cfg @@ -5,7 +5,7 @@ mode: ONCE hostname: "snekbox" cwd: "/snekbox" -time_limit: 2 +time_limit: 5 keep_env: false envar: "LANG=en_US.UTF-8" -- cgit v1.2.3 From 0274fa303ef94f96829a4588fd8ca0daeb462b3e Mon Sep 17 00:00:00 2001 From: Mark Date: Wed, 5 Feb 2020 12:12:43 -0800 Subject: Update time limit in docstring Co-Authored-By: Sebastiaan Zeeff <33516116+SebastiaanZ@users.noreply.github.com> --- snekbox/nsjail.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snekbox/nsjail.py b/snekbox/nsjail.py index df69e7a..b5586bb 100644 --- a/snekbox/nsjail.py +++ b/snekbox/nsjail.py @@ -35,7 +35,7 @@ class NsJail: Default NsJail configuration (snekbox.cfg): - All mounts are read-only - - Time limit of 2 seconds + - Time limit of 5 seconds - Maximum of 1 PID - Maximum memory of 52428800 bytes - Loopback interface is down -- cgit v1.2.3