Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 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 | //! `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 | |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 24 | mod loopdevice; |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 25 | |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 26 | use anyhow::{bail, Context, Result}; |
Alice Wang | 1bf3d78 | 2022-09-28 07:56:36 +0000 | [diff] [blame] | 27 | use apkverify::{HashAlgorithm, V4Signature}; |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 28 | use clap::{App, Arg}; |
Shikha Panwar | 414ea89 | 2022-10-12 13:45:52 +0000 | [diff] [blame^] | 29 | use dm::util; |
| 30 | use dm::verity::{DmVerityHashAlgorithm, DmVerityTargetBuilder}; |
Inseob Kim | 217038e | 2021-11-25 11:15:06 +0900 | [diff] [blame] | 31 | use itertools::Itertools; |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 32 | use std::fmt::Debug; |
| 33 | use std::fs; |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 34 | use std::os::unix::fs::FileTypeExt; |
| 35 | use std::path::{Path, PathBuf}; |
| 36 | |
| 37 | fn main() -> Result<()> { |
Jooyung Han | 7ce2e53 | 2021-06-16 16:52:02 +0900 | [diff] [blame] | 38 | let matches = App::new("apkdmverity") |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 39 | .about("Creates a dm-verity block device out of APK signed with APK signature scheme V4.") |
Inseob Kim | 217038e | 2021-11-25 11:15:06 +0900 | [diff] [blame] | 40 | .arg(Arg::from_usage( |
Inseob Kim | 197748b | 2021-12-01 19:49:00 +0900 | [diff] [blame] | 41 | "--apk... <apk_path> <idsig_path> <name> <root_hash> \ |
| 42 | 'Input APK file, idsig file, name of the block device, and root hash. \ |
| 43 | The APK file must be signed using the APK signature scheme 4. The \ |
| 44 | block device is created at \"/dev/mapper/<name>\".' root_hash is \ |
| 45 | optional; idsig file's root hash will be used if specified as \"none\"." |
| 46 | )) |
Jeff Vander Stoep | a8dc271 | 2022-07-29 02:33:45 +0200 | [diff] [blame] | 47 | .arg(Arg::with_name("verbose").short('v').long("verbose").help("Shows verbose output")) |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 48 | .get_matches(); |
| 49 | |
Inseob Kim | 217038e | 2021-11-25 11:15:06 +0900 | [diff] [blame] | 50 | let apks = matches.values_of("apk").unwrap(); |
Inseob Kim | 197748b | 2021-12-01 19:49:00 +0900 | [diff] [blame] | 51 | assert!(apks.len() % 4 == 0); |
Inseob Kim | 217038e | 2021-11-25 11:15:06 +0900 | [diff] [blame] | 52 | |
| 53 | let verbose = matches.is_present("verbose"); |
| 54 | |
Inseob Kim | 197748b | 2021-12-01 19:49:00 +0900 | [diff] [blame] | 55 | for (apk, idsig, name, roothash) in apks.tuples() { |
| 56 | let roothash = if roothash != "none" { |
| 57 | Some(util::parse_hexstring(roothash).expect("failed to parse roothash")) |
| 58 | } else { |
| 59 | None |
| 60 | }; |
Inseob Kim | 217038e | 2021-11-25 11:15:06 +0900 | [diff] [blame] | 61 | let ret = enable_verity(apk, idsig, name, roothash.as_deref())?; |
| 62 | if verbose { |
| 63 | println!( |
| 64 | "data_device: {:?}, hash_device: {:?}, mapper_device: {:?}", |
| 65 | ret.data_device, ret.hash_device, ret.mapper_device |
| 66 | ); |
| 67 | } |
Jiyong Park | 99a35b8 | 2021-06-07 10:13:44 +0900 | [diff] [blame] | 68 | } |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 69 | Ok(()) |
| 70 | } |
| 71 | |
| 72 | struct VerityResult { |
| 73 | data_device: PathBuf, |
| 74 | hash_device: PathBuf, |
| 75 | mapper_device: PathBuf, |
| 76 | } |
| 77 | |
| 78 | const BLOCK_SIZE: u64 = 4096; |
| 79 | |
| 80 | // Makes a dm-verity block device out of `apk` and its accompanying `idsig` files. |
Jiyong Park | bb4a987 | 2021-09-06 15:59:21 +0900 | [diff] [blame] | 81 | fn enable_verity<P: AsRef<Path> + Debug>( |
| 82 | apk: P, |
| 83 | idsig: P, |
| 84 | name: &str, |
| 85 | roothash: Option<&[u8]>, |
| 86 | ) -> Result<VerityResult> { |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 87 | // Attach the apk file to a loop device if the apk file is a regular file. If not (i.e. block |
| 88 | // device), we only need to get the size and use the block device as it is. |
| 89 | let (data_device, apk_size) = if fs::metadata(&apk)?.file_type().is_block_device() { |
| 90 | (apk.as_ref().to_path_buf(), util::blkgetsize64(apk.as_ref())?) |
| 91 | } else { |
| 92 | let apk_size = fs::metadata(&apk)?.len(); |
| 93 | if apk_size % BLOCK_SIZE != 0 { |
| 94 | bail!("The size of {:?} is not multiple of {}.", &apk, BLOCK_SIZE) |
| 95 | } |
Jooyung Han | 1b00bd2 | 2022-04-15 15:29:25 +0900 | [diff] [blame] | 96 | ( |
| 97 | loopdevice::attach(&apk, 0, apk_size, /*direct_io*/ true) |
| 98 | .context("Failed to attach APK to a loop device")?, |
| 99 | apk_size, |
| 100 | ) |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 101 | }; |
| 102 | |
| 103 | // Parse the idsig file to locate the merkle tree in it, then attach the file to a loop device |
| 104 | // with the offset so that the start of the merkle tree becomes the beginning of the loop |
| 105 | // device. |
Alice Wang | 89cff01 | 2022-09-26 10:05:16 +0000 | [diff] [blame] | 106 | let sig = V4Signature::from_idsig_path(&idsig)?; |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 107 | let offset = sig.merkle_tree_offset; |
| 108 | let size = sig.merkle_tree_size as u64; |
Jooyung Han | 1b00bd2 | 2022-04-15 15:29:25 +0900 | [diff] [blame] | 109 | // Due to unknown reason(b/191344832), we can't enable "direct IO" for the IDSIG file (backing |
| 110 | // the hash). For now we don't use "direct IO" but it seems OK since the IDSIG file is very |
| 111 | // small and the benefit of direct-IO would be negliable. |
| 112 | let hash_device = loopdevice::attach(&idsig, offset, size, /*direct_io*/ false) |
| 113 | .context("Failed to attach idsig to a loop device")?; |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 114 | |
| 115 | // Build a dm-verity target spec from the information from the idsig file. The apk and the |
| 116 | // idsig files are used as the data device and the hash device, respectively. |
Shikha Panwar | 414ea89 | 2022-10-12 13:45:52 +0000 | [diff] [blame^] | 117 | let target = DmVerityTargetBuilder::default() |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 118 | .data_device(&data_device, apk_size) |
| 119 | .hash_device(&hash_device) |
Jiyong Park | bb4a987 | 2021-09-06 15:59:21 +0900 | [diff] [blame] | 120 | .root_digest(if let Some(roothash) = roothash { |
| 121 | roothash |
| 122 | } else { |
| 123 | &sig.hashing_info.raw_root_hash |
| 124 | }) |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 125 | .hash_algorithm(match sig.hashing_info.hash_algorithm { |
Shikha Panwar | 414ea89 | 2022-10-12 13:45:52 +0000 | [diff] [blame^] | 126 | HashAlgorithm::SHA256 => DmVerityHashAlgorithm::SHA256, |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 127 | }) |
| 128 | .salt(&sig.hashing_info.salt) |
| 129 | .build() |
| 130 | .context(format!("Merkle tree in {:?} is not compatible with dm-verity", &idsig))?; |
| 131 | |
| 132 | // Actually create a dm-verity block device using the spec. |
| 133 | let dm = dm::DeviceMapper::new()?; |
| 134 | let mapper_device = |
Shikha Panwar | 414ea89 | 2022-10-12 13:45:52 +0000 | [diff] [blame^] | 135 | dm.create_verity_device(name, &target).context("Failed to create dm-verity device")?; |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 136 | |
| 137 | Ok(VerityResult { data_device, hash_device, mapper_device }) |
| 138 | } |
| 139 | |
| 140 | #[cfg(test)] |
| 141 | mod tests { |
| 142 | use crate::*; |
Alice Wang | 89cff01 | 2022-09-26 10:05:16 +0000 | [diff] [blame] | 143 | use std::fs::{File, OpenOptions}; |
| 144 | use std::io::Write; |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 145 | use std::os::unix::fs::FileExt; |
| 146 | |
| 147 | struct TestContext<'a> { |
| 148 | data_backing_file: &'a Path, |
| 149 | hash_backing_file: &'a Path, |
| 150 | result: &'a VerityResult, |
| 151 | } |
| 152 | |
Jiyong Park | d17ff4b | 2021-07-15 12:32:25 +0900 | [diff] [blame] | 153 | // On Android, skip the test on devices that doesn't have the virt APEX |
| 154 | // (b/193612136) |
| 155 | #[cfg(target_os = "android")] |
| 156 | fn should_skip() -> bool { |
| 157 | !Path::new("/apex/com.android.virt").exists() |
| 158 | } |
| 159 | #[cfg(not(target_os = "android"))] |
| 160 | fn should_skip() -> bool { |
| 161 | false |
| 162 | } |
| 163 | |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 164 | fn create_block_aligned_file(path: &Path, data: &[u8]) { |
| 165 | let mut f = File::create(&path).unwrap(); |
| 166 | f.write_all(data).unwrap(); |
| 167 | |
| 168 | // Add padding so that the size of the file is multiple of 4096. |
| 169 | let aligned_size = (data.len() as u64 + BLOCK_SIZE - 1) & !(BLOCK_SIZE - 1); |
| 170 | let padding = aligned_size - data.len() as u64; |
| 171 | f.write_all(vec![0; padding as usize].as_slice()).unwrap(); |
| 172 | } |
| 173 | |
| 174 | fn prepare_inputs(test_dir: &Path, apk: &[u8], idsig: &[u8]) -> (PathBuf, PathBuf) { |
| 175 | let apk_path = test_dir.join("test.apk"); |
| 176 | let idsig_path = test_dir.join("test.apk.idsig"); |
| 177 | create_block_aligned_file(&apk_path, apk); |
| 178 | create_block_aligned_file(&idsig_path, idsig); |
| 179 | (apk_path, idsig_path) |
| 180 | } |
| 181 | |
| 182 | fn run_test(apk: &[u8], idsig: &[u8], name: &str, check: fn(TestContext)) { |
Jiyong Park | bb4a987 | 2021-09-06 15:59:21 +0900 | [diff] [blame] | 183 | run_test_with_hash(apk, idsig, name, None, check); |
| 184 | } |
| 185 | |
| 186 | fn run_test_with_hash( |
| 187 | apk: &[u8], |
| 188 | idsig: &[u8], |
| 189 | name: &str, |
| 190 | roothash: Option<&[u8]>, |
| 191 | check: fn(TestContext), |
| 192 | ) { |
Jiyong Park | d17ff4b | 2021-07-15 12:32:25 +0900 | [diff] [blame] | 193 | if should_skip() { |
| 194 | return; |
| 195 | } |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 196 | let test_dir = tempfile::TempDir::new().unwrap(); |
Chris Wailes | 68c39f8 | 2021-07-27 16:03:44 -0700 | [diff] [blame] | 197 | let (apk_path, idsig_path) = prepare_inputs(test_dir.path(), apk, idsig); |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 198 | |
| 199 | // Run the program and register clean-ups. |
Jiyong Park | bb4a987 | 2021-09-06 15:59:21 +0900 | [diff] [blame] | 200 | let ret = enable_verity(&apk_path, &idsig_path, name, roothash).unwrap(); |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 201 | let ret = scopeguard::guard(ret, |ret| { |
| 202 | loopdevice::detach(ret.data_device).unwrap(); |
| 203 | loopdevice::detach(ret.hash_device).unwrap(); |
| 204 | let dm = dm::DeviceMapper::new().unwrap(); |
| 205 | dm.delete_device_deferred(name).unwrap(); |
| 206 | }); |
| 207 | |
| 208 | check(TestContext { |
| 209 | data_backing_file: &apk_path, |
| 210 | hash_backing_file: &idsig_path, |
| 211 | result: &ret, |
| 212 | }); |
| 213 | } |
| 214 | |
| 215 | #[test] |
| 216 | fn correct_inputs() { |
| 217 | let apk = include_bytes!("../testdata/test.apk"); |
| 218 | let idsig = include_bytes!("../testdata/test.apk.idsig"); |
| 219 | run_test(apk.as_ref(), idsig.as_ref(), "correct", |ctx| { |
| 220 | let verity = fs::read(&ctx.result.mapper_device).unwrap(); |
| 221 | let original = fs::read(&ctx.result.data_device).unwrap(); |
| 222 | assert_eq!(verity.len(), original.len()); // fail fast |
| 223 | assert_eq!(verity.as_slice(), original.as_slice()); |
| 224 | }); |
| 225 | } |
| 226 | |
| 227 | // A single byte change in the APK file causes an IO error |
| 228 | #[test] |
| 229 | fn incorrect_apk() { |
| 230 | let apk = include_bytes!("../testdata/test.apk"); |
| 231 | let idsig = include_bytes!("../testdata/test.apk.idsig"); |
| 232 | |
| 233 | let mut modified_apk = Vec::new(); |
| 234 | modified_apk.extend_from_slice(apk); |
| 235 | if let Some(byte) = modified_apk.get_mut(100) { |
| 236 | *byte = 1; |
| 237 | } |
| 238 | |
| 239 | run_test(modified_apk.as_slice(), idsig.as_ref(), "incorrect_apk", |ctx| { |
Jiyong Park | 7b08c57 | 2021-09-14 07:28:56 +0900 | [diff] [blame] | 240 | fs::read(&ctx.result.mapper_device).expect_err("Should fail"); |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 241 | }); |
| 242 | } |
| 243 | |
| 244 | // A single byte change in the merkle tree also causes an IO error |
| 245 | #[test] |
| 246 | fn incorrect_merkle_tree() { |
| 247 | let apk = include_bytes!("../testdata/test.apk"); |
| 248 | let idsig = include_bytes!("../testdata/test.apk.idsig"); |
| 249 | |
| 250 | // Make a single-byte change to the merkle tree |
Alice Wang | 89cff01 | 2022-09-26 10:05:16 +0000 | [diff] [blame] | 251 | let offset = V4Signature::from_idsig_path("testdata/test.apk.idsig") |
| 252 | .unwrap() |
| 253 | .merkle_tree_offset as usize; |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 254 | |
| 255 | let mut modified_idsig = Vec::new(); |
| 256 | modified_idsig.extend_from_slice(idsig); |
| 257 | if let Some(byte) = modified_idsig.get_mut(offset + 10) { |
| 258 | *byte = 1; |
| 259 | } |
| 260 | |
| 261 | run_test(apk.as_ref(), modified_idsig.as_slice(), "incorrect_merkle_tree", |ctx| { |
Jiyong Park | 7b08c57 | 2021-09-14 07:28:56 +0900 | [diff] [blame] | 262 | fs::read(&ctx.result.mapper_device).expect_err("Should fail"); |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 263 | }); |
| 264 | } |
| 265 | |
| 266 | // APK is not altered when the verity device is created, but later modified. IO error should |
| 267 | // occur when trying to read the data around the modified location. This is the main scenario |
| 268 | // that we'd like to protect. |
| 269 | #[test] |
| 270 | fn tampered_apk() { |
| 271 | let apk = include_bytes!("../testdata/test.apk"); |
| 272 | let idsig = include_bytes!("../testdata/test.apk.idsig"); |
| 273 | |
| 274 | run_test(apk.as_ref(), idsig.as_ref(), "tampered_apk", |ctx| { |
| 275 | // At this moment, the verity device is created. Then let's change 10 bytes in the |
| 276 | // backing data file. |
| 277 | const MODIFIED_OFFSET: u64 = 10000; |
| 278 | let f = OpenOptions::new().read(true).write(true).open(ctx.data_backing_file).unwrap(); |
| 279 | f.write_at(&[0, 1], MODIFIED_OFFSET).unwrap(); |
| 280 | |
| 281 | // Read around the modified location causes an error |
| 282 | let f = File::open(&ctx.result.mapper_device).unwrap(); |
| 283 | let mut buf = vec![0; 10]; // just read 10 bytes |
Jiyong Park | 7b08c57 | 2021-09-14 07:28:56 +0900 | [diff] [blame] | 284 | f.read_at(&mut buf, MODIFIED_OFFSET).expect_err("Should fail"); |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 285 | }); |
| 286 | } |
| 287 | |
| 288 | // idsig file is not alread when the verity device is created, but later modified. Unlike to |
| 289 | // the APK case, this doesn't occur IO error because the merkle tree is already cached. |
| 290 | #[test] |
| 291 | fn tampered_idsig() { |
| 292 | let apk = include_bytes!("../testdata/test.apk"); |
| 293 | let idsig = include_bytes!("../testdata/test.apk.idsig"); |
| 294 | run_test(apk.as_ref(), idsig.as_ref(), "tampered_idsig", |ctx| { |
| 295 | // Change 10 bytes in the merkle tree. |
| 296 | let f = OpenOptions::new().read(true).write(true).open(ctx.hash_backing_file).unwrap(); |
| 297 | f.write_at(&[0, 10], 100).unwrap(); |
| 298 | |
| 299 | let verity = fs::read(&ctx.result.mapper_device).unwrap(); |
| 300 | let original = fs::read(&ctx.result.data_device).unwrap(); |
| 301 | assert_eq!(verity.len(), original.len()); |
| 302 | assert_eq!(verity.as_slice(), original.as_slice()); |
| 303 | }); |
| 304 | } |
| 305 | |
| 306 | // test if both files are already block devices |
| 307 | #[test] |
| 308 | fn inputs_are_block_devices() { |
Jiyong Park | d17ff4b | 2021-07-15 12:32:25 +0900 | [diff] [blame] | 309 | if should_skip() { |
| 310 | return; |
| 311 | } |
| 312 | |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 313 | use std::ops::Deref; |
| 314 | let apk = include_bytes!("../testdata/test.apk"); |
| 315 | let idsig = include_bytes!("../testdata/test.apk.idsig"); |
| 316 | |
| 317 | let test_dir = tempfile::TempDir::new().unwrap(); |
Chris Wailes | 68c39f8 | 2021-07-27 16:03:44 -0700 | [diff] [blame] | 318 | let (apk_path, idsig_path) = prepare_inputs(test_dir.path(), apk, idsig); |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 319 | |
| 320 | // attach the files to loop devices to make them block devices |
| 321 | let apk_size = fs::metadata(&apk_path).unwrap().len(); |
| 322 | let idsig_size = fs::metadata(&idsig_path).unwrap().len(); |
| 323 | |
| 324 | // Note that apk_loop_device is not detatched. This is because, when the apk file is |
| 325 | // already a block device, `enable_verity` uses the block device as it is. The detatching |
| 326 | // of the data device is done in the scopeguard for the return value of `enable_verity` |
| 327 | // below. Only the idsig_loop_device needs detatching. |
Jooyung Han | 1b00bd2 | 2022-04-15 15:29:25 +0900 | [diff] [blame] | 328 | let apk_loop_device = loopdevice::attach(&apk_path, 0, apk_size, true).unwrap(); |
| 329 | let idsig_loop_device = scopeguard::guard( |
| 330 | loopdevice::attach(&idsig_path, 0, idsig_size, false).unwrap(), |
| 331 | |dev| loopdevice::detach(dev).unwrap(), |
| 332 | ); |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 333 | |
| 334 | let name = "loop_as_input"; |
| 335 | // Run the program WITH the loop devices, not the regular files. |
Jiyong Park | bb4a987 | 2021-09-06 15:59:21 +0900 | [diff] [blame] | 336 | let ret = |
| 337 | enable_verity(apk_loop_device.deref(), idsig_loop_device.deref(), name, None).unwrap(); |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 338 | let ret = scopeguard::guard(ret, |ret| { |
| 339 | loopdevice::detach(ret.data_device).unwrap(); |
| 340 | loopdevice::detach(ret.hash_device).unwrap(); |
| 341 | let dm = dm::DeviceMapper::new().unwrap(); |
| 342 | dm.delete_device_deferred(name).unwrap(); |
| 343 | }); |
| 344 | |
| 345 | let verity = fs::read(&ret.mapper_device).unwrap(); |
| 346 | let original = fs::read(&apk_path).unwrap(); |
| 347 | assert_eq!(verity.len(), original.len()); // fail fast |
| 348 | assert_eq!(verity.as_slice(), original.as_slice()); |
| 349 | } |
Jiyong Park | bb4a987 | 2021-09-06 15:59:21 +0900 | [diff] [blame] | 350 | |
| 351 | // test with custom roothash |
| 352 | #[test] |
| 353 | fn correct_custom_roothash() { |
| 354 | let apk = include_bytes!("../testdata/test.apk"); |
| 355 | let idsig = include_bytes!("../testdata/test.apk.idsig"); |
Alice Wang | 89cff01 | 2022-09-26 10:05:16 +0000 | [diff] [blame] | 356 | let roothash = V4Signature::from_idsig_path("testdata/test.apk.idsig") |
| 357 | .unwrap() |
| 358 | .hashing_info |
| 359 | .raw_root_hash; |
Jiyong Park | a204c76 | 2021-09-14 17:27:12 +0900 | [diff] [blame] | 360 | run_test_with_hash( |
| 361 | apk.as_ref(), |
| 362 | idsig.as_ref(), |
| 363 | "correct_custom_roothash", |
| 364 | Some(&roothash), |
| 365 | |ctx| { |
| 366 | let verity = fs::read(&ctx.result.mapper_device).unwrap(); |
| 367 | let original = fs::read(&ctx.result.data_device).unwrap(); |
| 368 | assert_eq!(verity.len(), original.len()); // fail fast |
| 369 | assert_eq!(verity.as_slice(), original.as_slice()); |
| 370 | }, |
| 371 | ); |
Jiyong Park | bb4a987 | 2021-09-06 15:59:21 +0900 | [diff] [blame] | 372 | } |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 373 | } |