blob: 13d3c8b0fc8327b72a9fee58174a074522127814 [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;
30use std::path::{Path, PathBuf};
Alan Stokeseb97d4a2021-08-26 14:24:32 +010031
Alan Stokeseb97d4a2021-08-26 14:24:32 +010032const MAX_FILE_SIZE_BYTES: u64 = 8 * 1024;
33
Alan Stokes9a79ce92021-11-25 11:47:54 +000034fn main() {
Alan Stokes17fd36a2021-09-06 17:22:37 +010035 android_logger::init_once(
36 android_logger::Config::default()
37 .with_tag("compos_verify_key")
38 .with_min_level(log::Level::Info),
39 );
40
Alan Stokes9a79ce92021-11-25 11:47:54 +000041 if let Err(e) = try_main() {
42 log::error!("{:?}", e);
43 std::process::exit(-1)
44 }
45}
46
47fn try_main() -> Result<()> {
Alan Stokeseb97d4a2021-08-26 14:24:32 +010048 let matches = clap::App::new("compos_verify_key")
49 .arg(
50 clap::Arg::with_name("instance")
51 .long("instance")
52 .takes_value(true)
53 .required(true)
Alan Stokes9a79ce92021-11-25 11:47:54 +000054 .possible_values(&["pending", "current", "test"]),
Alan Stokeseb97d4a2021-08-26 14:24:32 +010055 )
Alan Stokes9a79ce92021-11-25 11:47:54 +000056 .arg(clap::Arg::with_name("debug").long("debug"))
Alan Stokeseb97d4a2021-08-26 14:24:32 +010057 .get_matches();
Alan Stokeseb97d4a2021-08-26 14:24:32 +010058
Alan Stokes9a79ce92021-11-25 11:47:54 +000059 let debug_mode = matches.is_present("debug");
60 let (promote_if_valid, instance_dir) = match matches.value_of("instance").unwrap() {
61 "pending" => (true, PENDING_INSTANCE_DIR),
62 "current" => (false, CURRENT_INSTANCE_DIR),
63 "test" => (false, TEST_INSTANCE_DIR),
64 _ => unreachable!("Unexpected instance name"),
65 };
66
67 let instance_dir: PathBuf = [COMPOS_DATA_ROOT, instance_dir].iter().collect();
Alan Stokeseb97d4a2021-08-26 14:24:32 +010068
69 if !instance_dir.is_dir() {
Alan Stokes9a79ce92021-11-25 11:47:54 +000070 bail!("{:?} is not a directory", instance_dir);
Alan Stokeseb97d4a2021-08-26 14:24:32 +010071 }
72
73 // We need to start the thread pool to be able to receive Binder callbacks
74 ProcessState::start_thread_pool();
75
Alan Stokes9a79ce92021-11-25 11:47:54 +000076 let result = verify(debug_mode, &instance_dir).and_then(|_| {
77 log::info!("Verified {:?}", instance_dir);
78 if promote_if_valid {
79 // If the instance is ok, then it must actually match the current system state,
Alan Stokeseb97d4a2021-08-26 14:24:32 +010080 // so we promote it to current.
Alan Stokes9a79ce92021-11-25 11:47:54 +000081 log::info!("Promoting to current");
Alan Stokeseb97d4a2021-08-26 14:24:32 +010082 promote_to_current(&instance_dir)
83 } else {
84 Ok(())
85 }
86 });
87
88 if result.is_err() {
89 // This is best efforts, and we still want to report the original error as our result
Alan Stokes9a79ce92021-11-25 11:47:54 +000090 log::info!("Removing {:?}", instance_dir);
Alan Stokeseb97d4a2021-08-26 14:24:32 +010091 if let Err(e) = fs::remove_dir_all(&instance_dir) {
Alan Stokes17fd36a2021-09-06 17:22:37 +010092 log::warn!("Failed to remove directory: {}", e);
Alan Stokeseb97d4a2021-08-26 14:24:32 +010093 }
94 }
95
96 result
97}
98
Alan Stokes9a79ce92021-11-25 11:47:54 +000099fn verify(debug_mode: bool, instance_dir: &Path) -> Result<()> {
Alan Stokeseb97d4a2021-08-26 14:24:32 +0100100 let blob = instance_dir.join(PRIVATE_KEY_BLOB_FILE);
101 let public_key = instance_dir.join(PUBLIC_KEY_FILE);
Alan Stokes17fd36a2021-09-06 17:22:37 +0100102 let instance_image = instance_dir.join(INSTANCE_IMAGE_FILE);
Jooyung Hanccae66d2021-12-20 11:54:36 +0900103 let idsig = instance_dir.join(IDSIG_FILE);
Victor Hsieh0a5ab4b2022-01-05 11:45:52 -0800104 let idsig_manifest_apk = instance_dir.join(IDSIG_MANIFEST_APK_FILE);
Alan Stokeseb97d4a2021-08-26 14:24:32 +0100105
106 let blob = read_small_file(blob).context("Failed to read key blob")?;
107 let public_key = read_small_file(public_key).context("Failed to read public key")?;
Alan Stokes16e027f2021-10-04 17:57:31 +0100108 let instance_image = File::open(instance_image).context("Failed to open instance image")?;
Alan Stokeseb97d4a2021-08-26 14:24:32 +0100109
Alan Stokesd21764c2021-10-25 15:33:40 +0100110 let virtualization_service = VmInstance::connect_to_virtualization_service()?;
Alan Stokesb4a0e912021-12-01 11:43:59 +0000111 let vm_instance = VmInstance::start(
112 &*virtualization_service,
113 instance_image,
Jooyung Hanccae66d2021-12-20 11:54:36 +0900114 &idsig,
Victor Hsieh0a5ab4b2022-01-05 11:45:52 -0800115 &idsig_manifest_apk,
Alan Stokesb4a0e912021-12-01 11:43:59 +0000116 &VmParameters { debug_mode, ..Default::default() },
117 )?;
Alan Stokes17fd36a2021-09-06 17:22:37 +0100118 let service = vm_instance.get_service()?;
Alan Stokeseb97d4a2021-08-26 14:24:32 +0100119
120 let result = service.verifySigningKey(&blob, &public_key).context("Verifying signing key")?;
121
122 if !result {
123 bail!("Key files are not valid");
124 }
125
126 Ok(())
127}
128
Alan Stokes9a79ce92021-11-25 11:47:54 +0000129fn promote_to_current(instance_dir: &Path) -> Result<()> {
130 let current_dir: PathBuf = [COMPOS_DATA_ROOT, CURRENT_INSTANCE_DIR].iter().collect();
131
132 // This may fail if the directory doesn't exist - which is fine, we only care about the rename
133 // succeeding.
134 let _ = fs::remove_dir_all(&current_dir);
135
136 fs::rename(&instance_dir, &current_dir).context("Unable to promote instance to current")?;
137 Ok(())
138}
139
Alan Stokeseb97d4a2021-08-26 14:24:32 +0100140fn read_small_file(file: PathBuf) -> Result<Vec<u8>> {
141 let mut file = File::open(file)?;
142 if file.metadata()?.len() > MAX_FILE_SIZE_BYTES {
143 bail!("File is too big");
144 }
145 let mut data = vec![];
146 file.read_to_end(&mut data)?;
147 Ok(data)
148}