2015-07-24 08:07:13 +00:00
|
|
|
import unittest
|
|
|
|
import os
|
|
|
|
import shutil
|
2016-09-15 14:39:27 +00:00
|
|
|
import sys
|
2010-06-10 20:35:55 +00:00
|
|
|
from tempfile import mkdtemp
|
|
|
|
from subprocess import Popen, PIPE
|
|
|
|
|
2015-07-24 08:07:13 +00:00
|
|
|
|
2010-06-10 20:35:55 +00:00
|
|
|
class SandboxTests(unittest.TestCase):
|
2015-07-24 08:07:13 +00:00
|
|
|
|
2010-06-10 20:35:55 +00:00
|
|
|
def assertDenied(self, err):
|
2015-07-21 00:38:20 +00:00
|
|
|
self.assertTrue(b'Permission denied' in err,
|
2015-07-24 08:07:13 +00:00
|
|
|
'"Permission denied" not found in %r' % err)
|
|
|
|
|
2010-06-10 20:35:55 +00:00
|
|
|
def assertNotFound(self, err):
|
2015-07-21 00:38:20 +00:00
|
|
|
self.assertTrue(b'not found' in err,
|
2015-07-24 08:07:13 +00:00
|
|
|
'"not found" not found in %r' % err)
|
2010-06-10 20:35:55 +00:00
|
|
|
|
|
|
|
def assertFailure(self, status):
|
2015-07-21 00:38:20 +00:00
|
|
|
self.assertTrue(status != 0,
|
2015-07-24 08:07:13 +00:00
|
|
|
'"Succeeded when it should have failed')
|
2010-06-10 20:35:55 +00:00
|
|
|
|
|
|
|
def assertSuccess(self, status, err):
|
2015-07-21 00:38:20 +00:00
|
|
|
self.assertTrue(status == 0,
|
2015-07-24 08:07:13 +00:00
|
|
|
'"Sandbox should have succeeded for this test %r' % err)
|
2010-06-10 20:35:55 +00:00
|
|
|
|
|
|
|
def test_simple_success(self):
|
|
|
|
"Verify that we can read file descriptors handed to sandbox"
|
2015-07-24 08:07:13 +00:00
|
|
|
p1 = Popen(['cat', '/etc/passwd'], stdout=PIPE)
|
2016-09-15 14:39:27 +00:00
|
|
|
p2 = Popen([sys.executable, 'sandbox', 'grep', 'root'], stdin=p1.stdout, stdout=PIPE)
|
2016-09-15 14:39:28 +00:00
|
|
|
p1.stdout.close()
|
2010-06-10 20:35:55 +00:00
|
|
|
out, err = p2.communicate()
|
2015-07-21 00:38:20 +00:00
|
|
|
self.assertTrue(b'root' in out)
|
2010-06-10 20:35:55 +00:00
|
|
|
|
|
|
|
def test_cant_kill(self):
|
|
|
|
"Verify that we cannot send kill signal in the sandbox"
|
|
|
|
pid = os.getpid()
|
2016-09-15 14:39:27 +00:00
|
|
|
p = Popen([sys.executable, 'sandbox', 'kill', '-HUP', str(pid)], stdout=PIPE, stderr=PIPE)
|
2010-06-10 20:35:55 +00:00
|
|
|
out, err = p.communicate()
|
|
|
|
self.assertDenied(err)
|
|
|
|
|
|
|
|
def test_cant_ping(self):
|
|
|
|
"Verify that we can't ping within the sandbox"
|
2016-09-15 14:39:27 +00:00
|
|
|
p = Popen([sys.executable, 'sandbox', 'ping', '-c 1 ', '127.0.0.1'], stdout=PIPE, stderr=PIPE)
|
2010-06-10 20:35:55 +00:00
|
|
|
out, err = p.communicate()
|
|
|
|
self.assertDenied(err)
|
2015-07-24 08:07:13 +00:00
|
|
|
|
2010-06-10 20:35:55 +00:00
|
|
|
def test_cant_mkdir(self):
|
|
|
|
"Verify that we can't mkdir within the sandbox"
|
2016-09-15 14:39:27 +00:00
|
|
|
p = Popen([sys.executable, 'sandbox', 'mkdir', '~/test'], stdout=PIPE, stderr=PIPE)
|
2010-06-10 20:35:55 +00:00
|
|
|
out, err = p.communicate()
|
|
|
|
self.assertFailure(p.returncode)
|
|
|
|
|
|
|
|
def test_cant_list_homedir(self):
|
|
|
|
"Verify that we can't list homedir within the sandbox"
|
2016-09-15 14:39:27 +00:00
|
|
|
p = Popen([sys.executable, 'sandbox', 'ls', '~'], stdout=PIPE, stderr=PIPE)
|
2010-06-10 20:35:55 +00:00
|
|
|
out, err = p.communicate()
|
|
|
|
self.assertFailure(p.returncode)
|
|
|
|
|
|
|
|
def test_cant_send_mail(self):
|
|
|
|
"Verify that we can't send mail within the sandbox"
|
2016-09-15 14:39:27 +00:00
|
|
|
p = Popen([sys.executable, 'sandbox', 'mail'], stdout=PIPE, stderr=PIPE)
|
2010-06-10 20:35:55 +00:00
|
|
|
out, err = p.communicate()
|
|
|
|
self.assertDenied(err)
|
2015-07-24 08:07:13 +00:00
|
|
|
|
2010-06-10 20:35:55 +00:00
|
|
|
def test_cant_sudo(self):
|
|
|
|
"Verify that we can't run sudo within the sandbox"
|
2016-09-15 14:39:27 +00:00
|
|
|
p = Popen([sys.executable, 'sandbox', 'sudo'], stdout=PIPE, stderr=PIPE)
|
2010-06-10 20:35:55 +00:00
|
|
|
out, err = p.communicate()
|
|
|
|
self.assertFailure(p.returncode)
|
2015-07-24 08:07:13 +00:00
|
|
|
|
2010-06-10 20:35:55 +00:00
|
|
|
def test_mount(self):
|
|
|
|
"Verify that we mount a file system"
|
2016-09-15 14:39:27 +00:00
|
|
|
p = Popen([sys.executable, 'sandbox', '-M', 'id'], stdout=PIPE, stderr=PIPE)
|
2010-06-10 20:35:55 +00:00
|
|
|
out, err = p.communicate()
|
|
|
|
self.assertSuccess(p.returncode, err)
|
2015-07-24 08:07:13 +00:00
|
|
|
|
2010-06-10 20:35:55 +00:00
|
|
|
def test_set_level(self):
|
|
|
|
"Verify that we set level a file system"
|
2016-09-15 14:39:27 +00:00
|
|
|
p = Popen([sys.executable, 'sandbox', '-l', 's0', 'id'], stdout=PIPE, stderr=PIPE)
|
2010-06-10 20:35:55 +00:00
|
|
|
out, err = p.communicate()
|
|
|
|
self.assertSuccess(p.returncode, err)
|
2015-07-24 08:07:13 +00:00
|
|
|
|
2010-06-10 20:35:55 +00:00
|
|
|
def test_homedir(self):
|
|
|
|
"Verify that we set homedir a file system"
|
|
|
|
homedir = mkdtemp(dir=".", prefix=".sandbox_test")
|
2016-09-15 14:39:27 +00:00
|
|
|
p = Popen([sys.executable, 'sandbox', '-H', homedir, '-M', 'id'], stdout=PIPE, stderr=PIPE)
|
2010-06-10 20:35:55 +00:00
|
|
|
out, err = p.communicate()
|
|
|
|
shutil.rmtree(homedir)
|
|
|
|
self.assertSuccess(p.returncode, err)
|
2015-07-24 08:07:13 +00:00
|
|
|
|
2010-06-10 20:35:55 +00:00
|
|
|
def test_tmpdir(self):
|
|
|
|
"Verify that we set tmpdir a file system"
|
|
|
|
tmpdir = mkdtemp(dir="/tmp", prefix=".sandbox_test")
|
2016-09-15 14:39:27 +00:00
|
|
|
p = Popen([sys.executable, 'sandbox', '-T', tmpdir, '-M', 'id'], stdout=PIPE, stderr=PIPE)
|
2010-06-10 20:35:55 +00:00
|
|
|
out, err = p.communicate()
|
|
|
|
shutil.rmtree(tmpdir)
|
|
|
|
self.assertSuccess(p.returncode, err)
|
2015-07-24 08:07:13 +00:00
|
|
|
|
2016-09-15 14:39:29 +00:00
|
|
|
def test_include_file(self):
|
|
|
|
"Verify that sandbox can copy a file in the sandbox home and use it"
|
|
|
|
p = Popen([sys.executable, 'sandbox', '-i' ,'test_sandbox.py' , '-M', '/bin/cat', 'test_sandbox.py'],
|
|
|
|
stdout=PIPE, stderr=PIPE)
|
|
|
|
out, err = p.communicate()
|
|
|
|
self.assertSuccess(p.returncode, err)
|
|
|
|
|
|
|
|
|
2010-06-10 20:35:55 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
import selinux
|
2016-11-17 21:20:06 +00:00
|
|
|
if selinux.is_selinux_enabled() and selinux.security_getenforce() == 1:
|
2010-06-10 20:35:55 +00:00
|
|
|
unittest.main()
|
|
|
|
else:
|
2015-07-21 00:38:20 +00:00
|
|
|
print("SELinux must be in enforcing mode for this test")
|