blob: ae4a29d268918b8e19fdb0567d3b219db1a711c6 [file] [log] [blame]
Victor Hsieh51789de2021-08-06 16:50:49 -07001/*
2 * Copyright (C) 2021 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Alan Stokesfadcef22022-01-24 17:00:59 +000017use anyhow::{anyhow, bail, Context, Result};
Victor Hsieh616f8222022-01-14 13:06:32 -080018use log::{debug, info, warn};
Victor Hsieh51789de2021-08-06 16:50:49 -070019use minijail::{self, Minijail};
Alan Stokes92472512022-01-04 11:48:38 +000020use regex::Regex;
Alan Stokes2d2e4db2022-01-28 16:41:52 +000021use rustutils::system_properties;
Alan Stokesfadcef22022-01-24 17:00:59 +000022use std::collections::HashMap;
Victor Hsiehf9968692021-11-18 11:34:39 -080023use std::env;
Alan Stokes92472512022-01-04 11:48:38 +000024use std::ffi::OsString;
Victor Hsieh616f8222022-01-14 13:06:32 -080025use std::fs::read_dir;
Alan Stokes35bac3c2021-12-16 14:37:24 +000026use std::path::{self, Path, PathBuf};
Alan Stokes92472512022-01-04 11:48:38 +000027use std::process::Command;
Victor Hsieh51789de2021-08-06 16:50:49 -070028
Alan Stokes46a1dff2021-12-14 10:56:05 +000029use crate::artifact_signer::ArtifactSigner;
Alan Stokes611cc942022-02-01 10:16:21 +000030use crate::signing_key::DiceSigner;
Victor Hsieh51789de2021-08-06 16:50:49 -070031use authfs_aidl_interface::aidl::com::android::virt::fs::{
Victor Hsieh015bcb52021-11-17 17:28:01 -080032 AuthFsConfig::{
Victor Hsiehf9968692021-11-18 11:34:39 -080033 AuthFsConfig, InputDirFdAnnotation::InputDirFdAnnotation,
Victor Hsieh616f8222022-01-14 13:06:32 -080034 OutputDirFdAnnotation::OutputDirFdAnnotation,
Victor Hsieh015bcb52021-11-17 17:28:01 -080035 },
Victor Hsieh015bcb52021-11-17 17:28:01 -080036 IAuthFsService::IAuthFsService,
Victor Hsieh51789de2021-08-06 16:50:49 -070037};
Alan Stokesfadcef22022-01-24 17:00:59 +000038use authfs_aidl_interface::binder::Strong;
Alan Stokes2d2e4db2022-01-28 16:41:52 +000039use compos_aidl_interface::aidl::com::android::compos::ICompOsService::CompilationMode::CompilationMode;
Alan Stokes46a1dff2021-12-14 10:56:05 +000040use compos_common::odrefresh::ExitCode;
Victor Hsieh51789de2021-08-06 16:50:49 -070041
Victor Hsiehf9968692021-11-18 11:34:39 -080042const FD_SERVER_PORT: i32 = 3264; // TODO: support dynamic port
43
Alan Stokes46a1dff2021-12-14 10:56:05 +000044pub struct OdrefreshContext<'a> {
Alan Stokes2d2e4db2022-01-28 16:41:52 +000045 compilation_mode: CompilationMode,
Victor Hsiehf9968692021-11-18 11:34:39 -080046 system_dir_fd: i32,
47 output_dir_fd: i32,
Alan Stokes9646db92021-12-14 13:22:33 +000048 staging_dir_fd: i32,
Alan Stokes46a1dff2021-12-14 10:56:05 +000049 target_dir_name: &'a str,
50 zygote_arch: &'a str,
Victor Hsieh9bfbc5f2021-12-16 11:45:10 -080051 system_server_compiler_filter: &'a str,
Alan Stokes46a1dff2021-12-14 10:56:05 +000052}
53
54impl<'a> OdrefreshContext<'a> {
55 pub fn new(
Alan Stokes2d2e4db2022-01-28 16:41:52 +000056 compilation_mode: CompilationMode,
Alan Stokes46a1dff2021-12-14 10:56:05 +000057 system_dir_fd: i32,
58 output_dir_fd: i32,
59 staging_dir_fd: i32,
60 target_dir_name: &'a str,
61 zygote_arch: &'a str,
Victor Hsieh9bfbc5f2021-12-16 11:45:10 -080062 system_server_compiler_filter: &'a str,
Alan Stokes46a1dff2021-12-14 10:56:05 +000063 ) -> Result<Self> {
Alan Stokes2d2e4db2022-01-28 16:41:52 +000064 if compilation_mode != CompilationMode::NORMAL_COMPILE {
Alan Stokeseba7e652022-02-03 14:48:56 +000065 let debuggable = is_property_set("ro.boot.microdroid.debuggable")
66 || is_property_set("ro.boot.logd.enabled")
67 || is_property_set("ro.boot.adb.enabled");
Alan Stokes2d2e4db2022-01-28 16:41:52 +000068 if !debuggable {
69 bail!("Requested compilation mode only available in debuggable VMs");
70 }
71 }
72
Alan Stokes46a1dff2021-12-14 10:56:05 +000073 if system_dir_fd < 0 || output_dir_fd < 0 || staging_dir_fd < 0 {
74 bail!("The remote FDs are expected to be non-negative");
75 }
Victor Hsieh9bfbc5f2021-12-16 11:45:10 -080076 if !matches!(zygote_arch, "zygote64" | "zygote64_32") {
Alan Stokes46a1dff2021-12-14 10:56:05 +000077 bail!("Invalid zygote arch");
78 }
Alan Stokes35bac3c2021-12-16 14:37:24 +000079 // Disallow any sort of path traversal
80 if target_dir_name.contains(path::MAIN_SEPARATOR) {
81 bail!("Invalid target directory {}", target_dir_name);
82 }
83
Victor Hsieh9bfbc5f2021-12-16 11:45:10 -080084 // We're not validating/allowlisting the compiler filter, and just assume the compiler will
85 // reject an invalid string. We need to accept "verify" filter anyway, and potential
86 // performance degration by the attacker is not currently in scope. This also allows ART to
87 // specify new compiler filter and configure through system property without change to
88 // CompOS.
89
90 Ok(Self {
Alan Stokes2d2e4db2022-01-28 16:41:52 +000091 compilation_mode,
Victor Hsieh9bfbc5f2021-12-16 11:45:10 -080092 system_dir_fd,
93 output_dir_fd,
94 staging_dir_fd,
95 target_dir_name,
96 zygote_arch,
97 system_server_compiler_filter,
98 })
Alan Stokes46a1dff2021-12-14 10:56:05 +000099 }
100}
101
Alan Stokeseba7e652022-02-03 14:48:56 +0000102// Return whether the named property is definitely enabled. Deliberately conservative; returns
103// false if the property does not exist or cannot be read or is malformed.
104fn is_property_set(name: &str) -> bool {
105 system_properties::read_bool(name, false).unwrap_or(false)
106}
107
Alan Stokes46a1dff2021-12-14 10:56:05 +0000108pub fn odrefresh(
109 odrefresh_path: &Path,
110 context: OdrefreshContext,
Victor Hsiehf9968692021-11-18 11:34:39 -0800111 authfs_service: Strong<dyn IAuthFsService>,
Alan Stokes611cc942022-02-01 10:16:21 +0000112 signer: DiceSigner,
Alan Stokes46a1dff2021-12-14 10:56:05 +0000113) -> Result<ExitCode> {
Victor Hsiehf9968692021-11-18 11:34:39 -0800114 // Mount authfs (via authfs_service). The authfs instance unmounts once the `authfs` variable
115 // is out of scope.
116 let authfs_config = AuthFsConfig {
117 port: FD_SERVER_PORT,
118 inputDirFdAnnotations: vec![InputDirFdAnnotation {
Alan Stokes46a1dff2021-12-14 10:56:05 +0000119 fd: context.system_dir_fd,
Victor Hsieh99782572022-01-05 15:38:33 -0800120 // 0 is the index of extra_apks in vm_config_extra_apk.json
121 manifestPath: "/mnt/extra-apk/0/assets/build_manifest.pb".to_string(),
122 prefix: "system/".to_string(),
Victor Hsiehf9968692021-11-18 11:34:39 -0800123 }],
Alan Stokes9646db92021-12-14 13:22:33 +0000124 outputDirFdAnnotations: vec![
Alan Stokes46a1dff2021-12-14 10:56:05 +0000125 OutputDirFdAnnotation { fd: context.output_dir_fd },
126 OutputDirFdAnnotation { fd: context.staging_dir_fd },
Alan Stokes9646db92021-12-14 13:22:33 +0000127 ],
Victor Hsiehf9968692021-11-18 11:34:39 -0800128 ..Default::default()
129 };
130 let authfs = authfs_service.mount(&authfs_config)?;
131 let mountpoint = PathBuf::from(authfs.getMountPoint()?);
132
Alan Stokesfadcef22022-01-24 17:00:59 +0000133 // Make a copy of our environment as the basis of the one we will give odrefresh
134 let mut odrefresh_vars = EnvMap::from_current_env();
135
Victor Hsiehf9968692021-11-18 11:34:39 -0800136 let mut android_root = mountpoint.clone();
Alan Stokes46a1dff2021-12-14 10:56:05 +0000137 android_root.push(context.system_dir_fd.to_string());
Victor Hsiehf9968692021-11-18 11:34:39 -0800138 android_root.push("system");
Alan Stokesfadcef22022-01-24 17:00:59 +0000139 odrefresh_vars.set("ANDROID_ROOT", path_to_str(&android_root)?);
Alan Stokes46a1dff2021-12-14 10:56:05 +0000140 debug!("ANDROID_ROOT={:?}", &android_root);
Victor Hsiehf9968692021-11-18 11:34:39 -0800141
Alan Stokes46a1dff2021-12-14 10:56:05 +0000142 let art_apex_data = mountpoint.join(context.output_dir_fd.to_string());
Alan Stokesfadcef22022-01-24 17:00:59 +0000143 odrefresh_vars.set("ART_APEX_DATA", path_to_str(&art_apex_data)?);
Alan Stokes46a1dff2021-12-14 10:56:05 +0000144 debug!("ART_APEX_DATA={:?}", &art_apex_data);
Victor Hsieh64df53d2021-11-30 17:09:51 -0800145
Alan Stokes46a1dff2021-12-14 10:56:05 +0000146 let staging_dir = mountpoint.join(context.staging_dir_fd.to_string());
Victor Hsiehf9968692021-11-18 11:34:39 -0800147
Alan Stokesfadcef22022-01-24 17:00:59 +0000148 set_classpaths(&mut odrefresh_vars, &android_root)?;
Alan Stokes92472512022-01-04 11:48:38 +0000149
Victor Hsieh9bfbc5f2021-12-16 11:45:10 -0800150 let mut args = vec![
Victor Hsiehf9968692021-11-18 11:34:39 -0800151 "odrefresh".to_string(),
Alan Stokes48c1d2b2022-01-10 15:54:04 +0000152 "--compilation-os-mode".to_string(),
Alan Stokes46a1dff2021-12-14 10:56:05 +0000153 format!("--zygote-arch={}", context.zygote_arch),
154 format!("--dalvik-cache={}", context.target_dir_name),
Victor Hsiehf9968692021-11-18 11:34:39 -0800155 format!("--staging-dir={}", staging_dir.display()),
Victor Hsieh9bfbc5f2021-12-16 11:45:10 -0800156 "--no-refresh".to_string(),
Victor Hsiehf9968692021-11-18 11:34:39 -0800157 ];
Victor Hsieh9bfbc5f2021-12-16 11:45:10 -0800158
159 if !context.system_server_compiler_filter.is_empty() {
160 args.push(format!(
161 "--system-server-compiler-filter={}",
162 context.system_server_compiler_filter
163 ));
164 }
Alan Stokes48c1d2b2022-01-10 15:54:04 +0000165
Alan Stokes2d2e4db2022-01-28 16:41:52 +0000166 let compile_flag = match context.compilation_mode {
167 CompilationMode::NORMAL_COMPILE => "--compile",
168 CompilationMode::TEST_COMPILE => "--force-compile",
169 other => bail!("Unknown compilation mode {:?}", other),
170 };
171 args.push(compile_flag.to_string());
Victor Hsieh9bfbc5f2021-12-16 11:45:10 -0800172
Alan Stokes9646db92021-12-14 13:22:33 +0000173 debug!("Running odrefresh with args: {:?}", &args);
Alan Stokesfadcef22022-01-24 17:00:59 +0000174 let jail = spawn_jailed_task(odrefresh_path, &args, &odrefresh_vars.into_env())
Victor Hsiehf9968692021-11-18 11:34:39 -0800175 .context("Spawn odrefresh")?;
Alan Stokes46a1dff2021-12-14 10:56:05 +0000176 let exit_code = match jail.wait() {
Alan Stokes2d2e4db2022-01-28 16:41:52 +0000177 Ok(_) => 0,
178 Err(minijail::Error::ReturnCode(exit_code)) => exit_code,
179 Err(e) => bail!("Unexpected minijail error: {}", e),
180 };
Alan Stokes46a1dff2021-12-14 10:56:05 +0000181
Alan Stokes126fd512021-12-16 15:00:01 +0000182 let exit_code = ExitCode::from_i32(exit_code.into())?;
Alan Stokes46a1dff2021-12-14 10:56:05 +0000183 info!("odrefresh exited with {:?}", exit_code);
184
185 if exit_code == ExitCode::CompilationSuccess {
186 // authfs only shows us the files we created, so it's ok to just sign everything under
187 // the target directory.
188 let target_dir = art_apex_data.join(context.target_dir_name);
189 let mut artifact_signer = ArtifactSigner::new(&target_dir);
190 add_artifacts(&target_dir, &mut artifact_signer)?;
191
192 artifact_signer.write_info_and_signature(signer, &target_dir.join("compos.info"))?;
Victor Hsiehf9968692021-11-18 11:34:39 -0800193 }
Alan Stokes46a1dff2021-12-14 10:56:05 +0000194
195 Ok(exit_code)
196}
197
Alan Stokesfadcef22022-01-24 17:00:59 +0000198fn path_to_str(path: &Path) -> Result<&str> {
199 path.to_str().ok_or_else(|| anyhow!("Bad path {:?}", path))
200}
201
202fn set_classpaths(odrefresh_vars: &mut EnvMap, android_root: &Path) -> Result<()> {
Alan Stokes92472512022-01-04 11:48:38 +0000203 let export_lines = run_derive_classpath(android_root)?;
Alan Stokesfadcef22022-01-24 17:00:59 +0000204 load_classpath_vars(odrefresh_vars, &export_lines)
Alan Stokes92472512022-01-04 11:48:38 +0000205}
206
207fn run_derive_classpath(android_root: &Path) -> Result<String> {
208 let classpaths_root = android_root.join("etc/classpaths");
209
210 let mut bootclasspath_arg = OsString::new();
211 bootclasspath_arg.push("--bootclasspath-fragment=");
212 bootclasspath_arg.push(classpaths_root.join("bootclasspath.pb"));
213
214 let mut systemserverclasspath_arg = OsString::new();
215 systemserverclasspath_arg.push("--systemserverclasspath-fragment=");
216 systemserverclasspath_arg.push(classpaths_root.join("systemserverclasspath.pb"));
217
218 let result = Command::new("/apex/com.android.sdkext/bin/derive_classpath")
219 .arg(bootclasspath_arg)
220 .arg(systemserverclasspath_arg)
221 .arg("/proc/self/fd/1")
222 .output()
223 .context("Failed to run derive_classpath")?;
224
225 if !result.status.success() {
226 bail!("derive_classpath returned {}", result.status);
227 }
228
229 String::from_utf8(result.stdout).context("Converting derive_classpath output")
230}
231
Alan Stokesfadcef22022-01-24 17:00:59 +0000232fn load_classpath_vars(odrefresh_vars: &mut EnvMap, export_lines: &str) -> Result<()> {
Alan Stokes92472512022-01-04 11:48:38 +0000233 // Each line should be in the format "export <var name> <value>"
234 let pattern = Regex::new(r"^export ([^ ]+) ([^ ]+)$").context("Failed to construct Regex")?;
235 for line in export_lines.lines() {
236 if let Some(captures) = pattern.captures(line) {
237 let name = &captures[1];
238 let value = &captures[2];
Alan Stokesfadcef22022-01-24 17:00:59 +0000239 odrefresh_vars.set(name, value);
Alan Stokes92472512022-01-04 11:48:38 +0000240 } else {
241 warn!("Malformed line from derive_classpath: {}", line);
242 }
243 }
244
245 Ok(())
246}
247
Alan Stokes46a1dff2021-12-14 10:56:05 +0000248fn add_artifacts(target_dir: &Path, artifact_signer: &mut ArtifactSigner) -> Result<()> {
249 for entry in
250 read_dir(&target_dir).with_context(|| format!("Traversing {}", target_dir.display()))?
251 {
252 let entry = entry?;
253 let file_type = entry.file_type()?;
254 if file_type.is_dir() {
255 add_artifacts(&entry.path(), artifact_signer)?;
256 } else if file_type.is_file() {
257 artifact_signer.add_artifact(&entry.path())?;
258 } else {
259 // authfs shouldn't create anything else, but just in case
260 bail!("Unexpected file type in artifacts: {:?}", entry);
261 }
262 }
263 Ok(())
Victor Hsiehf9968692021-11-18 11:34:39 -0800264}
265
Alan Stokesfadcef22022-01-24 17:00:59 +0000266fn spawn_jailed_task(executable: &Path, args: &[String], env_vars: &[String]) -> Result<Minijail> {
Victor Hsieh51789de2021-08-06 16:50:49 -0700267 // TODO(b/185175567): Run in a more restricted sandbox.
268 let jail = Minijail::new()?;
Alan Stokesfadcef22022-01-24 17:00:59 +0000269 let keep_fds = [];
270 let command = minijail::Command::new_for_path(executable, &keep_fds, args, Some(env_vars))?;
271 let _pid = jail.run_command(command)?;
Victor Hsieh51789de2021-08-06 16:50:49 -0700272 Ok(jail)
273}
Alan Stokesfadcef22022-01-24 17:00:59 +0000274
275struct EnvMap(HashMap<String, String>);
276
277impl EnvMap {
278 fn from_current_env() -> Self {
279 Self(env::vars().collect())
280 }
281
282 fn set(&mut self, key: &str, value: &str) {
283 self.0.insert(key.to_owned(), value.to_owned());
284 }
285
286 fn into_env(self) -> Vec<String> {
287 // execve() expects an array of "k=v" strings, rather than a list of (k, v) pairs.
288 self.0.into_iter().map(|(k, v)| k + "=" + &v).collect()
289 }
290}