def test_disabling_vmem_limit(self): # Disable the limit, it will succeed. set_limit('VMEM', 0) res = jailpy(code="print len(bytearray(50000000))") self.assertEqual(res.stderr, "") self.assertEqual(res.stdout, "50000000\n") self.assertEqual(res.status, 0)
def get_jail(python_version=sys.version_info[0]): """Return codejail object. Note: - Please set environmental variables `OPALALGO_SANDBOX_VENV` and `OPALALGO_SANDBOX_USER` before calling this function. - `OPALALGO_SANDBOX_VENV` must be set to the path of the sandbox virtual environment. - `OPALALGO_SANDBOX_USER` must be set to the user running the sandboxed algorithms. """ sandbox_env = os.environ.get('OPALALGO_SANDBOX_VENV') sandbox_user = os.environ.get('OPALALGO_SANDBOX_USER') set_limit("REALTIME", None) codejail.configure('python', os.path.join(sandbox_env, 'bin', 'python'), user=sandbox_user) codejail.configure('python3', os.path.join(sandbox_env, 'bin', 'python'), user=sandbox_user) if python_version < 3: jail = codejail.get_codejail('python') else: jail = codejail.get_codejail('python3') return jail
def test_changing_vmem_limit(self): # Up the limit, it will succeed. set_limit('VMEM', 80000000) res = jailpy(code="print len(bytearray(40000000))") self.assertEqual(res.stderr, "") self.assertEqual(res.stdout, "40000000\n") self.assertEqual(res.status, 0)
def test_cant_use_too_much_memory(self): # This will fail after setting the limit to 30Mb. set_limit('VMEM', 30000000) res = jailpy(code="print len(bytearray(40000000))") self.assertEqual(res.stdout, "") self.assertIn("MemoryError", res.stderr) self.assertEqual(res.status, 1)
def test_we_can_remove_tmp_files(self): # This test is meant to create a tmp file in a temp folder as the # sandbox user that the application user can't delete. # This is because the sandbox user has the ability to delete # any toplevel files in the tmp directory but not the abilty # to delete files in folders that are only owned by the sandbox # user, such as the temp directory created below. set_limit('FSIZE', 1000) res = jailpy( code="""\ import os, shutil, tempfile temp_dir = tempfile.mkdtemp() with open("{}/myfile.txt".format(temp_dir), "w") as f: f.write("This is my file!") shutil.move("{}/myfile.txt".format(temp_dir), "{}/overthere.txt".format(temp_dir)) with open("{}/overthere.txt".format(temp_dir)) as f: print f.read() with open("{}/.myfile.txt".format(temp_dir), "w") as f: f.write("This is my dot file!") # Now make it secret! os.chmod("{}/overthere.txt".format(temp_dir), 0) print os.listdir(temp_dir) """) self.assertResultOk(res) self.assertEqual( res.stdout, "This is my file!\n['overthere.txt', '.myfile.txt']\n" )
def test_cant_use_too_much_time(self, log_log): # Default time limit is 1 second. Sleep for 1.5 seconds. set_limit('CPU', 100) res = jailpy(code="import time; time.sleep(1.5); print 'Done!'") self.assertEqual(res.stdout, "") self.assertEqual(res.status, -signal.SIGKILL) # -9 # Make sure we log that we are killing the process. log_text = text_of_logs(log_log.mock_calls) self.assertRegexpMatches(log_text, r"WARNING: Killing process \d+")
def test_can_write_temp_files(self): set_limit('FSIZE', 1000) res = jailpy(code="""\ import os, tempfile print "Trying mkstemp" f, path = tempfile.mkstemp() os.close(f) with open(path, "w") as f1: f1.write("hello") with open(path) as f2: print "Got this:", f2.read() """) self.assertResultOk(res) self.assertEqual(res.stdout, "Trying mkstemp\nGot this: hello\n")
def test_cant_write_large_temp_files(self): set_limit('FSIZE', 1000) res = jailpy(code="""\ import os, tempfile print "Trying mkstemp" f, path = tempfile.mkstemp() os.close(f) with open(path, "w") as f1: try: f1.write(".".join("%05d" % i for i in xrange(1000))) except IOError as e: print "Expected exception: %s" % e else: with open(path) as f2: print "Got this:", f2.read() """) self.assertResultOk(res) self.assertIn("Expected exception", res.stdout)
def test_cant_write_many_small_temp_files(self): # We would like this to fail, but there's nothing that checks total # file size written, so the sandbox does not prevent it yet. set_limit('FSIZE', 1000) res = jailpy(code="""\ import os, tempfile print "Trying mkstemp 250" for i in range(250): f, path = tempfile.mkstemp() os.close(f) with open(path, "w") as f1: f1.write("hello") with open(path) as f2: assert f2.read() == "hello" print "Finished 250" """) self.assertNotEqual(res.status, 0) self.assertEqual(res.stdout, "Trying mkstemp 250\n") self.assertIn("IOError", res.stderr)
def test_disabling_realtime_limit(self): # Disable the time limit, sleeping for 1.5 will be fine. set_limit('REALTIME', 0) res = jailpy(code="import time; time.sleep(1.5); print 'Done!'") self.assertResultOk(res) self.assertEqual(res.stdout, "Done!\n")
def test_changing_realtime_limit(self): # Change time limit to 2 seconds, sleeping for 1.5 will be fine. set_limit('REALTIME', 2) res = jailpy(code="import time; time.sleep(1.5); print 'Done!'") self.assertResultOk(res) self.assertEqual(res.stdout, "Done!\n")
def test_cant_use_too_much_cpu(self): set_limit('CPU', 1) set_limit('REALTIME', 100) res = jailpy(code="print sum(xrange(2**31-1))") self.assertEqual(res.stdout, "") self.assertEqual(res.status, 128+signal.SIGXCPU) # 137
def tearDown(self): for name, value in self.old_limits.items(): set_limit(name, value) super(TestLimits, self).tearDown()