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 | |
Alan Stokes | 126fd51 | 2021-12-16 15:00:01 +0000 | [diff] [blame] | 21 | use anyhow::{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 | 51789de | 2021-08-06 16:50:49 -0700 | [diff] [blame] | 26 | use std::path::PathBuf; |
Alan Stokes | 183d7d3 | 2021-12-08 16:10:45 +0000 | [diff] [blame] | 27 | use std::sync::RwLock; |
Victor Hsieh | 272aa24 | 2021-02-01 14:19:20 -0800 | [diff] [blame] | 28 | |
Alan Stokes | 46a1dff | 2021-12-14 10:56:05 +0000 | [diff] [blame] | 29 | use crate::compilation::{compile_cmd, odrefresh, CompilerOutput, OdrefreshContext}; |
| 30 | use crate::compos_key_service::{CompOsKeyService, Signer}; |
Alan Stokes | 71cc11e | 2022-01-17 10:39:05 +0000 | [diff] [blame] | 31 | use crate::dice::Dice; |
Victor Hsieh | 9ed2718 | 2021-08-25 15:52:42 -0700 | [diff] [blame] | 32 | use crate::fsverity; |
Victor Hsieh | 51789de | 2021-08-06 16:50:49 -0700 | [diff] [blame] | 33 | use authfs_aidl_interface::aidl::com::android::virt::fs::IAuthFsService::IAuthFsService; |
Victor Hsieh | 23f7359 | 2021-08-06 18:08:24 -0700 | [diff] [blame] | 34 | use compos_aidl_interface::aidl::com::android::compos::{ |
| 35 | CompOsKeyData::CompOsKeyData, |
Victor Hsieh | 9ed2718 | 2021-08-25 15:52:42 -0700 | [diff] [blame] | 36 | CompilationResult::CompilationResult, |
Victor Hsieh | 13333e8 | 2021-09-03 15:17:32 -0700 | [diff] [blame] | 37 | FdAnnotation::FdAnnotation, |
Victor Hsieh | 23f7359 | 2021-08-06 18:08:24 -0700 | [diff] [blame] | 38 | ICompOsService::{BnCompOsService, 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"; |
Victor Hsieh | 51789de | 2021-08-06 16:50:49 -0700 | [diff] [blame] | 46 | const DEX2OAT_PATH: &str = "/apex/com.android.art/bin/dex2oat64"; |
Alan Stokes | 9e2c5d5 | 2021-07-21 11:29:10 +0100 | [diff] [blame] | 47 | |
Victor Hsieh | a64194b | 2021-08-06 17:43:36 -0700 | [diff] [blame] | 48 | /// Constructs a binder object that implements ICompOsService. |
Victor Hsieh | 9ebf7ee | 2021-09-03 16:14:14 -0700 | [diff] [blame] | 49 | pub fn new_binder() -> Result<Strong<dyn ICompOsService>> { |
Victor Hsieh | 23f7359 | 2021-08-06 18:08:24 -0700 | [diff] [blame] | 50 | let service = CompOsService { |
| 51 | dex2oat_path: PathBuf::from(DEX2OAT_PATH), |
Victor Hsieh | f996869 | 2021-11-18 11:34:39 -0800 | [diff] [blame] | 52 | odrefresh_path: PathBuf::from(ODREFRESH_PATH), |
Victor Hsieh | 9ebf7ee | 2021-09-03 16:14:14 -0700 | [diff] [blame] | 53 | key_service: CompOsKeyService::new()?, |
Alan Stokes | 183d7d3 | 2021-12-08 16:10:45 +0000 | [diff] [blame] | 54 | key_blob: RwLock::new(Vec::new()), |
Victor Hsieh | 23f7359 | 2021-08-06 18:08:24 -0700 | [diff] [blame] | 55 | }; |
Victor Hsieh | a64194b | 2021-08-06 17:43:36 -0700 | [diff] [blame] | 56 | Ok(BnCompOsService::new_binder(service, BinderFeatures::default())) |
Alan Stokes | 9e2c5d5 | 2021-07-21 11:29:10 +0100 | [diff] [blame] | 57 | } |
| 58 | |
Victor Hsieh | a64194b | 2021-08-06 17:43:36 -0700 | [diff] [blame] | 59 | struct CompOsService { |
Victor Hsieh | 51789de | 2021-08-06 16:50:49 -0700 | [diff] [blame] | 60 | dex2oat_path: PathBuf, |
Victor Hsieh | f996869 | 2021-11-18 11:34:39 -0800 | [diff] [blame] | 61 | odrefresh_path: PathBuf, |
Victor Hsieh | a64194b | 2021-08-06 17:43:36 -0700 | [diff] [blame] | 62 | key_service: CompOsKeyService, |
Alan Stokes | 183d7d3 | 2021-12-08 16:10:45 +0000 | [diff] [blame] | 63 | key_blob: RwLock<Vec<u8>>, |
Victor Hsieh | 272aa24 | 2021-02-01 14:19:20 -0800 | [diff] [blame] | 64 | } |
| 65 | |
Victor Hsieh | 9ed2718 | 2021-08-25 15:52:42 -0700 | [diff] [blame] | 66 | impl CompOsService { |
| 67 | fn generate_raw_fsverity_signature( |
| 68 | &self, |
Victor Hsieh | 9ed2718 | 2021-08-25 15:52:42 -0700 | [diff] [blame] | 69 | fsverity_digest: &fsverity::Sha256Digest, |
Alan Stokes | 46a1dff | 2021-12-14 10:56:05 +0000 | [diff] [blame] | 70 | ) -> BinderResult<Vec<u8>> { |
Victor Hsieh | 9ed2718 | 2021-08-25 15:52:42 -0700 | [diff] [blame] | 71 | let formatted_digest = fsverity::to_formatted_digest(fsverity_digest); |
Alan Stokes | 126fd51 | 2021-12-16 15:00:01 +0000 | [diff] [blame] | 72 | to_binder_result(self.new_signer()?.sign(&formatted_digest[..])) |
Alan Stokes | 46a1dff | 2021-12-14 10:56:05 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | fn new_signer(&self) -> BinderResult<Signer> { |
| 76 | let key = &*self.key_blob.read().unwrap(); |
| 77 | if key.is_empty() { |
| 78 | Err(new_binder_exception(ExceptionCode::ILLEGAL_STATE, "Key is not initialized")) |
| 79 | } else { |
| 80 | Ok(self.key_service.new_signer(key)) |
| 81 | } |
Victor Hsieh | 9ed2718 | 2021-08-25 15:52:42 -0700 | [diff] [blame] | 82 | } |
Alan Stokes | 71cc11e | 2022-01-17 10:39:05 +0000 | [diff] [blame] | 83 | |
| 84 | fn get_boot_certificate_chain(&self) -> Result<Vec<u8>> { |
| 85 | let dice = Dice::new()?; |
| 86 | dice.get_boot_certificate_chain() |
| 87 | } |
Victor Hsieh | 9ed2718 | 2021-08-25 15:52:42 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Victor Hsieh | a64194b | 2021-08-06 17:43:36 -0700 | [diff] [blame] | 90 | impl Interface for CompOsService {} |
Victor Hsieh | 272aa24 | 2021-02-01 14:19:20 -0800 | [diff] [blame] | 91 | |
Victor Hsieh | a64194b | 2021-08-06 17:43:36 -0700 | [diff] [blame] | 92 | impl ICompOsService for CompOsService { |
Victor Hsieh | 8fd03f0 | 2021-08-24 17:23:01 -0700 | [diff] [blame] | 93 | fn initializeSigningKey(&self, key_blob: &[u8]) -> BinderResult<()> { |
| 94 | let mut w = self.key_blob.write().unwrap(); |
| 95 | if w.is_empty() { |
| 96 | *w = Vec::from(key_blob); |
| 97 | Ok(()) |
| 98 | } else { |
| 99 | Err(new_binder_exception(ExceptionCode::ILLEGAL_STATE, "Cannot re-initialize the key")) |
| 100 | } |
| 101 | } |
| 102 | |
Victor Hsieh | f996869 | 2021-11-18 11:34:39 -0800 | [diff] [blame] | 103 | fn odrefresh( |
| 104 | &self, |
| 105 | system_dir_fd: i32, |
| 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> { |
Alan Stokes | 126fd51 | 2021-12-16 15:00:01 +0000 | [diff] [blame] | 112 | let context = to_binder_result(OdrefreshContext::new( |
Victor Hsieh | f996869 | 2021-11-18 11:34:39 -0800 | [diff] [blame] | 113 | system_dir_fd, |
| 114 | output_dir_fd, |
Alan Stokes | 9646db9 | 2021-12-14 13:22:33 +0000 | [diff] [blame] | 115 | staging_dir_fd, |
Alan Stokes | 46a1dff | 2021-12-14 10:56:05 +0000 | [diff] [blame] | 116 | target_dir_name, |
Victor Hsieh | f996869 | 2021-11-18 11:34:39 -0800 | [diff] [blame] | 117 | zygote_arch, |
Victor Hsieh | 9bfbc5f | 2021-12-16 11:45:10 -0800 | [diff] [blame] | 118 | system_server_compiler_filter, |
Alan Stokes | 126fd51 | 2021-12-16 15:00:01 +0000 | [diff] [blame] | 119 | ))?; |
Alan Stokes | 46a1dff | 2021-12-14 10:56:05 +0000 | [diff] [blame] | 120 | |
| 121 | let authfs_service = get_authfs_service()?; |
Alan Stokes | 126fd51 | 2021-12-16 15:00:01 +0000 | [diff] [blame] | 122 | let exit_code = to_binder_result( |
| 123 | odrefresh(&self.odrefresh_path, context, authfs_service, self.new_signer()?) |
| 124 | .context("odrefresh failed"), |
| 125 | )?; |
Alan Stokes | 46a1dff | 2021-12-14 10:56:05 +0000 | [diff] [blame] | 126 | Ok(exit_code as i8) |
Victor Hsieh | f996869 | 2021-11-18 11:34:39 -0800 | [diff] [blame] | 127 | } |
| 128 | |
Victor Hsieh | 3c044c4 | 2021-10-01 17:17:10 -0700 | [diff] [blame] | 129 | fn compile_cmd( |
Victor Hsieh | 13333e8 | 2021-09-03 15:17:32 -0700 | [diff] [blame] | 130 | &self, |
| 131 | args: &[String], |
| 132 | fd_annotation: &FdAnnotation, |
| 133 | ) -> BinderResult<CompilationResult> { |
Victor Hsieh | 51789de | 2021-08-06 16:50:49 -0700 | [diff] [blame] | 134 | let authfs_service = get_authfs_service()?; |
Alan Stokes | 126fd51 | 2021-12-16 15:00:01 +0000 | [diff] [blame] | 135 | let output = to_binder_result( |
| 136 | compile_cmd(&self.dex2oat_path, args, authfs_service, fd_annotation) |
| 137 | .context("Compilation failed"), |
| 138 | )?; |
Victor Hsieh | 6e34038 | 2021-08-13 12:18:02 -0700 | [diff] [blame] | 139 | match output { |
| 140 | CompilerOutput::Digests { oat, vdex, image } => { |
Alan Stokes | 46a1dff | 2021-12-14 10:56:05 +0000 | [diff] [blame] | 141 | let oat_signature = self.generate_raw_fsverity_signature(&oat)?; |
| 142 | let vdex_signature = self.generate_raw_fsverity_signature(&vdex)?; |
| 143 | let image_signature = self.generate_raw_fsverity_signature(&image)?; |
| 144 | Ok(CompilationResult { |
| 145 | exitCode: 0, |
| 146 | oatSignature: oat_signature, |
| 147 | vdexSignature: vdex_signature, |
| 148 | imageSignature: image_signature, |
| 149 | }) |
Victor Hsieh | 6e34038 | 2021-08-13 12:18:02 -0700 | [diff] [blame] | 150 | } |
Victor Hsieh | 9ed2718 | 2021-08-25 15:52:42 -0700 | [diff] [blame] | 151 | CompilerOutput::ExitCode(exit_code) => { |
| 152 | Ok(CompilationResult { exitCode: exit_code, ..Default::default() }) |
| 153 | } |
Victor Hsieh | 6e34038 | 2021-08-13 12:18:02 -0700 | [diff] [blame] | 154 | } |
Victor Hsieh | 272aa24 | 2021-02-01 14:19:20 -0800 | [diff] [blame] | 155 | } |
Victor Hsieh | 23f7359 | 2021-08-06 18:08:24 -0700 | [diff] [blame] | 156 | |
Victor Hsieh | 3c044c4 | 2021-10-01 17:17:10 -0700 | [diff] [blame] | 157 | fn compile(&self, _marshaled: &[u8], _fd_annotation: &FdAnnotation) -> BinderResult<i8> { |
| 158 | Err(new_binder_exception(ExceptionCode::UNSUPPORTED_OPERATION, "Not yet implemented")) |
| 159 | } |
| 160 | |
Victor Hsieh | 23f7359 | 2021-08-06 18:08:24 -0700 | [diff] [blame] | 161 | fn generateSigningKey(&self) -> BinderResult<CompOsKeyData> { |
Alan Stokes | 126fd51 | 2021-12-16 15:00:01 +0000 | [diff] [blame] | 162 | to_binder_result(self.key_service.generate()) |
Victor Hsieh | 23f7359 | 2021-08-06 18:08:24 -0700 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | fn verifySigningKey(&self, key_blob: &[u8], public_key: &[u8]) -> BinderResult<bool> { |
Alan Stokes | 183d7d3 | 2021-12-08 16:10:45 +0000 | [diff] [blame] | 166 | Ok(if let Err(e) = self.key_service.verify(key_blob, public_key) { |
Alan Stokes | 126fd51 | 2021-12-16 15:00:01 +0000 | [diff] [blame] | 167 | warn!("Signing key verification failed: {:?}", e); |
Victor Hsieh | 23f7359 | 2021-08-06 18:08:24 -0700 | [diff] [blame] | 168 | false |
| 169 | } else { |
| 170 | true |
| 171 | }) |
| 172 | } |
Alan Stokes | 71cc11e | 2022-01-17 10:39:05 +0000 | [diff] [blame] | 173 | |
| 174 | fn getBootCertificateChain(&self) -> BinderResult<Vec<u8>> { |
| 175 | to_binder_result(self.get_boot_certificate_chain()) |
| 176 | } |
Victor Hsieh | 272aa24 | 2021-02-01 14:19:20 -0800 | [diff] [blame] | 177 | } |
Victor Hsieh | ebb1d90 | 2021-08-06 13:00:18 -0700 | [diff] [blame] | 178 | |
| 179 | fn get_authfs_service() -> BinderResult<Strong<dyn IAuthFsService>> { |
| 180 | Ok(authfs_aidl_interface::binder::get_interface(AUTHFS_SERVICE_NAME)?) |
| 181 | } |