blob: a5b0b6bd5523766133a568478e6987a2c1955411 [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;
Alan Stokesb2cc79e2021-09-14 14:08:46 +010023use compos_common::{
Alan Stokes388b88a2021-10-13 16:03:17 +010024 COMPOS_DATA_ROOT, CURRENT_INSTANCE_DIR, INSTANCE_IMAGE_FILE, PENDING_INSTANCE_DIR,
25 PRIVATE_KEY_BLOB_FILE, PUBLIC_KEY_FILE,
Alan Stokesb2cc79e2021-09-14 14:08:46 +010026};
Alan Stokeseb97d4a2021-08-26 14:24:32 +010027use std::fs::{self, File};
28use std::io::Read;
29use std::path::{Path, PathBuf};
Alan Stokeseb97d4a2021-08-26 14:24:32 +010030
Alan Stokeseb97d4a2021-08-26 14:24:32 +010031const MAX_FILE_SIZE_BYTES: u64 = 8 * 1024;
32
Alan Stokeseb97d4a2021-08-26 14:24:32 +010033fn main() -> Result<()> {
Alan Stokes17fd36a2021-09-06 17:22:37 +010034 android_logger::init_once(
35 android_logger::Config::default()
36 .with_tag("compos_verify_key")
37 .with_min_level(log::Level::Info),
38 );
39
Alan Stokeseb97d4a2021-08-26 14:24:32 +010040 let matches = clap::App::new("compos_verify_key")
41 .arg(
42 clap::Arg::with_name("instance")
43 .long("instance")
44 .takes_value(true)
45 .required(true)
46 .possible_values(&["pending", "current"]),
47 )
48 .get_matches();
49 let do_pending = matches.value_of("instance").unwrap() == "pending";
50
51 let instance_dir: PathBuf =
Alan Stokes388b88a2021-10-13 16:03:17 +010052 [COMPOS_DATA_ROOT, if do_pending { PENDING_INSTANCE_DIR } else { CURRENT_INSTANCE_DIR }]
53 .iter()
54 .collect();
Alan Stokeseb97d4a2021-08-26 14:24:32 +010055
56 if !instance_dir.is_dir() {
57 bail!("{} is not a directory", instance_dir.display());
58 }
59
60 // We need to start the thread pool to be able to receive Binder callbacks
61 ProcessState::start_thread_pool();
62
63 let result = verify(&instance_dir).and_then(|_| {
64 if do_pending {
65 // If the pending instance is ok, then it must actually match the current system state,
66 // so we promote it to current.
Alan Stokes17fd36a2021-09-06 17:22:37 +010067 log::info!("Promoting pending to current");
Alan Stokeseb97d4a2021-08-26 14:24:32 +010068 promote_to_current(&instance_dir)
69 } else {
70 Ok(())
71 }
72 });
73
74 if result.is_err() {
75 // This is best efforts, and we still want to report the original error as our result
Alan Stokes17fd36a2021-09-06 17:22:37 +010076 log::info!("Removing {}", instance_dir.display());
Alan Stokeseb97d4a2021-08-26 14:24:32 +010077 if let Err(e) = fs::remove_dir_all(&instance_dir) {
Alan Stokes17fd36a2021-09-06 17:22:37 +010078 log::warn!("Failed to remove directory: {}", e);
Alan Stokeseb97d4a2021-08-26 14:24:32 +010079 }
80 }
81
82 result
83}
84
85fn verify(instance_dir: &Path) -> Result<()> {
86 let blob = instance_dir.join(PRIVATE_KEY_BLOB_FILE);
87 let public_key = instance_dir.join(PUBLIC_KEY_FILE);
Alan Stokes17fd36a2021-09-06 17:22:37 +010088 let instance_image = instance_dir.join(INSTANCE_IMAGE_FILE);
Alan Stokeseb97d4a2021-08-26 14:24:32 +010089
90 let blob = read_small_file(blob).context("Failed to read key blob")?;
91 let public_key = read_small_file(public_key).context("Failed to read public key")?;
Alan Stokes16e027f2021-10-04 17:57:31 +010092 let instance_image = File::open(instance_image).context("Failed to open instance image")?;
Alan Stokeseb97d4a2021-08-26 14:24:32 +010093
Alan Stokes16e027f2021-10-04 17:57:31 +010094 let vm_instance = VmInstance::start(instance_image)?;
Alan Stokes17fd36a2021-09-06 17:22:37 +010095 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<()> {
Alan Stokes388b88a2021-10-13 16:03:17 +0100117 let current_dir: PathBuf = [COMPOS_DATA_ROOT, CURRENT_INSTANCE_DIR].iter().collect();
Alan Stokeseb97d4a2021-08-26 14:24:32 +0100118
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}