diff options
| author | Armin Kuster <akuster808@gmail.com> | 2019-05-18 08:39:25 -0700 |
|---|---|---|
| committer | Armin Kuster <akuster808@gmail.com> | 2019-05-26 21:58:11 -0700 |
| commit | 0acb5d2c7dcacc63f10ca71044b4d80e82d90684 (patch) | |
| tree | 6c98fc593b5baf0cdff05ee5492b778453cf802c /meta-integrity/lib/oeqa/runtime/ima.py | |
| parent | f5e9ba656e2350cbc5b373df307af2d460f1f6f0 (diff) | |
| download | meta-security-0acb5d2c7dcacc63f10ca71044b4d80e82d90684.tar.gz | |
runtime qa: moderize ima test
Signed-off-by: Armin Kuster <akuster808@gmail.com>
Diffstat (limited to 'meta-integrity/lib/oeqa/runtime/ima.py')
| -rw-r--r-- | meta-integrity/lib/oeqa/runtime/ima.py | 82 |
1 files changed, 0 insertions, 82 deletions
diff --git a/meta-integrity/lib/oeqa/runtime/ima.py b/meta-integrity/lib/oeqa/runtime/ima.py deleted file mode 100644 index 2e5b258..0000000 --- a/meta-integrity/lib/oeqa/runtime/ima.py +++ /dev/null | |||
| @@ -1,82 +0,0 @@ | |||
| 1 | #!/usr/bin/env python | ||
| 2 | # | ||
| 3 | # Authors: Cristina Moraru <cristina.moraru@intel.com> | ||
| 4 | # Alexandru Cornea <alexandru.cornea@intel.com> | ||
| 5 | |||
| 6 | import unittest | ||
| 7 | import string | ||
| 8 | from time import sleep | ||
| 9 | from oeqa.oetest import oeRuntimeTest, skipModule | ||
| 10 | from oeqa.utils.decorators import * | ||
| 11 | |||
| 12 | @tag(TestType = 'FVT', FeatureID = 'IOTOS-617,IOTOS-619') | ||
| 13 | class IMACheck(oeRuntimeTest): | ||
| 14 | def test_ima_before_systemd(self): | ||
| 15 | ''' Test if IMA policy is loaded before systemd starts''' | ||
| 16 | |||
| 17 | ima_search = "IMA: policy update completed" | ||
| 18 | systemd_search = "systemd .* running" | ||
| 19 | status, output = self.target.run("dmesg | grep -n '%s'" %ima_search) | ||
| 20 | self.assertEqual( status, 0, "Did not find '%s' in dmesg" %ima_search) | ||
| 21 | ima_id = int(output.split(":")[0]) | ||
| 22 | status, output = self.target.run("dmesg | grep -n '%s'" %systemd_search) | ||
| 23 | self.assertEqual(status, 0, "Did not find '%s' in dmesg" %systemd_search) | ||
| 24 | init_id = int(output.split(":")[0]) | ||
| 25 | if ima_id > init_id: | ||
| 26 | self.fail("IMA does not start before systemd") | ||
| 27 | |||
| 28 | def test_ima_hash(self): | ||
| 29 | ''' Test if IMA stores correct file hash ''' | ||
| 30 | filename = "/etc/filetest" | ||
| 31 | ima_measure_file = "/sys/kernel/security/ima/ascii_runtime_measurements" | ||
| 32 | status, output = self.target.run("echo test > %s" %filename) | ||
| 33 | self.assertEqual(status, 0, "Cannot create file %s on target" %filename) | ||
| 34 | |||
| 35 | # wait for the IMA system to update the entry | ||
| 36 | maximum_tries = 30 | ||
| 37 | tries = 0 | ||
| 38 | status, output = self.target.run("sha1sum %s" %filename) | ||
| 39 | current_hash = output.split()[0] | ||
| 40 | ima_hash = "" | ||
| 41 | |||
| 42 | while tries < maximum_tries: | ||
| 43 | status, output = self.target.run("cat %s | grep %s" \ | ||
| 44 | %(ima_measure_file, filename)) | ||
| 45 | # get last entry, 4th field | ||
| 46 | if status == 0: | ||
| 47 | tokens = output.split("\n")[-1].split()[3] | ||
| 48 | ima_hash = tokens.split(":")[1] | ||
| 49 | if ima_hash == current_hash: | ||
| 50 | break | ||
| 51 | |||
| 52 | tries += 1 | ||
| 53 | sleep(1) | ||
| 54 | |||
| 55 | # clean target | ||
| 56 | self.target.run("rm %s" %filename) | ||
| 57 | if ima_hash != current_hash: | ||
| 58 | self.fail("Hash stored by IMA does not match actual hash") | ||
| 59 | |||
| 60 | def test_ima_signature(self): | ||
| 61 | ''' Test if IMA stores correct signature for system binaries''' | ||
| 62 | locations = ["/bin", "/usr/bin"] | ||
| 63 | binaries = [] | ||
| 64 | for l in locations: | ||
| 65 | status, output = self.target.run("find %s -type f" %l) | ||
| 66 | binaries.extend(output.split("\n")) | ||
| 67 | |||
| 68 | for b in binaries: | ||
| 69 | status, output = self.target.run("evmctl ima_verify %s" %b) | ||
| 70 | if "Verification is OK" not in output: | ||
| 71 | self.fail("IMA signature verification fails for file %s" %b) | ||
| 72 | |||
| 73 | def test_ima_overwrite(self): | ||
| 74 | ''' Test if IMA prevents overwriting signed files ''' | ||
| 75 | status, output = self.target.run("find /bin -type f") | ||
| 76 | self.assertEqual(status, 0 , "ssh to device fail: %s" %output) | ||
| 77 | signed_file = output.strip().split()[0] | ||
| 78 | print("\n signed_file is %s" % signed_file) | ||
| 79 | status, output = self.target.run(" echo 'foo' >> %s" %signed_file) | ||
| 80 | self.assertNotEqual(status, 0 , "Signed file could be written") | ||
| 81 | self.assertIn("Permission denied", output, | ||
| 82 | "Did not find expected error message. Got: %s" %output) | ||
