Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 1 | from optparse import OptionParser |
| 2 | from optparse import Option, OptionValueError |
| 3 | import os |
| 4 | import policy |
| 5 | import re |
| 6 | import sys |
| 7 | |
| 8 | ############################################################# |
| 9 | # Tests |
| 10 | ############################################################# |
| 11 | def TestDataTypeViolations(pol): |
| 12 | return pol.AssertPathTypesHaveAttr(["/data/"], [], "data_file_type") |
| 13 | |
Nick Kralevich | dab131b | 2018-10-04 11:24:00 -0700 | [diff] [blame] | 14 | def TestSystemTypeViolations(pol): |
Steven Moreland | a01338d | 2020-04-27 15:54:54 -0700 | [diff] [blame^] | 15 | partitions = ["/system/", "/system_ext/", "/product/"] |
| 16 | exceptions = [ |
| 17 | # devices before treble don't have a vendor partition |
| 18 | "/system/vendor/", |
| 19 | |
| 20 | # overlay files are mounted over vendor |
| 21 | "/product/overlay/", |
| 22 | "/product/vendor_overlay/", |
| 23 | "/system/overlay/", |
| 24 | "/system/product/overlay/", |
| 25 | "/system/product/vendor_overlay/", |
| 26 | "/system/system_ext/overlay/", |
| 27 | "/system_ext/overlay/", |
| 28 | ] |
| 29 | |
| 30 | return pol.AssertPathTypesHaveAttr(partitions, exceptions, "system_file_type") |
Nick Kralevich | 5e37271 | 2018-09-27 10:21:37 -0700 | [diff] [blame] | 31 | |
Tri Vo | 4c80c2c | 2018-03-29 03:42:47 +0000 | [diff] [blame] | 32 | def TestProcTypeViolations(pol): |
| 33 | return pol.AssertGenfsFilesystemTypesHaveAttr("proc", "proc_type") |
| 34 | |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 35 | def TestSysfsTypeViolations(pol): |
Jeff Vander Stoep | 1b82844 | 2018-03-21 17:27:20 -0700 | [diff] [blame] | 36 | ret = pol.AssertGenfsFilesystemTypesHaveAttr("sysfs", "sysfs_type") |
| 37 | ret += pol.AssertPathTypesHaveAttr(["/sys/"], ["/sys/kernel/debug/", |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 38 | "/sys/kernel/tracing"], "sysfs_type") |
Jeff Vander Stoep | 1b82844 | 2018-03-21 17:27:20 -0700 | [diff] [blame] | 39 | return ret |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 40 | |
| 41 | def TestDebugfsTypeViolations(pol): |
Jeff Vander Stoep | 1b82844 | 2018-03-21 17:27:20 -0700 | [diff] [blame] | 42 | ret = pol.AssertGenfsFilesystemTypesHaveAttr("debugfs", "debugfs_type") |
| 43 | ret += pol.AssertGenfsFilesystemTypesHaveAttr("tracefs", "debugfs_type") |
| 44 | ret += pol.AssertPathTypesHaveAttr(["/sys/kernel/debug/", |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 45 | "/sys/kernel/tracing"], [], "debugfs_type") |
Jeff Vander Stoep | 1b82844 | 2018-03-21 17:27:20 -0700 | [diff] [blame] | 46 | return ret |
Tri Vo | 30c3c2a | 2018-01-10 11:04:06 -0800 | [diff] [blame] | 47 | |
| 48 | def TestVendorTypeViolations(pol): |
Steven Moreland | a01338d | 2020-04-27 15:54:54 -0700 | [diff] [blame^] | 49 | partitions = ["/vendor/", "/odm/"] |
| 50 | exceptions = [ |
| 51 | "/vendor/etc/selinux/", |
| 52 | "/vendor/odm/etc/selinux/", |
| 53 | "/odm/etc/selinux/", |
| 54 | ] |
| 55 | return pol.AssertPathTypesHaveAttr(partitions, exceptions, "vendor_file_type") |
Tri Vo | 30c3c2a | 2018-01-10 11:04:06 -0800 | [diff] [blame] | 56 | |
Jeff Vander Stoep | ccf965e | 2018-01-24 07:01:13 -0800 | [diff] [blame] | 57 | def TestCoreDataTypeViolations(pol): |
Jeff Vander Stoep | 370a52f | 2018-02-08 09:54:59 -0800 | [diff] [blame] | 58 | return pol.AssertPathTypesHaveAttr(["/data/"], ["/data/vendor", |
| 59 | "/data/vendor_ce", "/data/vendor_de"], "core_data_file_type") |
Jeff Vander Stoep | ccf965e | 2018-01-24 07:01:13 -0800 | [diff] [blame] | 60 | |
Inseob Kim | 1b8b1f6 | 2020-10-23 15:16:11 +0900 | [diff] [blame] | 61 | def TestPropertyTypeViolations(pol): |
| 62 | return pol.AssertPropertyOwnersAreExclusive() |
| 63 | |
| 64 | |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 65 | ### |
| 66 | # extend OptionParser to allow the same option flag to be used multiple times. |
| 67 | # This is used to allow multiple file_contexts files and tests to be |
| 68 | # specified. |
| 69 | # |
| 70 | class MultipleOption(Option): |
| 71 | ACTIONS = Option.ACTIONS + ("extend",) |
| 72 | STORE_ACTIONS = Option.STORE_ACTIONS + ("extend",) |
| 73 | TYPED_ACTIONS = Option.TYPED_ACTIONS + ("extend",) |
| 74 | ALWAYS_TYPED_ACTIONS = Option.ALWAYS_TYPED_ACTIONS + ("extend",) |
| 75 | |
| 76 | def take_action(self, action, dest, opt, value, values, parser): |
| 77 | if action == "extend": |
| 78 | values.ensure_value(dest, []).append(value) |
| 79 | else: |
| 80 | Option.take_action(self, action, dest, opt, value, values, parser) |
| 81 | |
Tri Vo | 4c80c2c | 2018-03-29 03:42:47 +0000 | [diff] [blame] | 82 | Tests = [ |
| 83 | "TestDataTypeViolators", |
| 84 | "TestProcTypeViolations", |
| 85 | "TestSysfsTypeViolations", |
Nick Kralevich | dab131b | 2018-10-04 11:24:00 -0700 | [diff] [blame] | 86 | "TestSystemTypeViolators", |
Tri Vo | 4c80c2c | 2018-03-29 03:42:47 +0000 | [diff] [blame] | 87 | "TestDebugfsTypeViolations", |
| 88 | "TestVendorTypeViolations", |
| 89 | "TestCoreDataTypeViolations", |
Inseob Kim | 1b8b1f6 | 2020-10-23 15:16:11 +0900 | [diff] [blame] | 90 | "TestPropertyTypeViolations" |
Tri Vo | 4c80c2c | 2018-03-29 03:42:47 +0000 | [diff] [blame] | 91 | ] |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 92 | |
| 93 | if __name__ == '__main__': |
Jeff Vander Stoep | 3ca843a | 2017-10-04 09:42:29 -0700 | [diff] [blame] | 94 | usage = "sepolicy_tests -l $(ANDROID_HOST_OUT)/lib64/libsepolwrap.so " |
Bowgo Tsai | afbcf21 | 2018-02-05 17:34:52 +0800 | [diff] [blame] | 95 | usage += "-f vendor_file_contexts -f " |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 96 | usage +="plat_file_contexts -p policy [--test test] [--help]" |
| 97 | parser = OptionParser(option_class=MultipleOption, usage=usage) |
| 98 | parser.add_option("-f", "--file_contexts", dest="file_contexts", |
| 99 | metavar="FILE", action="extend", type="string") |
| 100 | parser.add_option("-p", "--policy", dest="policy", metavar="FILE") |
| 101 | parser.add_option("-l", "--library-path", dest="libpath", metavar="FILE") |
| 102 | parser.add_option("-t", "--test", dest="test", action="extend", |
| 103 | help="Test options include "+str(Tests)) |
| 104 | |
| 105 | (options, args) = parser.parse_args() |
| 106 | |
| 107 | if not options.libpath: |
Jeff Vander Stoep | 3ca843a | 2017-10-04 09:42:29 -0700 | [diff] [blame] | 108 | sys.exit("Must specify path to libsepolwrap library\n" + parser.usage) |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 109 | if not os.path.exists(options.libpath): |
| 110 | sys.exit("Error: library-path " + options.libpath + " does not exist\n" |
| 111 | + parser.usage) |
| 112 | |
| 113 | if not options.policy: |
| 114 | sys.exit("Must specify monolithic policy file\n" + parser.usage) |
| 115 | if not os.path.exists(options.policy): |
| 116 | sys.exit("Error: policy file " + options.policy + " does not exist\n" |
| 117 | + parser.usage) |
| 118 | |
| 119 | if not options.file_contexts: |
| 120 | sys.exit("Error: Must specify file_contexts file(s)\n" + parser.usage) |
| 121 | for f in options.file_contexts: |
| 122 | if not os.path.exists(f): |
| 123 | sys.exit("Error: File_contexts file " + f + " does not exist\n" + |
| 124 | parser.usage) |
| 125 | |
| 126 | pol = policy.Policy(options.policy, options.file_contexts, options.libpath) |
| 127 | |
| 128 | results = "" |
| 129 | # If an individual test is not specified, run all tests. |
Jeff Vander Stoep | 3ca843a | 2017-10-04 09:42:29 -0700 | [diff] [blame] | 130 | if options.test is None or "TestDataTypeViolations" in options.test: |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 131 | results += TestDataTypeViolations(pol) |
Tri Vo | 4c80c2c | 2018-03-29 03:42:47 +0000 | [diff] [blame] | 132 | if options.test is None or "TestProcTypeViolations" in options.test: |
| 133 | results += TestProcTypeViolations(pol) |
Jeff Vander Stoep | 3ca843a | 2017-10-04 09:42:29 -0700 | [diff] [blame] | 134 | if options.test is None or "TestSysfsTypeViolations" in options.test: |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 135 | results += TestSysfsTypeViolations(pol) |
Nick Kralevich | dab131b | 2018-10-04 11:24:00 -0700 | [diff] [blame] | 136 | if options.test is None or "TestSystemTypeViolations" in options.test: |
| 137 | results += TestSystemTypeViolations(pol) |
Jeff Vander Stoep | 3ca843a | 2017-10-04 09:42:29 -0700 | [diff] [blame] | 138 | if options.test is None or "TestDebugfsTypeViolations" in options.test: |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 139 | results += TestDebugfsTypeViolations(pol) |
Tri Vo | 30c3c2a | 2018-01-10 11:04:06 -0800 | [diff] [blame] | 140 | if options.test is None or "TestVendorTypeViolations" in options.test: |
| 141 | results += TestVendorTypeViolations(pol) |
Jeff Vander Stoep | ccf965e | 2018-01-24 07:01:13 -0800 | [diff] [blame] | 142 | if options.test is None or "TestCoreDataTypeViolations" in options.test: |
| 143 | results += TestCoreDataTypeViolations(pol) |
Inseob Kim | 1b8b1f6 | 2020-10-23 15:16:11 +0900 | [diff] [blame] | 144 | if options.test is None or "TestPropertyTypeViolations" in options.test: |
| 145 | results += TestPropertyTypeViolations(pol) |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 146 | |
| 147 | if len(results) > 0: |
| 148 | sys.exit(results) |