• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
17 //! A tool to verify a CompOS signature. It starts a CompOS VM as part of this to retrieve the
18 //!  public key. The tool is intended to be run by odsign during boot.
19 
20 use android_logger::LogId;
21 use anyhow::{bail, Context, Result};
22 use compos_aidl_interface::binder::ProcessState;
23 use compos_common::compos_client::{VmInstance, VmParameters};
24 use compos_common::odrefresh::{
25     CURRENT_ARTIFACTS_SUBDIR, ODREFRESH_OUTPUT_ROOT_DIR, PENDING_ARTIFACTS_SUBDIR,
26     TEST_ARTIFACTS_SUBDIR,
27 };
28 use compos_common::{
29     COMPOS_DATA_ROOT, CURRENT_INSTANCE_DIR, IDSIG_FILE, IDSIG_MANIFEST_APK_FILE,
30     INSTANCE_IMAGE_FILE, TEST_INSTANCE_DIR,
31 };
32 use log::error;
33 use std::fs::File;
34 use std::io::Read;
35 use std::panic;
36 use std::path::Path;
37 
38 const MAX_FILE_SIZE_BYTES: u64 = 100 * 1024;
39 
main()40 fn main() {
41     android_logger::init_once(
42         android_logger::Config::default()
43             .with_tag("compos_verify")
44             .with_min_level(log::Level::Info)
45             .with_log_id(LogId::System), // Needed to log successfully early in boot
46     );
47 
48     // Redirect panic messages to logcat.
49     panic::set_hook(Box::new(|panic_info| {
50         error!("{}", panic_info);
51     }));
52 
53     if let Err(e) = try_main() {
54         error!("{:?}", e);
55         std::process::exit(1)
56     }
57 }
58 
try_main() -> Result<()>59 fn try_main() -> Result<()> {
60     let matches = clap::App::new("compos_verify")
61         .arg(
62             clap::Arg::with_name("instance")
63                 .long("instance")
64                 .takes_value(true)
65                 .required(true)
66                 .possible_values(&["current", "pending", "test"]),
67         )
68         .arg(clap::Arg::with_name("debug").long("debug"))
69         .get_matches();
70 
71     let debug_mode = matches.is_present("debug");
72     let (instance_dir, artifacts_dir) = match matches.value_of("instance").unwrap() {
73         "current" => (CURRENT_INSTANCE_DIR, CURRENT_ARTIFACTS_SUBDIR),
74         "pending" => (CURRENT_INSTANCE_DIR, PENDING_ARTIFACTS_SUBDIR),
75         "test" => (TEST_INSTANCE_DIR, TEST_ARTIFACTS_SUBDIR),
76         _ => unreachable!("Unexpected instance name"),
77     };
78 
79     let instance_dir = Path::new(COMPOS_DATA_ROOT).join(instance_dir);
80     let artifacts_dir = Path::new(ODREFRESH_OUTPUT_ROOT_DIR).join(artifacts_dir);
81 
82     if !instance_dir.is_dir() {
83         bail!("{:?} is not a directory", instance_dir);
84     }
85 
86     let instance_image = instance_dir.join(INSTANCE_IMAGE_FILE);
87     let idsig = instance_dir.join(IDSIG_FILE);
88     let idsig_manifest_apk = instance_dir.join(IDSIG_MANIFEST_APK_FILE);
89 
90     let instance_image = File::open(instance_image).context("Failed to open instance image")?;
91 
92     let info = artifacts_dir.join("compos.info");
93     let signature = artifacts_dir.join("compos.info.signature");
94 
95     let info = read_small_file(&info).context("Failed to read compos.info")?;
96     let signature = read_small_file(&signature).context("Failed to read compos.info signature")?;
97 
98     // We need to start the thread pool to be able to receive Binder callbacks
99     ProcessState::start_thread_pool();
100 
101     let virtualization_service = VmInstance::connect_to_virtualization_service()?;
102     let vm_instance = VmInstance::start(
103         &*virtualization_service,
104         instance_image,
105         &idsig,
106         &idsig_manifest_apk,
107         &VmParameters { debug_mode, never_log: !debug_mode, ..Default::default() },
108     )?;
109     let service = vm_instance.get_service()?;
110 
111     let public_key = service.getPublicKey().context("Getting public key")?;
112 
113     if !compos_verify_native::verify(&public_key, &signature, &info) {
114         bail!("Signature verification failed");
115     }
116 
117     Ok(())
118 }
119 
read_small_file(file: &Path) -> Result<Vec<u8>>120 fn read_small_file(file: &Path) -> Result<Vec<u8>> {
121     let mut file = File::open(file)?;
122     if file.metadata()?.len() > MAX_FILE_SIZE_BYTES {
123         bail!("File is too big");
124     }
125     let mut data = Vec::new();
126     file.read_to_end(&mut data)?;
127     Ok(data)
128 }
129