blob: a028264e6339d466f301f3bd7bdc4b6299bc7f95 [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::{
Jooyung Hanccae66d2021-12-20 11:54:36 +090024 COMPOS_DATA_ROOT, CURRENT_INSTANCE_DIR, IDSIG_FILE, INSTANCE_IMAGE_FILE, PENDING_INSTANCE_DIR,
Alan Stokes9a79ce92021-11-25 11:47:54 +000025 PRIVATE_KEY_BLOB_FILE, PUBLIC_KEY_FILE, TEST_INSTANCE_DIR,
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 Stokes9a79ce92021-11-25 11:47:54 +000033fn main() {
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 Stokes9a79ce92021-11-25 11:47:54 +000040 if let Err(e) = try_main() {
41 log::error!("{:?}", e);
42 std::process::exit(-1)
43 }
44}
45
46fn try_main() -> Result<()> {
Alan Stokeseb97d4a2021-08-26 14:24:32 +010047 let matches = clap::App::new("compos_verify_key")
48 .arg(
49 clap::Arg::with_name("instance")
50 .long("instance")
51 .takes_value(true)
52 .required(true)
Alan Stokes9a79ce92021-11-25 11:47:54 +000053 .possible_values(&["pending", "current", "test"]),
Alan Stokeseb97d4a2021-08-26 14:24:32 +010054 )
Alan Stokes9a79ce92021-11-25 11:47:54 +000055 .arg(clap::Arg::with_name("debug").long("debug"))
Alan Stokeseb97d4a2021-08-26 14:24:32 +010056 .get_matches();
Alan Stokeseb97d4a2021-08-26 14:24:32 +010057
Alan Stokes9a79ce92021-11-25 11:47:54 +000058 let debug_mode = matches.is_present("debug");
59 let (promote_if_valid, instance_dir) = match matches.value_of("instance").unwrap() {
60 "pending" => (true, PENDING_INSTANCE_DIR),
61 "current" => (false, CURRENT_INSTANCE_DIR),
62 "test" => (false, TEST_INSTANCE_DIR),
63 _ => unreachable!("Unexpected instance name"),
64 };
65
66 let instance_dir: PathBuf = [COMPOS_DATA_ROOT, instance_dir].iter().collect();
Alan Stokeseb97d4a2021-08-26 14:24:32 +010067
68 if !instance_dir.is_dir() {
Alan Stokes9a79ce92021-11-25 11:47:54 +000069 bail!("{:?} is not a directory", instance_dir);
Alan Stokeseb97d4a2021-08-26 14:24:32 +010070 }
71
72 // We need to start the thread pool to be able to receive Binder callbacks
73 ProcessState::start_thread_pool();
74
Alan Stokes9a79ce92021-11-25 11:47:54 +000075 let result = verify(debug_mode, &instance_dir).and_then(|_| {
76 log::info!("Verified {:?}", instance_dir);
77 if promote_if_valid {
78 // If the instance is ok, then it must actually match the current system state,
Alan Stokeseb97d4a2021-08-26 14:24:32 +010079 // so we promote it to current.
Alan Stokes9a79ce92021-11-25 11:47:54 +000080 log::info!("Promoting to current");
Alan Stokeseb97d4a2021-08-26 14:24:32 +010081 promote_to_current(&instance_dir)
82 } else {
83 Ok(())
84 }
85 });
86
87 if result.is_err() {
88 // This is best efforts, and we still want to report the original error as our result
Alan Stokes9a79ce92021-11-25 11:47:54 +000089 log::info!("Removing {:?}", instance_dir);
Alan Stokeseb97d4a2021-08-26 14:24:32 +010090 if let Err(e) = fs::remove_dir_all(&instance_dir) {
Alan Stokes17fd36a2021-09-06 17:22:37 +010091 log::warn!("Failed to remove directory: {}", e);
Alan Stokeseb97d4a2021-08-26 14:24:32 +010092 }
93 }
94
95 result
96}
97
Alan Stokes9a79ce92021-11-25 11:47:54 +000098fn verify(debug_mode: bool, instance_dir: &Path) -> Result<()> {
Alan Stokeseb97d4a2021-08-26 14:24:32 +010099 let blob = instance_dir.join(PRIVATE_KEY_BLOB_FILE);
100 let public_key = instance_dir.join(PUBLIC_KEY_FILE);
Alan Stokes17fd36a2021-09-06 17:22:37 +0100101 let instance_image = instance_dir.join(INSTANCE_IMAGE_FILE);
Jooyung Hanccae66d2021-12-20 11:54:36 +0900102 let idsig = instance_dir.join(IDSIG_FILE);
Alan Stokeseb97d4a2021-08-26 14:24:32 +0100103
104 let blob = read_small_file(blob).context("Failed to read key blob")?;
105 let public_key = read_small_file(public_key).context("Failed to read public key")?;
Alan Stokes16e027f2021-10-04 17:57:31 +0100106 let instance_image = File::open(instance_image).context("Failed to open instance image")?;
Alan Stokeseb97d4a2021-08-26 14:24:32 +0100107
Alan Stokesd21764c2021-10-25 15:33:40 +0100108 let virtualization_service = VmInstance::connect_to_virtualization_service()?;
Alan Stokesb4a0e912021-12-01 11:43:59 +0000109 let vm_instance = VmInstance::start(
110 &*virtualization_service,
111 instance_image,
Jooyung Hanccae66d2021-12-20 11:54:36 +0900112 &idsig,
Alan Stokesb4a0e912021-12-01 11:43:59 +0000113 &VmParameters { debug_mode, ..Default::default() },
114 )?;
Alan Stokes17fd36a2021-09-06 17:22:37 +0100115 let service = vm_instance.get_service()?;
Alan Stokeseb97d4a2021-08-26 14:24:32 +0100116
117 let result = service.verifySigningKey(&blob, &public_key).context("Verifying signing key")?;
118
119 if !result {
120 bail!("Key files are not valid");
121 }
122
123 Ok(())
124}
125
Alan Stokes9a79ce92021-11-25 11:47:54 +0000126fn promote_to_current(instance_dir: &Path) -> Result<()> {
127 let current_dir: PathBuf = [COMPOS_DATA_ROOT, CURRENT_INSTANCE_DIR].iter().collect();
128
129 // This may fail if the directory doesn't exist - which is fine, we only care about the rename
130 // succeeding.
131 let _ = fs::remove_dir_all(&current_dir);
132
133 fs::rename(&instance_dir, &current_dir).context("Unable to promote instance to current")?;
134 Ok(())
135}
136
Alan Stokeseb97d4a2021-08-26 14:24:32 +0100137fn read_small_file(file: PathBuf) -> Result<Vec<u8>> {
138 let mut file = File::open(file)?;
139 if file.metadata()?.len() > MAX_FILE_SIZE_BYTES {
140 bail!("File is too big");
141 }
142 let mut data = vec![];
143 file.read_to_end(&mut data)?;
144 Ok(data)
145}