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