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 | |
Victor Hsieh | 6e34038 | 2021-08-13 12:18:02 -0700 | [diff] [blame] | 17 | use anyhow::{anyhow, bail, Context, Result}; |
Alan Stokes | 9247251 | 2022-01-04 11:48:38 +0000 | [diff] [blame^] | 18 | use log::{debug, error, 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; |
Victor Hsieh | f996869 | 2021-11-18 11:34:39 -0800 | [diff] [blame] | 21 | use std::env; |
Alan Stokes | 9247251 | 2022-01-04 11:48:38 +0000 | [diff] [blame^] | 22 | use std::ffi::OsString; |
Alan Stokes | 46a1dff | 2021-12-14 10:56:05 +0000 | [diff] [blame] | 23 | use std::fs::{read_dir, File}; |
Victor Hsieh | 6e34038 | 2021-08-13 12:18:02 -0700 | [diff] [blame] | 24 | use std::os::unix::io::{AsRawFd, RawFd}; |
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 | |
Alan Stokes | 46a1dff | 2021-12-14 10:56:05 +0000 | [diff] [blame] | 28 | use crate::artifact_signer::ArtifactSigner; |
| 29 | use crate::compos_key_service::Signer; |
Victor Hsieh | 9ed2718 | 2021-08-25 15:52:42 -0700 | [diff] [blame] | 30 | use crate::fsverity; |
Victor Hsieh | 51789de | 2021-08-06 16:50:49 -0700 | [diff] [blame] | 31 | use authfs_aidl_interface::aidl::com::android::virt::fs::{ |
Victor Hsieh | 015bcb5 | 2021-11-17 17:28:01 -0800 | [diff] [blame] | 32 | AuthFsConfig::{ |
Victor Hsieh | f996869 | 2021-11-18 11:34:39 -0800 | [diff] [blame] | 33 | AuthFsConfig, InputDirFdAnnotation::InputDirFdAnnotation, |
| 34 | InputFdAnnotation::InputFdAnnotation, OutputDirFdAnnotation::OutputDirFdAnnotation, |
| 35 | OutputFdAnnotation::OutputFdAnnotation, |
Victor Hsieh | 015bcb5 | 2021-11-17 17:28:01 -0800 | [diff] [blame] | 36 | }, |
| 37 | IAuthFs::IAuthFs, |
| 38 | IAuthFsService::IAuthFsService, |
Victor Hsieh | 51789de | 2021-08-06 16:50:49 -0700 | [diff] [blame] | 39 | }; |
| 40 | use authfs_aidl_interface::binder::{ParcelFileDescriptor, Strong}; |
Victor Hsieh | 13333e8 | 2021-09-03 15:17:32 -0700 | [diff] [blame] | 41 | use compos_aidl_interface::aidl::com::android::compos::FdAnnotation::FdAnnotation; |
Alan Stokes | 46a1dff | 2021-12-14 10:56:05 +0000 | [diff] [blame] | 42 | use compos_common::odrefresh::ExitCode; |
Victor Hsieh | 51789de | 2021-08-06 16:50:49 -0700 | [diff] [blame] | 43 | |
Victor Hsieh | f996869 | 2021-11-18 11:34:39 -0800 | [diff] [blame] | 44 | const FD_SERVER_PORT: i32 = 3264; // TODO: support dynamic port |
| 45 | |
Victor Hsieh | 51789de | 2021-08-06 16:50:49 -0700 | [diff] [blame] | 46 | /// The number that represents the file descriptor number expecting by the task. The number may be |
| 47 | /// meaningless in the current process. |
| 48 | pub type PseudoRawFd = i32; |
| 49 | |
Victor Hsieh | 6e34038 | 2021-08-13 12:18:02 -0700 | [diff] [blame] | 50 | pub enum CompilerOutput { |
| 51 | /// Fs-verity digests of output files, if the compiler finishes successfully. |
Victor Hsieh | 9ed2718 | 2021-08-25 15:52:42 -0700 | [diff] [blame] | 52 | Digests { |
| 53 | oat: fsverity::Sha256Digest, |
| 54 | vdex: fsverity::Sha256Digest, |
| 55 | image: fsverity::Sha256Digest, |
| 56 | }, |
Victor Hsieh | 6e34038 | 2021-08-13 12:18:02 -0700 | [diff] [blame] | 57 | /// Exit code returned by the compiler, if not 0. |
| 58 | ExitCode(i8), |
| 59 | } |
| 60 | |
| 61 | struct CompilerOutputParcelFds { |
| 62 | oat: ParcelFileDescriptor, |
| 63 | vdex: ParcelFileDescriptor, |
| 64 | image: ParcelFileDescriptor, |
| 65 | } |
| 66 | |
Alan Stokes | 46a1dff | 2021-12-14 10:56:05 +0000 | [diff] [blame] | 67 | pub struct OdrefreshContext<'a> { |
Victor Hsieh | f996869 | 2021-11-18 11:34:39 -0800 | [diff] [blame] | 68 | system_dir_fd: i32, |
| 69 | output_dir_fd: i32, |
Alan Stokes | 9646db9 | 2021-12-14 13:22:33 +0000 | [diff] [blame] | 70 | staging_dir_fd: i32, |
Alan Stokes | 46a1dff | 2021-12-14 10:56:05 +0000 | [diff] [blame] | 71 | target_dir_name: &'a str, |
| 72 | zygote_arch: &'a str, |
| 73 | } |
| 74 | |
| 75 | impl<'a> OdrefreshContext<'a> { |
| 76 | pub fn new( |
| 77 | system_dir_fd: i32, |
| 78 | output_dir_fd: i32, |
| 79 | staging_dir_fd: i32, |
| 80 | target_dir_name: &'a str, |
| 81 | zygote_arch: &'a str, |
| 82 | ) -> Result<Self> { |
| 83 | if system_dir_fd < 0 || output_dir_fd < 0 || staging_dir_fd < 0 { |
| 84 | bail!("The remote FDs are expected to be non-negative"); |
| 85 | } |
| 86 | if zygote_arch != "zygote64" && zygote_arch != "zygote64_32" { |
| 87 | bail!("Invalid zygote arch"); |
| 88 | } |
Alan Stokes | 35bac3c | 2021-12-16 14:37:24 +0000 | [diff] [blame] | 89 | // Disallow any sort of path traversal |
| 90 | if target_dir_name.contains(path::MAIN_SEPARATOR) { |
| 91 | bail!("Invalid target directory {}", target_dir_name); |
| 92 | } |
| 93 | |
Alan Stokes | 46a1dff | 2021-12-14 10:56:05 +0000 | [diff] [blame] | 94 | Ok(Self { system_dir_fd, output_dir_fd, staging_dir_fd, target_dir_name, zygote_arch }) |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | pub fn odrefresh( |
| 99 | odrefresh_path: &Path, |
| 100 | context: OdrefreshContext, |
Victor Hsieh | f996869 | 2021-11-18 11:34:39 -0800 | [diff] [blame] | 101 | authfs_service: Strong<dyn IAuthFsService>, |
Alan Stokes | 46a1dff | 2021-12-14 10:56:05 +0000 | [diff] [blame] | 102 | signer: Signer, |
| 103 | ) -> Result<ExitCode> { |
Victor Hsieh | f996869 | 2021-11-18 11:34:39 -0800 | [diff] [blame] | 104 | // Mount authfs (via authfs_service). The authfs instance unmounts once the `authfs` variable |
| 105 | // is out of scope. |
| 106 | let authfs_config = AuthFsConfig { |
| 107 | port: FD_SERVER_PORT, |
| 108 | inputDirFdAnnotations: vec![InputDirFdAnnotation { |
Alan Stokes | 46a1dff | 2021-12-14 10:56:05 +0000 | [diff] [blame] | 109 | fd: context.system_dir_fd, |
Victor Hsieh | f996869 | 2021-11-18 11:34:39 -0800 | [diff] [blame] | 110 | // TODO(206869687): Replace /dev/null with the real path when possible. |
| 111 | manifestPath: "/dev/null".to_string(), |
| 112 | prefix: "/system".to_string(), |
| 113 | }], |
Alan Stokes | 9646db9 | 2021-12-14 13:22:33 +0000 | [diff] [blame] | 114 | outputDirFdAnnotations: vec![ |
Alan Stokes | 46a1dff | 2021-12-14 10:56:05 +0000 | [diff] [blame] | 115 | OutputDirFdAnnotation { fd: context.output_dir_fd }, |
| 116 | OutputDirFdAnnotation { fd: context.staging_dir_fd }, |
Alan Stokes | 9646db9 | 2021-12-14 13:22:33 +0000 | [diff] [blame] | 117 | ], |
Victor Hsieh | f996869 | 2021-11-18 11:34:39 -0800 | [diff] [blame] | 118 | ..Default::default() |
| 119 | }; |
| 120 | let authfs = authfs_service.mount(&authfs_config)?; |
| 121 | let mountpoint = PathBuf::from(authfs.getMountPoint()?); |
| 122 | |
| 123 | let mut android_root = mountpoint.clone(); |
Alan Stokes | 46a1dff | 2021-12-14 10:56:05 +0000 | [diff] [blame] | 124 | android_root.push(context.system_dir_fd.to_string()); |
Victor Hsieh | f996869 | 2021-11-18 11:34:39 -0800 | [diff] [blame] | 125 | android_root.push("system"); |
| 126 | env::set_var("ANDROID_ROOT", &android_root); |
Alan Stokes | 46a1dff | 2021-12-14 10:56:05 +0000 | [diff] [blame] | 127 | debug!("ANDROID_ROOT={:?}", &android_root); |
Victor Hsieh | f996869 | 2021-11-18 11:34:39 -0800 | [diff] [blame] | 128 | |
Alan Stokes | 46a1dff | 2021-12-14 10:56:05 +0000 | [diff] [blame] | 129 | let art_apex_data = mountpoint.join(context.output_dir_fd.to_string()); |
Victor Hsieh | 64df53d | 2021-11-30 17:09:51 -0800 | [diff] [blame] | 130 | env::set_var("ART_APEX_DATA", &art_apex_data); |
Alan Stokes | 46a1dff | 2021-12-14 10:56:05 +0000 | [diff] [blame] | 131 | debug!("ART_APEX_DATA={:?}", &art_apex_data); |
Victor Hsieh | 64df53d | 2021-11-30 17:09:51 -0800 | [diff] [blame] | 132 | |
Alan Stokes | 46a1dff | 2021-12-14 10:56:05 +0000 | [diff] [blame] | 133 | let staging_dir = mountpoint.join(context.staging_dir_fd.to_string()); |
Victor Hsieh | f996869 | 2021-11-18 11:34:39 -0800 | [diff] [blame] | 134 | |
Alan Stokes | 9247251 | 2022-01-04 11:48:38 +0000 | [diff] [blame^] | 135 | set_classpaths(&android_root)?; |
| 136 | |
Victor Hsieh | f996869 | 2021-11-18 11:34:39 -0800 | [diff] [blame] | 137 | let args = vec![ |
| 138 | "odrefresh".to_string(), |
Alan Stokes | 46a1dff | 2021-12-14 10:56:05 +0000 | [diff] [blame] | 139 | format!("--zygote-arch={}", context.zygote_arch), |
| 140 | format!("--dalvik-cache={}", context.target_dir_name), |
Victor Hsieh | 64df53d | 2021-11-30 17:09:51 -0800 | [diff] [blame] | 141 | "--no-refresh".to_string(), |
Victor Hsieh | f996869 | 2021-11-18 11:34:39 -0800 | [diff] [blame] | 142 | format!("--staging-dir={}", staging_dir.display()), |
| 143 | "--force-compile".to_string(), |
| 144 | ]; |
Alan Stokes | 9646db9 | 2021-12-14 13:22:33 +0000 | [diff] [blame] | 145 | debug!("Running odrefresh with args: {:?}", &args); |
Victor Hsieh | f996869 | 2021-11-18 11:34:39 -0800 | [diff] [blame] | 146 | let jail = spawn_jailed_task(odrefresh_path, &args, Vec::new() /* fd_mapping */) |
| 147 | .context("Spawn odrefresh")?; |
Alan Stokes | 46a1dff | 2021-12-14 10:56:05 +0000 | [diff] [blame] | 148 | let exit_code = match jail.wait() { |
| 149 | Ok(_) => Result::<u8>::Ok(0), |
| 150 | Err(minijail::Error::ReturnCode(exit_code)) => Ok(exit_code), |
Victor Hsieh | f996869 | 2021-11-18 11:34:39 -0800 | [diff] [blame] | 151 | Err(e) => { |
| 152 | bail!("Unexpected minijail error: {}", e) |
| 153 | } |
Alan Stokes | 46a1dff | 2021-12-14 10:56:05 +0000 | [diff] [blame] | 154 | }?; |
| 155 | |
Alan Stokes | 126fd51 | 2021-12-16 15:00:01 +0000 | [diff] [blame] | 156 | let exit_code = ExitCode::from_i32(exit_code.into())?; |
Alan Stokes | 46a1dff | 2021-12-14 10:56:05 +0000 | [diff] [blame] | 157 | info!("odrefresh exited with {:?}", exit_code); |
| 158 | |
| 159 | if exit_code == ExitCode::CompilationSuccess { |
| 160 | // authfs only shows us the files we created, so it's ok to just sign everything under |
| 161 | // the target directory. |
| 162 | let target_dir = art_apex_data.join(context.target_dir_name); |
| 163 | let mut artifact_signer = ArtifactSigner::new(&target_dir); |
| 164 | add_artifacts(&target_dir, &mut artifact_signer)?; |
| 165 | |
| 166 | artifact_signer.write_info_and_signature(signer, &target_dir.join("compos.info"))?; |
Victor Hsieh | f996869 | 2021-11-18 11:34:39 -0800 | [diff] [blame] | 167 | } |
Alan Stokes | 46a1dff | 2021-12-14 10:56:05 +0000 | [diff] [blame] | 168 | |
| 169 | Ok(exit_code) |
| 170 | } |
| 171 | |
Alan Stokes | 9247251 | 2022-01-04 11:48:38 +0000 | [diff] [blame^] | 172 | fn set_classpaths(android_root: &Path) -> Result<()> { |
| 173 | let export_lines = run_derive_classpath(android_root)?; |
| 174 | load_classpath_vars(&export_lines) |
| 175 | } |
| 176 | |
| 177 | fn run_derive_classpath(android_root: &Path) -> Result<String> { |
| 178 | let classpaths_root = android_root.join("etc/classpaths"); |
| 179 | |
| 180 | let mut bootclasspath_arg = OsString::new(); |
| 181 | bootclasspath_arg.push("--bootclasspath-fragment="); |
| 182 | bootclasspath_arg.push(classpaths_root.join("bootclasspath.pb")); |
| 183 | |
| 184 | let mut systemserverclasspath_arg = OsString::new(); |
| 185 | systemserverclasspath_arg.push("--systemserverclasspath-fragment="); |
| 186 | systemserverclasspath_arg.push(classpaths_root.join("systemserverclasspath.pb")); |
| 187 | |
| 188 | let result = Command::new("/apex/com.android.sdkext/bin/derive_classpath") |
| 189 | .arg(bootclasspath_arg) |
| 190 | .arg(systemserverclasspath_arg) |
| 191 | .arg("/proc/self/fd/1") |
| 192 | .output() |
| 193 | .context("Failed to run derive_classpath")?; |
| 194 | |
| 195 | if !result.status.success() { |
| 196 | bail!("derive_classpath returned {}", result.status); |
| 197 | } |
| 198 | |
| 199 | String::from_utf8(result.stdout).context("Converting derive_classpath output") |
| 200 | } |
| 201 | |
| 202 | fn load_classpath_vars(export_lines: &str) -> Result<()> { |
| 203 | // Each line should be in the format "export <var name> <value>" |
| 204 | let pattern = Regex::new(r"^export ([^ ]+) ([^ ]+)$").context("Failed to construct Regex")?; |
| 205 | for line in export_lines.lines() { |
| 206 | if let Some(captures) = pattern.captures(line) { |
| 207 | let name = &captures[1]; |
| 208 | let value = &captures[2]; |
| 209 | // TODO(b/213416778) Don't modify our env, construct a fresh one for odrefresh |
| 210 | env::set_var(name, value); |
| 211 | } else { |
| 212 | warn!("Malformed line from derive_classpath: {}", line); |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | Ok(()) |
| 217 | } |
| 218 | |
Alan Stokes | 46a1dff | 2021-12-14 10:56:05 +0000 | [diff] [blame] | 219 | fn add_artifacts(target_dir: &Path, artifact_signer: &mut ArtifactSigner) -> Result<()> { |
| 220 | for entry in |
| 221 | read_dir(&target_dir).with_context(|| format!("Traversing {}", target_dir.display()))? |
| 222 | { |
| 223 | let entry = entry?; |
| 224 | let file_type = entry.file_type()?; |
| 225 | if file_type.is_dir() { |
| 226 | add_artifacts(&entry.path(), artifact_signer)?; |
| 227 | } else if file_type.is_file() { |
| 228 | artifact_signer.add_artifact(&entry.path())?; |
| 229 | } else { |
| 230 | // authfs shouldn't create anything else, but just in case |
| 231 | bail!("Unexpected file type in artifacts: {:?}", entry); |
| 232 | } |
| 233 | } |
| 234 | Ok(()) |
Victor Hsieh | f996869 | 2021-11-18 11:34:39 -0800 | [diff] [blame] | 235 | } |
| 236 | |
Victor Hsieh | 13333e8 | 2021-09-03 15:17:32 -0700 | [diff] [blame] | 237 | /// Runs the compiler with given flags with file descriptors described in `fd_annotation` retrieved |
| 238 | /// via `authfs_service`. Returns exit code of the compiler process. |
Victor Hsieh | 3c044c4 | 2021-10-01 17:17:10 -0700 | [diff] [blame] | 239 | pub fn compile_cmd( |
Victor Hsieh | 51789de | 2021-08-06 16:50:49 -0700 | [diff] [blame] | 240 | compiler_path: &Path, |
| 241 | compiler_args: &[String], |
| 242 | authfs_service: Strong<dyn IAuthFsService>, |
Victor Hsieh | 13333e8 | 2021-09-03 15:17:32 -0700 | [diff] [blame] | 243 | fd_annotation: &FdAnnotation, |
Victor Hsieh | 6e34038 | 2021-08-13 12:18:02 -0700 | [diff] [blame] | 244 | ) -> Result<CompilerOutput> { |
| 245 | // Mount authfs (via authfs_service). The authfs instance unmounts once the `authfs` variable |
| 246 | // is out of scope. |
Victor Hsieh | 13333e8 | 2021-09-03 15:17:32 -0700 | [diff] [blame] | 247 | let authfs_config = build_authfs_config(fd_annotation); |
Victor Hsieh | 51789de | 2021-08-06 16:50:49 -0700 | [diff] [blame] | 248 | let authfs = authfs_service.mount(&authfs_config)?; |
| 249 | |
| 250 | // The task expects to receive FD numbers that match its flags (e.g. --zip-fd=42) prepared |
| 251 | // on the host side. Since the local FD opened from authfs (e.g. /authfs/42) may not match |
| 252 | // the task's expectation, prepare a FD mapping and let minijail prepare the correct FD |
| 253 | // setup. |
| 254 | let fd_mapping = |
| 255 | open_authfs_files_for_fd_mapping(&authfs, &authfs_config).context("Open on authfs")?; |
| 256 | |
| 257 | let jail = |
| 258 | spawn_jailed_task(compiler_path, compiler_args, fd_mapping).context("Spawn dex2oat")?; |
| 259 | let jail_result = jail.wait(); |
| 260 | |
Victor Hsieh | 6e34038 | 2021-08-13 12:18:02 -0700 | [diff] [blame] | 261 | let parcel_fds = parse_compiler_args(&authfs, compiler_args)?; |
| 262 | let oat_file: &File = parcel_fds.oat.as_ref(); |
| 263 | let vdex_file: &File = parcel_fds.vdex.as_ref(); |
| 264 | let image_file: &File = parcel_fds.image.as_ref(); |
Victor Hsieh | 51789de | 2021-08-06 16:50:49 -0700 | [diff] [blame] | 265 | |
| 266 | match jail_result { |
Victor Hsieh | 6e34038 | 2021-08-13 12:18:02 -0700 | [diff] [blame] | 267 | Ok(()) => Ok(CompilerOutput::Digests { |
Victor Hsieh | 9ed2718 | 2021-08-25 15:52:42 -0700 | [diff] [blame] | 268 | oat: fsverity::measure(oat_file.as_raw_fd())?, |
| 269 | vdex: fsverity::measure(vdex_file.as_raw_fd())?, |
| 270 | image: fsverity::measure(image_file.as_raw_fd())?, |
Victor Hsieh | 6e34038 | 2021-08-13 12:18:02 -0700 | [diff] [blame] | 271 | }), |
Victor Hsieh | 51789de | 2021-08-06 16:50:49 -0700 | [diff] [blame] | 272 | Err(minijail::Error::ReturnCode(exit_code)) => { |
Victor Hsieh | 6e34038 | 2021-08-13 12:18:02 -0700 | [diff] [blame] | 273 | error!("dex2oat failed with exit code {}", exit_code); |
| 274 | Ok(CompilerOutput::ExitCode(exit_code as i8)) |
Victor Hsieh | 51789de | 2021-08-06 16:50:49 -0700 | [diff] [blame] | 275 | } |
| 276 | Err(e) => { |
| 277 | bail!("Unexpected minijail error: {}", e) |
| 278 | } |
| 279 | } |
| 280 | } |
| 281 | |
Victor Hsieh | 6e34038 | 2021-08-13 12:18:02 -0700 | [diff] [blame] | 282 | fn parse_compiler_args( |
| 283 | authfs: &Strong<dyn IAuthFs>, |
| 284 | args: &[String], |
| 285 | ) -> Result<CompilerOutputParcelFds> { |
| 286 | const OAT_FD_PREFIX: &str = "--oat-fd="; |
| 287 | const VDEX_FD_PREFIX: &str = "--output-vdex-fd="; |
| 288 | const IMAGE_FD_PREFIX: &str = "--image-fd="; |
| 289 | const APP_IMAGE_FD_PREFIX: &str = "--app-image-fd="; |
| 290 | |
| 291 | let mut oat = None; |
| 292 | let mut vdex = None; |
| 293 | let mut image = None; |
| 294 | |
| 295 | for arg in args { |
| 296 | if let Some(value) = arg.strip_prefix(OAT_FD_PREFIX) { |
| 297 | let fd = value.parse::<RawFd>().context("Invalid --oat-fd flag")?; |
| 298 | debug_assert!(oat.is_none()); |
| 299 | oat = Some(authfs.openFile(fd, false)?); |
| 300 | } else if let Some(value) = arg.strip_prefix(VDEX_FD_PREFIX) { |
| 301 | let fd = value.parse::<RawFd>().context("Invalid --output-vdex-fd flag")?; |
| 302 | debug_assert!(vdex.is_none()); |
| 303 | vdex = Some(authfs.openFile(fd, false)?); |
| 304 | } else if let Some(value) = arg.strip_prefix(IMAGE_FD_PREFIX) { |
| 305 | let fd = value.parse::<RawFd>().context("Invalid --image-fd flag")?; |
| 306 | debug_assert!(image.is_none()); |
| 307 | image = Some(authfs.openFile(fd, false)?); |
| 308 | } else if let Some(value) = arg.strip_prefix(APP_IMAGE_FD_PREFIX) { |
| 309 | let fd = value.parse::<RawFd>().context("Invalid --app-image-fd flag")?; |
| 310 | debug_assert!(image.is_none()); |
| 311 | image = Some(authfs.openFile(fd, false)?); |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | Ok(CompilerOutputParcelFds { |
| 316 | oat: oat.ok_or_else(|| anyhow!("Missing --oat-fd"))?, |
| 317 | vdex: vdex.ok_or_else(|| anyhow!("Missing --vdex-fd"))?, |
| 318 | image: image.ok_or_else(|| anyhow!("Missing --image-fd or --app-image-fd"))?, |
| 319 | }) |
| 320 | } |
| 321 | |
Victor Hsieh | 13333e8 | 2021-09-03 15:17:32 -0700 | [diff] [blame] | 322 | fn build_authfs_config(fd_annotation: &FdAnnotation) -> AuthFsConfig { |
Victor Hsieh | 51789de | 2021-08-06 16:50:49 -0700 | [diff] [blame] | 323 | AuthFsConfig { |
Victor Hsieh | f996869 | 2021-11-18 11:34:39 -0800 | [diff] [blame] | 324 | port: FD_SERVER_PORT, |
Victor Hsieh | 13333e8 | 2021-09-03 15:17:32 -0700 | [diff] [blame] | 325 | inputFdAnnotations: fd_annotation |
| 326 | .input_fds |
Victor Hsieh | 51789de | 2021-08-06 16:50:49 -0700 | [diff] [blame] | 327 | .iter() |
Victor Hsieh | 13333e8 | 2021-09-03 15:17:32 -0700 | [diff] [blame] | 328 | .map(|fd| InputFdAnnotation { fd: *fd }) |
Victor Hsieh | 51789de | 2021-08-06 16:50:49 -0700 | [diff] [blame] | 329 | .collect(), |
Victor Hsieh | 13333e8 | 2021-09-03 15:17:32 -0700 | [diff] [blame] | 330 | outputFdAnnotations: fd_annotation |
| 331 | .output_fds |
Victor Hsieh | 51789de | 2021-08-06 16:50:49 -0700 | [diff] [blame] | 332 | .iter() |
Victor Hsieh | 13333e8 | 2021-09-03 15:17:32 -0700 | [diff] [blame] | 333 | .map(|fd| OutputFdAnnotation { fd: *fd }) |
Victor Hsieh | 51789de | 2021-08-06 16:50:49 -0700 | [diff] [blame] | 334 | .collect(), |
Victor Hsieh | f996869 | 2021-11-18 11:34:39 -0800 | [diff] [blame] | 335 | ..Default::default() |
Victor Hsieh | 51789de | 2021-08-06 16:50:49 -0700 | [diff] [blame] | 336 | } |
| 337 | } |
| 338 | |
| 339 | fn open_authfs_files_for_fd_mapping( |
| 340 | authfs: &Strong<dyn IAuthFs>, |
| 341 | config: &AuthFsConfig, |
| 342 | ) -> Result<Vec<(ParcelFileDescriptor, PseudoRawFd)>> { |
| 343 | let mut fd_mapping = Vec::new(); |
| 344 | |
| 345 | let results: Result<Vec<_>> = config |
| 346 | .inputFdAnnotations |
| 347 | .iter() |
| 348 | .map(|annotation| Ok((authfs.openFile(annotation.fd, false)?, annotation.fd))) |
| 349 | .collect(); |
| 350 | fd_mapping.append(&mut results?); |
| 351 | |
| 352 | let results: Result<Vec<_>> = config |
| 353 | .outputFdAnnotations |
| 354 | .iter() |
| 355 | .map(|annotation| Ok((authfs.openFile(annotation.fd, true)?, annotation.fd))) |
| 356 | .collect(); |
| 357 | fd_mapping.append(&mut results?); |
| 358 | |
| 359 | Ok(fd_mapping) |
| 360 | } |
| 361 | |
| 362 | fn spawn_jailed_task( |
| 363 | executable: &Path, |
| 364 | args: &[String], |
| 365 | fd_mapping: Vec<(ParcelFileDescriptor, PseudoRawFd)>, |
| 366 | ) -> Result<Minijail> { |
| 367 | // TODO(b/185175567): Run in a more restricted sandbox. |
| 368 | let jail = Minijail::new()?; |
| 369 | let preserve_fds: Vec<_> = fd_mapping.iter().map(|(f, id)| (f.as_raw_fd(), *id)).collect(); |
| 370 | let _pid = jail.run_remap(executable, preserve_fds.as_slice(), args)?; |
| 371 | Ok(jail) |
| 372 | } |