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 | 3189af0 | 2021-09-30 17:51:19 +0100 | [diff] [blame] | 22 | use binder_common::new_binder_exception; |
Alan Stokes | 126fd51 | 2021-12-16 15:00:01 +0000 | [diff] [blame] | 23 | use compos_common::binder::to_binder_result; |
Victor Hsieh | 9ed2718 | 2021-08-25 15:52:42 -0700 | [diff] [blame] | 24 | use log::warn; |
| 25 | use std::default::Default; |
Victor Hsieh | ec38ae2 | 2022-02-10 00:06:26 +0000 | [diff] [blame] | 26 | use std::fs::read_dir; |
| 27 | use std::path::{Path, PathBuf}; |
Alan Stokes | 183d7d3 | 2021-12-08 16:10:45 +0000 | [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 | 611cc94 | 2022-02-01 10:16:21 +0000 | [diff] [blame] | 33 | use crate::dice::Dice; |
Victor Hsieh | ec38ae2 | 2022-02-10 00:06:26 +0000 | [diff] [blame] | 34 | use crate::signing_key::DiceSigningKey; |
Victor Hsieh | 51789de | 2021-08-06 16:50:49 -0700 | [diff] [blame] | 35 | use authfs_aidl_interface::aidl::com::android::virt::fs::IAuthFsService::IAuthFsService; |
Victor Hsieh | 23f7359 | 2021-08-06 18:08:24 -0700 | [diff] [blame] | 36 | use compos_aidl_interface::aidl::com::android::compos::{ |
| 37 | CompOsKeyData::CompOsKeyData, |
Alan Stokes | 2d2e4db | 2022-01-28 16:41:52 +0000 | [diff] [blame] | 38 | ICompOsService::{BnCompOsService, CompilationMode::CompilationMode, ICompOsService}, |
Victor Hsieh | 272aa24 | 2021-02-01 14:19:20 -0800 | [diff] [blame] | 39 | }; |
Victor Hsieh | 272aa24 | 2021-02-01 14:19:20 -0800 | [diff] [blame] | 40 | use compos_aidl_interface::binder::{ |
Alan Stokes | 3189af0 | 2021-09-30 17:51:19 +0100 | [diff] [blame] | 41 | BinderFeatures, ExceptionCode, Interface, Result as BinderResult, Strong, |
Victor Hsieh | 272aa24 | 2021-02-01 14:19:20 -0800 | [diff] [blame] | 42 | }; |
Alan Stokes | 46a1dff | 2021-12-14 10:56:05 +0000 | [diff] [blame] | 43 | use compos_common::odrefresh::ODREFRESH_PATH; |
Victor Hsieh | 272aa24 | 2021-02-01 14:19:20 -0800 | [diff] [blame] | 44 | |
Victor Hsieh | ebb1d90 | 2021-08-06 13:00:18 -0700 | [diff] [blame] | 45 | const AUTHFS_SERVICE_NAME: &str = "authfs_service"; |
Alan Stokes | 9e2c5d5 | 2021-07-21 11:29:10 +0100 | [diff] [blame] | 46 | |
Victor Hsieh | a64194b | 2021-08-06 17:43:36 -0700 | [diff] [blame] | 47 | /// Constructs a binder object that implements ICompOsService. |
Victor Hsieh | 9ebf7ee | 2021-09-03 16:14:14 -0700 | [diff] [blame] | 48 | pub fn new_binder() -> Result<Strong<dyn ICompOsService>> { |
Victor Hsieh | 23f7359 | 2021-08-06 18:08:24 -0700 | [diff] [blame] | 49 | let service = CompOsService { |
Victor Hsieh | f996869 | 2021-11-18 11:34:39 -0800 | [diff] [blame] | 50 | odrefresh_path: PathBuf::from(ODREFRESH_PATH), |
Alan Stokes | 611cc94 | 2022-02-01 10:16:21 +0000 | [diff] [blame] | 51 | signing_key: DiceSigningKey::new(Dice::new()?), |
Alan Stokes | 183d7d3 | 2021-12-08 16:10:45 +0000 | [diff] [blame] | 52 | key_blob: RwLock::new(Vec::new()), |
Victor Hsieh | 23f7359 | 2021-08-06 18:08:24 -0700 | [diff] [blame] | 53 | }; |
Victor Hsieh | a64194b | 2021-08-06 17:43:36 -0700 | [diff] [blame] | 54 | Ok(BnCompOsService::new_binder(service, BinderFeatures::default())) |
Alan Stokes | 9e2c5d5 | 2021-07-21 11:29:10 +0100 | [diff] [blame] | 55 | } |
| 56 | |
Victor Hsieh | a64194b | 2021-08-06 17:43:36 -0700 | [diff] [blame] | 57 | struct CompOsService { |
Victor Hsieh | f996869 | 2021-11-18 11:34:39 -0800 | [diff] [blame] | 58 | odrefresh_path: PathBuf, |
Alan Stokes | 611cc94 | 2022-02-01 10:16:21 +0000 | [diff] [blame] | 59 | signing_key: DiceSigningKey, |
Alan Stokes | 183d7d3 | 2021-12-08 16:10:45 +0000 | [diff] [blame] | 60 | key_blob: RwLock<Vec<u8>>, |
Victor Hsieh | 272aa24 | 2021-02-01 14:19:20 -0800 | [diff] [blame] | 61 | } |
| 62 | |
Victor Hsieh | a64194b | 2021-08-06 17:43:36 -0700 | [diff] [blame] | 63 | impl Interface for CompOsService {} |
Victor Hsieh | 272aa24 | 2021-02-01 14:19:20 -0800 | [diff] [blame] | 64 | |
Victor Hsieh | a64194b | 2021-08-06 17:43:36 -0700 | [diff] [blame] | 65 | impl ICompOsService for CompOsService { |
Victor Hsieh | 8fd03f0 | 2021-08-24 17:23:01 -0700 | [diff] [blame] | 66 | fn initializeSigningKey(&self, key_blob: &[u8]) -> BinderResult<()> { |
| 67 | let mut w = self.key_blob.write().unwrap(); |
| 68 | if w.is_empty() { |
| 69 | *w = Vec::from(key_blob); |
| 70 | Ok(()) |
| 71 | } else { |
| 72 | Err(new_binder_exception(ExceptionCode::ILLEGAL_STATE, "Cannot re-initialize the key")) |
| 73 | } |
| 74 | } |
| 75 | |
Victor Hsieh | f996869 | 2021-11-18 11:34:39 -0800 | [diff] [blame] | 76 | fn odrefresh( |
| 77 | &self, |
Alan Stokes | 2d2e4db | 2022-01-28 16:41:52 +0000 | [diff] [blame] | 78 | compilation_mode: CompilationMode, |
Victor Hsieh | f996869 | 2021-11-18 11:34:39 -0800 | [diff] [blame] | 79 | system_dir_fd: i32, |
| 80 | output_dir_fd: i32, |
Alan Stokes | 9646db9 | 2021-12-14 13:22:33 +0000 | [diff] [blame] | 81 | staging_dir_fd: i32, |
| 82 | target_dir_name: &str, |
Victor Hsieh | f996869 | 2021-11-18 11:34:39 -0800 | [diff] [blame] | 83 | zygote_arch: &str, |
Victor Hsieh | 9bfbc5f | 2021-12-16 11:45:10 -0800 | [diff] [blame] | 84 | system_server_compiler_filter: &str, |
Alan Stokes | 9646db9 | 2021-12-14 13:22:33 +0000 | [diff] [blame] | 85 | ) -> BinderResult<i8> { |
Victor Hsieh | ec38ae2 | 2022-02-10 00:06:26 +0000 | [diff] [blame] | 86 | let key = &*self.key_blob.read().unwrap(); |
| 87 | if key.is_empty() { |
| 88 | return Err(new_binder_exception( |
| 89 | ExceptionCode::ILLEGAL_STATE, |
| 90 | "Key is not initialized", |
| 91 | )); |
| 92 | } |
| 93 | |
Alan Stokes | 126fd51 | 2021-12-16 15:00:01 +0000 | [diff] [blame] | 94 | let context = to_binder_result(OdrefreshContext::new( |
Alan Stokes | 2d2e4db | 2022-01-28 16:41:52 +0000 | [diff] [blame] | 95 | compilation_mode, |
Victor Hsieh | f996869 | 2021-11-18 11:34:39 -0800 | [diff] [blame] | 96 | system_dir_fd, |
| 97 | output_dir_fd, |
Alan Stokes | 9646db9 | 2021-12-14 13:22:33 +0000 | [diff] [blame] | 98 | staging_dir_fd, |
Alan Stokes | 46a1dff | 2021-12-14 10:56:05 +0000 | [diff] [blame] | 99 | target_dir_name, |
Victor Hsieh | f996869 | 2021-11-18 11:34:39 -0800 | [diff] [blame] | 100 | zygote_arch, |
Victor Hsieh | 9bfbc5f | 2021-12-16 11:45:10 -0800 | [diff] [blame] | 101 | system_server_compiler_filter, |
Alan Stokes | 126fd51 | 2021-12-16 15:00:01 +0000 | [diff] [blame] | 102 | ))?; |
Alan Stokes | 46a1dff | 2021-12-14 10:56:05 +0000 | [diff] [blame] | 103 | |
| 104 | let authfs_service = get_authfs_service()?; |
Alan Stokes | 126fd51 | 2021-12-16 15:00:01 +0000 | [diff] [blame] | 105 | let exit_code = to_binder_result( |
Victor Hsieh | ec38ae2 | 2022-02-10 00:06:26 +0000 | [diff] [blame] | 106 | odrefresh(&self.odrefresh_path, context, authfs_service, |output_dir| { |
| 107 | // authfs only shows us the files we created, so it's ok to just sign everything |
| 108 | // under the output directory. |
| 109 | let mut artifact_signer = ArtifactSigner::new(&output_dir); |
| 110 | add_artifacts(&output_dir, &mut artifact_signer)?; |
| 111 | |
Alan Stokes | 16fb855 | 2022-02-10 15:07:27 +0000 | [diff] [blame^] | 112 | artifact_signer.write_info_and_signature(&output_dir.join("compos.info")) |
Victor Hsieh | ec38ae2 | 2022-02-10 00:06:26 +0000 | [diff] [blame] | 113 | }) |
| 114 | .context("odrefresh failed"), |
Alan Stokes | 126fd51 | 2021-12-16 15:00:01 +0000 | [diff] [blame] | 115 | )?; |
Alan Stokes | 46a1dff | 2021-12-14 10:56:05 +0000 | [diff] [blame] | 116 | Ok(exit_code as i8) |
Victor Hsieh | f996869 | 2021-11-18 11:34:39 -0800 | [diff] [blame] | 117 | } |
| 118 | |
Victor Hsieh | 23f7359 | 2021-08-06 18:08:24 -0700 | [diff] [blame] | 119 | fn generateSigningKey(&self) -> BinderResult<CompOsKeyData> { |
Alan Stokes | 223a746 | 2022-01-20 14:12:24 +0000 | [diff] [blame] | 120 | to_binder_result(self.signing_key.generate()) |
Victor Hsieh | 23f7359 | 2021-08-06 18:08:24 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | fn verifySigningKey(&self, key_blob: &[u8], public_key: &[u8]) -> BinderResult<bool> { |
Alan Stokes | 223a746 | 2022-01-20 14:12:24 +0000 | [diff] [blame] | 124 | Ok(if let Err(e) = self.signing_key.verify(key_blob, public_key) { |
Alan Stokes | 126fd51 | 2021-12-16 15:00:01 +0000 | [diff] [blame] | 125 | warn!("Signing key verification failed: {:?}", e); |
Victor Hsieh | 23f7359 | 2021-08-06 18:08:24 -0700 | [diff] [blame] | 126 | false |
| 127 | } else { |
| 128 | true |
| 129 | }) |
| 130 | } |
Alan Stokes | 16fb855 | 2022-02-10 15:07:27 +0000 | [diff] [blame^] | 131 | |
| 132 | fn getPublicKey(&self) -> BinderResult<Vec<u8>> { |
| 133 | to_binder_result(compos_key::get_public_key()) |
| 134 | } |
Victor Hsieh | 272aa24 | 2021-02-01 14:19:20 -0800 | [diff] [blame] | 135 | } |
Victor Hsieh | ebb1d90 | 2021-08-06 13:00:18 -0700 | [diff] [blame] | 136 | |
| 137 | fn get_authfs_service() -> BinderResult<Strong<dyn IAuthFsService>> { |
| 138 | Ok(authfs_aidl_interface::binder::get_interface(AUTHFS_SERVICE_NAME)?) |
| 139 | } |
Victor Hsieh | ec38ae2 | 2022-02-10 00:06:26 +0000 | [diff] [blame] | 140 | |
| 141 | fn add_artifacts(target_dir: &Path, artifact_signer: &mut ArtifactSigner) -> Result<()> { |
| 142 | for entry in |
| 143 | read_dir(&target_dir).with_context(|| format!("Traversing {}", target_dir.display()))? |
| 144 | { |
| 145 | let entry = entry?; |
| 146 | let file_type = entry.file_type()?; |
| 147 | if file_type.is_dir() { |
| 148 | add_artifacts(&entry.path(), artifact_signer)?; |
| 149 | } else if file_type.is_file() { |
| 150 | artifact_signer.add_artifact(&entry.path())?; |
| 151 | } else { |
| 152 | // authfs shouldn't create anything else, but just in case |
| 153 | bail!("Unexpected file type in artifacts: {:?}", entry); |
| 154 | } |
| 155 | } |
| 156 | Ok(()) |
| 157 | } |