blob: 9454442461d9c99ad12f4a54e088c71758013bfb [file] [log] [blame]
Alan Stokeseb97d4a2021-08-26 14:24:32 +01001/*
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
Alan Stokes17fd36a2021-09-06 17:22:37 +010017//! A tool to verify whether a CompOS instance image and key pair are valid. It starts a CompOS VM
Alan Stokeseb97d4a2021-08-26 14:24:32 +010018//! as part of this. The tool is intended to be run by odsign during boot.
19
Alan Stokes17fd36a2021-09-06 17:22:37 +010020use anyhow::{bail, Context, Result};
21use compos_aidl_interface::binder::ProcessState;
Alan Stokesd21764c2021-10-25 15:33:40 +010022use compos_common::compos_client::{VmInstance, VmParameters};
Alan Stokesb2cc79e2021-09-14 14:08:46 +010023use compos_common::{
Victor Hsieh0a5ab4b2022-01-05 11:45:52 -080024 COMPOS_DATA_ROOT, CURRENT_INSTANCE_DIR, IDSIG_FILE, IDSIG_MANIFEST_APK_FILE,
25 INSTANCE_IMAGE_FILE, PENDING_INSTANCE_DIR, PRIVATE_KEY_BLOB_FILE, PUBLIC_KEY_FILE,
26 TEST_INSTANCE_DIR,
Alan Stokesb2cc79e2021-09-14 14:08:46 +010027};
Alan Stokeseb97d4a2021-08-26 14:24:32 +010028use std::fs::{self, File};
29use std::io::Read;
Alan Stokes7e8c9fc2022-02-02 18:02:41 +000030use std::panic;
Alan Stokeseb97d4a2021-08-26 14:24:32 +010031use std::path::{Path, PathBuf};
Alan Stokeseb97d4a2021-08-26 14:24:32 +010032
Alan Stokeseb97d4a2021-08-26 14:24:32 +010033const MAX_FILE_SIZE_BYTES: u64 = 8 * 1024;
34
Alan Stokes9a79ce92021-11-25 11:47:54 +000035fn main() {
Alan Stokes17fd36a2021-09-06 17:22:37 +010036 android_logger::init_once(
37 android_logger::Config::default()
38 .with_tag("compos_verify_key")
39 .with_min_level(log::Level::Info),
40 );
41
Alan Stokes7e8c9fc2022-02-02 18:02:41 +000042 // Redirect panic messages to logcat.
43 panic::set_hook(Box::new(|panic_info| {
44 log::error!("{}", panic_info);
45 }));
46
Alan Stokes9a79ce92021-11-25 11:47:54 +000047 if let Err(e) = try_main() {
48 log::error!("{:?}", e);
49 std::process::exit(-1)
50 }
51}
52
53fn try_main() -> Result<()> {
Alan Stokeseb97d4a2021-08-26 14:24:32 +010054 let matches = clap::App::new("compos_verify_key")
55 .arg(
56 clap::Arg::with_name("instance")
57 .long("instance")
58 .takes_value(true)
59 .required(true)
Alan Stokes9a79ce92021-11-25 11:47:54 +000060 .possible_values(&["pending", "current", "test"]),
Alan Stokeseb97d4a2021-08-26 14:24:32 +010061 )
Alan Stokes9a79ce92021-11-25 11:47:54 +000062 .arg(clap::Arg::with_name("debug").long("debug"))
Alan Stokeseb97d4a2021-08-26 14:24:32 +010063 .get_matches();
Alan Stokeseb97d4a2021-08-26 14:24:32 +010064
Alan Stokes9a79ce92021-11-25 11:47:54 +000065 let debug_mode = matches.is_present("debug");
66 let (promote_if_valid, instance_dir) = match matches.value_of("instance").unwrap() {
67 "pending" => (true, PENDING_INSTANCE_DIR),
68 "current" => (false, CURRENT_INSTANCE_DIR),
69 "test" => (false, TEST_INSTANCE_DIR),
70 _ => unreachable!("Unexpected instance name"),
71 };
72
73 let instance_dir: PathBuf = [COMPOS_DATA_ROOT, instance_dir].iter().collect();
Alan Stokeseb97d4a2021-08-26 14:24:32 +010074
75 if !instance_dir.is_dir() {
Alan Stokes9a79ce92021-11-25 11:47:54 +000076 bail!("{:?} is not a directory", instance_dir);
Alan Stokeseb97d4a2021-08-26 14:24:32 +010077 }
78
79 // We need to start the thread pool to be able to receive Binder callbacks
80 ProcessState::start_thread_pool();
81
Alan Stokes9a79ce92021-11-25 11:47:54 +000082 let result = verify(debug_mode, &instance_dir).and_then(|_| {
83 log::info!("Verified {:?}", instance_dir);
84 if promote_if_valid {
85 // If the instance is ok, then it must actually match the current system state,
Alan Stokeseb97d4a2021-08-26 14:24:32 +010086 // so we promote it to current.
Alan Stokes9a79ce92021-11-25 11:47:54 +000087 log::info!("Promoting to current");
Alan Stokeseb97d4a2021-08-26 14:24:32 +010088 promote_to_current(&instance_dir)
89 } else {
90 Ok(())
91 }
92 });
93
94 if result.is_err() {
95 // This is best efforts, and we still want to report the original error as our result
Alan Stokes9a79ce92021-11-25 11:47:54 +000096 log::info!("Removing {:?}", instance_dir);
Alan Stokeseb97d4a2021-08-26 14:24:32 +010097 if let Err(e) = fs::remove_dir_all(&instance_dir) {
Alan Stokes17fd36a2021-09-06 17:22:37 +010098 log::warn!("Failed to remove directory: {}", e);
Alan Stokeseb97d4a2021-08-26 14:24:32 +010099 }
100 }
101
102 result
103}
104
Alan Stokes9a79ce92021-11-25 11:47:54 +0000105fn verify(debug_mode: bool, instance_dir: &Path) -> Result<()> {
Alan Stokeseb97d4a2021-08-26 14:24:32 +0100106 let blob = instance_dir.join(PRIVATE_KEY_BLOB_FILE);
107 let public_key = instance_dir.join(PUBLIC_KEY_FILE);
Alan Stokes17fd36a2021-09-06 17:22:37 +0100108 let instance_image = instance_dir.join(INSTANCE_IMAGE_FILE);
Jooyung Hanccae66d2021-12-20 11:54:36 +0900109 let idsig = instance_dir.join(IDSIG_FILE);
Victor Hsieh0a5ab4b2022-01-05 11:45:52 -0800110 let idsig_manifest_apk = instance_dir.join(IDSIG_MANIFEST_APK_FILE);
Alan Stokeseb97d4a2021-08-26 14:24:32 +0100111
112 let blob = read_small_file(blob).context("Failed to read key blob")?;
113 let public_key = read_small_file(public_key).context("Failed to read public key")?;
Alan Stokes16e027f2021-10-04 17:57:31 +0100114 let instance_image = File::open(instance_image).context("Failed to open instance image")?;
Alan Stokeseb97d4a2021-08-26 14:24:32 +0100115
Alan Stokesd21764c2021-10-25 15:33:40 +0100116 let virtualization_service = VmInstance::connect_to_virtualization_service()?;
Alan Stokesb4a0e912021-12-01 11:43:59 +0000117 let vm_instance = VmInstance::start(
118 &*virtualization_service,
119 instance_image,
Jooyung Hanccae66d2021-12-20 11:54:36 +0900120 &idsig,
Victor Hsieh0a5ab4b2022-01-05 11:45:52 -0800121 &idsig_manifest_apk,
Alan Stokesb4a0e912021-12-01 11:43:59 +0000122 &VmParameters { debug_mode, ..Default::default() },
123 )?;
Alan Stokes17fd36a2021-09-06 17:22:37 +0100124 let service = vm_instance.get_service()?;
Alan Stokeseb97d4a2021-08-26 14:24:32 +0100125
126 let result = service.verifySigningKey(&blob, &public_key).context("Verifying signing key")?;
127
128 if !result {
129 bail!("Key files are not valid");
130 }
131
132 Ok(())
133}
134
Alan Stokes9a79ce92021-11-25 11:47:54 +0000135fn promote_to_current(instance_dir: &Path) -> Result<()> {
136 let current_dir: PathBuf = [COMPOS_DATA_ROOT, CURRENT_INSTANCE_DIR].iter().collect();
137
138 // This may fail if the directory doesn't exist - which is fine, we only care about the rename
139 // succeeding.
140 let _ = fs::remove_dir_all(&current_dir);
141
142 fs::rename(&instance_dir, &current_dir).context("Unable to promote instance to current")?;
143 Ok(())
144}
145
Alan Stokeseb97d4a2021-08-26 14:24:32 +0100146fn read_small_file(file: PathBuf) -> Result<Vec<u8>> {
147 let mut file = File::open(file)?;
148 if file.metadata()?.len() > MAX_FILE_SIZE_BYTES {
149 bail!("File is too big");
150 }
151 let mut data = vec![];
152 file.read_to_end(&mut data)?;
153 Ok(data)
154}