blob: 9d2f1dc9481d5dfb688b911d1a309f862145c878 [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;
Victor Hsieh8fd03f02021-08-24 17:23:01 -070027use std::sync::{Arc, RwLock};
Victor Hsieh272aa242021-02-01 14:19:20 -080028
Victor Hsieh3c044c42021-10-01 17:17:10 -070029use crate::compilation::{compile_cmd, CompilerOutput};
Victor Hsieh23f73592021-08-06 18:08:24 -070030use crate::compos_key_service::CompOsKeyService;
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};
42
Victor Hsiehebb1d902021-08-06 13:00:18 -070043const AUTHFS_SERVICE_NAME: &str = "authfs_service";
Victor Hsieh51789de2021-08-06 16:50:49 -070044const DEX2OAT_PATH: &str = "/apex/com.android.art/bin/dex2oat64";
Alan Stokes9e2c5d52021-07-21 11:29:10 +010045
Victor Hsieha64194b2021-08-06 17:43:36 -070046/// Constructs a binder object that implements ICompOsService.
Victor Hsieh9ebf7ee2021-09-03 16:14:14 -070047pub fn new_binder() -> Result<Strong<dyn ICompOsService>> {
Victor Hsieh23f73592021-08-06 18:08:24 -070048 let service = CompOsService {
49 dex2oat_path: PathBuf::from(DEX2OAT_PATH),
Victor Hsieh9ebf7ee2021-09-03 16:14:14 -070050 key_service: CompOsKeyService::new()?,
Victor Hsieh8fd03f02021-08-24 17:23:01 -070051 key_blob: Arc::new(RwLock::new(Vec::new())),
Victor Hsieh23f73592021-08-06 18:08:24 -070052 };
Victor Hsieha64194b2021-08-06 17:43:36 -070053 Ok(BnCompOsService::new_binder(service, BinderFeatures::default()))
Alan Stokes9e2c5d52021-07-21 11:29:10 +010054}
55
Victor Hsieha64194b2021-08-06 17:43:36 -070056struct CompOsService {
Victor Hsieh51789de2021-08-06 16:50:49 -070057 dex2oat_path: PathBuf,
Victor Hsieha64194b2021-08-06 17:43:36 -070058 key_service: CompOsKeyService,
Victor Hsieh8fd03f02021-08-24 17:23:01 -070059 key_blob: Arc<RwLock<Vec<u8>>>,
Victor Hsieh272aa242021-02-01 14:19:20 -080060}
61
Victor Hsieh9ed27182021-08-25 15:52:42 -070062impl CompOsService {
63 fn generate_raw_fsverity_signature(
64 &self,
65 key_blob: &[u8],
66 fsverity_digest: &fsverity::Sha256Digest,
67 ) -> Vec<u8> {
68 let formatted_digest = fsverity::to_formatted_digest(fsverity_digest);
69 self.key_service.do_sign(key_blob, &formatted_digest[..]).unwrap_or_else(|e| {
70 warn!("Failed to sign the fsverity digest, returning empty signature. Error: {}", e);
71 Vec::new()
72 })
73 }
74}
75
Victor Hsieha64194b2021-08-06 17:43:36 -070076impl Interface for CompOsService {}
Victor Hsieh272aa242021-02-01 14:19:20 -080077
Victor Hsieha64194b2021-08-06 17:43:36 -070078impl ICompOsService for CompOsService {
Victor Hsieh8fd03f02021-08-24 17:23:01 -070079 fn initializeSigningKey(&self, key_blob: &[u8]) -> BinderResult<()> {
80 let mut w = self.key_blob.write().unwrap();
81 if w.is_empty() {
82 *w = Vec::from(key_blob);
83 Ok(())
84 } else {
85 Err(new_binder_exception(ExceptionCode::ILLEGAL_STATE, "Cannot re-initialize the key"))
86 }
87 }
88
Victor Hsieh18775b12021-10-12 17:42:48 -070089 fn initializeClasspaths(
90 &self,
91 boot_classpath: &str,
92 dex2oat_boot_classpath: &str,
Victor Hsieh64290a52021-11-17 13:34:46 -080093 system_server_classpath: &str,
Victor Hsieh18775b12021-10-12 17:42:48 -070094 ) -> BinderResult<()> {
95 // TODO(198211396): Implement correctly.
96 env::set_var("BOOTCLASSPATH", boot_classpath);
97 env::set_var("DEX2OATBOOTCLASSPATH", dex2oat_boot_classpath);
Victor Hsieh64290a52021-11-17 13:34:46 -080098 env::set_var("SYSTEMSERVERCLASSPATH", system_server_classpath);
Victor Hsieh18775b12021-10-12 17:42:48 -070099 Ok(())
100 }
101
Victor Hsieh3c044c42021-10-01 17:17:10 -0700102 fn compile_cmd(
Victor Hsieh13333e82021-09-03 15:17:32 -0700103 &self,
104 args: &[String],
105 fd_annotation: &FdAnnotation,
106 ) -> BinderResult<CompilationResult> {
Victor Hsieh51789de2021-08-06 16:50:49 -0700107 let authfs_service = get_authfs_service()?;
Victor Hsieh13333e82021-09-03 15:17:32 -0700108 let output =
Victor Hsieh3c044c42021-10-01 17:17:10 -0700109 compile_cmd(&self.dex2oat_path, args, authfs_service, fd_annotation).map_err(|e| {
Victor Hsieh13333e82021-09-03 15:17:32 -0700110 new_binder_exception(
111 ExceptionCode::SERVICE_SPECIFIC,
112 format!("Compilation failed: {}", e),
113 )
114 })?;
Victor Hsieh6e340382021-08-13 12:18:02 -0700115 match output {
116 CompilerOutput::Digests { oat, vdex, image } => {
Victor Hsieh9ed27182021-08-25 15:52:42 -0700117 let key = &*self.key_blob.read().unwrap();
118 if key.is_empty() {
119 Err(new_binder_exception(
120 ExceptionCode::ILLEGAL_STATE,
121 "Key is not initialized",
122 ))
123 } else {
124 let oat_signature = self.generate_raw_fsverity_signature(key, &oat);
125 let vdex_signature = self.generate_raw_fsverity_signature(key, &vdex);
126 let image_signature = self.generate_raw_fsverity_signature(key, &image);
127 Ok(CompilationResult {
128 exitCode: 0,
129 oatSignature: oat_signature,
130 vdexSignature: vdex_signature,
131 imageSignature: image_signature,
132 })
133 }
Victor Hsieh6e340382021-08-13 12:18:02 -0700134 }
Victor Hsieh9ed27182021-08-25 15:52:42 -0700135 CompilerOutput::ExitCode(exit_code) => {
136 Ok(CompilationResult { exitCode: exit_code, ..Default::default() })
137 }
Victor Hsieh6e340382021-08-13 12:18:02 -0700138 }
Victor Hsieh272aa242021-02-01 14:19:20 -0800139 }
Victor Hsieh23f73592021-08-06 18:08:24 -0700140
Victor Hsieh3c044c42021-10-01 17:17:10 -0700141 fn compile(&self, _marshaled: &[u8], _fd_annotation: &FdAnnotation) -> BinderResult<i8> {
142 Err(new_binder_exception(ExceptionCode::UNSUPPORTED_OPERATION, "Not yet implemented"))
143 }
144
Victor Hsieh23f73592021-08-06 18:08:24 -0700145 fn generateSigningKey(&self) -> BinderResult<CompOsKeyData> {
146 self.key_service
147 .do_generate()
148 .map_err(|e| new_binder_exception(ExceptionCode::ILLEGAL_STATE, e.to_string()))
149 }
150
151 fn verifySigningKey(&self, key_blob: &[u8], public_key: &[u8]) -> BinderResult<bool> {
152 Ok(if let Err(e) = self.key_service.do_verify(key_blob, public_key) {
153 warn!("Signing key verification failed: {}", e.to_string());
154 false
155 } else {
156 true
157 })
158 }
159
Victor Hsieh8fd03f02021-08-24 17:23:01 -0700160 fn sign(&self, data: &[u8]) -> BinderResult<Vec<u8>> {
161 let key = &*self.key_blob.read().unwrap();
162 if key.is_empty() {
163 Err(new_binder_exception(ExceptionCode::ILLEGAL_STATE, "Key is not initialized"))
164 } else {
165 self.key_service
166 .do_sign(key, data)
167 .map_err(|e| new_binder_exception(ExceptionCode::ILLEGAL_STATE, e.to_string()))
168 }
Victor Hsieh23f73592021-08-06 18:08:24 -0700169 }
Victor Hsieh272aa242021-02-01 14:19:20 -0800170}
Victor Hsiehebb1d902021-08-06 13:00:18 -0700171
172fn get_authfs_service() -> BinderResult<Strong<dyn IAuthFsService>> {
173 Ok(authfs_aidl_interface::binder::get_interface(AUTHFS_SERVICE_NAME)?)
174}