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 | // `dm` module implements part of the `device-mapper` ioctl interfaces. It currently supports |
| 18 | // creation and deletion of the mapper device. It doesn't support other operations like querying |
| 19 | // the status of the mapper device. And there's no plan to extend the support unless it is |
| 20 | // required. |
| 21 | // |
| 22 | // Why in-house development? [`devicemapper`](https://crates.io/crates/devicemapper) is a public |
| 23 | // Rust implementation of the device mapper APIs. However, it doesn't provide any abstraction for |
| 24 | // the target-specific tables. User has to manually craft the table. Ironically, the library |
| 25 | // provides a lot of APIs for the features that are not required for `apkdmverity` such as listing |
| 26 | // the device mapper block devices that are currently listed in the kernel. Size is an important |
| 27 | // criteria for Microdroid. |
| 28 | |
Shikha Panwar | 414ea89 | 2022-10-12 13:45:52 +0000 | [diff] [blame] | 29 | //! A library to create device mapper spec & issue ioctls. |
| 30 | |
| 31 | #![allow(missing_docs)] |
Andrew Walbran | 3fcebdb | 2022-11-30 11:16:17 +0000 | [diff] [blame] | 32 | #![cfg_attr(test, allow(unused))] |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 33 | |
Jiyong Park | 0553ff2 | 2021-07-15 12:25:36 +0900 | [diff] [blame] | 34 | use anyhow::{Context, Result}; |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 35 | use std::fs::{File, OpenOptions}; |
| 36 | use std::io::Write; |
| 37 | use std::mem::size_of; |
| 38 | use std::os::unix::io::AsRawFd; |
| 39 | use std::path::{Path, PathBuf}; |
Frederick Mayle | 8f79590 | 2023-10-23 15:48:34 -0700 | [diff] [blame] | 40 | use zerocopy::AsBytes; |
| 41 | use zerocopy::FromZeroes; |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 42 | |
Shikha Panwar | 27cb7e7 | 2022-10-13 20:34:45 +0000 | [diff] [blame] | 43 | /// Exposes DmCryptTarget & related builder |
| 44 | pub mod crypt; |
Shikha Panwar | 414ea89 | 2022-10-12 13:45:52 +0000 | [diff] [blame] | 45 | /// Expose util functions |
| 46 | pub mod util; |
| 47 | /// Exposes the DmVerityTarget & related builder |
| 48 | pub mod verity; |
Shikha Panwar | b278b1c | 2022-10-14 12:38:32 +0000 | [diff] [blame] | 49 | // Expose loopdevice |
| 50 | pub mod loopdevice; |
Shikha Panwar | 414ea89 | 2022-10-12 13:45:52 +0000 | [diff] [blame] | 51 | |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 52 | mod sys; |
Shikha Panwar | 27cb7e7 | 2022-10-13 20:34:45 +0000 | [diff] [blame] | 53 | use crypt::DmCryptTarget; |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 54 | use sys::*; |
Shikha Panwar | 414ea89 | 2022-10-12 13:45:52 +0000 | [diff] [blame] | 55 | use util::*; |
Shikha Panwar | 27cb7e7 | 2022-10-13 20:34:45 +0000 | [diff] [blame] | 56 | use verity::DmVerityTarget; |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 57 | |
| 58 | nix::ioctl_readwrite!(_dm_dev_create, DM_IOCTL, Cmd::DM_DEV_CREATE, DmIoctl); |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 59 | nix::ioctl_readwrite!(_dm_dev_suspend, DM_IOCTL, Cmd::DM_DEV_SUSPEND, DmIoctl); |
| 60 | nix::ioctl_readwrite!(_dm_table_load, DM_IOCTL, Cmd::DM_TABLE_LOAD, DmIoctl); |
Jiyong Park | 99a35b8 | 2021-06-07 10:13:44 +0900 | [diff] [blame] | 61 | nix::ioctl_readwrite!(_dm_dev_remove, DM_IOCTL, Cmd::DM_DEV_REMOVE, DmIoctl); |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 62 | |
Shikha Panwar | 414ea89 | 2022-10-12 13:45:52 +0000 | [diff] [blame] | 63 | /// Create a new (mapper) device |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 64 | fn dm_dev_create(dm: &DeviceMapper, ioctl: *mut DmIoctl) -> Result<i32> { |
| 65 | // SAFETY: `ioctl` is copied into the kernel. It modifies the state in the kernel, not the |
| 66 | // state of this process in any way. |
| 67 | Ok(unsafe { _dm_dev_create(dm.0.as_raw_fd(), ioctl) }?) |
| 68 | } |
| 69 | |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 70 | fn dm_dev_suspend(dm: &DeviceMapper, ioctl: *mut DmIoctl) -> Result<i32> { |
| 71 | // SAFETY: `ioctl` is copied into the kernel. It modifies the state in the kernel, not the |
| 72 | // state of this process in any way. |
| 73 | Ok(unsafe { _dm_dev_suspend(dm.0.as_raw_fd(), ioctl) }?) |
| 74 | } |
| 75 | |
| 76 | fn dm_table_load(dm: &DeviceMapper, ioctl: *mut DmIoctl) -> Result<i32> { |
| 77 | // SAFETY: `ioctl` is copied into the kernel. It modifies the state in the kernel, not the |
| 78 | // state of this process in any way. |
| 79 | Ok(unsafe { _dm_table_load(dm.0.as_raw_fd(), ioctl) }?) |
| 80 | } |
| 81 | |
Jiyong Park | 99a35b8 | 2021-06-07 10:13:44 +0900 | [diff] [blame] | 82 | fn dm_dev_remove(dm: &DeviceMapper, ioctl: *mut DmIoctl) -> Result<i32> { |
| 83 | // SAFETY: `ioctl` is copied into the kernel. It modifies the state in the kernel, not the |
| 84 | // state of this process in any way. |
| 85 | Ok(unsafe { _dm_dev_remove(dm.0.as_raw_fd(), ioctl) }?) |
| 86 | } |
| 87 | |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 88 | // `DmTargetSpec` is the header of the data structure for a device-mapper target. When doing the |
| 89 | // ioctl, one of more `DmTargetSpec` (and its body) are appened to the `DmIoctl` struct. |
| 90 | #[repr(C)] |
Frederick Mayle | 8f79590 | 2023-10-23 15:48:34 -0700 | [diff] [blame] | 91 | #[derive(Copy, Clone, AsBytes, FromZeroes)] |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 92 | struct DmTargetSpec { |
| 93 | sector_start: u64, |
| 94 | length: u64, // number of 512 sectors |
| 95 | status: i32, |
| 96 | next: u32, |
| 97 | target_type: [u8; DM_MAX_TYPE_NAME], |
| 98 | } |
| 99 | |
| 100 | impl DmTargetSpec { |
| 101 | fn new(target_type: &str) -> Result<Self> { |
Frederick Mayle | 8f79590 | 2023-10-23 15:48:34 -0700 | [diff] [blame] | 102 | let mut spec = Self::new_zeroed(); |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 103 | spec.target_type.as_mut().write_all(target_type.as_bytes())?; |
| 104 | Ok(spec) |
| 105 | } |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | impl DmIoctl { |
| 109 | fn new(name: &str) -> Result<DmIoctl> { |
Frederick Mayle | 8f79590 | 2023-10-23 15:48:34 -0700 | [diff] [blame] | 110 | let mut data: Self = Self::new_zeroed(); |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 111 | data.version[0] = DM_VERSION_MAJOR; |
| 112 | data.version[1] = DM_VERSION_MINOR; |
| 113 | data.version[2] = DM_VERSION_PATCHLEVEL; |
| 114 | data.data_size = size_of::<Self>() as u32; |
| 115 | data.data_start = 0; |
| 116 | data.name.as_mut().write_all(name.as_bytes())?; |
| 117 | Ok(data) |
| 118 | } |
| 119 | |
| 120 | fn set_uuid(&mut self, uuid: &str) -> Result<()> { |
| 121 | let mut dst = self.uuid.as_mut(); |
| 122 | dst.fill(0); |
| 123 | dst.write_all(uuid.as_bytes())?; |
| 124 | Ok(()) |
| 125 | } |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | /// `DeviceMapper` is the entry point for the device mapper framework. It essentially is a file |
| 129 | /// handle to "/dev/mapper/control". |
| 130 | pub struct DeviceMapper(File); |
| 131 | |
Jiyong Park | 5f0ebea | 2021-06-07 12:53:35 +0900 | [diff] [blame] | 132 | #[cfg(not(target_os = "android"))] |
| 133 | const MAPPER_CONTROL: &str = "/dev/mapper/control"; |
| 134 | #[cfg(not(target_os = "android"))] |
| 135 | const MAPPER_DEV_ROOT: &str = "/dev/mapper"; |
| 136 | |
| 137 | #[cfg(target_os = "android")] |
| 138 | const MAPPER_CONTROL: &str = "/dev/device-mapper"; |
| 139 | #[cfg(target_os = "android")] |
| 140 | const MAPPER_DEV_ROOT: &str = "/dev/block/mapper"; |
| 141 | |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 142 | impl DeviceMapper { |
| 143 | /// Constructs a new `DeviceMapper` entrypoint. This is essentially the same as opening |
| 144 | /// "/dev/mapper/control". |
| 145 | pub fn new() -> Result<DeviceMapper> { |
Jiyong Park | 0553ff2 | 2021-07-15 12:25:36 +0900 | [diff] [blame] | 146 | let f = OpenOptions::new() |
| 147 | .read(true) |
| 148 | .write(true) |
| 149 | .open(MAPPER_CONTROL) |
| 150 | .context(format!("failed to open {}", MAPPER_CONTROL))?; |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 151 | Ok(DeviceMapper(f)) |
| 152 | } |
| 153 | |
Shikha Panwar | 27cb7e7 | 2022-10-13 20:34:45 +0000 | [diff] [blame] | 154 | /// Creates a (crypt) device and configure it according to the `target` specification. |
| 155 | /// The path to the generated device is "/dev/mapper/<name>". |
| 156 | pub fn create_crypt_device(&self, name: &str, target: &DmCryptTarget) -> Result<PathBuf> { |
| 157 | self.create_device(name, target.as_slice(), uuid("crypto".as_bytes())?, true) |
| 158 | } |
| 159 | |
| 160 | /// Creates a (verity) device and configure it according to the `target` specification. |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 161 | /// The path to the generated device is "/dev/mapper/<name>". |
Shikha Panwar | 414ea89 | 2022-10-12 13:45:52 +0000 | [diff] [blame] | 162 | pub fn create_verity_device(&self, name: &str, target: &DmVerityTarget) -> Result<PathBuf> { |
Shikha Panwar | 27cb7e7 | 2022-10-13 20:34:45 +0000 | [diff] [blame] | 163 | self.create_device(name, target.as_slice(), uuid("apkver".as_bytes())?, false) |
| 164 | } |
| 165 | |
| 166 | /// Removes a mapper device. |
| 167 | pub fn delete_device_deferred(&self, name: &str) -> Result<()> { |
| 168 | let mut data = DmIoctl::new(name)?; |
| 169 | data.flags |= Flag::DM_DEFERRED_REMOVE; |
| 170 | dm_dev_remove(self, &mut data) |
| 171 | .context(format!("failed to remove device with name {}", &name))?; |
| 172 | Ok(()) |
| 173 | } |
| 174 | |
| 175 | fn create_device( |
| 176 | &self, |
| 177 | name: &str, |
| 178 | target: &[u8], |
| 179 | uid: String, |
| 180 | writable: bool, |
| 181 | ) -> Result<PathBuf> { |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 182 | // Step 1: create an empty device |
Chris Wailes | 68c39f8 | 2021-07-27 16:03:44 -0700 | [diff] [blame] | 183 | let mut data = DmIoctl::new(name)?; |
Shikha Panwar | 27cb7e7 | 2022-10-13 20:34:45 +0000 | [diff] [blame] | 184 | data.set_uuid(&uid)?; |
Chris Wailes | 68c39f8 | 2021-07-27 16:03:44 -0700 | [diff] [blame] | 185 | dm_dev_create(self, &mut data) |
Jiyong Park | 0553ff2 | 2021-07-15 12:25:36 +0900 | [diff] [blame] | 186 | .context(format!("failed to create an empty device with name {}", &name))?; |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 187 | |
| 188 | // Step 2: load table onto the device |
Shikha Panwar | 27cb7e7 | 2022-10-13 20:34:45 +0000 | [diff] [blame] | 189 | let payload_size = size_of::<DmIoctl>() + target.len(); |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 190 | |
Chris Wailes | 68c39f8 | 2021-07-27 16:03:44 -0700 | [diff] [blame] | 191 | let mut data = DmIoctl::new(name)?; |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 192 | data.data_size = payload_size as u32; |
| 193 | data.data_start = size_of::<DmIoctl>() as u32; |
| 194 | data.target_count = 1; |
Shikha Panwar | 27cb7e7 | 2022-10-13 20:34:45 +0000 | [diff] [blame] | 195 | |
| 196 | if !writable { |
| 197 | data.flags |= Flag::DM_READONLY_FLAG; |
| 198 | } |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 199 | |
| 200 | let mut payload = Vec::with_capacity(payload_size); |
Frederick Mayle | 8f79590 | 2023-10-23 15:48:34 -0700 | [diff] [blame] | 201 | payload.extend_from_slice(data.as_bytes()); |
Shikha Panwar | 27cb7e7 | 2022-10-13 20:34:45 +0000 | [diff] [blame] | 202 | payload.extend_from_slice(target); |
Chris Wailes | 68c39f8 | 2021-07-27 16:03:44 -0700 | [diff] [blame] | 203 | dm_table_load(self, payload.as_mut_ptr() as *mut DmIoctl) |
Jiyong Park | 0553ff2 | 2021-07-15 12:25:36 +0900 | [diff] [blame] | 204 | .context("failed to load table")?; |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 205 | |
| 206 | // Step 3: activate the device (note: the term 'suspend' might be misleading, but it |
| 207 | // actually activates the table. See include/uapi/linux/dm-ioctl.h |
Chris Wailes | 68c39f8 | 2021-07-27 16:03:44 -0700 | [diff] [blame] | 208 | let mut data = DmIoctl::new(name)?; |
| 209 | dm_dev_suspend(self, &mut data).context("failed to activate")?; |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 210 | |
| 211 | // Step 4: wait unti the device is created and return the device path |
Chris Wailes | 9b866f0 | 2022-11-16 15:17:16 -0800 | [diff] [blame] | 212 | let path = Path::new(MAPPER_DEV_ROOT).join(name); |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 213 | wait_for_path(&path)?; |
| 214 | Ok(path) |
| 215 | } |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | /// Used to derive a UUID that uniquely identifies a device mapper device when creating it. |
Shikha Panwar | 414ea89 | 2022-10-12 13:45:52 +0000 | [diff] [blame] | 219 | fn uuid(node_id: &[u8]) -> Result<String> { |
Jiyong Park | f02061f | 2021-06-07 09:44:44 +0900 | [diff] [blame] | 220 | use std::time::{SystemTime, UNIX_EPOCH}; |
| 221 | use uuid::v1::{Context, Timestamp}; |
| 222 | use uuid::Uuid; |
| 223 | |
| 224 | let context = Context::new(0); |
| 225 | let now = SystemTime::now().duration_since(UNIX_EPOCH)?; |
Charisee | 96113f3 | 2023-01-26 09:00:42 +0000 | [diff] [blame] | 226 | let ts = Timestamp::from_unix(context, now.as_secs(), now.subsec_nanos()); |
Chris Wailes | c7c1144 | 2023-01-26 15:25:27 -0800 | [diff] [blame] | 227 | let uuid = Uuid::new_v1(ts, node_id.try_into()?); |
| 228 | Ok(String::from(uuid.hyphenated().encode_lower(&mut Uuid::encode_buffer()))) |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 229 | } |
Shikha Panwar | 27cb7e7 | 2022-10-13 20:34:45 +0000 | [diff] [blame] | 230 | |
| 231 | #[cfg(test)] |
Andrew Walbran | 31e059b | 2023-06-29 16:33:54 +0000 | [diff] [blame] | 232 | rdroidtest::test_main!(); |
Andrew Walbran | 3fcebdb | 2022-11-30 11:16:17 +0000 | [diff] [blame] | 233 | |
| 234 | #[cfg(test)] |
Shikha Panwar | 27cb7e7 | 2022-10-13 20:34:45 +0000 | [diff] [blame] | 235 | mod tests { |
| 236 | use super::*; |
Shikha Panwar | 8e48a17 | 2022-11-25 19:01:28 +0000 | [diff] [blame] | 237 | use crypt::{CipherType, DmCryptTargetBuilder}; |
Andrew Walbran | a99b1ce | 2024-01-16 16:52:28 +0000 | [diff] [blame] | 238 | use rdroidtest::{ignore_if, rdroidtest}; |
Shikha Panwar | 8e48a17 | 2022-11-25 19:01:28 +0000 | [diff] [blame] | 239 | use rustutils::system_properties; |
Shikha Panwar | 27cb7e7 | 2022-10-13 20:34:45 +0000 | [diff] [blame] | 240 | use std::fs::{read, File, OpenOptions}; |
| 241 | use std::io::Write; |
| 242 | |
Shikha Panwar | 8e48a17 | 2022-11-25 19:01:28 +0000 | [diff] [blame] | 243 | // Just a logical set of keys to make testing easy. This has no real meaning. |
| 244 | struct KeySet<'a> { |
| 245 | cipher: CipherType, |
| 246 | key: &'a [u8], |
| 247 | different_key: &'a [u8], |
| 248 | } |
| 249 | |
| 250 | const KEY_SET_XTS: KeySet = KeySet { |
| 251 | cipher: CipherType::AES256XTS, |
| 252 | key: b"sixtyfourbyteslongsentencearerarebutletsgiveitatrycantbethathard", |
| 253 | different_key: b"drahtahtebtnacyrtatievigsteltuberareraecnetnesgnolsetybruofytxis", |
| 254 | }; |
| 255 | const KEY_SET_HCTR2: KeySet = KeySet { |
| 256 | cipher: CipherType::AES256HCTR2, |
| 257 | key: b"thirtytwobyteslongreallylongword", |
| 258 | different_key: b"drowgnolyllaergnolsetybowtytriht", |
| 259 | }; |
Shikha Panwar | 27cb7e7 | 2022-10-13 20:34:45 +0000 | [diff] [blame] | 260 | |
| 261 | // Create a file in given temp directory with given size |
| 262 | fn prepare_tmpfile(test_dir: &Path, filename: &str, sz: u64) -> PathBuf { |
| 263 | let filepath = test_dir.join(filename); |
| 264 | let f = File::create(&filepath).unwrap(); |
| 265 | f.set_len(sz).unwrap(); |
| 266 | filepath |
| 267 | } |
| 268 | |
| 269 | fn write_to_dev(path: &Path, data: &[u8]) { |
Chris Wailes | 9b866f0 | 2022-11-16 15:17:16 -0800 | [diff] [blame] | 270 | let mut f = OpenOptions::new().read(true).write(true).open(path).unwrap(); |
Shikha Panwar | 27cb7e7 | 2022-10-13 20:34:45 +0000 | [diff] [blame] | 271 | f.write_all(data).unwrap(); |
| 272 | } |
| 273 | |
Shikha Panwar | 8e48a17 | 2022-11-25 19:01:28 +0000 | [diff] [blame] | 274 | // TODO(b/250880499): delete_device() doesn't really delete it even without DM_DEFERRED_REMOVE. |
| 275 | // Hence, we have to create a new device with a different name for each test. Retrying |
| 276 | // the test on same machine without reboot will also fail. |
Shikha Panwar | 27cb7e7 | 2022-10-13 20:34:45 +0000 | [diff] [blame] | 277 | fn delete_device(dm: &DeviceMapper, name: &str) -> Result<()> { |
| 278 | dm.delete_device_deferred(name)?; |
Chris Wailes | 9b866f0 | 2022-11-16 15:17:16 -0800 | [diff] [blame] | 279 | wait_for_path_disappears(Path::new(MAPPER_DEV_ROOT).join(name))?; |
Shikha Panwar | 27cb7e7 | 2022-10-13 20:34:45 +0000 | [diff] [blame] | 280 | Ok(()) |
| 281 | } |
| 282 | |
Shikha Panwar | 8e48a17 | 2022-11-25 19:01:28 +0000 | [diff] [blame] | 283 | fn is_hctr2_supported() -> bool { |
| 284 | // hctr2 is NOT enabled in kernel 5.10 or lower. We run Microdroid tests on kernel versions |
| 285 | // 5.10 or above & therefore, we don't really care to skip test on other versions. |
| 286 | if let Some(version) = system_properties::read("ro.kernel.version") |
| 287 | .expect("Unable to read system property ro.kernel.version") |
| 288 | { |
| 289 | version != "5.10" |
| 290 | } else { |
| 291 | panic!("Could not read property: kernel.version!!"); |
| 292 | } |
| 293 | } |
| 294 | |
Andrew Walbran | a99b1ce | 2024-01-16 16:52:28 +0000 | [diff] [blame] | 295 | #[rdroidtest] |
Shikha Panwar | 8e48a17 | 2022-11-25 19:01:28 +0000 | [diff] [blame] | 296 | fn mapping_again_keeps_data_xts() { |
| 297 | mapping_again_keeps_data(&KEY_SET_XTS, "name1"); |
| 298 | } |
| 299 | |
Andrew Walbran | a99b1ce | 2024-01-16 16:52:28 +0000 | [diff] [blame] | 300 | #[rdroidtest] |
| 301 | #[ignore_if(!is_hctr2_supported())] |
Shikha Panwar | 8e48a17 | 2022-11-25 19:01:28 +0000 | [diff] [blame] | 302 | fn mapping_again_keeps_data_hctr2() { |
Shikha Panwar | 8e48a17 | 2022-11-25 19:01:28 +0000 | [diff] [blame] | 303 | mapping_again_keeps_data(&KEY_SET_HCTR2, "name2"); |
| 304 | } |
Andrew Walbran | 3fcebdb | 2022-11-30 11:16:17 +0000 | [diff] [blame] | 305 | |
Andrew Walbran | a99b1ce | 2024-01-16 16:52:28 +0000 | [diff] [blame] | 306 | #[rdroidtest] |
Shikha Panwar | 8e48a17 | 2022-11-25 19:01:28 +0000 | [diff] [blame] | 307 | fn data_inaccessible_with_diff_key_xts() { |
| 308 | data_inaccessible_with_diff_key(&KEY_SET_XTS, "name3"); |
| 309 | } |
| 310 | |
Andrew Walbran | a99b1ce | 2024-01-16 16:52:28 +0000 | [diff] [blame] | 311 | #[rdroidtest] |
| 312 | #[ignore_if(!is_hctr2_supported())] |
Shikha Panwar | 8e48a17 | 2022-11-25 19:01:28 +0000 | [diff] [blame] | 313 | fn data_inaccessible_with_diff_key_hctr2() { |
Shikha Panwar | 8e48a17 | 2022-11-25 19:01:28 +0000 | [diff] [blame] | 314 | data_inaccessible_with_diff_key(&KEY_SET_HCTR2, "name4"); |
| 315 | } |
| 316 | |
| 317 | fn mapping_again_keeps_data(keyset: &KeySet, device: &str) { |
Shikha Panwar | 27cb7e7 | 2022-10-13 20:34:45 +0000 | [diff] [blame] | 318 | // This test creates 2 different crypt devices using same key backed by same data_device |
| 319 | // -> Write data on dev1 -> Check the data is visible & same on dev2 |
| 320 | let dm = DeviceMapper::new().unwrap(); |
| 321 | let inputimg = include_bytes!("../testdata/rand8k"); |
| 322 | let sz = inputimg.len() as u64; |
| 323 | |
| 324 | let test_dir = tempfile::TempDir::new().unwrap(); |
| 325 | let backing_file = prepare_tmpfile(test_dir.path(), "storage", sz); |
| 326 | let data_device = loopdevice::attach( |
| 327 | backing_file, |
| 328 | 0, |
| 329 | sz, |
Andrew Walbran | a99b1ce | 2024-01-16 16:52:28 +0000 | [diff] [blame] | 330 | /* direct_io */ true, |
| 331 | /* writable */ true, |
Shikha Panwar | 27cb7e7 | 2022-10-13 20:34:45 +0000 | [diff] [blame] | 332 | ) |
| 333 | .unwrap(); |
Shikha Panwar | 8e48a17 | 2022-11-25 19:01:28 +0000 | [diff] [blame] | 334 | let device_diff = device.to_owned() + "_diff"; |
| 335 | |
Shikha Panwar | 27cb7e7 | 2022-10-13 20:34:45 +0000 | [diff] [blame] | 336 | scopeguard::defer! { |
| 337 | loopdevice::detach(&data_device).unwrap(); |
Dan Albert | 04e5dbd | 2023-05-08 22:49:40 +0000 | [diff] [blame] | 338 | let _ignored1 = delete_device(&dm, device); |
| 339 | let _ignored2 = delete_device(&dm, &device_diff); |
Shikha Panwar | 27cb7e7 | 2022-10-13 20:34:45 +0000 | [diff] [blame] | 340 | } |
| 341 | |
Shikha Panwar | 8e48a17 | 2022-11-25 19:01:28 +0000 | [diff] [blame] | 342 | let target = DmCryptTargetBuilder::default() |
| 343 | .data_device(&data_device, sz) |
| 344 | .cipher(keyset.cipher) |
| 345 | .key(keyset.key) |
| 346 | .build() |
| 347 | .unwrap(); |
Shikha Panwar | 27cb7e7 | 2022-10-13 20:34:45 +0000 | [diff] [blame] | 348 | |
Shikha Panwar | 8e48a17 | 2022-11-25 19:01:28 +0000 | [diff] [blame] | 349 | let mut crypt_device = dm.create_crypt_device(device, &target).unwrap(); |
Shikha Panwar | 27cb7e7 | 2022-10-13 20:34:45 +0000 | [diff] [blame] | 350 | write_to_dev(&crypt_device, inputimg); |
| 351 | |
| 352 | // Recreate another device using same target spec & check if the content is the same |
Shikha Panwar | 8e48a17 | 2022-11-25 19:01:28 +0000 | [diff] [blame] | 353 | crypt_device = dm.create_crypt_device(&device_diff, &target).unwrap(); |
Shikha Panwar | 27cb7e7 | 2022-10-13 20:34:45 +0000 | [diff] [blame] | 354 | |
| 355 | let crypt = read(crypt_device).unwrap(); |
| 356 | assert_eq!(inputimg.len(), crypt.len()); // fail early if the size doesn't match |
| 357 | assert_eq!(inputimg, crypt.as_slice()); |
| 358 | } |
| 359 | |
Shikha Panwar | 8e48a17 | 2022-11-25 19:01:28 +0000 | [diff] [blame] | 360 | fn data_inaccessible_with_diff_key(keyset: &KeySet, device: &str) { |
Shikha Panwar | 27cb7e7 | 2022-10-13 20:34:45 +0000 | [diff] [blame] | 361 | // This test creates 2 different crypt devices using different keys backed |
Andrew Walbran | a99b1ce | 2024-01-16 16:52:28 +0000 | [diff] [blame] | 362 | // by same data_device -> Write data on dev1 -> Check the data is visible but not the same |
| 363 | // on dev2 |
Shikha Panwar | 27cb7e7 | 2022-10-13 20:34:45 +0000 | [diff] [blame] | 364 | let dm = DeviceMapper::new().unwrap(); |
| 365 | let inputimg = include_bytes!("../testdata/rand8k"); |
| 366 | let sz = inputimg.len() as u64; |
| 367 | |
| 368 | let test_dir = tempfile::TempDir::new().unwrap(); |
| 369 | let backing_file = prepare_tmpfile(test_dir.path(), "storage", sz); |
| 370 | let data_device = loopdevice::attach( |
| 371 | backing_file, |
| 372 | 0, |
| 373 | sz, |
Andrew Walbran | a99b1ce | 2024-01-16 16:52:28 +0000 | [diff] [blame] | 374 | /* direct_io */ true, |
| 375 | /* writable */ true, |
Shikha Panwar | 27cb7e7 | 2022-10-13 20:34:45 +0000 | [diff] [blame] | 376 | ) |
| 377 | .unwrap(); |
Shikha Panwar | 8e48a17 | 2022-11-25 19:01:28 +0000 | [diff] [blame] | 378 | let device_diff = device.to_owned() + "_diff"; |
Shikha Panwar | 27cb7e7 | 2022-10-13 20:34:45 +0000 | [diff] [blame] | 379 | scopeguard::defer! { |
| 380 | loopdevice::detach(&data_device).unwrap(); |
Dan Albert | 04e5dbd | 2023-05-08 22:49:40 +0000 | [diff] [blame] | 381 | let _ignored1 = delete_device(&dm, device); |
| 382 | let _ignored2 = delete_device(&dm, &device_diff); |
Shikha Panwar | 27cb7e7 | 2022-10-13 20:34:45 +0000 | [diff] [blame] | 383 | } |
| 384 | |
Shikha Panwar | 8e48a17 | 2022-11-25 19:01:28 +0000 | [diff] [blame] | 385 | let target = DmCryptTargetBuilder::default() |
| 386 | .data_device(&data_device, sz) |
| 387 | .cipher(keyset.cipher) |
| 388 | .key(keyset.key) |
| 389 | .build() |
| 390 | .unwrap(); |
Shikha Panwar | 27cb7e7 | 2022-10-13 20:34:45 +0000 | [diff] [blame] | 391 | let target2 = DmCryptTargetBuilder::default() |
| 392 | .data_device(&data_device, sz) |
Shikha Panwar | 8e48a17 | 2022-11-25 19:01:28 +0000 | [diff] [blame] | 393 | .cipher(keyset.cipher) |
| 394 | .key(keyset.different_key) |
Shikha Panwar | 27cb7e7 | 2022-10-13 20:34:45 +0000 | [diff] [blame] | 395 | .build() |
| 396 | .unwrap(); |
| 397 | |
Shikha Panwar | 8e48a17 | 2022-11-25 19:01:28 +0000 | [diff] [blame] | 398 | let mut crypt_device = dm.create_crypt_device(device, &target).unwrap(); |
Shikha Panwar | 27cb7e7 | 2022-10-13 20:34:45 +0000 | [diff] [blame] | 399 | |
| 400 | write_to_dev(&crypt_device, inputimg); |
| 401 | |
| 402 | // Recreate the crypt device again diff key & check if the content is changed |
Shikha Panwar | 8e48a17 | 2022-11-25 19:01:28 +0000 | [diff] [blame] | 403 | crypt_device = dm.create_crypt_device(&device_diff, &target2).unwrap(); |
Shikha Panwar | 27cb7e7 | 2022-10-13 20:34:45 +0000 | [diff] [blame] | 404 | let crypt = read(crypt_device).unwrap(); |
| 405 | assert_ne!(inputimg, crypt.as_slice()); |
| 406 | } |
| 407 | } |