Yi-Yo Chiang | 41c34d6 | 2021-04-13 02:44:41 +0800 | [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 | |
| 15 | package selinux |
| 16 | |
| 17 | import ( |
Inseob Kim | 16d3be3 | 2022-01-07 09:15:27 +0900 | [diff] [blame] | 18 | "fmt" |
| 19 | |
Yi-Yo Chiang | 41c34d6 | 2021-04-13 02:44:41 +0800 | [diff] [blame] | 20 | "github.com/google/blueprint/proptools" |
| 21 | |
| 22 | "android/soong/android" |
| 23 | ) |
| 24 | |
Inseob Kim | 16d3be3 | 2022-01-07 09:15:27 +0900 | [diff] [blame] | 25 | var ( |
| 26 | compatTestDepTag = dependencyTag{name: "compat_test"} |
| 27 | ) |
| 28 | |
Yi-Yo Chiang | 41c34d6 | 2021-04-13 02:44:41 +0800 | [diff] [blame] | 29 | func init() { |
Inseob Kim | 16d3be3 | 2022-01-07 09:15:27 +0900 | [diff] [blame] | 30 | ctx := android.InitRegistrationContext |
| 31 | ctx.RegisterModuleType("se_compat_cil", compatCilFactory) |
Cole Faust | 8fe6568 | 2024-11-04 16:31:39 -0800 | [diff] [blame] | 32 | ctx.RegisterModuleType("se_compat_test", compatTestFactory) |
Yi-Yo Chiang | 41c34d6 | 2021-04-13 02:44:41 +0800 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | // se_compat_cil collects and installs backwards compatibility cil files. |
| 36 | func compatCilFactory() android.Module { |
| 37 | c := &compatCil{} |
| 38 | c.AddProperties(&c.properties) |
| 39 | android.InitAndroidArchModule(c, android.DeviceSupported, android.MultilibCommon) |
| 40 | return c |
| 41 | } |
| 42 | |
| 43 | type compatCil struct { |
| 44 | android.ModuleBase |
| 45 | properties compatCilProperties |
Inseob Kim | 61d6beb | 2023-08-23 08:37:20 +0000 | [diff] [blame] | 46 | installSource android.OptionalPath |
Yi-Yo Chiang | 41c34d6 | 2021-04-13 02:44:41 +0800 | [diff] [blame] | 47 | installPath android.InstallPath |
| 48 | } |
| 49 | |
| 50 | type compatCilProperties struct { |
Inseob Kim | 4196403 | 2022-04-22 07:50:22 +0900 | [diff] [blame] | 51 | // List of source files. Can reference se_build_files type modules with the ":module" syntax. |
Paul Duffin | 532bde1 | 2021-07-09 22:53:03 +0100 | [diff] [blame] | 52 | Srcs []string `android:"path"` |
Yi-Yo Chiang | 41c34d6 | 2021-04-13 02:44:41 +0800 | [diff] [blame] | 53 | |
| 54 | // Output file name. Defaults to module name if unspecified. |
| 55 | Stem *string |
Inseob Kim | 61d6beb | 2023-08-23 08:37:20 +0000 | [diff] [blame] | 56 | |
| 57 | // Target version that this module supports. This module will be ignored if platform sepolicy |
| 58 | // version is same as this module's version. |
| 59 | Version *string |
Yi-Yo Chiang | 41c34d6 | 2021-04-13 02:44:41 +0800 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | func (c *compatCil) stem() string { |
| 63 | return proptools.StringDefault(c.properties.Stem, c.Name()) |
| 64 | } |
| 65 | |
| 66 | func (c *compatCil) expandSeSources(ctx android.ModuleContext) android.Paths { |
Inseob Kim | 4196403 | 2022-04-22 07:50:22 +0900 | [diff] [blame] | 67 | return android.PathsForModuleSrc(ctx, c.properties.Srcs) |
Yi-Yo Chiang | 41c34d6 | 2021-04-13 02:44:41 +0800 | [diff] [blame] | 68 | } |
| 69 | |
Inseob Kim | 61d6beb | 2023-08-23 08:37:20 +0000 | [diff] [blame] | 70 | func (c *compatCil) shouldSkipBuild(ctx android.ModuleContext) bool { |
| 71 | return proptools.String(c.properties.Version) == ctx.DeviceConfig().PlatformSepolicyVersion() |
| 72 | } |
| 73 | |
Yi-Yo Chiang | 41c34d6 | 2021-04-13 02:44:41 +0800 | [diff] [blame] | 74 | func (c *compatCil) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 75 | if c.ProductSpecific() || c.SocSpecific() || c.DeviceSpecific() { |
| 76 | ctx.ModuleErrorf("Compat cil files only support system and system_ext partitions") |
| 77 | } |
| 78 | |
Inseob Kim | 61d6beb | 2023-08-23 08:37:20 +0000 | [diff] [blame] | 79 | if c.shouldSkipBuild(ctx) { |
| 80 | return |
| 81 | } |
| 82 | |
Yi-Yo Chiang | 41c34d6 | 2021-04-13 02:44:41 +0800 | [diff] [blame] | 83 | srcPaths := c.expandSeSources(ctx) |
| 84 | out := android.PathForModuleGen(ctx, c.Name()) |
Yi-Yo Chiang | 41c34d6 | 2021-04-13 02:44:41 +0800 | [diff] [blame] | 85 | ctx.Build(pctx, android.BuildParams{ |
| 86 | Rule: android.Cat, |
| 87 | Inputs: srcPaths, |
| 88 | Output: out, |
| 89 | Description: "Combining compat cil for " + c.Name(), |
| 90 | }) |
| 91 | |
| 92 | c.installPath = android.PathForModuleInstall(ctx, "etc", "selinux", "mapping") |
Inseob Kim | 61d6beb | 2023-08-23 08:37:20 +0000 | [diff] [blame] | 93 | c.installSource = android.OptionalPathForPath(out) |
| 94 | ctx.InstallFile(c.installPath, c.stem(), out) |
mrziwang | cb3f550 | 2024-06-06 10:04:23 -0700 | [diff] [blame] | 95 | |
| 96 | if c.installSource.Valid() { |
| 97 | ctx.SetOutputFiles(android.Paths{c.installSource.Path()}, "") |
| 98 | } |
Yi-Yo Chiang | 41c34d6 | 2021-04-13 02:44:41 +0800 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | func (c *compatCil) AndroidMkEntries() []android.AndroidMkEntries { |
Inseob Kim | 61d6beb | 2023-08-23 08:37:20 +0000 | [diff] [blame] | 102 | if !c.installSource.Valid() { |
| 103 | return nil |
| 104 | } |
Yi-Yo Chiang | 41c34d6 | 2021-04-13 02:44:41 +0800 | [diff] [blame] | 105 | return []android.AndroidMkEntries{android.AndroidMkEntries{ |
| 106 | Class: "ETC", |
Inseob Kim | 61d6beb | 2023-08-23 08:37:20 +0000 | [diff] [blame] | 107 | OutputFile: c.installSource, |
Yi-Yo Chiang | 41c34d6 | 2021-04-13 02:44:41 +0800 | [diff] [blame] | 108 | ExtraEntries: []android.AndroidMkExtraEntriesFunc{ |
| 109 | func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) { |
Colin Cross | 6c7f937 | 2022-01-11 19:35:43 -0800 | [diff] [blame] | 110 | entries.SetPath("LOCAL_MODULE_PATH", c.installPath) |
Yi-Yo Chiang | 41c34d6 | 2021-04-13 02:44:41 +0800 | [diff] [blame] | 111 | entries.SetString("LOCAL_INSTALLED_MODULE_STEM", c.stem()) |
| 112 | }, |
| 113 | }, |
| 114 | }} |
| 115 | } |
Inseob Kim | 16d3be3 | 2022-01-07 09:15:27 +0900 | [diff] [blame] | 116 | |
Inseob Kim | 16d3be3 | 2022-01-07 09:15:27 +0900 | [diff] [blame] | 117 | // se_compat_test checks if compat files ({ver}.cil, {ver}.compat.cil) files are compatible with |
| 118 | // current policy. |
Cole Faust | 8fe6568 | 2024-11-04 16:31:39 -0800 | [diff] [blame] | 119 | func compatTestFactory() android.Module { |
Inseob Kim | 16d3be3 | 2022-01-07 09:15:27 +0900 | [diff] [blame] | 120 | f := &compatTestModule{} |
Inseob Kim | 085f22f | 2023-11-09 11:13:01 +0900 | [diff] [blame] | 121 | f.AddProperties(&f.properties) |
Cole Faust | 09e326f | 2024-10-07 16:54:31 -0700 | [diff] [blame] | 122 | android.InitAndroidArchModule(f, android.DeviceSupported, android.MultilibCommon) |
Inseob Kim | 16d3be3 | 2022-01-07 09:15:27 +0900 | [diff] [blame] | 123 | android.AddLoadHook(f, func(ctx android.LoadHookContext) { |
| 124 | f.loadHook(ctx) |
| 125 | }) |
| 126 | return f |
| 127 | } |
| 128 | |
| 129 | type compatTestModule struct { |
Cole Faust | 8fe6568 | 2024-11-04 16:31:39 -0800 | [diff] [blame] | 130 | android.ModuleBase |
Inseob Kim | 085f22f | 2023-11-09 11:13:01 +0900 | [diff] [blame] | 131 | properties struct { |
| 132 | // Default modules for conf |
| 133 | Defaults []string |
| 134 | } |
Inseob Kim | 16d3be3 | 2022-01-07 09:15:27 +0900 | [diff] [blame] | 135 | |
| 136 | compatTestTimestamp android.ModuleOutPath |
| 137 | } |
| 138 | |
Inseob Kim | 16d3be3 | 2022-01-07 09:15:27 +0900 | [diff] [blame] | 139 | func (f *compatTestModule) createCompatTestModule(ctx android.LoadHookContext, ver string) { |
| 140 | srcs := []string{ |
| 141 | ":plat_sepolicy.cil", |
| 142 | ":system_ext_sepolicy.cil", |
| 143 | ":product_sepolicy.cil", |
| 144 | fmt.Sprintf(":plat_%s.cil", ver), |
| 145 | fmt.Sprintf(":%s.compat.cil", ver), |
| 146 | fmt.Sprintf(":system_ext_%s.cil", ver), |
| 147 | fmt.Sprintf(":system_ext_%s.compat.cil", ver), |
| 148 | fmt.Sprintf(":product_%s.cil", ver), |
| 149 | } |
| 150 | |
| 151 | if ver == ctx.DeviceConfig().BoardSepolicyVers() { |
| 152 | srcs = append(srcs, |
| 153 | ":plat_pub_versioned.cil", |
| 154 | ":vendor_sepolicy.cil", |
| 155 | ":odm_sepolicy.cil", |
| 156 | ) |
| 157 | } else { |
Inseob Kim | 10f0450 | 2024-07-18 10:26:43 +0900 | [diff] [blame] | 158 | srcs = append(srcs, fmt.Sprintf(":%s_plat_pub_versioned.cil", ver)) |
Inseob Kim | 16d3be3 | 2022-01-07 09:15:27 +0900 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | compatTestName := fmt.Sprintf("%s_compat_test", ver) |
| 162 | ctx.CreateModule(policyBinaryFactory, &nameProperties{ |
| 163 | Name: proptools.StringPtr(compatTestName), |
| 164 | }, &policyBinaryProperties{ |
| 165 | Srcs: srcs, |
| 166 | Ignore_neverallow: proptools.BoolPtr(true), |
| 167 | Installable: proptools.BoolPtr(false), |
| 168 | }) |
| 169 | } |
| 170 | |
| 171 | func (f *compatTestModule) loadHook(ctx android.LoadHookContext) { |
| 172 | for _, ver := range ctx.DeviceConfig().PlatformSepolicyCompatVersions() { |
Inseob Kim | 16d3be3 | 2022-01-07 09:15:27 +0900 | [diff] [blame] | 173 | f.createCompatTestModule(ctx, ver) |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | func (f *compatTestModule) DepsMutator(ctx android.BottomUpMutatorContext) { |
| 178 | for _, ver := range ctx.DeviceConfig().PlatformSepolicyCompatVersions() { |
| 179 | ctx.AddDependency(f, compatTestDepTag, fmt.Sprintf("%s_compat_test", ver)) |
| 180 | } |
| 181 | } |
| 182 | |
Inseob Kim | 16d3be3 | 2022-01-07 09:15:27 +0900 | [diff] [blame] | 183 | func (f *compatTestModule) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
Cole Faust | 8fe6568 | 2024-11-04 16:31:39 -0800 | [diff] [blame] | 184 | if ctx.ModuleName() != "sepolicy_compat_test" || ctx.ModuleDir() != "system/sepolicy/compat" { |
| 185 | // two compat test modules don't make sense. |
| 186 | ctx.ModuleErrorf("There can only be 1 se_compat_test module named sepolicy_compat_test in system/sepolicy/compat") |
| 187 | } |
Inseob Kim | 16d3be3 | 2022-01-07 09:15:27 +0900 | [diff] [blame] | 188 | var inputs android.Paths |
| 189 | ctx.VisitDirectDepsWithTag(compatTestDepTag, func(child android.Module) { |
mrziwang | cb3f550 | 2024-06-06 10:04:23 -0700 | [diff] [blame] | 190 | outputs := android.OutputFilesForModule(ctx, child, "") |
Inseob Kim | 16d3be3 | 2022-01-07 09:15:27 +0900 | [diff] [blame] | 191 | if len(outputs) != 1 { |
| 192 | panic(fmt.Errorf("Module %q should produce exactly one output, but did %q", ctx.OtherModuleName(child), outputs.Strings())) |
| 193 | } |
| 194 | |
| 195 | inputs = append(inputs, outputs[0]) |
| 196 | }) |
| 197 | |
| 198 | f.compatTestTimestamp = android.PathForModuleOut(ctx, "timestamp") |
| 199 | rule := android.NewRuleBuilder(pctx, ctx) |
| 200 | rule.Command().Text("touch").Output(f.compatTestTimestamp).Implicits(inputs) |
| 201 | rule.Build("compat", "compat test timestamp for: "+f.Name()) |
| 202 | } |
| 203 | |
| 204 | func (f *compatTestModule) AndroidMkEntries() []android.AndroidMkEntries { |
| 205 | return []android.AndroidMkEntries{android.AndroidMkEntries{ |
| 206 | Class: "FAKE", |
| 207 | // OutputFile is needed, even though BUILD_PHONY_PACKAGE doesn't use it. |
| 208 | // Without OutputFile this module won't be exported to Makefile. |
| 209 | OutputFile: android.OptionalPathForPath(f.compatTestTimestamp), |
| 210 | Include: "$(BUILD_PHONY_PACKAGE)", |
| 211 | ExtraEntries: []android.AndroidMkExtraEntriesFunc{ |
| 212 | func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) { |
| 213 | entries.SetString("LOCAL_ADDITIONAL_DEPENDENCIES", f.compatTestTimestamp.String()) |
| 214 | }, |
| 215 | }, |
| 216 | }} |
| 217 | } |