xref: /aosp_15_r20/system/sepolicy/build/soong/versioned_policy.go (revision e4a36f4174b17bbab9dc043f4a65dc8d87377290)
1*e4a36f41SAndroid Build Coastguard Worker// Copyright (C) 2021 The Android Open Source Project
2*e4a36f41SAndroid Build Coastguard Worker//
3*e4a36f41SAndroid Build Coastguard Worker// Licensed under the Apache License, Version 2.0 (the "License");
4*e4a36f41SAndroid Build Coastguard Worker// you may not use this file except in compliance with the License.
5*e4a36f41SAndroid Build Coastguard Worker// You may obtain a copy of the License at
6*e4a36f41SAndroid Build Coastguard Worker//
7*e4a36f41SAndroid Build Coastguard Worker//     http://www.apache.org/licenses/LICENSE-2.0
8*e4a36f41SAndroid Build Coastguard Worker//
9*e4a36f41SAndroid Build Coastguard Worker// Unless required by applicable law or agreed to in writing, software
10*e4a36f41SAndroid Build Coastguard Worker// distributed under the License is distributed on an "AS IS" BASIS,
11*e4a36f41SAndroid Build Coastguard Worker// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*e4a36f41SAndroid Build Coastguard Worker// See the License for the specific language governing permissions and
13*e4a36f41SAndroid Build Coastguard Worker// limitations under the License.
14*e4a36f41SAndroid Build Coastguard Worker
15*e4a36f41SAndroid Build Coastguard Workerpackage selinux
16*e4a36f41SAndroid Build Coastguard Worker
17*e4a36f41SAndroid Build Coastguard Workerimport (
18*e4a36f41SAndroid Build Coastguard Worker	"os"
19*e4a36f41SAndroid Build Coastguard Worker	"strconv"
20*e4a36f41SAndroid Build Coastguard Worker
21*e4a36f41SAndroid Build Coastguard Worker	"github.com/google/blueprint/proptools"
22*e4a36f41SAndroid Build Coastguard Worker
23*e4a36f41SAndroid Build Coastguard Worker	"android/soong/android"
24*e4a36f41SAndroid Build Coastguard Worker)
25*e4a36f41SAndroid Build Coastguard Worker
26*e4a36f41SAndroid Build Coastguard Workerfunc init() {
27*e4a36f41SAndroid Build Coastguard Worker	android.RegisterModuleType("se_versioned_policy", versionedPolicyFactory)
28*e4a36f41SAndroid Build Coastguard Worker}
29*e4a36f41SAndroid Build Coastguard Worker
30*e4a36f41SAndroid Build Coastguard Workertype versionedPolicyProperties struct {
31*e4a36f41SAndroid Build Coastguard Worker	// Base cil file for versioning.
32*e4a36f41SAndroid Build Coastguard Worker	Base *string `android:"path"`
33*e4a36f41SAndroid Build Coastguard Worker
34*e4a36f41SAndroid Build Coastguard Worker	// Output file name. Defaults to {name} if target_policy is set, {version}.cil if mapping is set
35*e4a36f41SAndroid Build Coastguard Worker	Stem *string
36*e4a36f41SAndroid Build Coastguard Worker
37*e4a36f41SAndroid Build Coastguard Worker	// Target sepolicy version. Can be a specific version number (e.g. "30.0" for R), "current"
38*e4a36f41SAndroid Build Coastguard Worker	// (PLATFORM_SEPOLICY_VERSION), or "vendor" (BOARD_SEPOLICY_VERS). Defaults to "current"
39*e4a36f41SAndroid Build Coastguard Worker	Version *string
40*e4a36f41SAndroid Build Coastguard Worker
41*e4a36f41SAndroid Build Coastguard Worker	// If true, generate mapping file from given base cil file. Cannot be set with target_policy.
42*e4a36f41SAndroid Build Coastguard Worker	Mapping *bool
43*e4a36f41SAndroid Build Coastguard Worker
44*e4a36f41SAndroid Build Coastguard Worker	// If given, version target policy file according to base policy. Cannot be set with mapping.
45*e4a36f41SAndroid Build Coastguard Worker	Target_policy *string `android:"path"`
46*e4a36f41SAndroid Build Coastguard Worker
47*e4a36f41SAndroid Build Coastguard Worker	// Cil files to be filtered out by the filter_out tool of "build_sepolicy".
48*e4a36f41SAndroid Build Coastguard Worker	Filter_out []string `android:"path"`
49*e4a36f41SAndroid Build Coastguard Worker
50*e4a36f41SAndroid Build Coastguard Worker	// Cil files to which this mapping file depends. If specified, secilc checks whether the output
51*e4a36f41SAndroid Build Coastguard Worker	// file can be merged with specified cil files or not.
52*e4a36f41SAndroid Build Coastguard Worker	Dependent_cils []string `android:"path"`
53*e4a36f41SAndroid Build Coastguard Worker
54*e4a36f41SAndroid Build Coastguard Worker	// Whether this module is directly installable to one of the partitions. Default is true
55*e4a36f41SAndroid Build Coastguard Worker	Installable *bool
56*e4a36f41SAndroid Build Coastguard Worker
57*e4a36f41SAndroid Build Coastguard Worker	// install to a subdirectory of the default install path for the module
58*e4a36f41SAndroid Build Coastguard Worker	Relative_install_path *string
59*e4a36f41SAndroid Build Coastguard Worker}
60*e4a36f41SAndroid Build Coastguard Worker
61*e4a36f41SAndroid Build Coastguard Workertype versionedPolicy struct {
62*e4a36f41SAndroid Build Coastguard Worker	android.ModuleBase
63*e4a36f41SAndroid Build Coastguard Worker
64*e4a36f41SAndroid Build Coastguard Worker	properties versionedPolicyProperties
65*e4a36f41SAndroid Build Coastguard Worker
66*e4a36f41SAndroid Build Coastguard Worker	installSource android.Path
67*e4a36f41SAndroid Build Coastguard Worker	installPath   android.InstallPath
68*e4a36f41SAndroid Build Coastguard Worker}
69*e4a36f41SAndroid Build Coastguard Worker
70*e4a36f41SAndroid Build Coastguard Worker// se_versioned_policy generates versioned cil file with "version_policy". This can generate either
71*e4a36f41SAndroid Build Coastguard Worker// mapping file for public plat policies, or associate a target policy file with the version that
72*e4a36f41SAndroid Build Coastguard Worker// non-platform policy targets.
73*e4a36f41SAndroid Build Coastguard Workerfunc versionedPolicyFactory() android.Module {
74*e4a36f41SAndroid Build Coastguard Worker	m := &versionedPolicy{}
75*e4a36f41SAndroid Build Coastguard Worker	m.AddProperties(&m.properties)
76*e4a36f41SAndroid Build Coastguard Worker	android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon)
77*e4a36f41SAndroid Build Coastguard Worker	return m
78*e4a36f41SAndroid Build Coastguard Worker}
79*e4a36f41SAndroid Build Coastguard Worker
80*e4a36f41SAndroid Build Coastguard Workerfunc (m *versionedPolicy) installable() bool {
81*e4a36f41SAndroid Build Coastguard Worker	return proptools.BoolDefault(m.properties.Installable, true)
82*e4a36f41SAndroid Build Coastguard Worker}
83*e4a36f41SAndroid Build Coastguard Worker
84*e4a36f41SAndroid Build Coastguard Workerfunc (m *versionedPolicy) DepsMutator(ctx android.BottomUpMutatorContext) {
85*e4a36f41SAndroid Build Coastguard Worker	// do nothing
86*e4a36f41SAndroid Build Coastguard Worker}
87*e4a36f41SAndroid Build Coastguard Worker
88*e4a36f41SAndroid Build Coastguard Workerfunc (m *versionedPolicy) GenerateAndroidBuildActions(ctx android.ModuleContext) {
89*e4a36f41SAndroid Build Coastguard Worker	version := proptools.StringDefault(m.properties.Version, "current")
90*e4a36f41SAndroid Build Coastguard Worker	if version == "current" {
91*e4a36f41SAndroid Build Coastguard Worker		version = ctx.DeviceConfig().PlatformSepolicyVersion()
92*e4a36f41SAndroid Build Coastguard Worker	} else if version == "vendor" {
93*e4a36f41SAndroid Build Coastguard Worker		version = ctx.DeviceConfig().BoardSepolicyVers()
94*e4a36f41SAndroid Build Coastguard Worker	}
95*e4a36f41SAndroid Build Coastguard Worker
96*e4a36f41SAndroid Build Coastguard Worker	var stem string
97*e4a36f41SAndroid Build Coastguard Worker	if s := proptools.String(m.properties.Stem); s != "" {
98*e4a36f41SAndroid Build Coastguard Worker		stem = s
99*e4a36f41SAndroid Build Coastguard Worker	} else if proptools.Bool(m.properties.Mapping) {
100*e4a36f41SAndroid Build Coastguard Worker		stem = version + ".cil"
101*e4a36f41SAndroid Build Coastguard Worker	} else {
102*e4a36f41SAndroid Build Coastguard Worker		stem = ctx.ModuleName()
103*e4a36f41SAndroid Build Coastguard Worker	}
104*e4a36f41SAndroid Build Coastguard Worker
105*e4a36f41SAndroid Build Coastguard Worker	out := pathForModuleOut(ctx, stem)
106*e4a36f41SAndroid Build Coastguard Worker	rule := android.NewRuleBuilder(pctx, ctx)
107*e4a36f41SAndroid Build Coastguard Worker
108*e4a36f41SAndroid Build Coastguard Worker	if proptools.String(m.properties.Base) == "" {
109*e4a36f41SAndroid Build Coastguard Worker		ctx.PropertyErrorf("base", "must be specified")
110*e4a36f41SAndroid Build Coastguard Worker		return
111*e4a36f41SAndroid Build Coastguard Worker	}
112*e4a36f41SAndroid Build Coastguard Worker
113*e4a36f41SAndroid Build Coastguard Worker	versionCmd := rule.Command().BuiltTool("version_policy").
114*e4a36f41SAndroid Build Coastguard Worker		FlagWithInput("-b ", android.PathForModuleSrc(ctx, *m.properties.Base)).
115*e4a36f41SAndroid Build Coastguard Worker		FlagWithArg("-n ", version).
116*e4a36f41SAndroid Build Coastguard Worker		FlagWithOutput("-o ", out)
117*e4a36f41SAndroid Build Coastguard Worker
118*e4a36f41SAndroid Build Coastguard Worker	if proptools.Bool(m.properties.Mapping) && proptools.String(m.properties.Target_policy) != "" {
119*e4a36f41SAndroid Build Coastguard Worker		ctx.ModuleErrorf("Can't set both mapping and target_policy")
120*e4a36f41SAndroid Build Coastguard Worker		return
121*e4a36f41SAndroid Build Coastguard Worker	}
122*e4a36f41SAndroid Build Coastguard Worker
123*e4a36f41SAndroid Build Coastguard Worker	if proptools.Bool(m.properties.Mapping) {
124*e4a36f41SAndroid Build Coastguard Worker		versionCmd.Flag("-m")
125*e4a36f41SAndroid Build Coastguard Worker	} else if target := proptools.String(m.properties.Target_policy); target != "" {
126*e4a36f41SAndroid Build Coastguard Worker		versionCmd.FlagWithInput("-t ", android.PathForModuleSrc(ctx, target))
127*e4a36f41SAndroid Build Coastguard Worker	} else {
128*e4a36f41SAndroid Build Coastguard Worker		ctx.ModuleErrorf("Either mapping or target_policy must be set")
129*e4a36f41SAndroid Build Coastguard Worker		return
130*e4a36f41SAndroid Build Coastguard Worker	}
131*e4a36f41SAndroid Build Coastguard Worker
132*e4a36f41SAndroid Build Coastguard Worker	if len(m.properties.Filter_out) > 0 {
133*e4a36f41SAndroid Build Coastguard Worker		rule.Command().BuiltTool("build_sepolicy").
134*e4a36f41SAndroid Build Coastguard Worker			Text("filter_out").
135*e4a36f41SAndroid Build Coastguard Worker			Flag("-f").
136*e4a36f41SAndroid Build Coastguard Worker			Inputs(android.PathsForModuleSrc(ctx, m.properties.Filter_out)).
137*e4a36f41SAndroid Build Coastguard Worker			FlagWithOutput("-t ", out)
138*e4a36f41SAndroid Build Coastguard Worker	}
139*e4a36f41SAndroid Build Coastguard Worker
140*e4a36f41SAndroid Build Coastguard Worker	if len(m.properties.Dependent_cils) > 0 {
141*e4a36f41SAndroid Build Coastguard Worker		rule.Command().BuiltTool("secilc").
142*e4a36f41SAndroid Build Coastguard Worker			Flag("-m").
143*e4a36f41SAndroid Build Coastguard Worker			FlagWithArg("-M ", "true").
144*e4a36f41SAndroid Build Coastguard Worker			Flag("-G").
145*e4a36f41SAndroid Build Coastguard Worker			Flag("-N").
146*e4a36f41SAndroid Build Coastguard Worker			FlagWithArg("-c ", strconv.Itoa(PolicyVers)).
147*e4a36f41SAndroid Build Coastguard Worker			Inputs(android.PathsForModuleSrc(ctx, m.properties.Dependent_cils)).
148*e4a36f41SAndroid Build Coastguard Worker			Text(out.String()).
149*e4a36f41SAndroid Build Coastguard Worker			FlagWithArg("-o ", os.DevNull).
150*e4a36f41SAndroid Build Coastguard Worker			FlagWithArg("-f ", os.DevNull)
151*e4a36f41SAndroid Build Coastguard Worker	}
152*e4a36f41SAndroid Build Coastguard Worker
153*e4a36f41SAndroid Build Coastguard Worker	rule.Build("mapping", "Versioning mapping file "+ctx.ModuleName())
154*e4a36f41SAndroid Build Coastguard Worker
155*e4a36f41SAndroid Build Coastguard Worker	if !m.installable() {
156*e4a36f41SAndroid Build Coastguard Worker		m.SkipInstall()
157*e4a36f41SAndroid Build Coastguard Worker	}
158*e4a36f41SAndroid Build Coastguard Worker
159*e4a36f41SAndroid Build Coastguard Worker	m.installSource = out
160*e4a36f41SAndroid Build Coastguard Worker	m.installPath = android.PathForModuleInstall(ctx, "etc", "selinux")
161*e4a36f41SAndroid Build Coastguard Worker	if subdir := proptools.String(m.properties.Relative_install_path); subdir != "" {
162*e4a36f41SAndroid Build Coastguard Worker		m.installPath = m.installPath.Join(ctx, subdir)
163*e4a36f41SAndroid Build Coastguard Worker	}
164*e4a36f41SAndroid Build Coastguard Worker	ctx.InstallFile(m.installPath, m.installSource.Base(), m.installSource)
165*e4a36f41SAndroid Build Coastguard Worker
166*e4a36f41SAndroid Build Coastguard Worker	ctx.SetOutputFiles(android.Paths{m.installSource}, "")
167*e4a36f41SAndroid Build Coastguard Worker}
168*e4a36f41SAndroid Build Coastguard Worker
169*e4a36f41SAndroid Build Coastguard Workerfunc (m *versionedPolicy) AndroidMkEntries() []android.AndroidMkEntries {
170*e4a36f41SAndroid Build Coastguard Worker	return []android.AndroidMkEntries{android.AndroidMkEntries{
171*e4a36f41SAndroid Build Coastguard Worker		OutputFile: android.OptionalPathForPath(m.installSource),
172*e4a36f41SAndroid Build Coastguard Worker		Class:      "ETC",
173*e4a36f41SAndroid Build Coastguard Worker		ExtraEntries: []android.AndroidMkExtraEntriesFunc{
174*e4a36f41SAndroid Build Coastguard Worker			func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
175*e4a36f41SAndroid Build Coastguard Worker				entries.SetBool("LOCAL_UNINSTALLABLE_MODULE", !m.installable())
176*e4a36f41SAndroid Build Coastguard Worker				entries.SetPath("LOCAL_MODULE_PATH", m.installPath)
177*e4a36f41SAndroid Build Coastguard Worker				entries.SetString("LOCAL_INSTALLED_MODULE_STEM", m.installSource.Base())
178*e4a36f41SAndroid Build Coastguard Worker			},
179*e4a36f41SAndroid Build Coastguard Worker		},
180*e4a36f41SAndroid Build Coastguard Worker	}}
181*e4a36f41SAndroid Build Coastguard Worker}
182