blob: 2fc964bccc31a6625fd93ea814d5ad0e0fb72489 [file] [log] [blame]
Jiyong Park86c9b082021-06-04 19:03:48 +09001/*
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//! `apkdmverity` is a program that protects a signed APK file using dm-verity. The APK is assumed
18//! to be signed using APK signature scheme V4. The idsig file generated by the signing scheme is
19//! also used as an input to provide the merkle tree. This program is currently intended to be used
20//! to securely mount the APK inside Microdroid. Since the APK is physically stored in the file
21//! system managed by the host Android which is assumed to be compromisable, it is important to
22//! keep the integrity of the file "inside" Microdroid.
23
Andrew Walbranf714eeb2022-11-30 11:15:56 +000024#![cfg_attr(test, allow(unused))]
25
Jiyong Park86c9b082021-06-04 19:03:48 +090026use anyhow::{bail, Context, Result};
Alice Wang1bf3d782022-09-28 07:56:36 +000027use apkverify::{HashAlgorithm, V4Signature};
Andrew Walbranaa1efc42022-08-10 13:33:57 +000028use clap::{arg, Arg, ArgAction, Command};
Shikha Panwarb278b1c2022-10-14 12:38:32 +000029use dm::loopdevice;
Hung Nguyen109cdfa2024-12-06 11:02:44 -080030use dm::loopdevice::LoopConfigOptions;
Shikha Panwar414ea892022-10-12 13:45:52 +000031use dm::util;
32use dm::verity::{DmVerityHashAlgorithm, DmVerityTargetBuilder};
Inseob Kim217038e2021-11-25 11:15:06 +090033use itertools::Itertools;
Jiyong Park86c9b082021-06-04 19:03:48 +090034use std::fmt::Debug;
35use std::fs;
Jiyong Park86c9b082021-06-04 19:03:48 +090036use std::os::unix::fs::FileTypeExt;
37use std::path::{Path, PathBuf};
38
Andrew Walbranf714eeb2022-11-30 11:15:56 +000039#[cfg(not(test))]
Jiyong Park86c9b082021-06-04 19:03:48 +090040fn main() -> Result<()> {
Andrew Walbranaa1efc42022-08-10 13:33:57 +000041 let matches = clap_command().get_matches();
Jiyong Park86c9b082021-06-04 19:03:48 +090042
Andrew Walbranaa1efc42022-08-10 13:33:57 +000043 let apks = matches.get_many::<String>("apk").unwrap();
Inseob Kim197748b2021-12-01 19:49:00 +090044 assert!(apks.len() % 4 == 0);
Inseob Kim217038e2021-11-25 11:15:06 +090045
Andrew Walbranaa1efc42022-08-10 13:33:57 +000046 let verbose = matches.get_flag("verbose");
Inseob Kim217038e2021-11-25 11:15:06 +090047
Inseob Kim197748b2021-12-01 19:49:00 +090048 for (apk, idsig, name, roothash) in apks.tuples() {
49 let roothash = if roothash != "none" {
Alice Wang8ed29ce2023-12-01 08:33:12 +000050 Some(hex::decode(roothash).expect("failed to parse roothash"))
Inseob Kim197748b2021-12-01 19:49:00 +090051 } else {
52 None
53 };
Inseob Kim217038e2021-11-25 11:15:06 +090054 let ret = enable_verity(apk, idsig, name, roothash.as_deref())?;
55 if verbose {
56 println!(
57 "data_device: {:?}, hash_device: {:?}, mapper_device: {:?}",
58 ret.data_device, ret.hash_device, ret.mapper_device
59 );
60 }
Jiyong Park99a35b82021-06-07 10:13:44 +090061 }
Jiyong Park86c9b082021-06-04 19:03:48 +090062 Ok(())
63}
64
Andrew Walbranaa1efc42022-08-10 13:33:57 +000065fn clap_command() -> Command {
66 Command::new("apkdmverity")
67 .about("Creates a dm-verity block device out of APK signed with APK signature scheme V4.")
68 .arg(
69 arg!(--apk ...
70 "Input APK file, idsig file, name of the block device, and root hash. \
71 The APK file must be signed using the APK signature scheme 4. The \
72 block device is created at \"/dev/mapper/<name>\".' root_hash is \
73 optional; idsig file's root hash will be used if specified as \"none\"."
74 )
75 .action(ArgAction::Append)
Chris Wailes75269622022-12-05 23:01:44 -080076 .value_names(["apk_path", "idsig_path", "name", "root_hash"]),
Andrew Walbranaa1efc42022-08-10 13:33:57 +000077 )
78 .arg(
79 Arg::new("verbose")
80 .short('v')
81 .long("verbose")
82 .action(ArgAction::SetTrue)
83 .help("Shows verbose output"),
84 )
85}
86
Jiyong Park86c9b082021-06-04 19:03:48 +090087struct VerityResult {
88 data_device: PathBuf,
89 hash_device: PathBuf,
90 mapper_device: PathBuf,
91}
92
93const BLOCK_SIZE: u64 = 4096;
94
95// Makes a dm-verity block device out of `apk` and its accompanying `idsig` files.
Jiyong Parkbb4a9872021-09-06 15:59:21 +090096fn enable_verity<P: AsRef<Path> + Debug>(
97 apk: P,
98 idsig: P,
99 name: &str,
100 roothash: Option<&[u8]>,
101) -> Result<VerityResult> {
Jiyong Park86c9b082021-06-04 19:03:48 +0900102 // Attach the apk file to a loop device if the apk file is a regular file. If not (i.e. block
103 // device), we only need to get the size and use the block device as it is.
104 let (data_device, apk_size) = if fs::metadata(&apk)?.file_type().is_block_device() {
105 (apk.as_ref().to_path_buf(), util::blkgetsize64(apk.as_ref())?)
106 } else {
107 let apk_size = fs::metadata(&apk)?.len();
108 if apk_size % BLOCK_SIZE != 0 {
109 bail!("The size of {:?} is not multiple of {}.", &apk, BLOCK_SIZE)
110 }
Jooyung Han1b00bd22022-04-15 15:29:25 +0900111 (
Alice Wang46211912023-12-07 14:30:37 +0000112 loopdevice::attach(
Hung Nguyen109cdfa2024-12-06 11:02:44 -0800113 &apk,
114 0,
115 apk_size,
116 &LoopConfigOptions { direct_io: true, ..Default::default() },
Alice Wang46211912023-12-07 14:30:37 +0000117 )
Hung Nguyen109cdfa2024-12-06 11:02:44 -0800118 .context("Failed to attach APK to a loop device")?
119 .path,
Jooyung Han1b00bd22022-04-15 15:29:25 +0900120 apk_size,
121 )
Jiyong Park86c9b082021-06-04 19:03:48 +0900122 };
123
124 // Parse the idsig file to locate the merkle tree in it, then attach the file to a loop device
125 // with the offset so that the start of the merkle tree becomes the beginning of the loop
126 // device.
Alice Wang89cff012022-09-26 10:05:16 +0000127 let sig = V4Signature::from_idsig_path(&idsig)?;
Jiyong Park86c9b082021-06-04 19:03:48 +0900128 let offset = sig.merkle_tree_offset;
129 let size = sig.merkle_tree_size as u64;
Jooyung Han1b00bd22022-04-15 15:29:25 +0900130 // Due to unknown reason(b/191344832), we can't enable "direct IO" for the IDSIG file (backing
131 // the hash). For now we don't use "direct IO" but it seems OK since the IDSIG file is very
132 // small and the benefit of direct-IO would be negliable.
Hung Nguyen109cdfa2024-12-06 11:02:44 -0800133 let hash_device = loopdevice::attach(&idsig, offset, size, &LoopConfigOptions::default())
134 .context("Failed to attach idsig to a loop device")?
135 .path;
Jiyong Park86c9b082021-06-04 19:03:48 +0900136
137 // Build a dm-verity target spec from the information from the idsig file. The apk and the
138 // idsig files are used as the data device and the hash device, respectively.
Shikha Panwar414ea892022-10-12 13:45:52 +0000139 let target = DmVerityTargetBuilder::default()
Jiyong Park86c9b082021-06-04 19:03:48 +0900140 .data_device(&data_device, apk_size)
141 .hash_device(&hash_device)
Jiyong Parkbb4a9872021-09-06 15:59:21 +0900142 .root_digest(if let Some(roothash) = roothash {
143 roothash
144 } else {
145 &sig.hashing_info.raw_root_hash
146 })
Jiyong Park86c9b082021-06-04 19:03:48 +0900147 .hash_algorithm(match sig.hashing_info.hash_algorithm {
Shikha Panwar414ea892022-10-12 13:45:52 +0000148 HashAlgorithm::SHA256 => DmVerityHashAlgorithm::SHA256,
Jiyong Park86c9b082021-06-04 19:03:48 +0900149 })
150 .salt(&sig.hashing_info.salt)
151 .build()
152 .context(format!("Merkle tree in {:?} is not compatible with dm-verity", &idsig))?;
153
154 // Actually create a dm-verity block device using the spec.
155 let dm = dm::DeviceMapper::new()?;
156 let mapper_device =
Shikha Panwar414ea892022-10-12 13:45:52 +0000157 dm.create_verity_device(name, &target).context("Failed to create dm-verity device")?;
Jiyong Park86c9b082021-06-04 19:03:48 +0900158
159 Ok(VerityResult { data_device, hash_device, mapper_device })
160}
161
162#[cfg(test)]
Andrew Walbran31e059b2023-06-29 16:33:54 +0000163rdroidtest::test_main!();
Andrew Walbranf714eeb2022-11-30 11:15:56 +0000164
165#[cfg(test)]
Jiyong Park86c9b082021-06-04 19:03:48 +0900166mod tests {
167 use crate::*;
Andrew Walbrana99b1ce2024-01-16 16:52:28 +0000168 use rdroidtest::{ignore_if, rdroidtest};
Alice Wang89cff012022-09-26 10:05:16 +0000169 use std::fs::{File, OpenOptions};
170 use std::io::Write;
Andrew Walbranf714eeb2022-11-30 11:15:56 +0000171 use std::ops::Deref;
Jiyong Park86c9b082021-06-04 19:03:48 +0900172 use std::os::unix::fs::FileExt;
173
174 struct TestContext<'a> {
175 data_backing_file: &'a Path,
176 hash_backing_file: &'a Path,
177 result: &'a VerityResult,
178 }
179
Jiyong Parkd17ff4b2021-07-15 12:32:25 +0900180 // On Android, skip the test on devices that doesn't have the virt APEX
181 // (b/193612136)
182 #[cfg(target_os = "android")]
183 fn should_skip() -> bool {
184 !Path::new("/apex/com.android.virt").exists()
185 }
186 #[cfg(not(target_os = "android"))]
187 fn should_skip() -> bool {
188 false
189 }
190
Jiyong Park86c9b082021-06-04 19:03:48 +0900191 fn create_block_aligned_file(path: &Path, data: &[u8]) {
Chris Wailes9b866f02022-11-16 15:17:16 -0800192 let mut f = File::create(path).unwrap();
Jiyong Park86c9b082021-06-04 19:03:48 +0900193 f.write_all(data).unwrap();
194
195 // Add padding so that the size of the file is multiple of 4096.
196 let aligned_size = (data.len() as u64 + BLOCK_SIZE - 1) & !(BLOCK_SIZE - 1);
197 let padding = aligned_size - data.len() as u64;
198 f.write_all(vec![0; padding as usize].as_slice()).unwrap();
199 }
200
201 fn prepare_inputs(test_dir: &Path, apk: &[u8], idsig: &[u8]) -> (PathBuf, PathBuf) {
202 let apk_path = test_dir.join("test.apk");
203 let idsig_path = test_dir.join("test.apk.idsig");
204 create_block_aligned_file(&apk_path, apk);
205 create_block_aligned_file(&idsig_path, idsig);
206 (apk_path, idsig_path)
207 }
208
209 fn run_test(apk: &[u8], idsig: &[u8], name: &str, check: fn(TestContext)) {
Jiyong Parkbb4a9872021-09-06 15:59:21 +0900210 run_test_with_hash(apk, idsig, name, None, check);
211 }
212
213 fn run_test_with_hash(
214 apk: &[u8],
215 idsig: &[u8],
216 name: &str,
217 roothash: Option<&[u8]>,
218 check: fn(TestContext),
219 ) {
Jiyong Park86c9b082021-06-04 19:03:48 +0900220 let test_dir = tempfile::TempDir::new().unwrap();
Chris Wailes68c39f82021-07-27 16:03:44 -0700221 let (apk_path, idsig_path) = prepare_inputs(test_dir.path(), apk, idsig);
Jiyong Park86c9b082021-06-04 19:03:48 +0900222
223 // Run the program and register clean-ups.
Jiyong Parkbb4a9872021-09-06 15:59:21 +0900224 let ret = enable_verity(&apk_path, &idsig_path, name, roothash).unwrap();
Jiyong Park86c9b082021-06-04 19:03:48 +0900225 let ret = scopeguard::guard(ret, |ret| {
226 loopdevice::detach(ret.data_device).unwrap();
227 loopdevice::detach(ret.hash_device).unwrap();
228 let dm = dm::DeviceMapper::new().unwrap();
229 dm.delete_device_deferred(name).unwrap();
230 });
231
232 check(TestContext {
233 data_backing_file: &apk_path,
234 hash_backing_file: &idsig_path,
235 result: &ret,
236 });
237 }
238
Andrew Walbrana99b1ce2024-01-16 16:52:28 +0000239 #[rdroidtest]
240 #[ignore_if(should_skip())]
Jiyong Park86c9b082021-06-04 19:03:48 +0900241 fn correct_inputs() {
242 let apk = include_bytes!("../testdata/test.apk");
243 let idsig = include_bytes!("../testdata/test.apk.idsig");
244 run_test(apk.as_ref(), idsig.as_ref(), "correct", |ctx| {
245 let verity = fs::read(&ctx.result.mapper_device).unwrap();
246 let original = fs::read(&ctx.result.data_device).unwrap();
247 assert_eq!(verity.len(), original.len()); // fail fast
248 assert_eq!(verity.as_slice(), original.as_slice());
249 });
250 }
251
252 // A single byte change in the APK file causes an IO error
Andrew Walbrana99b1ce2024-01-16 16:52:28 +0000253 #[rdroidtest]
254 #[ignore_if(should_skip())]
Jiyong Park86c9b082021-06-04 19:03:48 +0900255 fn incorrect_apk() {
256 let apk = include_bytes!("../testdata/test.apk");
257 let idsig = include_bytes!("../testdata/test.apk.idsig");
258
259 let mut modified_apk = Vec::new();
260 modified_apk.extend_from_slice(apk);
261 if let Some(byte) = modified_apk.get_mut(100) {
262 *byte = 1;
263 }
264
265 run_test(modified_apk.as_slice(), idsig.as_ref(), "incorrect_apk", |ctx| {
Jiyong Park7b08c572021-09-14 07:28:56 +0900266 fs::read(&ctx.result.mapper_device).expect_err("Should fail");
Jiyong Park86c9b082021-06-04 19:03:48 +0900267 });
268 }
269
270 // A single byte change in the merkle tree also causes an IO error
Andrew Walbrana99b1ce2024-01-16 16:52:28 +0000271 #[rdroidtest]
272 #[ignore_if(should_skip())]
Jiyong Park86c9b082021-06-04 19:03:48 +0900273 fn incorrect_merkle_tree() {
274 let apk = include_bytes!("../testdata/test.apk");
275 let idsig = include_bytes!("../testdata/test.apk.idsig");
276
277 // Make a single-byte change to the merkle tree
Alice Wang89cff012022-09-26 10:05:16 +0000278 let offset = V4Signature::from_idsig_path("testdata/test.apk.idsig")
279 .unwrap()
280 .merkle_tree_offset as usize;
Jiyong Park86c9b082021-06-04 19:03:48 +0900281
282 let mut modified_idsig = Vec::new();
283 modified_idsig.extend_from_slice(idsig);
284 if let Some(byte) = modified_idsig.get_mut(offset + 10) {
285 *byte = 1;
286 }
287
288 run_test(apk.as_ref(), modified_idsig.as_slice(), "incorrect_merkle_tree", |ctx| {
Jiyong Park7b08c572021-09-14 07:28:56 +0900289 fs::read(&ctx.result.mapper_device).expect_err("Should fail");
Jiyong Park86c9b082021-06-04 19:03:48 +0900290 });
291 }
292
293 // APK is not altered when the verity device is created, but later modified. IO error should
294 // occur when trying to read the data around the modified location. This is the main scenario
295 // that we'd like to protect.
Andrew Walbrana99b1ce2024-01-16 16:52:28 +0000296 #[rdroidtest]
297 #[ignore_if(should_skip())]
Jiyong Park86c9b082021-06-04 19:03:48 +0900298 fn tampered_apk() {
299 let apk = include_bytes!("../testdata/test.apk");
300 let idsig = include_bytes!("../testdata/test.apk.idsig");
301
302 run_test(apk.as_ref(), idsig.as_ref(), "tampered_apk", |ctx| {
303 // At this moment, the verity device is created. Then let's change 10 bytes in the
304 // backing data file.
305 const MODIFIED_OFFSET: u64 = 10000;
306 let f = OpenOptions::new().read(true).write(true).open(ctx.data_backing_file).unwrap();
307 f.write_at(&[0, 1], MODIFIED_OFFSET).unwrap();
308
309 // Read around the modified location causes an error
310 let f = File::open(&ctx.result.mapper_device).unwrap();
311 let mut buf = vec![0; 10]; // just read 10 bytes
Jiyong Park7b08c572021-09-14 07:28:56 +0900312 f.read_at(&mut buf, MODIFIED_OFFSET).expect_err("Should fail");
Jiyong Park86c9b082021-06-04 19:03:48 +0900313 });
314 }
315
316 // idsig file is not alread when the verity device is created, but later modified. Unlike to
317 // the APK case, this doesn't occur IO error because the merkle tree is already cached.
Andrew Walbrana99b1ce2024-01-16 16:52:28 +0000318 #[rdroidtest]
319 #[ignore_if(should_skip())]
Jiyong Park86c9b082021-06-04 19:03:48 +0900320 fn tampered_idsig() {
321 let apk = include_bytes!("../testdata/test.apk");
322 let idsig = include_bytes!("../testdata/test.apk.idsig");
323 run_test(apk.as_ref(), idsig.as_ref(), "tampered_idsig", |ctx| {
324 // Change 10 bytes in the merkle tree.
325 let f = OpenOptions::new().read(true).write(true).open(ctx.hash_backing_file).unwrap();
326 f.write_at(&[0, 10], 100).unwrap();
327
328 let verity = fs::read(&ctx.result.mapper_device).unwrap();
329 let original = fs::read(&ctx.result.data_device).unwrap();
330 assert_eq!(verity.len(), original.len());
331 assert_eq!(verity.as_slice(), original.as_slice());
332 });
333 }
334
335 // test if both files are already block devices
Andrew Walbrana99b1ce2024-01-16 16:52:28 +0000336 #[rdroidtest]
337 #[ignore_if(should_skip())]
Jiyong Park86c9b082021-06-04 19:03:48 +0900338 fn inputs_are_block_devices() {
Jiyong Park86c9b082021-06-04 19:03:48 +0900339 let apk = include_bytes!("../testdata/test.apk");
340 let idsig = include_bytes!("../testdata/test.apk.idsig");
341
342 let test_dir = tempfile::TempDir::new().unwrap();
Chris Wailes68c39f82021-07-27 16:03:44 -0700343 let (apk_path, idsig_path) = prepare_inputs(test_dir.path(), apk, idsig);
Jiyong Park86c9b082021-06-04 19:03:48 +0900344
345 // attach the files to loop devices to make them block devices
346 let apk_size = fs::metadata(&apk_path).unwrap().len();
347 let idsig_size = fs::metadata(&idsig_path).unwrap().len();
348
349 // Note that apk_loop_device is not detatched. This is because, when the apk file is
350 // already a block device, `enable_verity` uses the block device as it is. The detatching
351 // of the data device is done in the scopeguard for the return value of `enable_verity`
352 // below. Only the idsig_loop_device needs detatching.
Shikha Panwar743454c2022-10-18 12:50:30 +0000353 let apk_loop_device = loopdevice::attach(
Hung Nguyen109cdfa2024-12-06 11:02:44 -0800354 &apk_path,
355 0,
356 apk_size,
357 &LoopConfigOptions { direct_io: true, ..Default::default() },
Shikha Panwar743454c2022-10-18 12:50:30 +0000358 )
Hung Nguyen109cdfa2024-12-06 11:02:44 -0800359 .unwrap()
360 .path;
Jooyung Han1b00bd22022-04-15 15:29:25 +0900361 let idsig_loop_device = scopeguard::guard(
Hung Nguyen109cdfa2024-12-06 11:02:44 -0800362 loopdevice::attach(&idsig_path, 0, idsig_size, &LoopConfigOptions::default())
363 .unwrap()
364 .path,
Jooyung Han1b00bd22022-04-15 15:29:25 +0900365 |dev| loopdevice::detach(dev).unwrap(),
366 );
Jiyong Park86c9b082021-06-04 19:03:48 +0900367
368 let name = "loop_as_input";
369 // Run the program WITH the loop devices, not the regular files.
Jiyong Parkbb4a9872021-09-06 15:59:21 +0900370 let ret =
371 enable_verity(apk_loop_device.deref(), idsig_loop_device.deref(), name, None).unwrap();
Jiyong Park86c9b082021-06-04 19:03:48 +0900372 let ret = scopeguard::guard(ret, |ret| {
373 loopdevice::detach(ret.data_device).unwrap();
374 loopdevice::detach(ret.hash_device).unwrap();
375 let dm = dm::DeviceMapper::new().unwrap();
376 dm.delete_device_deferred(name).unwrap();
377 });
378
379 let verity = fs::read(&ret.mapper_device).unwrap();
380 let original = fs::read(&apk_path).unwrap();
381 assert_eq!(verity.len(), original.len()); // fail fast
382 assert_eq!(verity.as_slice(), original.as_slice());
383 }
Jiyong Parkbb4a9872021-09-06 15:59:21 +0900384
385 // test with custom roothash
Andrew Walbrana99b1ce2024-01-16 16:52:28 +0000386 #[rdroidtest]
387 #[ignore_if(should_skip())]
Jiyong Parkbb4a9872021-09-06 15:59:21 +0900388 fn correct_custom_roothash() {
389 let apk = include_bytes!("../testdata/test.apk");
390 let idsig = include_bytes!("../testdata/test.apk.idsig");
Alice Wang89cff012022-09-26 10:05:16 +0000391 let roothash = V4Signature::from_idsig_path("testdata/test.apk.idsig")
392 .unwrap()
393 .hashing_info
394 .raw_root_hash;
Jiyong Parka204c762021-09-14 17:27:12 +0900395 run_test_with_hash(
396 apk.as_ref(),
397 idsig.as_ref(),
398 "correct_custom_roothash",
399 Some(&roothash),
400 |ctx| {
401 let verity = fs::read(&ctx.result.mapper_device).unwrap();
402 let original = fs::read(&ctx.result.data_device).unwrap();
403 assert_eq!(verity.len(), original.len()); // fail fast
404 assert_eq!(verity.as_slice(), original.as_slice());
405 },
406 );
Jiyong Parkbb4a9872021-09-06 15:59:21 +0900407 }
Andrew Walbranaa1efc42022-08-10 13:33:57 +0000408
Andrew Walbrana99b1ce2024-01-16 16:52:28 +0000409 #[rdroidtest]
Andrew Walbranaa1efc42022-08-10 13:33:57 +0000410 fn verify_command() {
411 // Check that the command parsing has been configured in a valid way.
412 clap_command().debug_assert();
413 }
Jiyong Park86c9b082021-06-04 19:03:48 +0900414}