ThiƩbaud Weksteen | f24b457 | 2021-11-26 09:12:41 +1100 | [diff] [blame] | 1 | # Copyright 2021 The Android Open Source Project |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 15 | from optparse import OptionParser |
| 16 | from optparse import Option, OptionValueError |
| 17 | import os |
| 18 | import policy |
| 19 | import re |
| 20 | import sys |
Inseob Kim | 6fa8efd | 2021-12-29 13:56:14 +0900 | [diff] [blame^] | 21 | import distutils.ccompiler |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 22 | |
| 23 | ############################################################# |
| 24 | # Tests |
| 25 | ############################################################# |
| 26 | def TestDataTypeViolations(pol): |
| 27 | return pol.AssertPathTypesHaveAttr(["/data/"], [], "data_file_type") |
| 28 | |
Nick Kralevich | dab131b | 2018-10-04 11:24:00 -0700 | [diff] [blame] | 29 | def TestSystemTypeViolations(pol): |
Steven Moreland | a01338d | 2020-04-27 15:54:54 -0700 | [diff] [blame] | 30 | partitions = ["/system/", "/system_ext/", "/product/"] |
| 31 | exceptions = [ |
| 32 | # devices before treble don't have a vendor partition |
| 33 | "/system/vendor/", |
| 34 | |
| 35 | # overlay files are mounted over vendor |
| 36 | "/product/overlay/", |
| 37 | "/product/vendor_overlay/", |
| 38 | "/system/overlay/", |
| 39 | "/system/product/overlay/", |
| 40 | "/system/product/vendor_overlay/", |
| 41 | "/system/system_ext/overlay/", |
| 42 | "/system_ext/overlay/", |
| 43 | ] |
| 44 | |
| 45 | return pol.AssertPathTypesHaveAttr(partitions, exceptions, "system_file_type") |
Nick Kralevich | 5e37271 | 2018-09-27 10:21:37 -0700 | [diff] [blame] | 46 | |
Tri Vo | 4c80c2c | 2018-03-29 03:42:47 +0000 | [diff] [blame] | 47 | def TestProcTypeViolations(pol): |
| 48 | return pol.AssertGenfsFilesystemTypesHaveAttr("proc", "proc_type") |
| 49 | |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 50 | def TestSysfsTypeViolations(pol): |
Jeff Vander Stoep | 1b82844 | 2018-03-21 17:27:20 -0700 | [diff] [blame] | 51 | ret = pol.AssertGenfsFilesystemTypesHaveAttr("sysfs", "sysfs_type") |
| 52 | ret += pol.AssertPathTypesHaveAttr(["/sys/"], ["/sys/kernel/debug/", |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 53 | "/sys/kernel/tracing"], "sysfs_type") |
Jeff Vander Stoep | 1b82844 | 2018-03-21 17:27:20 -0700 | [diff] [blame] | 54 | return ret |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 55 | |
| 56 | def TestDebugfsTypeViolations(pol): |
Jeff Vander Stoep | 1b82844 | 2018-03-21 17:27:20 -0700 | [diff] [blame] | 57 | ret = pol.AssertGenfsFilesystemTypesHaveAttr("debugfs", "debugfs_type") |
Jeff Vander Stoep | 1b82844 | 2018-03-21 17:27:20 -0700 | [diff] [blame] | 58 | ret += pol.AssertPathTypesHaveAttr(["/sys/kernel/debug/", |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 59 | "/sys/kernel/tracing"], [], "debugfs_type") |
Jeff Vander Stoep | 1b82844 | 2018-03-21 17:27:20 -0700 | [diff] [blame] | 60 | return ret |
Tri Vo | 30c3c2a | 2018-01-10 11:04:06 -0800 | [diff] [blame] | 61 | |
Hridya Valsaraju | edccaa8 | 2021-05-14 14:01:11 -0700 | [diff] [blame] | 62 | def TestTracefsTypeViolations(pol): |
| 63 | ret = pol.AssertGenfsFilesystemTypesHaveAttr("tracefs", "tracefs_type") |
| 64 | ret += pol.AssertPathTypesHaveAttr(["/sys/kernel/tracing"], [], "tracefs_type") |
| 65 | ret += pol.AssertPathTypesDoNotHaveAttr(["/sys/kernel/debug"], |
| 66 | ["/sys/kernel/debug/tracing"], "tracefs_type", |
| 67 | []) |
| 68 | return ret |
| 69 | |
Tri Vo | 30c3c2a | 2018-01-10 11:04:06 -0800 | [diff] [blame] | 70 | def TestVendorTypeViolations(pol): |
Steven Moreland | a01338d | 2020-04-27 15:54:54 -0700 | [diff] [blame] | 71 | partitions = ["/vendor/", "/odm/"] |
| 72 | exceptions = [ |
| 73 | "/vendor/etc/selinux/", |
| 74 | "/vendor/odm/etc/selinux/", |
| 75 | "/odm/etc/selinux/", |
| 76 | ] |
| 77 | return pol.AssertPathTypesHaveAttr(partitions, exceptions, "vendor_file_type") |
Tri Vo | 30c3c2a | 2018-01-10 11:04:06 -0800 | [diff] [blame] | 78 | |
Jeff Vander Stoep | ccf965e | 2018-01-24 07:01:13 -0800 | [diff] [blame] | 79 | def TestCoreDataTypeViolations(pol): |
Jeff Vander Stoep | 370a52f | 2018-02-08 09:54:59 -0800 | [diff] [blame] | 80 | return pol.AssertPathTypesHaveAttr(["/data/"], ["/data/vendor", |
| 81 | "/data/vendor_ce", "/data/vendor_de"], "core_data_file_type") |
Jeff Vander Stoep | ccf965e | 2018-01-24 07:01:13 -0800 | [diff] [blame] | 82 | |
Inseob Kim | 1b8b1f6 | 2020-10-23 15:16:11 +0900 | [diff] [blame] | 83 | def TestPropertyTypeViolations(pol): |
| 84 | return pol.AssertPropertyOwnersAreExclusive() |
| 85 | |
Alan Stokes | 668e74f | 2020-11-12 18:08:18 +0000 | [diff] [blame] | 86 | def TestAppDataTypeViolations(pol): |
| 87 | # Types with the app_data_file_type should only be used for app data files |
| 88 | # (/data/data/package.name etc) via seapp_contexts, and never applied |
| 89 | # explicitly to other files. |
| 90 | partitions = [ |
| 91 | "/data/", |
| 92 | "/vendor/", |
| 93 | "/odm/", |
| 94 | "/product/", |
| 95 | ] |
| 96 | exceptions = [ |
| 97 | # These are used for app data files for the corresponding user and |
| 98 | # assorted other files. |
| 99 | # TODO(b/172812577): Use different types for the different purposes |
| 100 | "shell_data_file", |
| 101 | "bluetooth_data_file", |
| 102 | "nfc_data_file", |
| 103 | "radio_data_file", |
| 104 | ] |
| 105 | return pol.AssertPathTypesDoNotHaveAttr(partitions, [], "app_data_file_type", |
| 106 | exceptions) |
Hridya Valsaraju | 8c9cf62 | 2020-12-14 22:57:49 -0800 | [diff] [blame] | 107 | def TestDmaHeapDevTypeViolations(pol): |
| 108 | return pol.AssertPathTypesHaveAttr(["/dev/dma_heap/"], [], |
| 109 | "dmabuf_heap_device_type") |
| 110 | |
Alan Stokes | 668e74f | 2020-11-12 18:08:18 +0000 | [diff] [blame] | 111 | |
Inseob Kim | 1b8b1f6 | 2020-10-23 15:16:11 +0900 | [diff] [blame] | 112 | |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 113 | ### |
| 114 | # extend OptionParser to allow the same option flag to be used multiple times. |
| 115 | # This is used to allow multiple file_contexts files and tests to be |
| 116 | # specified. |
| 117 | # |
| 118 | class MultipleOption(Option): |
| 119 | ACTIONS = Option.ACTIONS + ("extend",) |
| 120 | STORE_ACTIONS = Option.STORE_ACTIONS + ("extend",) |
| 121 | TYPED_ACTIONS = Option.TYPED_ACTIONS + ("extend",) |
| 122 | ALWAYS_TYPED_ACTIONS = Option.ALWAYS_TYPED_ACTIONS + ("extend",) |
| 123 | |
| 124 | def take_action(self, action, dest, opt, value, values, parser): |
| 125 | if action == "extend": |
| 126 | values.ensure_value(dest, []).append(value) |
| 127 | else: |
| 128 | Option.take_action(self, action, dest, opt, value, values, parser) |
| 129 | |
Tri Vo | 4c80c2c | 2018-03-29 03:42:47 +0000 | [diff] [blame] | 130 | Tests = [ |
| 131 | "TestDataTypeViolators", |
| 132 | "TestProcTypeViolations", |
| 133 | "TestSysfsTypeViolations", |
Nick Kralevich | dab131b | 2018-10-04 11:24:00 -0700 | [diff] [blame] | 134 | "TestSystemTypeViolators", |
Tri Vo | 4c80c2c | 2018-03-29 03:42:47 +0000 | [diff] [blame] | 135 | "TestDebugfsTypeViolations", |
Hridya Valsaraju | edccaa8 | 2021-05-14 14:01:11 -0700 | [diff] [blame] | 136 | "TestTracefsTypeViolations", |
Tri Vo | 4c80c2c | 2018-03-29 03:42:47 +0000 | [diff] [blame] | 137 | "TestVendorTypeViolations", |
| 138 | "TestCoreDataTypeViolations", |
Alan Stokes | 668e74f | 2020-11-12 18:08:18 +0000 | [diff] [blame] | 139 | "TestPropertyTypeViolations", |
| 140 | "TestAppDataTypeViolations", |
Hridya Valsaraju | 8c9cf62 | 2020-12-14 22:57:49 -0800 | [diff] [blame] | 141 | "TestDmaHeapDevTypeViolations", |
Tri Vo | 4c80c2c | 2018-03-29 03:42:47 +0000 | [diff] [blame] | 142 | ] |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 143 | |
| 144 | if __name__ == '__main__': |
Inseob Kim | 6fa8efd | 2021-12-29 13:56:14 +0900 | [diff] [blame^] | 145 | usage = "sepolicy_tests -f vendor_file_contexts -f " |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 146 | usage +="plat_file_contexts -p policy [--test test] [--help]" |
| 147 | parser = OptionParser(option_class=MultipleOption, usage=usage) |
| 148 | parser.add_option("-f", "--file_contexts", dest="file_contexts", |
| 149 | metavar="FILE", action="extend", type="string") |
| 150 | parser.add_option("-p", "--policy", dest="policy", metavar="FILE") |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 151 | parser.add_option("-t", "--test", dest="test", action="extend", |
| 152 | help="Test options include "+str(Tests)) |
| 153 | |
| 154 | (options, args) = parser.parse_args() |
| 155 | |
Inseob Kim | 6fa8efd | 2021-12-29 13:56:14 +0900 | [diff] [blame^] | 156 | libpath = os.path.join(os.path.dirname(os.path.realpath(__file__)), |
| 157 | "libsepolwrap" + distutils.ccompiler.new_compiler().shared_lib_extension) |
| 158 | if not os.path.exists(libpath): |
| 159 | sys.exit("Error: libsepolwrap does not exist. Is this binary corrupted?\n") |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 160 | |
| 161 | if not options.policy: |
| 162 | sys.exit("Must specify monolithic policy file\n" + parser.usage) |
| 163 | if not os.path.exists(options.policy): |
| 164 | sys.exit("Error: policy file " + options.policy + " does not exist\n" |
| 165 | + parser.usage) |
| 166 | |
| 167 | if not options.file_contexts: |
| 168 | sys.exit("Error: Must specify file_contexts file(s)\n" + parser.usage) |
| 169 | for f in options.file_contexts: |
| 170 | if not os.path.exists(f): |
| 171 | sys.exit("Error: File_contexts file " + f + " does not exist\n" + |
| 172 | parser.usage) |
| 173 | |
Inseob Kim | 6fa8efd | 2021-12-29 13:56:14 +0900 | [diff] [blame^] | 174 | pol = policy.Policy(options.policy, options.file_contexts, libpath) |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 175 | |
| 176 | results = "" |
| 177 | # If an individual test is not specified, run all tests. |
Jeff Vander Stoep | 3ca843a | 2017-10-04 09:42:29 -0700 | [diff] [blame] | 178 | if options.test is None or "TestDataTypeViolations" in options.test: |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 179 | results += TestDataTypeViolations(pol) |
Tri Vo | 4c80c2c | 2018-03-29 03:42:47 +0000 | [diff] [blame] | 180 | if options.test is None or "TestProcTypeViolations" in options.test: |
| 181 | results += TestProcTypeViolations(pol) |
Jeff Vander Stoep | 3ca843a | 2017-10-04 09:42:29 -0700 | [diff] [blame] | 182 | if options.test is None or "TestSysfsTypeViolations" in options.test: |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 183 | results += TestSysfsTypeViolations(pol) |
Nick Kralevich | dab131b | 2018-10-04 11:24:00 -0700 | [diff] [blame] | 184 | if options.test is None or "TestSystemTypeViolations" in options.test: |
| 185 | results += TestSystemTypeViolations(pol) |
Jeff Vander Stoep | 3ca843a | 2017-10-04 09:42:29 -0700 | [diff] [blame] | 186 | if options.test is None or "TestDebugfsTypeViolations" in options.test: |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 187 | results += TestDebugfsTypeViolations(pol) |
Hridya Valsaraju | edccaa8 | 2021-05-14 14:01:11 -0700 | [diff] [blame] | 188 | if options.test is None or "TestTracefsTypeViolations" in options.test: |
| 189 | results += TestTracefsTypeViolations(pol) |
Tri Vo | 30c3c2a | 2018-01-10 11:04:06 -0800 | [diff] [blame] | 190 | if options.test is None or "TestVendorTypeViolations" in options.test: |
| 191 | results += TestVendorTypeViolations(pol) |
Jeff Vander Stoep | ccf965e | 2018-01-24 07:01:13 -0800 | [diff] [blame] | 192 | if options.test is None or "TestCoreDataTypeViolations" in options.test: |
| 193 | results += TestCoreDataTypeViolations(pol) |
Inseob Kim | 1b8b1f6 | 2020-10-23 15:16:11 +0900 | [diff] [blame] | 194 | if options.test is None or "TestPropertyTypeViolations" in options.test: |
| 195 | results += TestPropertyTypeViolations(pol) |
Alan Stokes | 668e74f | 2020-11-12 18:08:18 +0000 | [diff] [blame] | 196 | if options.test is None or "TestAppDataTypeViolations" in options.test: |
| 197 | results += TestAppDataTypeViolations(pol) |
Hridya Valsaraju | 8c9cf62 | 2020-12-14 22:57:49 -0800 | [diff] [blame] | 198 | if options.test is None or "TestDmaHeapDevTypeViolations" in options.test: |
| 199 | results += TestDmaHeapDevTypeViolations(pol) |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 200 | |
| 201 | if len(results) > 0: |
| 202 | sys.exit(results) |