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