Victor Hsieh | 51789de | 2021-08-06 16:50:49 -0700 | [diff] [blame] | 1 | /* |
| 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 Stokes | fadcef2 | 2022-01-24 17:00:59 +0000 | [diff] [blame] | 17 | use anyhow::{anyhow, bail, Context, Result}; |
Victor Hsieh | 616f822 | 2022-01-14 13:06:32 -0800 | [diff] [blame] | 18 | use log::{debug, info, warn}; |
Victor Hsieh | 51789de | 2021-08-06 16:50:49 -0700 | [diff] [blame] | 19 | use minijail::{self, Minijail}; |
Alan Stokes | 9247251 | 2022-01-04 11:48:38 +0000 | [diff] [blame] | 20 | use regex::Regex; |
Alan Stokes | 2d2e4db | 2022-01-28 16:41:52 +0000 | [diff] [blame] | 21 | use rustutils::system_properties; |
Alan Stokes | fadcef2 | 2022-01-24 17:00:59 +0000 | [diff] [blame] | 22 | use std::collections::HashMap; |
Victor Hsieh | f996869 | 2021-11-18 11:34:39 -0800 | [diff] [blame] | 23 | use std::env; |
Alan Stokes | 9247251 | 2022-01-04 11:48:38 +0000 | [diff] [blame] | 24 | use std::ffi::OsString; |
Alan Stokes | 35bac3c | 2021-12-16 14:37:24 +0000 | [diff] [blame] | 25 | use std::path::{self, Path, PathBuf}; |
Alan Stokes | 9247251 | 2022-01-04 11:48:38 +0000 | [diff] [blame] | 26 | use std::process::Command; |
Victor Hsieh | 51789de | 2021-08-06 16:50:49 -0700 | [diff] [blame] | 27 | |
| 28 | use authfs_aidl_interface::aidl::com::android::virt::fs::{ |
Victor Hsieh | 015bcb5 | 2021-11-17 17:28:01 -0800 | [diff] [blame] | 29 | AuthFsConfig::{ |
Victor Hsieh | f996869 | 2021-11-18 11:34:39 -0800 | [diff] [blame] | 30 | AuthFsConfig, InputDirFdAnnotation::InputDirFdAnnotation, |
Victor Hsieh | 616f822 | 2022-01-14 13:06:32 -0800 | [diff] [blame] | 31 | OutputDirFdAnnotation::OutputDirFdAnnotation, |
Victor Hsieh | 015bcb5 | 2021-11-17 17:28:01 -0800 | [diff] [blame] | 32 | }, |
Victor Hsieh | 015bcb5 | 2021-11-17 17:28:01 -0800 | [diff] [blame] | 33 | IAuthFsService::IAuthFsService, |
Victor Hsieh | 51789de | 2021-08-06 16:50:49 -0700 | [diff] [blame] | 34 | }; |
Alan Stokes | 0e82b50 | 2022-08-08 14:44:48 +0100 | [diff] [blame] | 35 | use binder::Strong; |
Victor Hsieh | e769867 | 2022-09-23 16:22:28 -0700 | [diff] [blame] | 36 | use compos_aidl_interface::aidl::com::android::compos::ICompOsService::{ |
| 37 | CompilationMode::CompilationMode, OdrefreshArgs::OdrefreshArgs, |
| 38 | }; |
Alan Stokes | 46a1dff | 2021-12-14 10:56:05 +0000 | [diff] [blame] | 39 | use compos_common::odrefresh::ExitCode; |
Victor Hsieh | 51789de | 2021-08-06 16:50:49 -0700 | [diff] [blame] | 40 | |
Victor Hsieh | f996869 | 2021-11-18 11:34:39 -0800 | [diff] [blame] | 41 | const FD_SERVER_PORT: i32 = 3264; // TODO: support dynamic port |
| 42 | |
Victor Hsieh | e769867 | 2022-09-23 16:22:28 -0700 | [diff] [blame] | 43 | fn validate_args(args: &OdrefreshArgs) -> Result<()> { |
| 44 | if args.compilationMode != CompilationMode::NORMAL_COMPILE { |
| 45 | // Conservatively check debuggability. |
Seungjae Yoo | fa22bb0 | 2022-12-08 16:38:42 +0900 | [diff] [blame^] | 46 | let debuggable = |
| 47 | system_properties::read_bool("ro.boot.microdroid.debuggable", false).unwrap_or(false); |
Victor Hsieh | e769867 | 2022-09-23 16:22:28 -0700 | [diff] [blame] | 48 | if !debuggable { |
| 49 | bail!("Requested compilation mode only available in debuggable VMs"); |
Alan Stokes | 2d2e4db | 2022-01-28 16:41:52 +0000 | [diff] [blame] | 50 | } |
Alan Stokes | 46a1dff | 2021-12-14 10:56:05 +0000 | [diff] [blame] | 51 | } |
Victor Hsieh | e769867 | 2022-09-23 16:22:28 -0700 | [diff] [blame] | 52 | |
| 53 | if args.systemDirFd < 0 || args.outputDirFd < 0 || args.stagingDirFd < 0 { |
| 54 | bail!("The remote FDs are expected to be non-negative"); |
| 55 | } |
| 56 | if !matches!(&args.zygoteArch[..], "zygote64" | "zygote64_32") { |
| 57 | bail!("Invalid zygote arch"); |
| 58 | } |
| 59 | // Disallow any sort of path traversal |
| 60 | if args.targetDirName.contains(path::MAIN_SEPARATOR) { |
| 61 | bail!("Invalid target directory {}", args.targetDirName); |
| 62 | } |
| 63 | |
| 64 | // We're not validating/allowlisting the compiler filter, and just assume the compiler will |
| 65 | // reject an invalid string. We need to accept "verify" filter anyway, and potential |
| 66 | // performance degration by the attacker is not currently in scope. This also allows ART to |
| 67 | // specify new compiler filter and configure through system property without change to |
| 68 | // CompOS. |
| 69 | Ok(()) |
Alan Stokes | 46a1dff | 2021-12-14 10:56:05 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Victor Hsieh | ec38ae2 | 2022-02-10 00:06:26 +0000 | [diff] [blame] | 72 | pub fn odrefresh<F>( |
Alan Stokes | 46a1dff | 2021-12-14 10:56:05 +0000 | [diff] [blame] | 73 | odrefresh_path: &Path, |
Victor Hsieh | e769867 | 2022-09-23 16:22:28 -0700 | [diff] [blame] | 74 | args: &OdrefreshArgs, |
Victor Hsieh | f996869 | 2021-11-18 11:34:39 -0800 | [diff] [blame] | 75 | authfs_service: Strong<dyn IAuthFsService>, |
Victor Hsieh | ec38ae2 | 2022-02-10 00:06:26 +0000 | [diff] [blame] | 76 | success_fn: F, |
| 77 | ) -> Result<ExitCode> |
| 78 | where |
| 79 | F: FnOnce(PathBuf) -> Result<()>, |
| 80 | { |
Victor Hsieh | e769867 | 2022-09-23 16:22:28 -0700 | [diff] [blame] | 81 | validate_args(args)?; |
| 82 | |
Victor Hsieh | f996869 | 2021-11-18 11:34:39 -0800 | [diff] [blame] | 83 | // Mount authfs (via authfs_service). The authfs instance unmounts once the `authfs` variable |
| 84 | // is out of scope. |
Victor Hsieh | 64d8862 | 2022-09-21 17:32:00 -0700 | [diff] [blame] | 85 | |
| 86 | let mut input_dir_fd_annotations = vec![InputDirFdAnnotation { |
Victor Hsieh | e769867 | 2022-09-23 16:22:28 -0700 | [diff] [blame] | 87 | fd: args.systemDirFd, |
Victor Hsieh | 64d8862 | 2022-09-21 17:32:00 -0700 | [diff] [blame] | 88 | // Use the 0th APK of the extra_apks in compos/apk/assets/vm_config*.json |
| 89 | manifestPath: "/mnt/extra-apk/0/assets/build_manifest.pb".to_string(), |
| 90 | prefix: "system/".to_string(), |
| 91 | }]; |
Victor Hsieh | e769867 | 2022-09-23 16:22:28 -0700 | [diff] [blame] | 92 | if args.systemExtDirFd >= 0 { |
Victor Hsieh | 64d8862 | 2022-09-21 17:32:00 -0700 | [diff] [blame] | 93 | input_dir_fd_annotations.push(InputDirFdAnnotation { |
Victor Hsieh | e769867 | 2022-09-23 16:22:28 -0700 | [diff] [blame] | 94 | fd: args.systemExtDirFd, |
Victor Hsieh | 64d8862 | 2022-09-21 17:32:00 -0700 | [diff] [blame] | 95 | // Use the 1st APK of the extra_apks in compos/apk/assets/vm_config_system_ext_*.json |
| 96 | manifestPath: "/mnt/extra-apk/1/assets/build_manifest.pb".to_string(), |
| 97 | prefix: "system_ext/".to_string(), |
| 98 | }); |
| 99 | } |
| 100 | |
Victor Hsieh | f996869 | 2021-11-18 11:34:39 -0800 | [diff] [blame] | 101 | let authfs_config = AuthFsConfig { |
| 102 | port: FD_SERVER_PORT, |
Victor Hsieh | 64d8862 | 2022-09-21 17:32:00 -0700 | [diff] [blame] | 103 | inputDirFdAnnotations: input_dir_fd_annotations, |
Alan Stokes | 9646db9 | 2021-12-14 13:22:33 +0000 | [diff] [blame] | 104 | outputDirFdAnnotations: vec![ |
Victor Hsieh | e769867 | 2022-09-23 16:22:28 -0700 | [diff] [blame] | 105 | OutputDirFdAnnotation { fd: args.outputDirFd }, |
| 106 | OutputDirFdAnnotation { fd: args.stagingDirFd }, |
Alan Stokes | 9646db9 | 2021-12-14 13:22:33 +0000 | [diff] [blame] | 107 | ], |
Victor Hsieh | f996869 | 2021-11-18 11:34:39 -0800 | [diff] [blame] | 108 | ..Default::default() |
| 109 | }; |
| 110 | let authfs = authfs_service.mount(&authfs_config)?; |
| 111 | let mountpoint = PathBuf::from(authfs.getMountPoint()?); |
| 112 | |
Alan Stokes | fadcef2 | 2022-01-24 17:00:59 +0000 | [diff] [blame] | 113 | // Make a copy of our environment as the basis of the one we will give odrefresh |
| 114 | let mut odrefresh_vars = EnvMap::from_current_env(); |
| 115 | |
Victor Hsieh | f996869 | 2021-11-18 11:34:39 -0800 | [diff] [blame] | 116 | let mut android_root = mountpoint.clone(); |
Victor Hsieh | e769867 | 2022-09-23 16:22:28 -0700 | [diff] [blame] | 117 | android_root.push(args.systemDirFd.to_string()); |
Victor Hsieh | f996869 | 2021-11-18 11:34:39 -0800 | [diff] [blame] | 118 | android_root.push("system"); |
Alan Stokes | fadcef2 | 2022-01-24 17:00:59 +0000 | [diff] [blame] | 119 | odrefresh_vars.set("ANDROID_ROOT", path_to_str(&android_root)?); |
Alan Stokes | 46a1dff | 2021-12-14 10:56:05 +0000 | [diff] [blame] | 120 | debug!("ANDROID_ROOT={:?}", &android_root); |
Victor Hsieh | f996869 | 2021-11-18 11:34:39 -0800 | [diff] [blame] | 121 | |
Victor Hsieh | e769867 | 2022-09-23 16:22:28 -0700 | [diff] [blame] | 122 | if args.systemExtDirFd >= 0 { |
Victor Hsieh | 64d8862 | 2022-09-21 17:32:00 -0700 | [diff] [blame] | 123 | let mut system_ext_root = mountpoint.clone(); |
Victor Hsieh | e769867 | 2022-09-23 16:22:28 -0700 | [diff] [blame] | 124 | system_ext_root.push(args.systemExtDirFd.to_string()); |
Victor Hsieh | 64d8862 | 2022-09-21 17:32:00 -0700 | [diff] [blame] | 125 | system_ext_root.push("system_ext"); |
| 126 | odrefresh_vars.set("SYSTEM_EXT_ROOT", path_to_str(&system_ext_root)?); |
| 127 | debug!("SYSTEM_EXT_ROOT={:?}", &system_ext_root); |
| 128 | } |
| 129 | |
Victor Hsieh | e769867 | 2022-09-23 16:22:28 -0700 | [diff] [blame] | 130 | let art_apex_data = mountpoint.join(args.outputDirFd.to_string()); |
Alan Stokes | fadcef2 | 2022-01-24 17:00:59 +0000 | [diff] [blame] | 131 | odrefresh_vars.set("ART_APEX_DATA", path_to_str(&art_apex_data)?); |
Alan Stokes | 46a1dff | 2021-12-14 10:56:05 +0000 | [diff] [blame] | 132 | debug!("ART_APEX_DATA={:?}", &art_apex_data); |
Victor Hsieh | 64df53d | 2021-11-30 17:09:51 -0800 | [diff] [blame] | 133 | |
Victor Hsieh | e769867 | 2022-09-23 16:22:28 -0700 | [diff] [blame] | 134 | let staging_dir = mountpoint.join(args.stagingDirFd.to_string()); |
Victor Hsieh | f996869 | 2021-11-18 11:34:39 -0800 | [diff] [blame] | 135 | |
Alan Stokes | fadcef2 | 2022-01-24 17:00:59 +0000 | [diff] [blame] | 136 | set_classpaths(&mut odrefresh_vars, &android_root)?; |
Alan Stokes | 9247251 | 2022-01-04 11:48:38 +0000 | [diff] [blame] | 137 | |
Victor Hsieh | e769867 | 2022-09-23 16:22:28 -0700 | [diff] [blame] | 138 | let mut command_line_args = vec![ |
Victor Hsieh | f996869 | 2021-11-18 11:34:39 -0800 | [diff] [blame] | 139 | "odrefresh".to_string(), |
Alan Stokes | 48c1d2b | 2022-01-10 15:54:04 +0000 | [diff] [blame] | 140 | "--compilation-os-mode".to_string(), |
Victor Hsieh | e769867 | 2022-09-23 16:22:28 -0700 | [diff] [blame] | 141 | format!("--zygote-arch={}", args.zygoteArch), |
| 142 | format!("--dalvik-cache={}", args.targetDirName), |
Victor Hsieh | f996869 | 2021-11-18 11:34:39 -0800 | [diff] [blame] | 143 | format!("--staging-dir={}", staging_dir.display()), |
Victor Hsieh | 9bfbc5f | 2021-12-16 11:45:10 -0800 | [diff] [blame] | 144 | "--no-refresh".to_string(), |
Victor Hsieh | f996869 | 2021-11-18 11:34:39 -0800 | [diff] [blame] | 145 | ]; |
Victor Hsieh | 9bfbc5f | 2021-12-16 11:45:10 -0800 | [diff] [blame] | 146 | |
Victor Hsieh | e769867 | 2022-09-23 16:22:28 -0700 | [diff] [blame] | 147 | if !args.systemServerCompilerFilter.is_empty() { |
| 148 | command_line_args |
| 149 | .push(format!("--system-server-compiler-filter={}", args.systemServerCompilerFilter)); |
Victor Hsieh | 9bfbc5f | 2021-12-16 11:45:10 -0800 | [diff] [blame] | 150 | } |
Alan Stokes | 48c1d2b | 2022-01-10 15:54:04 +0000 | [diff] [blame] | 151 | |
Victor Hsieh | e769867 | 2022-09-23 16:22:28 -0700 | [diff] [blame] | 152 | let compile_flag = match args.compilationMode { |
Alan Stokes | 2d2e4db | 2022-01-28 16:41:52 +0000 | [diff] [blame] | 153 | CompilationMode::NORMAL_COMPILE => "--compile", |
| 154 | CompilationMode::TEST_COMPILE => "--force-compile", |
| 155 | other => bail!("Unknown compilation mode {:?}", other), |
| 156 | }; |
Victor Hsieh | e769867 | 2022-09-23 16:22:28 -0700 | [diff] [blame] | 157 | command_line_args.push(compile_flag.to_string()); |
Victor Hsieh | 9bfbc5f | 2021-12-16 11:45:10 -0800 | [diff] [blame] | 158 | |
Victor Hsieh | e769867 | 2022-09-23 16:22:28 -0700 | [diff] [blame] | 159 | debug!("Running odrefresh with args: {:?}", &command_line_args); |
| 160 | let jail = spawn_jailed_task(odrefresh_path, &command_line_args, &odrefresh_vars.into_env()) |
Victor Hsieh | f996869 | 2021-11-18 11:34:39 -0800 | [diff] [blame] | 161 | .context("Spawn odrefresh")?; |
Alan Stokes | 46a1dff | 2021-12-14 10:56:05 +0000 | [diff] [blame] | 162 | let exit_code = match jail.wait() { |
Alan Stokes | 2d2e4db | 2022-01-28 16:41:52 +0000 | [diff] [blame] | 163 | Ok(_) => 0, |
| 164 | Err(minijail::Error::ReturnCode(exit_code)) => exit_code, |
| 165 | Err(e) => bail!("Unexpected minijail error: {}", e), |
| 166 | }; |
Alan Stokes | 46a1dff | 2021-12-14 10:56:05 +0000 | [diff] [blame] | 167 | |
Alan Stokes | 126fd51 | 2021-12-16 15:00:01 +0000 | [diff] [blame] | 168 | let exit_code = ExitCode::from_i32(exit_code.into())?; |
Alan Stokes | 46a1dff | 2021-12-14 10:56:05 +0000 | [diff] [blame] | 169 | info!("odrefresh exited with {:?}", exit_code); |
| 170 | |
| 171 | if exit_code == ExitCode::CompilationSuccess { |
Victor Hsieh | e769867 | 2022-09-23 16:22:28 -0700 | [diff] [blame] | 172 | let target_dir = art_apex_data.join(&args.targetDirName); |
Victor Hsieh | ec38ae2 | 2022-02-10 00:06:26 +0000 | [diff] [blame] | 173 | success_fn(target_dir)?; |
Victor Hsieh | f996869 | 2021-11-18 11:34:39 -0800 | [diff] [blame] | 174 | } |
Alan Stokes | 46a1dff | 2021-12-14 10:56:05 +0000 | [diff] [blame] | 175 | |
| 176 | Ok(exit_code) |
| 177 | } |
| 178 | |
Alan Stokes | fadcef2 | 2022-01-24 17:00:59 +0000 | [diff] [blame] | 179 | fn path_to_str(path: &Path) -> Result<&str> { |
| 180 | path.to_str().ok_or_else(|| anyhow!("Bad path {:?}", path)) |
| 181 | } |
| 182 | |
| 183 | fn set_classpaths(odrefresh_vars: &mut EnvMap, android_root: &Path) -> Result<()> { |
Alan Stokes | 9247251 | 2022-01-04 11:48:38 +0000 | [diff] [blame] | 184 | let export_lines = run_derive_classpath(android_root)?; |
Alan Stokes | fadcef2 | 2022-01-24 17:00:59 +0000 | [diff] [blame] | 185 | load_classpath_vars(odrefresh_vars, &export_lines) |
Alan Stokes | 9247251 | 2022-01-04 11:48:38 +0000 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | fn run_derive_classpath(android_root: &Path) -> Result<String> { |
| 189 | let classpaths_root = android_root.join("etc/classpaths"); |
| 190 | |
| 191 | let mut bootclasspath_arg = OsString::new(); |
| 192 | bootclasspath_arg.push("--bootclasspath-fragment="); |
| 193 | bootclasspath_arg.push(classpaths_root.join("bootclasspath.pb")); |
| 194 | |
| 195 | let mut systemserverclasspath_arg = OsString::new(); |
| 196 | systemserverclasspath_arg.push("--systemserverclasspath-fragment="); |
| 197 | systemserverclasspath_arg.push(classpaths_root.join("systemserverclasspath.pb")); |
| 198 | |
| 199 | let result = Command::new("/apex/com.android.sdkext/bin/derive_classpath") |
| 200 | .arg(bootclasspath_arg) |
| 201 | .arg(systemserverclasspath_arg) |
| 202 | .arg("/proc/self/fd/1") |
| 203 | .output() |
| 204 | .context("Failed to run derive_classpath")?; |
| 205 | |
| 206 | if !result.status.success() { |
| 207 | bail!("derive_classpath returned {}", result.status); |
| 208 | } |
| 209 | |
| 210 | String::from_utf8(result.stdout).context("Converting derive_classpath output") |
| 211 | } |
| 212 | |
Alan Stokes | fadcef2 | 2022-01-24 17:00:59 +0000 | [diff] [blame] | 213 | fn load_classpath_vars(odrefresh_vars: &mut EnvMap, export_lines: &str) -> Result<()> { |
Alan Stokes | 9247251 | 2022-01-04 11:48:38 +0000 | [diff] [blame] | 214 | // Each line should be in the format "export <var name> <value>" |
| 215 | let pattern = Regex::new(r"^export ([^ ]+) ([^ ]+)$").context("Failed to construct Regex")?; |
| 216 | for line in export_lines.lines() { |
| 217 | if let Some(captures) = pattern.captures(line) { |
| 218 | let name = &captures[1]; |
| 219 | let value = &captures[2]; |
Alan Stokes | fadcef2 | 2022-01-24 17:00:59 +0000 | [diff] [blame] | 220 | odrefresh_vars.set(name, value); |
Alan Stokes | 9247251 | 2022-01-04 11:48:38 +0000 | [diff] [blame] | 221 | } else { |
| 222 | warn!("Malformed line from derive_classpath: {}", line); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | Ok(()) |
| 227 | } |
| 228 | |
Alan Stokes | fadcef2 | 2022-01-24 17:00:59 +0000 | [diff] [blame] | 229 | fn spawn_jailed_task(executable: &Path, args: &[String], env_vars: &[String]) -> Result<Minijail> { |
Victor Hsieh | 51789de | 2021-08-06 16:50:49 -0700 | [diff] [blame] | 230 | // TODO(b/185175567): Run in a more restricted sandbox. |
| 231 | let jail = Minijail::new()?; |
Alan Stokes | fadcef2 | 2022-01-24 17:00:59 +0000 | [diff] [blame] | 232 | let keep_fds = []; |
| 233 | let command = minijail::Command::new_for_path(executable, &keep_fds, args, Some(env_vars))?; |
| 234 | let _pid = jail.run_command(command)?; |
Victor Hsieh | 51789de | 2021-08-06 16:50:49 -0700 | [diff] [blame] | 235 | Ok(jail) |
| 236 | } |
Alan Stokes | fadcef2 | 2022-01-24 17:00:59 +0000 | [diff] [blame] | 237 | |
| 238 | struct EnvMap(HashMap<String, String>); |
| 239 | |
| 240 | impl EnvMap { |
| 241 | fn from_current_env() -> Self { |
| 242 | Self(env::vars().collect()) |
| 243 | } |
| 244 | |
| 245 | fn set(&mut self, key: &str, value: &str) { |
| 246 | self.0.insert(key.to_owned(), value.to_owned()); |
| 247 | } |
| 248 | |
| 249 | fn into_env(self) -> Vec<String> { |
| 250 | // execve() expects an array of "k=v" strings, rather than a list of (k, v) pairs. |
| 251 | self.0.into_iter().map(|(k, v)| k + "=" + &v).collect() |
| 252 | } |
| 253 | } |