blob: b55fb7cb525258b72ce432a60276410274bc08ce [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;
Victor Hsieh6e340382021-08-13 12:18:02 -070022use log::{debug, warn};
Victor Hsieha64194b2021-08-06 17:43:36 -070023use std::ffi::CString;
Victor Hsieh51789de2021-08-06 16:50:49 -070024use std::path::PathBuf;
Victor Hsieh8fd03f02021-08-24 17:23:01 -070025use std::sync::{Arc, RwLock};
Victor Hsieh272aa242021-02-01 14:19:20 -080026
Victor Hsieh6e340382021-08-13 12:18:02 -070027use crate::compilation::{compile, CompilerOutput};
Victor Hsieh23f73592021-08-06 18:08:24 -070028use crate::compos_key_service::CompOsKeyService;
Victor Hsieh51789de2021-08-06 16:50:49 -070029use authfs_aidl_interface::aidl::com::android::virt::fs::IAuthFsService::IAuthFsService;
Victor Hsieh23f73592021-08-06 18:08:24 -070030use compos_aidl_interface::aidl::com::android::compos::{
31 CompOsKeyData::CompOsKeyData,
32 ICompOsService::{BnCompOsService, ICompOsService},
33 Metadata::Metadata,
Victor Hsieh272aa242021-02-01 14:19:20 -080034};
Victor Hsieh272aa242021-02-01 14:19:20 -080035use compos_aidl_interface::binder::{
Victor Hsieh51789de2021-08-06 16:50:49 -070036 BinderFeatures, ExceptionCode, Interface, Result as BinderResult, Status, Strong,
Victor Hsieh272aa242021-02-01 14:19:20 -080037};
38
Victor Hsiehebb1d902021-08-06 13:00:18 -070039const AUTHFS_SERVICE_NAME: &str = "authfs_service";
Victor Hsieh51789de2021-08-06 16:50:49 -070040const DEX2OAT_PATH: &str = "/apex/com.android.art/bin/dex2oat64";
Alan Stokes9e2c5d52021-07-21 11:29:10 +010041
Victor Hsieha64194b2021-08-06 17:43:36 -070042/// Constructs a binder object that implements ICompOsService.
Victor Hsieh23f73592021-08-06 18:08:24 -070043pub fn new_binder(rpc_binder: bool) -> Result<Strong<dyn ICompOsService>> {
44 let service = CompOsService {
45 dex2oat_path: PathBuf::from(DEX2OAT_PATH),
46 key_service: CompOsKeyService::new(rpc_binder)?,
Victor Hsieh8fd03f02021-08-24 17:23:01 -070047 key_blob: Arc::new(RwLock::new(Vec::new())),
Victor Hsieh23f73592021-08-06 18:08:24 -070048 };
Victor Hsieha64194b2021-08-06 17:43:36 -070049 Ok(BnCompOsService::new_binder(service, BinderFeatures::default()))
Alan Stokes9e2c5d52021-07-21 11:29:10 +010050}
51
Victor Hsieha64194b2021-08-06 17:43:36 -070052struct CompOsService {
Victor Hsieh51789de2021-08-06 16:50:49 -070053 dex2oat_path: PathBuf,
Victor Hsieha64194b2021-08-06 17:43:36 -070054 key_service: CompOsKeyService,
Victor Hsieh8fd03f02021-08-24 17:23:01 -070055 key_blob: Arc<RwLock<Vec<u8>>>,
Victor Hsieh272aa242021-02-01 14:19:20 -080056}
57
Victor Hsieha64194b2021-08-06 17:43:36 -070058impl Interface for CompOsService {}
Victor Hsieh272aa242021-02-01 14:19:20 -080059
Victor Hsieha64194b2021-08-06 17:43:36 -070060impl ICompOsService for CompOsService {
Victor Hsieh8fd03f02021-08-24 17:23:01 -070061 fn initializeSigningKey(&self, key_blob: &[u8]) -> BinderResult<()> {
62 let mut w = self.key_blob.write().unwrap();
63 if w.is_empty() {
64 *w = Vec::from(key_blob);
65 Ok(())
66 } else {
67 Err(new_binder_exception(ExceptionCode::ILLEGAL_STATE, "Cannot re-initialize the key"))
68 }
69 }
70
Victor Hsieh272aa242021-02-01 14:19:20 -080071 fn execute(&self, args: &[String], metadata: &Metadata) -> BinderResult<i8> {
Victor Hsieh51789de2021-08-06 16:50:49 -070072 let authfs_service = get_authfs_service()?;
Victor Hsieh6e340382021-08-13 12:18:02 -070073 let output = compile(&self.dex2oat_path, args, authfs_service, metadata).map_err(|e| {
Victor Hsieh51789de2021-08-06 16:50:49 -070074 new_binder_exception(
75 ExceptionCode::SERVICE_SPECIFIC,
76 format!("Compilation failed: {}", e),
77 )
Victor Hsieh6e340382021-08-13 12:18:02 -070078 })?;
79 match output {
80 CompilerOutput::Digests { oat, vdex, image } => {
81 // TODO(b/161471326): Sign the output on succeed.
82 debug!("oat fs-verity digest: {:02x?}", oat);
83 debug!("vdex fs-verity digest: {:02x?}", vdex);
84 debug!("image fs-verity digest: {:02x?}", image);
85 Ok(0)
86 }
87 CompilerOutput::ExitCode(exit_code) => Ok(exit_code),
88 }
Victor Hsieh272aa242021-02-01 14:19:20 -080089 }
Victor Hsieh23f73592021-08-06 18:08:24 -070090
91 fn generateSigningKey(&self) -> BinderResult<CompOsKeyData> {
92 self.key_service
93 .do_generate()
94 .map_err(|e| new_binder_exception(ExceptionCode::ILLEGAL_STATE, e.to_string()))
95 }
96
97 fn verifySigningKey(&self, key_blob: &[u8], public_key: &[u8]) -> BinderResult<bool> {
98 Ok(if let Err(e) = self.key_service.do_verify(key_blob, public_key) {
99 warn!("Signing key verification failed: {}", e.to_string());
100 false
101 } else {
102 true
103 })
104 }
105
Victor Hsieh8fd03f02021-08-24 17:23:01 -0700106 fn sign(&self, data: &[u8]) -> BinderResult<Vec<u8>> {
107 let key = &*self.key_blob.read().unwrap();
108 if key.is_empty() {
109 Err(new_binder_exception(ExceptionCode::ILLEGAL_STATE, "Key is not initialized"))
110 } else {
111 self.key_service
112 .do_sign(key, data)
113 .map_err(|e| new_binder_exception(ExceptionCode::ILLEGAL_STATE, e.to_string()))
114 }
Victor Hsieh23f73592021-08-06 18:08:24 -0700115 }
Victor Hsieh272aa242021-02-01 14:19:20 -0800116}
Victor Hsiehebb1d902021-08-06 13:00:18 -0700117
118fn get_authfs_service() -> BinderResult<Strong<dyn IAuthFsService>> {
119 Ok(authfs_aidl_interface::binder::get_interface(AUTHFS_SERVICE_NAME)?)
120}
121
Victor Hsiehebb1d902021-08-06 13:00:18 -0700122fn new_binder_exception<T: AsRef<str>>(exception: ExceptionCode, message: T) -> Status {
123 Status::new_exception(exception, CString::new(message.as_ref()).as_deref().ok())
124}