blob: aa4b9bd02be9bfde239306645e66a44eac0e0001 [file] [log] [blame]
Victor Hsieh272aa242021-02-01 14:19:20 -08001/*
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 Hsieh51789de2021-08-06 16:50:49 -070017//! compsvc is a service to run compilation tasks in a PVM upon request. It is able to set up
Victor Hsiehebb1d902021-08-06 13:00:18 -070018//! file descriptors backed by authfs (via authfs_service) and pass the file descriptors to the
Victor Hsieh51789de2021-08-06 16:50:49 -070019//! actual compiler.
Victor Hsieh272aa242021-02-01 14:19:20 -080020
Victor Hsieha64194b2021-08-06 17:43:36 -070021use anyhow::Result;
Alan Stokes3189af02021-09-30 17:51:19 +010022use binder_common::new_binder_exception;
Victor Hsieh9ed27182021-08-25 15:52:42 -070023use log::warn;
24use std::default::Default;
Victor Hsieh18775b12021-10-12 17:42:48 -070025use std::env;
Victor Hsieh51789de2021-08-06 16:50:49 -070026use std::path::PathBuf;
Alan Stokes183d7d32021-12-08 16:10:45 +000027use std::sync::RwLock;
Victor Hsieh272aa242021-02-01 14:19:20 -080028
Alan Stokes46a1dff2021-12-14 10:56:05 +000029use crate::compilation::{compile_cmd, odrefresh, CompilerOutput, OdrefreshContext};
30use crate::compos_key_service::{CompOsKeyService, Signer};
Victor Hsieh9ed27182021-08-25 15:52:42 -070031use crate::fsverity;
Victor Hsieh51789de2021-08-06 16:50:49 -070032use authfs_aidl_interface::aidl::com::android::virt::fs::IAuthFsService::IAuthFsService;
Victor Hsieh23f73592021-08-06 18:08:24 -070033use compos_aidl_interface::aidl::com::android::compos::{
34 CompOsKeyData::CompOsKeyData,
Victor Hsieh9ed27182021-08-25 15:52:42 -070035 CompilationResult::CompilationResult,
Victor Hsieh13333e82021-09-03 15:17:32 -070036 FdAnnotation::FdAnnotation,
Victor Hsieh23f73592021-08-06 18:08:24 -070037 ICompOsService::{BnCompOsService, ICompOsService},
Victor Hsieh272aa242021-02-01 14:19:20 -080038};
Victor Hsieh272aa242021-02-01 14:19:20 -080039use compos_aidl_interface::binder::{
Alan Stokes3189af02021-09-30 17:51:19 +010040 BinderFeatures, ExceptionCode, Interface, Result as BinderResult, Strong,
Victor Hsieh272aa242021-02-01 14:19:20 -080041};
Alan Stokes46a1dff2021-12-14 10:56:05 +000042use compos_common::odrefresh::ODREFRESH_PATH;
Victor Hsieh272aa242021-02-01 14:19:20 -080043
Victor Hsiehebb1d902021-08-06 13:00:18 -070044const AUTHFS_SERVICE_NAME: &str = "authfs_service";
Victor Hsieh51789de2021-08-06 16:50:49 -070045const DEX2OAT_PATH: &str = "/apex/com.android.art/bin/dex2oat64";
Alan Stokes9e2c5d52021-07-21 11:29:10 +010046
Victor Hsieha64194b2021-08-06 17:43:36 -070047/// Constructs a binder object that implements ICompOsService.
Victor Hsieh9ebf7ee2021-09-03 16:14:14 -070048pub fn new_binder() -> Result<Strong<dyn ICompOsService>> {
Victor Hsieh23f73592021-08-06 18:08:24 -070049 let service = CompOsService {
50 dex2oat_path: PathBuf::from(DEX2OAT_PATH),
Victor Hsiehf9968692021-11-18 11:34:39 -080051 odrefresh_path: PathBuf::from(ODREFRESH_PATH),
Victor Hsieh9ebf7ee2021-09-03 16:14:14 -070052 key_service: CompOsKeyService::new()?,
Alan Stokes183d7d32021-12-08 16:10:45 +000053 key_blob: RwLock::new(Vec::new()),
Victor Hsieh23f73592021-08-06 18:08:24 -070054 };
Victor Hsieha64194b2021-08-06 17:43:36 -070055 Ok(BnCompOsService::new_binder(service, BinderFeatures::default()))
Alan Stokes9e2c5d52021-07-21 11:29:10 +010056}
57
Victor Hsieha64194b2021-08-06 17:43:36 -070058struct CompOsService {
Victor Hsieh51789de2021-08-06 16:50:49 -070059 dex2oat_path: PathBuf,
Victor Hsiehf9968692021-11-18 11:34:39 -080060 odrefresh_path: PathBuf,
Victor Hsieha64194b2021-08-06 17:43:36 -070061 key_service: CompOsKeyService,
Alan Stokes183d7d32021-12-08 16:10:45 +000062 key_blob: RwLock<Vec<u8>>,
Victor Hsieh272aa242021-02-01 14:19:20 -080063}
64
Victor Hsieh9ed27182021-08-25 15:52:42 -070065impl CompOsService {
66 fn generate_raw_fsverity_signature(
67 &self,
Victor Hsieh9ed27182021-08-25 15:52:42 -070068 fsverity_digest: &fsverity::Sha256Digest,
Alan Stokes46a1dff2021-12-14 10:56:05 +000069 ) -> BinderResult<Vec<u8>> {
Victor Hsieh9ed27182021-08-25 15:52:42 -070070 let formatted_digest = fsverity::to_formatted_digest(fsverity_digest);
Alan Stokes46a1dff2021-12-14 10:56:05 +000071 self.new_signer()?
72 .sign(&formatted_digest[..])
73 .map_err(|e| new_binder_exception(ExceptionCode::SERVICE_SPECIFIC, e.to_string()))
74 }
75
76 fn new_signer(&self) -> BinderResult<Signer> {
77 let key = &*self.key_blob.read().unwrap();
78 if key.is_empty() {
79 Err(new_binder_exception(ExceptionCode::ILLEGAL_STATE, "Key is not initialized"))
80 } else {
81 Ok(self.key_service.new_signer(key))
82 }
Victor Hsieh9ed27182021-08-25 15:52:42 -070083 }
84}
85
Victor Hsieha64194b2021-08-06 17:43:36 -070086impl Interface for CompOsService {}
Victor Hsieh272aa242021-02-01 14:19:20 -080087
Victor Hsieha64194b2021-08-06 17:43:36 -070088impl ICompOsService for CompOsService {
Victor Hsieh8fd03f02021-08-24 17:23:01 -070089 fn initializeSigningKey(&self, key_blob: &[u8]) -> BinderResult<()> {
90 let mut w = self.key_blob.write().unwrap();
91 if w.is_empty() {
92 *w = Vec::from(key_blob);
93 Ok(())
94 } else {
95 Err(new_binder_exception(ExceptionCode::ILLEGAL_STATE, "Cannot re-initialize the key"))
96 }
97 }
98
Victor Hsieh18775b12021-10-12 17:42:48 -070099 fn initializeClasspaths(
100 &self,
101 boot_classpath: &str,
102 dex2oat_boot_classpath: &str,
Victor Hsieh64290a52021-11-17 13:34:46 -0800103 system_server_classpath: &str,
Victor Hsieh18775b12021-10-12 17:42:48 -0700104 ) -> BinderResult<()> {
105 // TODO(198211396): Implement correctly.
106 env::set_var("BOOTCLASSPATH", boot_classpath);
107 env::set_var("DEX2OATBOOTCLASSPATH", dex2oat_boot_classpath);
Victor Hsieh64290a52021-11-17 13:34:46 -0800108 env::set_var("SYSTEMSERVERCLASSPATH", system_server_classpath);
Victor Hsieh18775b12021-10-12 17:42:48 -0700109 Ok(())
110 }
111
Victor Hsiehf9968692021-11-18 11:34:39 -0800112 fn odrefresh(
113 &self,
114 system_dir_fd: i32,
115 output_dir_fd: i32,
Alan Stokes9646db92021-12-14 13:22:33 +0000116 staging_dir_fd: i32,
117 target_dir_name: &str,
Victor Hsiehf9968692021-11-18 11:34:39 -0800118 zygote_arch: &str,
Alan Stokes9646db92021-12-14 13:22:33 +0000119 ) -> BinderResult<i8> {
Alan Stokes46a1dff2021-12-14 10:56:05 +0000120 let context = OdrefreshContext::new(
Victor Hsiehf9968692021-11-18 11:34:39 -0800121 system_dir_fd,
122 output_dir_fd,
Alan Stokes9646db92021-12-14 13:22:33 +0000123 staging_dir_fd,
Alan Stokes46a1dff2021-12-14 10:56:05 +0000124 target_dir_name,
Victor Hsiehf9968692021-11-18 11:34:39 -0800125 zygote_arch,
Victor Hsiehf9968692021-11-18 11:34:39 -0800126 )
Alan Stokes46a1dff2021-12-14 10:56:05 +0000127 .map_err(|e| new_binder_exception(ExceptionCode::ILLEGAL_ARGUMENT, e.to_string()))?;
128
129 let authfs_service = get_authfs_service()?;
130 let exit_code =
131 odrefresh(&self.odrefresh_path, context, authfs_service, self.new_signer()?).map_err(
132 |e| {
133 warn!("odrefresh failed: {:?}", e);
134 new_binder_exception(
135 ExceptionCode::SERVICE_SPECIFIC,
136 format!("odrefresh failed: {}", e),
137 )
138 },
139 )?;
140 Ok(exit_code as i8)
Victor Hsiehf9968692021-11-18 11:34:39 -0800141 }
142
Victor Hsieh3c044c42021-10-01 17:17:10 -0700143 fn compile_cmd(
Victor Hsieh13333e82021-09-03 15:17:32 -0700144 &self,
145 args: &[String],
146 fd_annotation: &FdAnnotation,
147 ) -> BinderResult<CompilationResult> {
Victor Hsieh51789de2021-08-06 16:50:49 -0700148 let authfs_service = get_authfs_service()?;
Victor Hsieh13333e82021-09-03 15:17:32 -0700149 let output =
Victor Hsieh3c044c42021-10-01 17:17:10 -0700150 compile_cmd(&self.dex2oat_path, args, authfs_service, fd_annotation).map_err(|e| {
Victor Hsieh13333e82021-09-03 15:17:32 -0700151 new_binder_exception(
152 ExceptionCode::SERVICE_SPECIFIC,
153 format!("Compilation failed: {}", e),
154 )
155 })?;
Victor Hsieh6e340382021-08-13 12:18:02 -0700156 match output {
157 CompilerOutput::Digests { oat, vdex, image } => {
Alan Stokes46a1dff2021-12-14 10:56:05 +0000158 let oat_signature = self.generate_raw_fsverity_signature(&oat)?;
159 let vdex_signature = self.generate_raw_fsverity_signature(&vdex)?;
160 let image_signature = self.generate_raw_fsverity_signature(&image)?;
161 Ok(CompilationResult {
162 exitCode: 0,
163 oatSignature: oat_signature,
164 vdexSignature: vdex_signature,
165 imageSignature: image_signature,
166 })
Victor Hsieh6e340382021-08-13 12:18:02 -0700167 }
Victor Hsieh9ed27182021-08-25 15:52:42 -0700168 CompilerOutput::ExitCode(exit_code) => {
169 Ok(CompilationResult { exitCode: exit_code, ..Default::default() })
170 }
Victor Hsieh6e340382021-08-13 12:18:02 -0700171 }
Victor Hsieh272aa242021-02-01 14:19:20 -0800172 }
Victor Hsieh23f73592021-08-06 18:08:24 -0700173
Victor Hsieh3c044c42021-10-01 17:17:10 -0700174 fn compile(&self, _marshaled: &[u8], _fd_annotation: &FdAnnotation) -> BinderResult<i8> {
175 Err(new_binder_exception(ExceptionCode::UNSUPPORTED_OPERATION, "Not yet implemented"))
176 }
177
Victor Hsieh23f73592021-08-06 18:08:24 -0700178 fn generateSigningKey(&self) -> BinderResult<CompOsKeyData> {
179 self.key_service
Alan Stokes183d7d32021-12-08 16:10:45 +0000180 .generate()
Victor Hsieh23f73592021-08-06 18:08:24 -0700181 .map_err(|e| new_binder_exception(ExceptionCode::ILLEGAL_STATE, e.to_string()))
182 }
183
184 fn verifySigningKey(&self, key_blob: &[u8], public_key: &[u8]) -> BinderResult<bool> {
Alan Stokes183d7d32021-12-08 16:10:45 +0000185 Ok(if let Err(e) = self.key_service.verify(key_blob, public_key) {
Victor Hsieh23f73592021-08-06 18:08:24 -0700186 warn!("Signing key verification failed: {}", e.to_string());
187 false
188 } else {
189 true
190 })
191 }
192
Victor Hsieh8fd03f02021-08-24 17:23:01 -0700193 fn sign(&self, data: &[u8]) -> BinderResult<Vec<u8>> {
Alan Stokes46a1dff2021-12-14 10:56:05 +0000194 self.new_signer()?
195 .sign(data)
196 .map_err(|e| new_binder_exception(ExceptionCode::ILLEGAL_STATE, e.to_string()))
Victor Hsieh23f73592021-08-06 18:08:24 -0700197 }
Victor Hsieh272aa242021-02-01 14:19:20 -0800198}
Victor Hsiehebb1d902021-08-06 13:00:18 -0700199
200fn get_authfs_service() -> BinderResult<Strong<dyn IAuthFsService>> {
201 Ok(authfs_aidl_interface::binder::get_interface(AUTHFS_SERVICE_NAME)?)
202}