blob: 2e3d2067c821dca85c37847440de7d0dcf57dd5e [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;
22use compos_common::compos_client::VmInstance;
23use compos_common::COMPOS_DATA_ROOT;
Alan Stokeseb97d4a2021-08-26 14:24:32 +010024use std::fs::{self, File};
25use std::io::Read;
26use std::path::{Path, PathBuf};
Alan Stokeseb97d4a2021-08-26 14:24:32 +010027
Alan Stokeseb97d4a2021-08-26 14:24:32 +010028const CURRENT_DIR: &str = "current";
29const PENDING_DIR: &str = "pending";
30const PRIVATE_KEY_BLOB_FILE: &str = "key.blob";
31const PUBLIC_KEY_FILE: &str = "key.pubkey";
32const INSTANCE_IMAGE_FILE: &str = "instance.img";
33
34const MAX_FILE_SIZE_BYTES: u64 = 8 * 1024;
35
Alan Stokeseb97d4a2021-08-26 14:24:32 +010036fn main() -> Result<()> {
Alan Stokes17fd36a2021-09-06 17:22:37 +010037 android_logger::init_once(
38 android_logger::Config::default()
39 .with_tag("compos_verify_key")
40 .with_min_level(log::Level::Info),
41 );
42
Alan Stokeseb97d4a2021-08-26 14:24:32 +010043 let matches = clap::App::new("compos_verify_key")
44 .arg(
45 clap::Arg::with_name("instance")
46 .long("instance")
47 .takes_value(true)
48 .required(true)
49 .possible_values(&["pending", "current"]),
50 )
51 .get_matches();
52 let do_pending = matches.value_of("instance").unwrap() == "pending";
53
54 let instance_dir: PathBuf =
55 [COMPOS_DATA_ROOT, if do_pending { PENDING_DIR } else { CURRENT_DIR }].iter().collect();
56
57 if !instance_dir.is_dir() {
58 bail!("{} is not a directory", instance_dir.display());
59 }
60
61 // We need to start the thread pool to be able to receive Binder callbacks
62 ProcessState::start_thread_pool();
63
64 let result = verify(&instance_dir).and_then(|_| {
65 if do_pending {
66 // If the pending instance is ok, then it must actually match the current system state,
67 // so we promote it to current.
Alan Stokes17fd36a2021-09-06 17:22:37 +010068 log::info!("Promoting pending to current");
Alan Stokeseb97d4a2021-08-26 14:24:32 +010069 promote_to_current(&instance_dir)
70 } else {
71 Ok(())
72 }
73 });
74
75 if result.is_err() {
76 // This is best efforts, and we still want to report the original error as our result
Alan Stokes17fd36a2021-09-06 17:22:37 +010077 log::info!("Removing {}", instance_dir.display());
Alan Stokeseb97d4a2021-08-26 14:24:32 +010078 if let Err(e) = fs::remove_dir_all(&instance_dir) {
Alan Stokes17fd36a2021-09-06 17:22:37 +010079 log::warn!("Failed to remove directory: {}", e);
Alan Stokeseb97d4a2021-08-26 14:24:32 +010080 }
81 }
82
83 result
84}
85
86fn verify(instance_dir: &Path) -> Result<()> {
87 let blob = instance_dir.join(PRIVATE_KEY_BLOB_FILE);
88 let public_key = instance_dir.join(PUBLIC_KEY_FILE);
Alan Stokes17fd36a2021-09-06 17:22:37 +010089 let instance_image = instance_dir.join(INSTANCE_IMAGE_FILE);
Alan Stokeseb97d4a2021-08-26 14:24:32 +010090
91 let blob = read_small_file(blob).context("Failed to read key blob")?;
92 let public_key = read_small_file(public_key).context("Failed to read public key")?;
93
Alan Stokes17fd36a2021-09-06 17:22:37 +010094 let vm_instance = VmInstance::start(&instance_image)?;
95 let service = vm_instance.get_service()?;
Alan Stokeseb97d4a2021-08-26 14:24:32 +010096
97 let result = service.verifySigningKey(&blob, &public_key).context("Verifying signing key")?;
98
99 if !result {
100 bail!("Key files are not valid");
101 }
102
103 Ok(())
104}
105
106fn read_small_file(file: PathBuf) -> Result<Vec<u8>> {
107 let mut file = File::open(file)?;
108 if file.metadata()?.len() > MAX_FILE_SIZE_BYTES {
109 bail!("File is too big");
110 }
111 let mut data = vec![];
112 file.read_to_end(&mut data)?;
113 Ok(data)
114}
115
Alan Stokeseb97d4a2021-08-26 14:24:32 +0100116fn promote_to_current(instance_dir: &Path) -> Result<()> {
117 let current_dir: PathBuf = [COMPOS_DATA_ROOT, CURRENT_DIR].iter().collect();
118
119 // This may fail if the directory doesn't exist - which is fine, we only care about the rename
120 // succeeding.
121 let _ = fs::remove_dir_all(&current_dir);
122
123 fs::rename(&instance_dir, &current_dir)
124 .context("Unable to promote pending instance to current")?;
125 Ok(())
126}