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 |
Jiyong Park | 5f0ebea | 2021-06-07 12:53:35 +0900 | [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::*; |
| 237 | use crypt::DmCryptTargetBuilder; |
| 238 | use std::fs::{read, File, OpenOptions}; |
| 239 | use std::io::Write; |
| 240 | |
| 241 | const KEY: &[u8; 32] = b"thirtytwobyteslongreallylongword"; |
| 242 | const DIFFERENT_KEY: &[u8; 32] = b"drowgnolyllaergnolsetybowtytriht"; |
| 243 | |
| 244 | // Create a file in given temp directory with given size |
| 245 | fn prepare_tmpfile(test_dir: &Path, filename: &str, sz: u64) -> PathBuf { |
| 246 | let filepath = test_dir.join(filename); |
| 247 | let f = File::create(&filepath).unwrap(); |
| 248 | f.set_len(sz).unwrap(); |
| 249 | filepath |
| 250 | } |
| 251 | |
| 252 | fn write_to_dev(path: &Path, data: &[u8]) { |
| 253 | let mut f = OpenOptions::new().read(true).write(true).open(&path).unwrap(); |
| 254 | f.write_all(data).unwrap(); |
| 255 | } |
| 256 | |
| 257 | fn delete_device(dm: &DeviceMapper, name: &str) -> Result<()> { |
| 258 | dm.delete_device_deferred(name)?; |
| 259 | wait_for_path_disappears(Path::new(MAPPER_DEV_ROOT).join(&name))?; |
| 260 | Ok(()) |
| 261 | } |
| 262 | |
| 263 | #[test] |
| 264 | fn mapping_again_keeps_data() { |
| 265 | // This test creates 2 different crypt devices using same key backed by same data_device |
| 266 | // -> Write data on dev1 -> Check the data is visible & same on dev2 |
| 267 | let dm = DeviceMapper::new().unwrap(); |
| 268 | let inputimg = include_bytes!("../testdata/rand8k"); |
| 269 | let sz = inputimg.len() as u64; |
| 270 | |
| 271 | let test_dir = tempfile::TempDir::new().unwrap(); |
| 272 | let backing_file = prepare_tmpfile(test_dir.path(), "storage", sz); |
| 273 | let data_device = loopdevice::attach( |
| 274 | backing_file, |
| 275 | 0, |
| 276 | sz, |
| 277 | /*direct_io*/ true, |
| 278 | /*writable*/ true, |
| 279 | ) |
| 280 | .unwrap(); |
| 281 | scopeguard::defer! { |
| 282 | loopdevice::detach(&data_device).unwrap(); |
| 283 | _ = delete_device(&dm, "crypt1"); |
| 284 | _ = delete_device(&dm, "crypt2"); |
| 285 | } |
| 286 | |
| 287 | let target = |
| 288 | DmCryptTargetBuilder::default().data_device(&data_device, sz).key(KEY).build().unwrap(); |
| 289 | |
| 290 | let mut crypt_device = dm.create_crypt_device("crypt1", &target).unwrap(); |
| 291 | write_to_dev(&crypt_device, inputimg); |
| 292 | |
| 293 | // Recreate another device using same target spec & check if the content is the same |
| 294 | crypt_device = dm.create_crypt_device("crypt2", &target).unwrap(); |
| 295 | |
| 296 | let crypt = read(crypt_device).unwrap(); |
| 297 | assert_eq!(inputimg.len(), crypt.len()); // fail early if the size doesn't match |
| 298 | assert_eq!(inputimg, crypt.as_slice()); |
| 299 | } |
| 300 | |
| 301 | #[test] |
| 302 | fn data_inaccessible_with_diff_key() { |
| 303 | // This test creates 2 different crypt devices using different keys backed |
| 304 | // by same data_device -> Write data on dev1 -> Check the data is visible but not the same on dev2 |
| 305 | let dm = DeviceMapper::new().unwrap(); |
| 306 | let inputimg = include_bytes!("../testdata/rand8k"); |
| 307 | let sz = inputimg.len() as u64; |
| 308 | |
| 309 | let test_dir = tempfile::TempDir::new().unwrap(); |
| 310 | let backing_file = prepare_tmpfile(test_dir.path(), "storage", sz); |
| 311 | let data_device = loopdevice::attach( |
| 312 | backing_file, |
| 313 | 0, |
| 314 | sz, |
| 315 | /*direct_io*/ true, |
| 316 | /*writable*/ true, |
| 317 | ) |
| 318 | .unwrap(); |
| 319 | scopeguard::defer! { |
| 320 | loopdevice::detach(&data_device).unwrap(); |
| 321 | _ = delete_device(&dm, "crypt3"); |
| 322 | _ = delete_device(&dm, "crypt4"); |
| 323 | } |
| 324 | |
| 325 | let target = |
| 326 | DmCryptTargetBuilder::default().data_device(&data_device, sz).key(KEY).build().unwrap(); |
| 327 | let target2 = DmCryptTargetBuilder::default() |
| 328 | .data_device(&data_device, sz) |
| 329 | .key(DIFFERENT_KEY) |
| 330 | .build() |
| 331 | .unwrap(); |
| 332 | |
| 333 | let mut crypt_device = dm.create_crypt_device("crypt3", &target).unwrap(); |
| 334 | |
| 335 | write_to_dev(&crypt_device, inputimg); |
| 336 | |
| 337 | // Recreate the crypt device again diff key & check if the content is changed |
| 338 | crypt_device = dm.create_crypt_device("crypt4", &target2).unwrap(); |
| 339 | let crypt = read(crypt_device).unwrap(); |
| 340 | assert_ne!(inputimg, crypt.as_slice()); |
| 341 | } |
| 342 | } |