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