Victor Hsieh | 272aa24 | 2021-02-01 14:19:20 -0800 | [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 | 51789de | 2021-08-06 16:50:49 -0700 | [diff] [blame] | 17 | //! compsvc is a service to run compilation tasks in a PVM upon request. It is able to set up |
Victor Hsieh | ebb1d90 | 2021-08-06 13:00:18 -0700 | [diff] [blame] | 18 | //! file descriptors backed by authfs (via authfs_service) and pass the file descriptors to the |
Victor Hsieh | 51789de | 2021-08-06 16:50:49 -0700 | [diff] [blame] | 19 | //! actual compiler. |
Victor Hsieh | 272aa24 | 2021-02-01 14:19:20 -0800 | [diff] [blame] | 20 | |
Victor Hsieh | ec38ae2 | 2022-02-10 00:06:26 +0000 | [diff] [blame] | 21 | use anyhow::{bail, Context, Result}; |
Alan Stokes | 0fc6ce5 | 2022-08-02 17:01:48 +0100 | [diff] [blame] | 22 | use log::{error, info}; |
Victor Hsieh | cb6d66b | 2022-05-10 16:12:06 -0700 | [diff] [blame] | 23 | use rustutils::system_properties; |
Victor Hsieh | 9ed2718 | 2021-08-25 15:52:42 -0700 | [diff] [blame] | 24 | use std::default::Default; |
Victor Hsieh | ec38ae2 | 2022-02-10 00:06:26 +0000 | [diff] [blame] | 25 | use std::fs::read_dir; |
Victor Hsieh | cb6d66b | 2022-05-10 16:12:06 -0700 | [diff] [blame] | 26 | use std::iter::zip; |
Victor Hsieh | ec38ae2 | 2022-02-10 00:06:26 +0000 | [diff] [blame] | 27 | use std::path::{Path, PathBuf}; |
Victor Hsieh | cb6d66b | 2022-05-10 16:12:06 -0700 | [diff] [blame] | 28 | use std::sync::RwLock; |
Victor Hsieh | 272aa24 | 2021-02-01 14:19:20 -0800 | [diff] [blame] | 29 | |
Victor Hsieh | ec38ae2 | 2022-02-10 00:06:26 +0000 | [diff] [blame] | 30 | use crate::artifact_signer::ArtifactSigner; |
Victor Hsieh | 616f822 | 2022-01-14 13:06:32 -0800 | [diff] [blame] | 31 | use crate::compilation::{odrefresh, OdrefreshContext}; |
Alan Stokes | 16fb855 | 2022-02-10 15:07:27 +0000 | [diff] [blame] | 32 | use crate::compos_key; |
Alan Stokes | 0e82b50 | 2022-08-08 14:44:48 +0100 | [diff] [blame] | 33 | use binder::{BinderFeatures, ExceptionCode, Interface, Result as BinderResult, Status, Strong}; |
Alan Stokes | 6542fdd | 2022-02-17 15:21:46 +0000 | [diff] [blame] | 34 | use compos_aidl_interface::aidl::com::android::compos::ICompOsService::{ |
| 35 | BnCompOsService, CompilationMode::CompilationMode, ICompOsService, |
Victor Hsieh | 272aa24 | 2021-02-01 14:19:20 -0800 | [diff] [blame] | 36 | }; |
Alan Stokes | 6542fdd | 2022-02-17 15:21:46 +0000 | [diff] [blame] | 37 | use compos_common::binder::to_binder_result; |
Victor Hsieh | cb6d66b | 2022-05-10 16:12:06 -0700 | [diff] [blame] | 38 | use compos_common::odrefresh::{is_system_property_interesting, ODREFRESH_PATH}; |
Victor Hsieh | 272aa24 | 2021-02-01 14:19:20 -0800 | [diff] [blame] | 39 | |
Victor Hsieh | ebb1d90 | 2021-08-06 13:00:18 -0700 | [diff] [blame] | 40 | const AUTHFS_SERVICE_NAME: &str = "authfs_service"; |
Alan Stokes | 9e2c5d5 | 2021-07-21 11:29:10 +0100 | [diff] [blame] | 41 | |
Victor Hsieh | a64194b | 2021-08-06 17:43:36 -0700 | [diff] [blame] | 42 | /// Constructs a binder object that implements ICompOsService. |
Victor Hsieh | 9ebf7ee | 2021-09-03 16:14:14 -0700 | [diff] [blame] | 43 | pub fn new_binder() -> Result<Strong<dyn ICompOsService>> { |
Victor Hsieh | cb6d66b | 2022-05-10 16:12:06 -0700 | [diff] [blame] | 44 | let service = CompOsService { |
| 45 | odrefresh_path: PathBuf::from(ODREFRESH_PATH), |
| 46 | initialized: RwLock::new(None), |
| 47 | }; |
Victor Hsieh | a64194b | 2021-08-06 17:43:36 -0700 | [diff] [blame] | 48 | Ok(BnCompOsService::new_binder(service, BinderFeatures::default())) |
Alan Stokes | 9e2c5d5 | 2021-07-21 11:29:10 +0100 | [diff] [blame] | 49 | } |
| 50 | |
Victor Hsieh | a64194b | 2021-08-06 17:43:36 -0700 | [diff] [blame] | 51 | struct CompOsService { |
Victor Hsieh | f996869 | 2021-11-18 11:34:39 -0800 | [diff] [blame] | 52 | odrefresh_path: PathBuf, |
Victor Hsieh | cb6d66b | 2022-05-10 16:12:06 -0700 | [diff] [blame] | 53 | |
| 54 | /// A locked protected tri-state. |
| 55 | /// * None: uninitialized |
| 56 | /// * Some(true): initialized successfully |
| 57 | /// * Some(false): failed to initialize |
| 58 | initialized: RwLock<Option<bool>>, |
Victor Hsieh | 272aa24 | 2021-02-01 14:19:20 -0800 | [diff] [blame] | 59 | } |
| 60 | |
Victor Hsieh | a64194b | 2021-08-06 17:43:36 -0700 | [diff] [blame] | 61 | impl Interface for CompOsService {} |
Victor Hsieh | 272aa24 | 2021-02-01 14:19:20 -0800 | [diff] [blame] | 62 | |
Victor Hsieh | a64194b | 2021-08-06 17:43:36 -0700 | [diff] [blame] | 63 | impl ICompOsService for CompOsService { |
Victor Hsieh | cb6d66b | 2022-05-10 16:12:06 -0700 | [diff] [blame] | 64 | fn initializeSystemProperties(&self, names: &[String], values: &[String]) -> BinderResult<()> { |
| 65 | let mut initialized = self.initialized.write().unwrap(); |
| 66 | if initialized.is_some() { |
Andrew Walbran | dcf9d58 | 2022-08-03 11:25:24 +0000 | [diff] [blame] | 67 | return Err(Status::new_exception_str( |
Victor Hsieh | cb6d66b | 2022-05-10 16:12:06 -0700 | [diff] [blame] | 68 | ExceptionCode::ILLEGAL_STATE, |
Andrew Walbran | dcf9d58 | 2022-08-03 11:25:24 +0000 | [diff] [blame] | 69 | Some(format!("Already initialized: {:?}", initialized)), |
Victor Hsieh | cb6d66b | 2022-05-10 16:12:06 -0700 | [diff] [blame] | 70 | )); |
| 71 | } |
| 72 | *initialized = Some(false); |
| 73 | |
| 74 | if names.len() != values.len() { |
Andrew Walbran | dcf9d58 | 2022-08-03 11:25:24 +0000 | [diff] [blame] | 75 | return Err(Status::new_exception_str( |
Victor Hsieh | cb6d66b | 2022-05-10 16:12:06 -0700 | [diff] [blame] | 76 | ExceptionCode::ILLEGAL_ARGUMENT, |
Andrew Walbran | dcf9d58 | 2022-08-03 11:25:24 +0000 | [diff] [blame] | 77 | Some(format!( |
Victor Hsieh | cb6d66b | 2022-05-10 16:12:06 -0700 | [diff] [blame] | 78 | "Received inconsistent number of keys ({}) and values ({})", |
| 79 | names.len(), |
| 80 | values.len() |
Andrew Walbran | dcf9d58 | 2022-08-03 11:25:24 +0000 | [diff] [blame] | 81 | )), |
Victor Hsieh | cb6d66b | 2022-05-10 16:12:06 -0700 | [diff] [blame] | 82 | )); |
| 83 | } |
| 84 | for (name, value) in zip(names, values) { |
| 85 | if !is_system_property_interesting(name) { |
Andrew Walbran | dcf9d58 | 2022-08-03 11:25:24 +0000 | [diff] [blame] | 86 | return Err(Status::new_exception_str( |
Victor Hsieh | cb6d66b | 2022-05-10 16:12:06 -0700 | [diff] [blame] | 87 | ExceptionCode::ILLEGAL_ARGUMENT, |
Andrew Walbran | dcf9d58 | 2022-08-03 11:25:24 +0000 | [diff] [blame] | 88 | Some(format!("Received invalid system property {}", &name)), |
Victor Hsieh | cb6d66b | 2022-05-10 16:12:06 -0700 | [diff] [blame] | 89 | )); |
| 90 | } |
| 91 | let result = system_properties::write(name, value); |
| 92 | if result.is_err() { |
| 93 | error!("Failed to setprop {}", &name); |
| 94 | return to_binder_result(result); |
| 95 | } |
| 96 | } |
| 97 | *initialized = Some(true); |
| 98 | Ok(()) |
| 99 | } |
| 100 | |
Victor Hsieh | f996869 | 2021-11-18 11:34:39 -0800 | [diff] [blame] | 101 | fn odrefresh( |
| 102 | &self, |
Alan Stokes | 2d2e4db | 2022-01-28 16:41:52 +0000 | [diff] [blame] | 103 | compilation_mode: CompilationMode, |
Victor Hsieh | f996869 | 2021-11-18 11:34:39 -0800 | [diff] [blame] | 104 | system_dir_fd: i32, |
Victor Hsieh | 64d8862 | 2022-09-21 17:32:00 -0700 | [diff] [blame] | 105 | system_ext_dir_fd: i32, |
Victor Hsieh | f996869 | 2021-11-18 11:34:39 -0800 | [diff] [blame] | 106 | output_dir_fd: i32, |
Alan Stokes | 9646db9 | 2021-12-14 13:22:33 +0000 | [diff] [blame] | 107 | staging_dir_fd: i32, |
| 108 | target_dir_name: &str, |
Victor Hsieh | f996869 | 2021-11-18 11:34:39 -0800 | [diff] [blame] | 109 | zygote_arch: &str, |
Victor Hsieh | 9bfbc5f | 2021-12-16 11:45:10 -0800 | [diff] [blame] | 110 | system_server_compiler_filter: &str, |
Alan Stokes | 9646db9 | 2021-12-14 13:22:33 +0000 | [diff] [blame] | 111 | ) -> BinderResult<i8> { |
Victor Hsieh | cb6d66b | 2022-05-10 16:12:06 -0700 | [diff] [blame] | 112 | let initialized = *self.initialized.read().unwrap(); |
| 113 | if !initialized.unwrap_or(false) { |
Andrew Walbran | dcf9d58 | 2022-08-03 11:25:24 +0000 | [diff] [blame] | 114 | return Err(Status::new_exception_str( |
Victor Hsieh | cb6d66b | 2022-05-10 16:12:06 -0700 | [diff] [blame] | 115 | ExceptionCode::ILLEGAL_STATE, |
Andrew Walbran | dcf9d58 | 2022-08-03 11:25:24 +0000 | [diff] [blame] | 116 | Some("Service has not been initialized"), |
Victor Hsieh | cb6d66b | 2022-05-10 16:12:06 -0700 | [diff] [blame] | 117 | )); |
| 118 | } |
| 119 | |
Alan Stokes | 01b3ef0 | 2022-09-22 17:43:24 +0100 | [diff] [blame] | 120 | let context = OdrefreshContext::new( |
Alan Stokes | 2d2e4db | 2022-01-28 16:41:52 +0000 | [diff] [blame] | 121 | compilation_mode, |
Victor Hsieh | f996869 | 2021-11-18 11:34:39 -0800 | [diff] [blame] | 122 | system_dir_fd, |
Victor Hsieh | 64d8862 | 2022-09-21 17:32:00 -0700 | [diff] [blame] | 123 | if system_ext_dir_fd >= 0 { Some(system_ext_dir_fd) } else { None }, |
Victor Hsieh | f996869 | 2021-11-18 11:34:39 -0800 | [diff] [blame] | 124 | output_dir_fd, |
Alan Stokes | 9646db9 | 2021-12-14 13:22:33 +0000 | [diff] [blame] | 125 | staging_dir_fd, |
Alan Stokes | 46a1dff | 2021-12-14 10:56:05 +0000 | [diff] [blame] | 126 | target_dir_name, |
Victor Hsieh | f996869 | 2021-11-18 11:34:39 -0800 | [diff] [blame] | 127 | zygote_arch, |
Victor Hsieh | 9bfbc5f | 2021-12-16 11:45:10 -0800 | [diff] [blame] | 128 | system_server_compiler_filter, |
Alan Stokes | 01b3ef0 | 2022-09-22 17:43:24 +0100 | [diff] [blame] | 129 | ); |
Alan Stokes | 46a1dff | 2021-12-14 10:56:05 +0000 | [diff] [blame] | 130 | |
Alan Stokes | 01b3ef0 | 2022-09-22 17:43:24 +0100 | [diff] [blame] | 131 | to_binder_result(context.and_then(|c| self.do_odrefresh(c))) |
Victor Hsieh | f996869 | 2021-11-18 11:34:39 -0800 | [diff] [blame] | 132 | } |
| 133 | |
Alan Stokes | 16fb855 | 2022-02-10 15:07:27 +0000 | [diff] [blame] | 134 | fn getPublicKey(&self) -> BinderResult<Vec<u8>> { |
| 135 | to_binder_result(compos_key::get_public_key()) |
| 136 | } |
Alan Stokes | 5430eca | 2022-03-21 14:02:09 +0000 | [diff] [blame] | 137 | |
| 138 | fn getAttestationChain(&self) -> BinderResult<Vec<u8>> { |
| 139 | to_binder_result(compos_key::get_attestation_chain()) |
| 140 | } |
Alan Stokes | 7140377 | 2022-06-21 14:56:28 +0100 | [diff] [blame] | 141 | |
| 142 | fn quit(&self) -> BinderResult<()> { |
Alan Stokes | 7140377 | 2022-06-21 14:56:28 +0100 | [diff] [blame] | 143 | // When our process exits, Microdroid will shut down the VM. |
Alan Stokes | 0fc6ce5 | 2022-08-02 17:01:48 +0100 | [diff] [blame] | 144 | info!("Received quit request, exiting"); |
Alan Stokes | 7140377 | 2022-06-21 14:56:28 +0100 | [diff] [blame] | 145 | std::process::exit(0); |
| 146 | } |
Victor Hsieh | 272aa24 | 2021-02-01 14:19:20 -0800 | [diff] [blame] | 147 | } |
Victor Hsieh | ebb1d90 | 2021-08-06 13:00:18 -0700 | [diff] [blame] | 148 | |
Alan Stokes | 01b3ef0 | 2022-09-22 17:43:24 +0100 | [diff] [blame] | 149 | impl CompOsService { |
| 150 | fn do_odrefresh(&self, context: OdrefreshContext) -> Result<i8> { |
| 151 | let authfs_service = binder::get_interface(AUTHFS_SERVICE_NAME) |
| 152 | .context("Unable to connect to AuthFS service")?; |
| 153 | let exit_code = odrefresh(&self.odrefresh_path, context, authfs_service, |output_dir| { |
| 154 | // authfs only shows us the files we created, so it's ok to just sign everything |
| 155 | // under the output directory. |
| 156 | let mut artifact_signer = ArtifactSigner::new(&output_dir); |
| 157 | add_artifacts(&output_dir, &mut artifact_signer)?; |
| 158 | |
| 159 | artifact_signer.write_info_and_signature(&output_dir.join("compos.info")) |
| 160 | }) |
| 161 | .context("odrefresh failed")?; |
| 162 | Ok(exit_code as i8) |
| 163 | } |
| 164 | } |
| 165 | |
Victor Hsieh | ec38ae2 | 2022-02-10 00:06:26 +0000 | [diff] [blame] | 166 | fn add_artifacts(target_dir: &Path, artifact_signer: &mut ArtifactSigner) -> Result<()> { |
| 167 | for entry in |
| 168 | read_dir(&target_dir).with_context(|| format!("Traversing {}", target_dir.display()))? |
| 169 | { |
| 170 | let entry = entry?; |
| 171 | let file_type = entry.file_type()?; |
| 172 | if file_type.is_dir() { |
| 173 | add_artifacts(&entry.path(), artifact_signer)?; |
| 174 | } else if file_type.is_file() { |
| 175 | artifact_signer.add_artifact(&entry.path())?; |
| 176 | } else { |
| 177 | // authfs shouldn't create anything else, but just in case |
| 178 | bail!("Unexpected file type in artifacts: {:?}", entry); |
| 179 | } |
| 180 | } |
| 181 | Ok(()) |
| 182 | } |