blob: 5a2c3ca1e9900ae04c5ee23aced2874e6e0ca486 [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
Alan Stokes126fd512021-12-16 15:00:01 +000021use anyhow::{Context, Result};
Alan Stokes3189af02021-09-30 17:51:19 +010022use binder_common::new_binder_exception;
Alan Stokes126fd512021-12-16 15:00:01 +000023use compos_common::binder::to_binder_result;
Victor Hsieh9ed27182021-08-25 15:52:42 -070024use log::warn;
25use std::default::Default;
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 Stokes126fd512021-12-16 15:00:01 +000071 to_binder_result(self.new_signer()?.sign(&formatted_digest[..]))
Alan Stokes46a1dff2021-12-14 10:56:05 +000072 }
73
74 fn new_signer(&self) -> BinderResult<Signer> {
75 let key = &*self.key_blob.read().unwrap();
76 if key.is_empty() {
77 Err(new_binder_exception(ExceptionCode::ILLEGAL_STATE, "Key is not initialized"))
78 } else {
79 Ok(self.key_service.new_signer(key))
80 }
Victor Hsieh9ed27182021-08-25 15:52:42 -070081 }
82}
83
Victor Hsieha64194b2021-08-06 17:43:36 -070084impl Interface for CompOsService {}
Victor Hsieh272aa242021-02-01 14:19:20 -080085
Victor Hsieha64194b2021-08-06 17:43:36 -070086impl ICompOsService for CompOsService {
Victor Hsieh8fd03f02021-08-24 17:23:01 -070087 fn initializeSigningKey(&self, key_blob: &[u8]) -> BinderResult<()> {
88 let mut w = self.key_blob.write().unwrap();
89 if w.is_empty() {
90 *w = Vec::from(key_blob);
91 Ok(())
92 } else {
93 Err(new_binder_exception(ExceptionCode::ILLEGAL_STATE, "Cannot re-initialize the key"))
94 }
95 }
96
Victor Hsiehf9968692021-11-18 11:34:39 -080097 fn odrefresh(
98 &self,
99 system_dir_fd: i32,
100 output_dir_fd: i32,
Alan Stokes9646db92021-12-14 13:22:33 +0000101 staging_dir_fd: i32,
102 target_dir_name: &str,
Victor Hsiehf9968692021-11-18 11:34:39 -0800103 zygote_arch: &str,
Victor Hsieh9bfbc5f2021-12-16 11:45:10 -0800104 system_server_compiler_filter: &str,
Alan Stokes9646db92021-12-14 13:22:33 +0000105 ) -> BinderResult<i8> {
Alan Stokes126fd512021-12-16 15:00:01 +0000106 let context = to_binder_result(OdrefreshContext::new(
Victor Hsiehf9968692021-11-18 11:34:39 -0800107 system_dir_fd,
108 output_dir_fd,
Alan Stokes9646db92021-12-14 13:22:33 +0000109 staging_dir_fd,
Alan Stokes46a1dff2021-12-14 10:56:05 +0000110 target_dir_name,
Victor Hsiehf9968692021-11-18 11:34:39 -0800111 zygote_arch,
Victor Hsieh9bfbc5f2021-12-16 11:45:10 -0800112 system_server_compiler_filter,
Alan Stokes126fd512021-12-16 15:00:01 +0000113 ))?;
Alan Stokes46a1dff2021-12-14 10:56:05 +0000114
115 let authfs_service = get_authfs_service()?;
Alan Stokes126fd512021-12-16 15:00:01 +0000116 let exit_code = to_binder_result(
117 odrefresh(&self.odrefresh_path, context, authfs_service, self.new_signer()?)
118 .context("odrefresh failed"),
119 )?;
Alan Stokes46a1dff2021-12-14 10:56:05 +0000120 Ok(exit_code as i8)
Victor Hsiehf9968692021-11-18 11:34:39 -0800121 }
122
Victor Hsieh3c044c42021-10-01 17:17:10 -0700123 fn compile_cmd(
Victor Hsieh13333e82021-09-03 15:17:32 -0700124 &self,
125 args: &[String],
126 fd_annotation: &FdAnnotation,
127 ) -> BinderResult<CompilationResult> {
Victor Hsieh51789de2021-08-06 16:50:49 -0700128 let authfs_service = get_authfs_service()?;
Alan Stokes126fd512021-12-16 15:00:01 +0000129 let output = to_binder_result(
130 compile_cmd(&self.dex2oat_path, args, authfs_service, fd_annotation)
131 .context("Compilation failed"),
132 )?;
Victor Hsieh6e340382021-08-13 12:18:02 -0700133 match output {
134 CompilerOutput::Digests { oat, vdex, image } => {
Alan Stokes46a1dff2021-12-14 10:56:05 +0000135 let oat_signature = self.generate_raw_fsverity_signature(&oat)?;
136 let vdex_signature = self.generate_raw_fsverity_signature(&vdex)?;
137 let image_signature = self.generate_raw_fsverity_signature(&image)?;
138 Ok(CompilationResult {
139 exitCode: 0,
140 oatSignature: oat_signature,
141 vdexSignature: vdex_signature,
142 imageSignature: image_signature,
143 })
Victor Hsieh6e340382021-08-13 12:18:02 -0700144 }
Victor Hsieh9ed27182021-08-25 15:52:42 -0700145 CompilerOutput::ExitCode(exit_code) => {
146 Ok(CompilationResult { exitCode: exit_code, ..Default::default() })
147 }
Victor Hsieh6e340382021-08-13 12:18:02 -0700148 }
Victor Hsieh272aa242021-02-01 14:19:20 -0800149 }
Victor Hsieh23f73592021-08-06 18:08:24 -0700150
Victor Hsieh3c044c42021-10-01 17:17:10 -0700151 fn compile(&self, _marshaled: &[u8], _fd_annotation: &FdAnnotation) -> BinderResult<i8> {
152 Err(new_binder_exception(ExceptionCode::UNSUPPORTED_OPERATION, "Not yet implemented"))
153 }
154
Victor Hsieh23f73592021-08-06 18:08:24 -0700155 fn generateSigningKey(&self) -> BinderResult<CompOsKeyData> {
Alan Stokes126fd512021-12-16 15:00:01 +0000156 to_binder_result(self.key_service.generate())
Victor Hsieh23f73592021-08-06 18:08:24 -0700157 }
158
159 fn verifySigningKey(&self, key_blob: &[u8], public_key: &[u8]) -> BinderResult<bool> {
Alan Stokes183d7d32021-12-08 16:10:45 +0000160 Ok(if let Err(e) = self.key_service.verify(key_blob, public_key) {
Alan Stokes126fd512021-12-16 15:00:01 +0000161 warn!("Signing key verification failed: {:?}", e);
Victor Hsieh23f73592021-08-06 18:08:24 -0700162 false
163 } else {
164 true
165 })
166 }
Victor Hsieh272aa242021-02-01 14:19:20 -0800167}
Victor Hsiehebb1d902021-08-06 13:00:18 -0700168
169fn get_authfs_service() -> BinderResult<Strong<dyn IAuthFsService>> {
170 Ok(authfs_aidl_interface::binder::get_interface(AUTHFS_SERVICE_NAME)?)
171}