sandbox: tests - use sandbox from cwd
PATCH 1/3 sandbox: tests - use sandbox from cwd
The tests executed sandbox from $PATH while they should test sandbox in cwd. At the same time, tests should be run using the same python as is used by make to run them.
--- policycoreutils/sandbox/test_sandbox.py | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-)
policycoreutils/sandbox/test_sandbox.py +12 -11
@@ -1,6 +1,7 @@ import unittest import os import shutil +import sys from tempfile import mkdtemp from subprocess import Popen, PIPE
@@ -26,63 +27,63 @@ class SandboxTests(unittest.TestCase): def test_simple_success(self): "Verify that we can read file descriptors handed to sandbox" p1 = Popen(['cat', '/etc/passwd'], stdout=PIPE) - p2 = Popen(['sandbox', 'grep', 'root'], stdin=p1.stdout, stdout=PIPE) + p2 = Popen([sys.executable, 'sandbox', 'grep', 'root'], stdin=p1.stdout, stdout=PIPE) out, err = p2.communicate() self.assertTrue(b'root' in out) def test_cant_kill(self): "Verify that we cannot send kill signal in the sandbox" pid = os.getpid() - p = Popen(['sandbox', 'kill', '-HUP', str(pid)], stdout=PIPE, stderr=PIPE) + p = Popen([sys.executable, 'sandbox', 'kill', '-HUP', str(pid)], stdout=PIPE, stderr=PIPE) out, err = p.communicate() self.assertDenied(err) def test_cant_ping(self): "Verify that we can't ping within the sandbox" - p = Popen(['sandbox', 'ping', '-c 1 ', '127.0.0.1'], stdout=PIPE, stderr=PIPE) + p = Popen([sys.executable, 'sandbox', 'ping', '-c 1 ', '127.0.0.1'], stdout=PIPE, stderr=PIPE) out, err = p.communicate() self.assertDenied(err) def test_cant_mkdir(self): "Verify that we can't mkdir within the sandbox" - p = Popen(['sandbox', 'mkdir', '~/test'], stdout=PIPE, stderr=PIPE) + p = Popen([sys.executable, 'sandbox', 'mkdir', '~/test'], stdout=PIPE, stderr=PIPE) out, err = p.communicate() self.assertFailure(p.returncode) def test_cant_list_homedir(self): "Verify that we can't list homedir within the sandbox" - p = Popen(['sandbox', 'ls', '~'], stdout=PIPE, stderr=PIPE) + p = Popen([sys.executable, 'sandbox', 'ls', '~'], stdout=PIPE, stderr=PIPE) out, err = p.communicate() self.assertFailure(p.returncode) def test_cant_send_mail(self): "Verify that we can't send mail within the sandbox" - p = Popen(['sandbox', 'mail'], stdout=PIPE, stderr=PIPE) + p = Popen([sys.executable, 'sandbox', 'mail'], stdout=PIPE, stderr=PIPE) out, err = p.communicate() self.assertDenied(err) def test_cant_sudo(self): "Verify that we can't run sudo within the sandbox" - p = Popen(['sandbox', 'sudo'], stdout=PIPE, stderr=PIPE) + p = Popen([sys.executable, 'sandbox', 'sudo'], stdout=PIPE, stderr=PIPE) out, err = p.communicate() self.assertFailure(p.returncode) def test_mount(self): "Verify that we mount a file system" - p = Popen(['sandbox', '-M', 'id'], stdout=PIPE, stderr=PIPE) + p = Popen([sys.executable, 'sandbox', '-M', 'id'], stdout=PIPE, stderr=PIPE) out, err = p.communicate() self.assertSuccess(p.returncode, err) def test_set_level(self): "Verify that we set level a file system" - p = Popen(['sandbox', '-l', 's0', 'id'], stdout=PIPE, stderr=PIPE) + p = Popen([sys.executable, 'sandbox', '-l', 's0', 'id'], stdout=PIPE, stderr=PIPE) out, err = p.communicate() self.assertSuccess(p.returncode, err) def test_homedir(self): "Verify that we set homedir a file system" homedir = mkdtemp(dir=".", prefix=".sandbox_test") - p = Popen(['sandbox', '-H', homedir, '-M', 'id'], stdout=PIPE, stderr=PIPE) + p = Popen([sys.executable, 'sandbox', '-H', homedir, '-M', 'id'], stdout=PIPE, stderr=PIPE) out, err = p.communicate() shutil.rmtree(homedir) self.assertSuccess(p.returncode, err)
@@ -90,7 +91,7 @@ class SandboxTests(unittest.TestCase): def test_tmpdir(self): "Verify that we set tmpdir a file system" tmpdir = mkdtemp(dir="/tmp", prefix=".sandbox_test") - p = Popen(['sandbox', '-T', tmpdir, '-M', 'id'], stdout=PIPE, stderr=PIPE) + p = Popen([sys.executable, 'sandbox', '-T', tmpdir, '-M', 'id'], stdout=PIPE, stderr=PIPE) out, err = p.communicate() shutil.rmtree(tmpdir) self.assertSuccess(p.returncode, err)
Signature · 1 line
1.8.3.1
PATCH 2/3 sandbox: tests - close stdout of p1
p1.stdout should be closed after it's connected to p2 according to
https://docs.python.org/3/library/subprocess.html#replacing-shell-pipeline
Fixes:
$ make PYTHON=python3 test Verify that we can read file descriptors handed to sandbox ... /usr/lib64/python3.5/unittest/case.py:638: ResourceWarning: unclosed file <_io.BufferedReader name=4> testMethod()
--- policycoreutils/sandbox/test_sandbox.py | 1 + 1 file changed, 1 insertion(+)
policycoreutils/sandbox/test_sandbox.py +1 -0
@@ -28,6 +28,7 @@ class SandboxTests(unittest.TestCase): "Verify that we can read file descriptors handed to sandbox" p1 = Popen(['cat', '/etc/passwd'], stdout=PIPE) p2 = Popen([sys.executable, 'sandbox', 'grep', 'root'], stdin=p1.stdout, stdout=PIPE) + p1.stdout.close() out, err = p2.communicate() self.assertTrue(b'root' in out)
Signature · 1 line
1.8.3.1
PATCH 3/3 sandbox: fix file labels on copied files
In reply to Petr LautrbachSince python 3.3, shutil.copy2() tries to preserve extended file system attributes. It means that when a user uses -i or -I, copied files have the original labels and sandboxed process can't read them.
With this change, homedir and tmpdir is recursively relabeled with the expected sandbox labels after all items are in their place.
--- policycoreutils/sandbox/sandbox | 9 ++++----- policycoreutils/sandbox/test_sandbox.py | 8 ++++++++ 2 files changed, 12 insertions(+), 5 deletions(-)
policycoreutils/sandbox/sandbox +4 -5
@@ -425,21 +425,20 @@ sandbox [-h] [-l level ] [-[X|M] [-H homedir] [-T tempdir]] [-I includefile ] [- self.__filecon = "%s:object_r:sandbox_file_t:%s" % (con[0], level) def __setup_dir(self): + selinux.setfscreatecon(self.__filecon) if self.__options.homedir: - selinux.chcon(self.__options.homedir, self.__filecon, recursive=True) self.__homedir = self.__options.homedir else: - selinux.setfscreatecon(self.__filecon) self.__homedir = mkdtemp(dir="/tmp", prefix=".sandbox_home_") if self.__options.tmpdir: - selinux.chcon(self.__options.tmpdir, self.__filecon, recursive=True) self.__tmpdir = self.__options.tmpdir else: - selinux.setfscreatecon(self.__filecon) self.__tmpdir = mkdtemp(dir="/tmp", prefix=".sandbox_tmp_") - selinux.setfscreatecon(None) self.__copyfiles() + selinux.chcon(self.__homedir, self.__filecon, recursive=True) + selinux.chcon(self.__tmpdir, self.__filecon, recursive=True) + selinux.setfscreatecon(None) def __execute(self): try:
policycoreutils/sandbox/test_sandbox.py +8 -0
@@ -97,6 +97,14 @@ class SandboxTests(unittest.TestCase): shutil.rmtree(tmpdir) self.assertSuccess(p.returncode, err) + 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) + + if __name__ == "__main__": import selinux if selinux.security_getenforce() == 1:
Signature · 1 line
1.8.3.1
Stephen Smalley
AppliedRe: [PATCH 3/3] sandbox: fix file labels on copied files
Quoted 10 lines · Petr Lautrbach
Since python 3.3,
shutil.copy2()tries to preserve extended file system attributes. It means that when a user uses -i or -I, copied files have the original labels and sandboxed process can't read them.With this change, homedir and tmpdir is recursively relabeled with the expected sandbox labels after all items are in their place.
Signed-off-by Petr Lautrbach <[email protected]>
Thanks, applied.
Quoted 55 lines
--- policycoreutils/sandbox/sandbox | 9 ++++----- policycoreutils/sandbox/test_sandbox.py | 8 ++++++++ 2 files changed, 12 insertions(+), 5 deletions(-)policycoreutils/sandbox/sandbox +4
-5@@ -425,21 +425,20 @@ sandbox [-h] [-l level ] [-[X|M] [-H homedir] [-T tempdir]] [-I includefile ] [- self.__filecon = "%s:object_r:sandbox_file_t:%s" % (con[0], level) def __setup_dir(self): + selinux.setfscreatecon(self.__filecon) if self.__options.homedir: - selinux.chcon(self.__options.homedir, self.__filecon, recursive=True) self.__homedir = self.__options.homedir else: - selinux.setfscreatecon(self.__filecon) self.__homedir = mkdtemp(dir="/tmp", prefix=".sandbox_home_") if self.__options.tmpdir: - selinux.chcon(self.__options.tmpdir, self.__filecon, recursive=True) self.__tmpdir = self.__options.tmpdir else: - selinux.setfscreatecon(self.__filecon) self.__tmpdir = mkdtemp(dir="/tmp", prefix=".sandbox_tmp_") - selinux.setfscreatecon(None) self.__copyfiles() + selinux.chcon(self.__homedir, self.__filecon, recursive=True) + selinux.chcon(self.__tmpdir, self.__filecon, recursive=True) + selinux.setfscreatecon(None) def __execute(self): try:policycoreutils/sandbox/test_sandbox.py +8
-0@@ -97,6 +97,14 @@ class SandboxTests(unittest.TestCase): shutil.rmtree(tmpdir) self.assertSuccess(p.returncode, err) + 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) + + if __name__ == "__main__": import selinux if selinux.security_getenforce() == 1:
Quoted 5 lines · Petr Lautrbach
The tests executed sandbox from $PATH while they should test sandbox in cwd. At the same time, tests should be run using the same python as is used by make to run them.
Signed-off-by Petr Lautrbach <[email protected]>
Do the sandbox tests actually work for you? For me, most of them fail with the error:
AssertionError: "Sandbox should have succeeded for this test 'ERROR:
could not find datum for type sandbox_t\nsandbox: Sandbox Policy is not currently installed.\nYou need to install the selinux-policy-sandbox package in order to run this command\n'
But I do have selinux-policy-sandbox installed. This is on F24. sandbox_x_t is defined, but not sandbox_t.
Quoted 100 lines
--- policycoreutils/sandbox/test_sandbox.py | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-)policycoreutils/sandbox/test_sandbox.py +12
-11@@ -1,6 +1,7 @@ import unittest import os import shutil +import sys from tempfile import mkdtemp from subprocess import Popen, PIPE@@ -26,63 +27,63 @@ class SandboxTests(unittest.TestCase): def test_simple_success(self): "Verify that we can read file descriptors handed to sandbox" p1 = Popen(['cat', '/etc/passwd'], stdout=PIPE) - p2 = Popen(['sandbox', 'grep', 'root'], stdin=p1.stdout, stdout=PIPE) + p2 = Popen([sys.executable, 'sandbox', 'grep', 'root'], stdin=p1.stdout, stdout=PIPE) out, err = p2.communicate() self.assertTrue(b'root' in out) def test_cant_kill(self): "Verify that we cannot send kill signal in the sandbox" pid = os.getpid() - p = Popen(['sandbox', 'kill', '-HUP', str(pid)], stdout=PIPE, stderr=PIPE) + p = Popen([sys.executable, 'sandbox', 'kill', '-HUP', str(pid)], stdout=PIPE, stderr=PIPE) out, err = p.communicate() self.assertDenied(err) def test_cant_ping(self): "Verify that we can't ping within the sandbox" - p = Popen(['sandbox', 'ping', '-c 1 ', '127.0.0.1'], stdout=PIPE, stderr=PIPE) + p = Popen([sys.executable, 'sandbox', 'ping', '-c 1 ', '127.0.0.1'], stdout=PIPE, stderr=PIPE) out, err = p.communicate() self.assertDenied(err) def test_cant_mkdir(self): "Verify that we can't mkdir within the sandbox" - p = Popen(['sandbox', 'mkdir', '~/test'], stdout=PIPE, stderr=PIPE) + p = Popen([sys.executable, 'sandbox', 'mkdir', '~/test'], stdout=PIPE, stderr=PIPE) out, err = p.communicate() self.assertFailure(p.returncode) def test_cant_list_homedir(self): "Verify that we can't list homedir within the sandbox" - p = Popen(['sandbox', 'ls', '~'], stdout=PIPE, stderr=PIPE) + p = Popen([sys.executable, 'sandbox', 'ls', '~'], stdout=PIPE, stderr=PIPE) out, err = p.communicate() self.assertFailure(p.returncode) def test_cant_send_mail(self): "Verify that we can't send mail within the sandbox" - p = Popen(['sandbox', 'mail'], stdout=PIPE, stderr=PIPE) + p = Popen([sys.executable, 'sandbox', 'mail'], stdout=PIPE, stderr=PIPE) out, err = p.communicate() self.assertDenied(err) def test_cant_sudo(self): "Verify that we can't run sudo within the sandbox" - p = Popen(['sandbox', 'sudo'], stdout=PIPE, stderr=PIPE) + p = Popen([sys.executable, 'sandbox', 'sudo'], stdout=PIPE, stderr=PIPE) out, err = p.communicate() self.assertFailure(p.returncode) def test_mount(self): "Verify that we mount a file system" - p = Popen(['sandbox', '-M', 'id'], stdout=PIPE, stderr=PIPE) + p = Popen([sys.executable, 'sandbox', '-M', 'id'], stdout=PIPE, stderr=PIPE) out, err = p.communicate() self.assertSuccess(p.returncode, err) def test_set_level(self): "Verify that we set level a file system" - p = Popen(['sandbox', '-l', 's0', 'id'], stdout=PIPE, stderr=PIPE) + p = Popen([sys.executable, 'sandbox', '-l', 's0', 'id'], stdout=PIPE, stderr=PIPE) out, err = p.communicate() self.assertSuccess(p.returncode, err) def test_homedir(self): "Verify that we set homedir a file system" homedir = mkdtemp(dir=".", prefix=".sandbox_test") - p = Popen(['sandbox', '-H', homedir, '-M', 'id'], stdout=PIPE, stderr=PIPE) + p = Popen([sys.executable, 'sandbox', '-H', homedir, '-M', 'id'], stdout=PIPE, stderr=PIPE) out, err = p.communicate() shutil.rmtree(homedir) self.assertSuccess(p.returncode, err)@@ -90,7 +91,7 @@ class SandboxTests(unittest.TestCase): def test_tmpdir(self): "Verify that we set tmpdir a file system" tmpdir = mkdtemp(dir="/tmp", prefix=".sandbox_test") - p = Popen(['sandbox', '-T', tmpdir, '-M', 'id'], stdout=PIPE, stderr=PIPE) + p = Popen([sys.executable, 'sandbox', '-T', tmpdir, '-M', 'id'], stdout=PIPE, stderr=PIPE) out, err = p.communicate() shutil.rmtree(tmpdir) self.assertSuccess(p.returncode, err)
Quoted 14 lines · Stephen Smalley
Quoted 5 lines · Petr Lautrbach
The tests executed sandbox from $PATH while they should test sandbox in cwd. At the same time, tests should be run using the same python as is used by make to run them.
Signed-off-by Petr Lautrbach <[email protected]>Do the sandbox tests actually work for you? For me, most of them fail with the error:
AssertionError: "Sandbox should have succeeded for this test 'ERROR:
could not find datum for type sandbox_t\nsandbox: Sandbox Policy is not currently installed.\nYou need to install the selinux-policy-sandbox package in order to run this command\n'
Quoted 2 lines
But I do have selinux-policy-sandbox installed. This is on F24.
sandbox_x_tis defined, but notsandbox_t.
The tests work for me with both pythons on Fedora 24.
For sandbox_t you need to enable sandbox module:
semodule -e sandbox
It should be enabled by default when you install selinux-policy-sandbox. But there's probably a bug which causes the module is not enabled in some cases.
Simon Sekidde suggested to change the error message a little, see bellow. But it probably won't be sufficient as well.
try:
sepolicy.info(sepolicy.TYPE, "sandbox_t")
except RuntimeError:
- raise ValueError(_("Sandbox Policy is not currently
installed.\nYou need to install the selinux-policy-sandbox package in order to run this command"))
+ raise ValueError(_("Sandbox Policy is currently
disabled.\nYou need to enable the sandbox module in order to run this command"))
It would need a detection if the module is only disabled or is not installed at all.
Petr
Quoted 102 lines
Quoted 101 lines
--- policycoreutils/sandbox/test_sandbox.py | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-)policycoreutils/sandbox/test_sandbox.py +12
-11@@ -1,6 +1,7 @@ import unittest import os import shutil +import sys from tempfile import mkdtemp from subprocess import Popen, PIPE@@ -26,63 +27,63 @@ class SandboxTests(unittest.TestCase): def test_simple_success(self): "Verify that we can read file descriptors handed to sandbox" p1 = Popen(['cat', '/etc/passwd'], stdout=PIPE) - p2 = Popen(['sandbox', 'grep', 'root'], stdin=p1.stdout, stdout=PIPE) + p2 = Popen([sys.executable, 'sandbox', 'grep', 'root'], stdin=p1.stdout, stdout=PIPE) out, err = p2.communicate() self.assertTrue(b'root' in out) def test_cant_kill(self): "Verify that we cannot send kill signal in the sandbox" pid = os.getpid() - p = Popen(['sandbox', 'kill', '-HUP', str(pid)], stdout=PIPE, stderr=PIPE) + p = Popen([sys.executable, 'sandbox', 'kill', '-HUP', str(pid)], stdout=PIPE, stderr=PIPE) out, err = p.communicate() self.assertDenied(err) def test_cant_ping(self): "Verify that we can't ping within the sandbox" - p = Popen(['sandbox', 'ping', '-c 1 ', '127.0.0.1'], stdout=PIPE, stderr=PIPE) + p = Popen([sys.executable, 'sandbox', 'ping', '-c 1 ', '127.0.0.1'], stdout=PIPE, stderr=PIPE) out, err = p.communicate() self.assertDenied(err) def test_cant_mkdir(self): "Verify that we can't mkdir within the sandbox" - p = Popen(['sandbox', 'mkdir', '~/test'], stdout=PIPE, stderr=PIPE) + p = Popen([sys.executable, 'sandbox', 'mkdir', '~/test'], stdout=PIPE, stderr=PIPE) out, err = p.communicate() self.assertFailure(p.returncode) def test_cant_list_homedir(self): "Verify that we can't list homedir within the sandbox" - p = Popen(['sandbox', 'ls', '~'], stdout=PIPE, stderr=PIPE) + p = Popen([sys.executable, 'sandbox', 'ls', '~'], stdout=PIPE, stderr=PIPE) out, err = p.communicate() self.assertFailure(p.returncode) def test_cant_send_mail(self): "Verify that we can't send mail within the sandbox" - p = Popen(['sandbox', 'mail'], stdout=PIPE, stderr=PIPE) + p = Popen([sys.executable, 'sandbox', 'mail'], stdout=PIPE, stderr=PIPE) out, err = p.communicate() self.assertDenied(err) def test_cant_sudo(self): "Verify that we can't run sudo within the sandbox" - p = Popen(['sandbox', 'sudo'], stdout=PIPE, stderr=PIPE) + p = Popen([sys.executable, 'sandbox', 'sudo'], stdout=PIPE, stderr=PIPE) out, err = p.communicate() self.assertFailure(p.returncode) def test_mount(self): "Verify that we mount a file system" - p = Popen(['sandbox', '-M', 'id'], stdout=PIPE, stderr=PIPE) + p = Popen([sys.executable, 'sandbox', '-M', 'id'], stdout=PIPE, stderr=PIPE) out, err = p.communicate() self.assertSuccess(p.returncode, err) def test_set_level(self): "Verify that we set level a file system" - p = Popen(['sandbox', '-l', 's0', 'id'], stdout=PIPE, stderr=PIPE) + p = Popen([sys.executable, 'sandbox', '-l', 's0', 'id'], stdout=PIPE, stderr=PIPE) out, err = p.communicate() self.assertSuccess(p.returncode, err) def test_homedir(self): "Verify that we set homedir a file system" homedir = mkdtemp(dir=".", prefix=".sandbox_test") - p = Popen(['sandbox', '-H', homedir, '-M', 'id'], stdout=PIPE, stderr=PIPE) + p = Popen([sys.executable, 'sandbox', '-H', homedir, '-M', 'id'], stdout=PIPE, stderr=PIPE) out, err = p.communicate() shutil.rmtree(homedir) self.assertSuccess(p.returncode, err)@@ -90,7 +91,7 @@ class SandboxTests(unittest.TestCase): def test_tmpdir(self): "Verify that we set tmpdir a file system" tmpdir = mkdtemp(dir="/tmp", prefix=".sandbox_test") - p = Popen(['sandbox', '-T', tmpdir, '-M', 'id'], stdout=PIPE, stderr=PIPE) + p = Popen([sys.executable, 'sandbox', '-T', tmpdir, '-M', 'id'], stdout=PIPE, stderr=PIPE) out, err = p.communicate() shutil.rmtree(tmpdir) self.assertSuccess(p.returncode, err)
Signature · 1 line
Petr Lautrbach