blob: 70f94e0a9562c33e57437f4a5bb3bd056298d08e [file] [log] [blame]
Inseob Kim53391842024-03-29 17:44:07 +09001// Copyright (C) 2024 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
15package filesystem
16
17import (
18 "path/filepath"
19 "strings"
20
21 "android/soong/android"
22)
23
24type fsverityProperties struct {
25 // Patterns of files for fsverity metadata generation. For each matched file, a .fsv_meta file
26 // will be generated and included to the filesystem image.
27 // etc/security/fsverity/BuildManifest.apk will also be generated which contains information
28 // about generated .fsv_meta files.
29 Inputs []string
30}
31
32func (f *filesystem) writeManifestGeneratorListFile(ctx android.ModuleContext, outputPath android.OutputPath, matchedSpecs []android.PackagingSpec, rebasedDir android.OutputPath) {
33 var buf strings.Builder
34 for _, spec := range matchedSpecs {
35 buf.WriteString(rebasedDir.Join(ctx, spec.RelPathInPackage()).String())
36 buf.WriteRune('\n')
37 }
38 android.WriteFileRuleVerbatim(ctx, outputPath, buf.String())
39}
40
41func (f *filesystem) buildFsverityMetadataFiles(ctx android.ModuleContext, builder *android.RuleBuilder, specs map[string]android.PackagingSpec, rootDir android.OutputPath, rebasedDir android.OutputPath) {
42 match := func(path string) bool {
43 for _, pattern := range f.properties.Fsverity.Inputs {
44 if matched, err := filepath.Match(pattern, path); matched {
45 return true
46 } else if err != nil {
47 ctx.PropertyErrorf("fsverity.inputs", "bad pattern %q", pattern)
48 return false
49 }
50 }
51 return false
52 }
53
54 var matchedSpecs []android.PackagingSpec
55 for _, relPath := range android.SortedKeys(specs) {
56 if match(relPath) {
57 matchedSpecs = append(matchedSpecs, specs[relPath])
58 }
59 }
60
61 if len(matchedSpecs) == 0 {
62 return
63 }
64
65 fsverityBuilderPath := android.PathForModuleOut(ctx, "fsverity_builder.sh")
66 metadataGeneratorPath := ctx.Config().HostToolPath(ctx, "fsverity_metadata_generator")
67 fsverityPath := ctx.Config().HostToolPath(ctx, "fsverity")
68
69 cmd := builder.Command().Tool(fsverityBuilderPath)
70
71 // STEP 1: generate .fsv_meta
72 var sb strings.Builder
73 sb.WriteString("set -e\n")
74 cmd.Implicit(metadataGeneratorPath).Implicit(fsverityPath)
75 for _, spec := range matchedSpecs {
76 // srcPath is copied by CopySpecsToDir()
77 srcPath := rebasedDir.Join(ctx, spec.RelPathInPackage())
78 destPath := rebasedDir.Join(ctx, spec.RelPathInPackage()+".fsv_meta")
79 sb.WriteString(metadataGeneratorPath.String())
80 sb.WriteString(" --fsverity-path ")
81 sb.WriteString(fsverityPath.String())
82 sb.WriteString(" --signature none --hash-alg sha256 --output ")
83 sb.WriteString(destPath.String())
84 sb.WriteRune(' ')
85 sb.WriteString(srcPath.String())
86 sb.WriteRune('\n')
87 }
88
89 // STEP 2: generate signed BuildManifest.apk
90 // STEP 2-1: generate build_manifest.pb
91 assetsPath := android.PathForModuleOut(ctx, "fsverity_manifest/assets")
92 manifestPbPath := assetsPath.Join(ctx, "build_manifest.pb")
93 manifestGeneratorPath := ctx.Config().HostToolPath(ctx, "fsverity_manifest_generator")
94 cmd.Implicit(manifestGeneratorPath)
95 sb.WriteString("rm -rf ")
96 sb.WriteString(assetsPath.String())
97 sb.WriteString(" && mkdir -p ")
98 sb.WriteString(assetsPath.String())
99 sb.WriteRune('\n')
100 sb.WriteString(manifestGeneratorPath.String())
101 sb.WriteString(" --fsverity-path ")
102 sb.WriteString(fsverityPath.String())
103 sb.WriteString(" --base-dir ")
104 sb.WriteString(rootDir.String())
105 sb.WriteString(" --output ")
106 sb.WriteString(manifestPbPath.String())
107 sb.WriteRune(' ')
108
109 manifestGeneratorListPath := android.PathForModuleOut(ctx, "fsverity_manifest.list")
110 f.writeManifestGeneratorListFile(ctx, manifestGeneratorListPath.OutputPath, matchedSpecs, rebasedDir)
111 sb.WriteRune('@')
112 sb.WriteString(manifestGeneratorListPath.String())
113 sb.WriteRune('\n')
114 cmd.Implicit(manifestGeneratorListPath)
115
116 // STEP 2-2: generate BuildManifest.apk (unsigned)
117 aapt2Path := ctx.Config().HostToolPath(ctx, "aapt2")
118 apkPath := rebasedDir.Join(ctx, "etc", "security", "fsverity", "BuildManifest.apk")
119 manifestTemplatePath := android.PathForSource(ctx, "system/security/fsverity/AndroidManifest.xml")
120 // package-export is currently generated by Makefile.
121 // TODO(b/330282551): fully migrate into Soong
122 frameworkResPath := android.PathForArbitraryOutput(ctx, "target/common/obj/APPS/framework-res_intermediates/package-export.apk")
123 cmd.Implicit(aapt2Path)
124 cmd.Implicit(manifestTemplatePath)
125 cmd.Implicit(frameworkResPath)
126
127 sb.WriteString(aapt2Path.String())
128 sb.WriteString(" link -o ")
129 sb.WriteString(apkPath.String())
130 sb.WriteString(" -A ")
131 sb.WriteString(assetsPath.String())
132 sb.WriteString(" -I ")
133 sb.WriteString(frameworkResPath.String())
134 minSdkVersion := ctx.Config().PlatformSdkCodename()
135 if minSdkVersion == "REL" {
136 minSdkVersion = ctx.Config().PlatformSdkVersion().String()
137 }
138 sb.WriteString(" --min-sdk-version ")
139 sb.WriteString(minSdkVersion)
140 sb.WriteString(" --version-code ")
141 sb.WriteString(ctx.Config().PlatformSdkVersion().String())
142 sb.WriteString(" --version-name ")
143 sb.WriteString(ctx.Config().AppsDefaultVersionName())
144 sb.WriteString(" --manifest ")
145 sb.WriteString(manifestTemplatePath.String())
146 sb.WriteString(" --rename-manifest-package com.android.security.fsverity_metadata.")
147 sb.WriteString(f.partitionName())
148 sb.WriteRune('\n')
149
150 // STEP 2-3: sign BuildManifest.apk
151 apksignerPath := ctx.Config().HostToolPath(ctx, "apksigner")
152 pemPath, keyPath := ctx.Config().DefaultAppCertificate(ctx)
153 cmd.Implicit(apksignerPath)
154 cmd.Implicit(pemPath)
155 cmd.Implicit(keyPath)
156 sb.WriteString(apksignerPath.String())
157 sb.WriteString(" sign --in ")
158 sb.WriteString(apkPath.String())
159 sb.WriteString(" --cert ")
160 sb.WriteString(pemPath.String())
161 sb.WriteString(" --key ")
162 sb.WriteString(keyPath.String())
163 sb.WriteRune('\n')
164
165 android.WriteExecutableFileRuleVerbatim(ctx, fsverityBuilderPath, sb.String())
166}