blob: 2a5534c0e6cc548c458b01891a6366754a154b0d [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,
93 ) -> BinderResult<()> {
94 // TODO(198211396): Implement correctly.
95 env::set_var("BOOTCLASSPATH", boot_classpath);
96 env::set_var("DEX2OATBOOTCLASSPATH", dex2oat_boot_classpath);
97 Ok(())
98 }
99
Victor Hsieh3c044c42021-10-01 17:17:10 -0700100 fn compile_cmd(
Victor Hsieh13333e82021-09-03 15:17:32 -0700101 &self,
102 args: &[String],
103 fd_annotation: &FdAnnotation,
104 ) -> BinderResult<CompilationResult> {
Victor Hsieh51789de2021-08-06 16:50:49 -0700105 let authfs_service = get_authfs_service()?;
Victor Hsieh13333e82021-09-03 15:17:32 -0700106 let output =
Victor Hsieh3c044c42021-10-01 17:17:10 -0700107 compile_cmd(&self.dex2oat_path, args, authfs_service, fd_annotation).map_err(|e| {
Victor Hsieh13333e82021-09-03 15:17:32 -0700108 new_binder_exception(
109 ExceptionCode::SERVICE_SPECIFIC,
110 format!("Compilation failed: {}", e),
111 )
112 })?;
Victor Hsieh6e340382021-08-13 12:18:02 -0700113 match output {
114 CompilerOutput::Digests { oat, vdex, image } => {
Victor Hsieh9ed27182021-08-25 15:52:42 -0700115 let key = &*self.key_blob.read().unwrap();
116 if key.is_empty() {
117 Err(new_binder_exception(
118 ExceptionCode::ILLEGAL_STATE,
119 "Key is not initialized",
120 ))
121 } else {
122 let oat_signature = self.generate_raw_fsverity_signature(key, &oat);
123 let vdex_signature = self.generate_raw_fsverity_signature(key, &vdex);
124 let image_signature = self.generate_raw_fsverity_signature(key, &image);
125 Ok(CompilationResult {
126 exitCode: 0,
127 oatSignature: oat_signature,
128 vdexSignature: vdex_signature,
129 imageSignature: image_signature,
130 })
131 }
Victor Hsieh6e340382021-08-13 12:18:02 -0700132 }
Victor Hsieh9ed27182021-08-25 15:52:42 -0700133 CompilerOutput::ExitCode(exit_code) => {
134 Ok(CompilationResult { exitCode: exit_code, ..Default::default() })
135 }
Victor Hsieh6e340382021-08-13 12:18:02 -0700136 }
Victor Hsieh272aa242021-02-01 14:19:20 -0800137 }
Victor Hsieh23f73592021-08-06 18:08:24 -0700138
Victor Hsieh3c044c42021-10-01 17:17:10 -0700139 fn compile(&self, _marshaled: &[u8], _fd_annotation: &FdAnnotation) -> BinderResult<i8> {
140 Err(new_binder_exception(ExceptionCode::UNSUPPORTED_OPERATION, "Not yet implemented"))
141 }
142
Victor Hsieh23f73592021-08-06 18:08:24 -0700143 fn generateSigningKey(&self) -> BinderResult<CompOsKeyData> {
144 self.key_service
145 .do_generate()
146 .map_err(|e| new_binder_exception(ExceptionCode::ILLEGAL_STATE, e.to_string()))
147 }
148
149 fn verifySigningKey(&self, key_blob: &[u8], public_key: &[u8]) -> BinderResult<bool> {
150 Ok(if let Err(e) = self.key_service.do_verify(key_blob, public_key) {
151 warn!("Signing key verification failed: {}", e.to_string());
152 false
153 } else {
154 true
155 })
156 }
157
Victor Hsieh8fd03f02021-08-24 17:23:01 -0700158 fn sign(&self, data: &[u8]) -> BinderResult<Vec<u8>> {
159 let key = &*self.key_blob.read().unwrap();
160 if key.is_empty() {
161 Err(new_binder_exception(ExceptionCode::ILLEGAL_STATE, "Key is not initialized"))
162 } else {
163 self.key_service
164 .do_sign(key, data)
165 .map_err(|e| new_binder_exception(ExceptionCode::ILLEGAL_STATE, e.to_string()))
166 }
Victor Hsieh23f73592021-08-06 18:08:24 -0700167 }
Victor Hsieh272aa242021-02-01 14:19:20 -0800168}
Victor Hsiehebb1d902021-08-06 13:00:18 -0700169
170fn get_authfs_service() -> BinderResult<Strong<dyn IAuthFsService>> {
171 Ok(authfs_aidl_interface::binder::get_interface(AUTHFS_SERVICE_NAME)?)
172}