blob: a8c2833749ded37a3cf0e77cf208b82bc362735e [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// `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 Panwar414ea892022-10-12 13:45:52 +000029//! A library to create device mapper spec & issue ioctls.
30
31#![allow(missing_docs)]
Andrew Walbran3fcebdb2022-11-30 11:16:17 +000032#![cfg_attr(test, allow(unused))]
Jiyong Park86c9b082021-06-04 19:03:48 +090033
Jiyong Park0553ff22021-07-15 12:25:36 +090034use anyhow::{Context, Result};
Jiyong Park86c9b082021-06-04 19:03:48 +090035use std::fs::{File, OpenOptions};
36use std::io::Write;
37use std::mem::size_of;
38use std::os::unix::io::AsRawFd;
39use std::path::{Path, PathBuf};
Andrew Walbran47d316e2024-11-28 18:41:09 +000040use zerocopy::FromZeros;
41use zerocopy::Immutable;
42use zerocopy::IntoBytes;
Jiyong Park86c9b082021-06-04 19:03:48 +090043
Shikha Panwar27cb7e72022-10-13 20:34:45 +000044/// Exposes DmCryptTarget & related builder
45pub mod crypt;
Shikha Panwar414ea892022-10-12 13:45:52 +000046/// Expose util functions
47pub mod util;
48/// Exposes the DmVerityTarget & related builder
49pub mod verity;
Shikha Panwarb278b1c2022-10-14 12:38:32 +000050// Expose loopdevice
51pub mod loopdevice;
Shikha Panwar414ea892022-10-12 13:45:52 +000052
Jiyong Park86c9b082021-06-04 19:03:48 +090053mod sys;
Shikha Panwar27cb7e72022-10-13 20:34:45 +000054use crypt::DmCryptTarget;
Jiyong Park86c9b082021-06-04 19:03:48 +090055use sys::*;
Shikha Panwar414ea892022-10-12 13:45:52 +000056use util::*;
Shikha Panwar27cb7e72022-10-13 20:34:45 +000057use verity::DmVerityTarget;
Jiyong Park86c9b082021-06-04 19:03:48 +090058
59nix::ioctl_readwrite!(_dm_dev_create, DM_IOCTL, Cmd::DM_DEV_CREATE, DmIoctl);
Jiyong Park86c9b082021-06-04 19:03:48 +090060nix::ioctl_readwrite!(_dm_dev_suspend, DM_IOCTL, Cmd::DM_DEV_SUSPEND, DmIoctl);
61nix::ioctl_readwrite!(_dm_table_load, DM_IOCTL, Cmd::DM_TABLE_LOAD, DmIoctl);
Jiyong Park99a35b82021-06-07 10:13:44 +090062nix::ioctl_readwrite!(_dm_dev_remove, DM_IOCTL, Cmd::DM_DEV_REMOVE, DmIoctl);
Jiyong Park86c9b082021-06-04 19:03:48 +090063
Shikha Panwar414ea892022-10-12 13:45:52 +000064/// Create a new (mapper) device
Jiyong Park86c9b082021-06-04 19:03:48 +090065fn dm_dev_create(dm: &DeviceMapper, ioctl: *mut DmIoctl) -> Result<i32> {
66 // SAFETY: `ioctl` is copied into the kernel. It modifies the state in the kernel, not the
67 // state of this process in any way.
68 Ok(unsafe { _dm_dev_create(dm.0.as_raw_fd(), ioctl) }?)
69}
70
Jiyong Park86c9b082021-06-04 19:03:48 +090071fn dm_dev_suspend(dm: &DeviceMapper, ioctl: *mut DmIoctl) -> Result<i32> {
72 // SAFETY: `ioctl` is copied into the kernel. It modifies the state in the kernel, not the
73 // state of this process in any way.
74 Ok(unsafe { _dm_dev_suspend(dm.0.as_raw_fd(), ioctl) }?)
75}
76
77fn dm_table_load(dm: &DeviceMapper, ioctl: *mut DmIoctl) -> Result<i32> {
78 // SAFETY: `ioctl` is copied into the kernel. It modifies the state in the kernel, not the
79 // state of this process in any way.
80 Ok(unsafe { _dm_table_load(dm.0.as_raw_fd(), ioctl) }?)
81}
82
Jiyong Park99a35b82021-06-07 10:13:44 +090083fn dm_dev_remove(dm: &DeviceMapper, ioctl: *mut DmIoctl) -> Result<i32> {
84 // SAFETY: `ioctl` is copied into the kernel. It modifies the state in the kernel, not the
85 // state of this process in any way.
86 Ok(unsafe { _dm_dev_remove(dm.0.as_raw_fd(), ioctl) }?)
87}
88
Jiyong Park86c9b082021-06-04 19:03:48 +090089// `DmTargetSpec` is the header of the data structure for a device-mapper target. When doing the
90// ioctl, one of more `DmTargetSpec` (and its body) are appened to the `DmIoctl` struct.
91#[repr(C)]
Andrew Walbran47d316e2024-11-28 18:41:09 +000092#[derive(Copy, Clone, Immutable, IntoBytes, FromZeros)]
Jiyong Park86c9b082021-06-04 19:03:48 +090093struct DmTargetSpec {
94 sector_start: u64,
95 length: u64, // number of 512 sectors
96 status: i32,
97 next: u32,
98 target_type: [u8; DM_MAX_TYPE_NAME],
99}
100
101impl DmTargetSpec {
102 fn new(target_type: &str) -> Result<Self> {
Frederick Mayle8f795902023-10-23 15:48:34 -0700103 let mut spec = Self::new_zeroed();
Jiyong Park86c9b082021-06-04 19:03:48 +0900104 spec.target_type.as_mut().write_all(target_type.as_bytes())?;
105 Ok(spec)
106 }
Jiyong Park86c9b082021-06-04 19:03:48 +0900107}
108
109impl DmIoctl {
110 fn new(name: &str) -> Result<DmIoctl> {
Frederick Mayle8f795902023-10-23 15:48:34 -0700111 let mut data: Self = Self::new_zeroed();
Jiyong Park86c9b082021-06-04 19:03:48 +0900112 data.version[0] = DM_VERSION_MAJOR;
113 data.version[1] = DM_VERSION_MINOR;
114 data.version[2] = DM_VERSION_PATCHLEVEL;
115 data.data_size = size_of::<Self>() as u32;
116 data.data_start = 0;
117 data.name.as_mut().write_all(name.as_bytes())?;
118 Ok(data)
119 }
120
121 fn set_uuid(&mut self, uuid: &str) -> Result<()> {
122 let mut dst = self.uuid.as_mut();
123 dst.fill(0);
124 dst.write_all(uuid.as_bytes())?;
125 Ok(())
126 }
Jiyong Park86c9b082021-06-04 19:03:48 +0900127}
128
129/// `DeviceMapper` is the entry point for the device mapper framework. It essentially is a file
130/// handle to "/dev/mapper/control".
131pub struct DeviceMapper(File);
132
Jiyong Park5f0ebea2021-06-07 12:53:35 +0900133#[cfg(not(target_os = "android"))]
134const MAPPER_CONTROL: &str = "/dev/mapper/control";
135#[cfg(not(target_os = "android"))]
136const MAPPER_DEV_ROOT: &str = "/dev/mapper";
137
138#[cfg(target_os = "android")]
139const MAPPER_CONTROL: &str = "/dev/device-mapper";
140#[cfg(target_os = "android")]
141const MAPPER_DEV_ROOT: &str = "/dev/block/mapper";
142
Jiyong Park86c9b082021-06-04 19:03:48 +0900143impl DeviceMapper {
144 /// Constructs a new `DeviceMapper` entrypoint. This is essentially the same as opening
145 /// "/dev/mapper/control".
146 pub fn new() -> Result<DeviceMapper> {
Jiyong Park0553ff22021-07-15 12:25:36 +0900147 let f = OpenOptions::new()
148 .read(true)
149 .write(true)
150 .open(MAPPER_CONTROL)
151 .context(format!("failed to open {}", MAPPER_CONTROL))?;
Jiyong Park86c9b082021-06-04 19:03:48 +0900152 Ok(DeviceMapper(f))
153 }
154
Shikha Panwar27cb7e72022-10-13 20:34:45 +0000155 /// Creates a (crypt) device and configure it according to the `target` specification.
156 /// The path to the generated device is "/dev/mapper/<name>".
157 pub fn create_crypt_device(&self, name: &str, target: &DmCryptTarget) -> Result<PathBuf> {
158 self.create_device(name, target.as_slice(), uuid("crypto".as_bytes())?, true)
159 }
160
161 /// Creates a (verity) device and configure it according to the `target` specification.
Jiyong Park86c9b082021-06-04 19:03:48 +0900162 /// The path to the generated device is "/dev/mapper/<name>".
Shikha Panwar414ea892022-10-12 13:45:52 +0000163 pub fn create_verity_device(&self, name: &str, target: &DmVerityTarget) -> Result<PathBuf> {
Shikha Panwar27cb7e72022-10-13 20:34:45 +0000164 self.create_device(name, target.as_slice(), uuid("apkver".as_bytes())?, false)
165 }
166
167 /// Removes a mapper device.
168 pub fn delete_device_deferred(&self, name: &str) -> Result<()> {
169 let mut data = DmIoctl::new(name)?;
170 data.flags |= Flag::DM_DEFERRED_REMOVE;
171 dm_dev_remove(self, &mut data)
172 .context(format!("failed to remove device with name {}", &name))?;
173 Ok(())
174 }
175
176 fn create_device(
177 &self,
178 name: &str,
179 target: &[u8],
180 uid: String,
181 writable: bool,
182 ) -> Result<PathBuf> {
Jiyong Park86c9b082021-06-04 19:03:48 +0900183 // Step 1: create an empty device
Chris Wailes68c39f82021-07-27 16:03:44 -0700184 let mut data = DmIoctl::new(name)?;
Shikha Panwar27cb7e72022-10-13 20:34:45 +0000185 data.set_uuid(&uid)?;
Chris Wailes68c39f82021-07-27 16:03:44 -0700186 dm_dev_create(self, &mut data)
Jiyong Park0553ff22021-07-15 12:25:36 +0900187 .context(format!("failed to create an empty device with name {}", &name))?;
Jiyong Park86c9b082021-06-04 19:03:48 +0900188
189 // Step 2: load table onto the device
Shikha Panwar27cb7e72022-10-13 20:34:45 +0000190 let payload_size = size_of::<DmIoctl>() + target.len();
Jiyong Park86c9b082021-06-04 19:03:48 +0900191
Chris Wailes68c39f82021-07-27 16:03:44 -0700192 let mut data = DmIoctl::new(name)?;
Jiyong Park86c9b082021-06-04 19:03:48 +0900193 data.data_size = payload_size as u32;
194 data.data_start = size_of::<DmIoctl>() as u32;
195 data.target_count = 1;
Shikha Panwar27cb7e72022-10-13 20:34:45 +0000196
197 if !writable {
198 data.flags |= Flag::DM_READONLY_FLAG;
199 }
Jiyong Park86c9b082021-06-04 19:03:48 +0900200
201 let mut payload = Vec::with_capacity(payload_size);
Frederick Mayle8f795902023-10-23 15:48:34 -0700202 payload.extend_from_slice(data.as_bytes());
Shikha Panwar27cb7e72022-10-13 20:34:45 +0000203 payload.extend_from_slice(target);
Chris Wailes68c39f82021-07-27 16:03:44 -0700204 dm_table_load(self, payload.as_mut_ptr() as *mut DmIoctl)
Jiyong Park0553ff22021-07-15 12:25:36 +0900205 .context("failed to load table")?;
Jiyong Park86c9b082021-06-04 19:03:48 +0900206
207 // Step 3: activate the device (note: the term 'suspend' might be misleading, but it
208 // actually activates the table. See include/uapi/linux/dm-ioctl.h
Chris Wailes68c39f82021-07-27 16:03:44 -0700209 let mut data = DmIoctl::new(name)?;
210 dm_dev_suspend(self, &mut data).context("failed to activate")?;
Jiyong Park86c9b082021-06-04 19:03:48 +0900211
212 // Step 4: wait unti the device is created and return the device path
Chris Wailes9b866f02022-11-16 15:17:16 -0800213 let path = Path::new(MAPPER_DEV_ROOT).join(name);
Jiyong Park86c9b082021-06-04 19:03:48 +0900214 wait_for_path(&path)?;
215 Ok(path)
216 }
Jiyong Park86c9b082021-06-04 19:03:48 +0900217}
218
219/// Used to derive a UUID that uniquely identifies a device mapper device when creating it.
Shikha Panwar414ea892022-10-12 13:45:52 +0000220fn uuid(node_id: &[u8]) -> Result<String> {
Jiyong Parkf02061f2021-06-07 09:44:44 +0900221 use std::time::{SystemTime, UNIX_EPOCH};
222 use uuid::v1::{Context, Timestamp};
223 use uuid::Uuid;
224
225 let context = Context::new(0);
226 let now = SystemTime::now().duration_since(UNIX_EPOCH)?;
Charisee96113f32023-01-26 09:00:42 +0000227 let ts = Timestamp::from_unix(context, now.as_secs(), now.subsec_nanos());
Chris Wailesc7c11442023-01-26 15:25:27 -0800228 let uuid = Uuid::new_v1(ts, node_id.try_into()?);
229 Ok(String::from(uuid.hyphenated().encode_lower(&mut Uuid::encode_buffer())))
Jiyong Park86c9b082021-06-04 19:03:48 +0900230}
Shikha Panwar27cb7e72022-10-13 20:34:45 +0000231
232#[cfg(test)]
Andrew Walbran31e059b2023-06-29 16:33:54 +0000233rdroidtest::test_main!();
Andrew Walbran3fcebdb2022-11-30 11:16:17 +0000234
235#[cfg(test)]
Shikha Panwar27cb7e72022-10-13 20:34:45 +0000236mod tests {
237 use super::*;
Hung Nguyen109cdfa2024-12-06 11:02:44 -0800238 use crate::loopdevice::LoopConfigOptions;
Shikha Panwar8e48a172022-11-25 19:01:28 +0000239 use crypt::{CipherType, DmCryptTargetBuilder};
Andrew Walbrana99b1ce2024-01-16 16:52:28 +0000240 use rdroidtest::{ignore_if, rdroidtest};
Shikha Panwar8e48a172022-11-25 19:01:28 +0000241 use rustutils::system_properties;
Shikha Panwar27cb7e72022-10-13 20:34:45 +0000242 use std::fs::{read, File, OpenOptions};
243 use std::io::Write;
244
Shikha Panwar8e48a172022-11-25 19:01:28 +0000245 // Just a logical set of keys to make testing easy. This has no real meaning.
246 struct KeySet<'a> {
247 cipher: CipherType,
248 key: &'a [u8],
249 different_key: &'a [u8],
250 }
251
252 const KEY_SET_XTS: KeySet = KeySet {
253 cipher: CipherType::AES256XTS,
254 key: b"sixtyfourbyteslongsentencearerarebutletsgiveitatrycantbethathard",
255 different_key: b"drahtahtebtnacyrtatievigsteltuberareraecnetnesgnolsetybruofytxis",
256 };
257 const KEY_SET_HCTR2: KeySet = KeySet {
258 cipher: CipherType::AES256HCTR2,
259 key: b"thirtytwobyteslongreallylongword",
260 different_key: b"drowgnolyllaergnolsetybowtytriht",
261 };
Shikha Panwar27cb7e72022-10-13 20:34:45 +0000262
263 // Create a file in given temp directory with given size
264 fn prepare_tmpfile(test_dir: &Path, filename: &str, sz: u64) -> PathBuf {
265 let filepath = test_dir.join(filename);
266 let f = File::create(&filepath).unwrap();
267 f.set_len(sz).unwrap();
268 filepath
269 }
270
271 fn write_to_dev(path: &Path, data: &[u8]) {
Chris Wailes9b866f02022-11-16 15:17:16 -0800272 let mut f = OpenOptions::new().read(true).write(true).open(path).unwrap();
Shikha Panwar27cb7e72022-10-13 20:34:45 +0000273 f.write_all(data).unwrap();
274 }
275
Shikha Panwar8e48a172022-11-25 19:01:28 +0000276 // TODO(b/250880499): delete_device() doesn't really delete it even without DM_DEFERRED_REMOVE.
277 // Hence, we have to create a new device with a different name for each test. Retrying
278 // the test on same machine without reboot will also fail.
Shikha Panwar27cb7e72022-10-13 20:34:45 +0000279 fn delete_device(dm: &DeviceMapper, name: &str) -> Result<()> {
280 dm.delete_device_deferred(name)?;
Chris Wailes9b866f02022-11-16 15:17:16 -0800281 wait_for_path_disappears(Path::new(MAPPER_DEV_ROOT).join(name))?;
Shikha Panwar27cb7e72022-10-13 20:34:45 +0000282 Ok(())
283 }
284
Shikha Panwar8e48a172022-11-25 19:01:28 +0000285 fn is_hctr2_supported() -> bool {
286 // hctr2 is NOT enabled in kernel 5.10 or lower. We run Microdroid tests on kernel versions
287 // 5.10 or above & therefore, we don't really care to skip test on other versions.
288 if let Some(version) = system_properties::read("ro.kernel.version")
289 .expect("Unable to read system property ro.kernel.version")
290 {
291 version != "5.10"
292 } else {
293 panic!("Could not read property: kernel.version!!");
294 }
295 }
296
Andrew Walbrana99b1ce2024-01-16 16:52:28 +0000297 #[rdroidtest]
Shikha Panwar8e48a172022-11-25 19:01:28 +0000298 fn mapping_again_keeps_data_xts() {
299 mapping_again_keeps_data(&KEY_SET_XTS, "name1");
300 }
301
Andrew Walbrana99b1ce2024-01-16 16:52:28 +0000302 #[rdroidtest]
303 #[ignore_if(!is_hctr2_supported())]
Shikha Panwar8e48a172022-11-25 19:01:28 +0000304 fn mapping_again_keeps_data_hctr2() {
Shikha Panwar8e48a172022-11-25 19:01:28 +0000305 mapping_again_keeps_data(&KEY_SET_HCTR2, "name2");
306 }
Andrew Walbran3fcebdb2022-11-30 11:16:17 +0000307
Andrew Walbrana99b1ce2024-01-16 16:52:28 +0000308 #[rdroidtest]
Shikha Panwar8e48a172022-11-25 19:01:28 +0000309 fn data_inaccessible_with_diff_key_xts() {
310 data_inaccessible_with_diff_key(&KEY_SET_XTS, "name3");
311 }
312
Andrew Walbrana99b1ce2024-01-16 16:52:28 +0000313 #[rdroidtest]
314 #[ignore_if(!is_hctr2_supported())]
Shikha Panwar8e48a172022-11-25 19:01:28 +0000315 fn data_inaccessible_with_diff_key_hctr2() {
Shikha Panwar8e48a172022-11-25 19:01:28 +0000316 data_inaccessible_with_diff_key(&KEY_SET_HCTR2, "name4");
317 }
318
319 fn mapping_again_keeps_data(keyset: &KeySet, device: &str) {
Shikha Panwar27cb7e72022-10-13 20:34:45 +0000320 // This test creates 2 different crypt devices using same key backed by same data_device
321 // -> Write data on dev1 -> Check the data is visible & same on dev2
322 let dm = DeviceMapper::new().unwrap();
323 let inputimg = include_bytes!("../testdata/rand8k");
324 let sz = inputimg.len() as u64;
325
326 let test_dir = tempfile::TempDir::new().unwrap();
327 let backing_file = prepare_tmpfile(test_dir.path(), "storage", sz);
328 let data_device = loopdevice::attach(
329 backing_file,
330 0,
331 sz,
Hung Nguyen109cdfa2024-12-06 11:02:44 -0800332 &LoopConfigOptions { direct_io: true, writable: true, ..Default::default() },
Shikha Panwar27cb7e72022-10-13 20:34:45 +0000333 )
Hung Nguyen109cdfa2024-12-06 11:02:44 -0800334 .unwrap()
335 .path;
Shikha Panwar8e48a172022-11-25 19:01:28 +0000336 let device_diff = device.to_owned() + "_diff";
337
Shikha Panwar27cb7e72022-10-13 20:34:45 +0000338 scopeguard::defer! {
339 loopdevice::detach(&data_device).unwrap();
Dan Albert04e5dbd2023-05-08 22:49:40 +0000340 let _ignored1 = delete_device(&dm, device);
341 let _ignored2 = delete_device(&dm, &device_diff);
Shikha Panwar27cb7e72022-10-13 20:34:45 +0000342 }
343
Shikha Panwar8e48a172022-11-25 19:01:28 +0000344 let target = DmCryptTargetBuilder::default()
345 .data_device(&data_device, sz)
346 .cipher(keyset.cipher)
347 .key(keyset.key)
348 .build()
349 .unwrap();
Shikha Panwar27cb7e72022-10-13 20:34:45 +0000350
Shikha Panwar8e48a172022-11-25 19:01:28 +0000351 let mut crypt_device = dm.create_crypt_device(device, &target).unwrap();
Shikha Panwar27cb7e72022-10-13 20:34:45 +0000352 write_to_dev(&crypt_device, inputimg);
353
354 // Recreate another device using same target spec & check if the content is the same
Shikha Panwar8e48a172022-11-25 19:01:28 +0000355 crypt_device = dm.create_crypt_device(&device_diff, &target).unwrap();
Shikha Panwar27cb7e72022-10-13 20:34:45 +0000356
357 let crypt = read(crypt_device).unwrap();
358 assert_eq!(inputimg.len(), crypt.len()); // fail early if the size doesn't match
359 assert_eq!(inputimg, crypt.as_slice());
360 }
361
Shikha Panwar8e48a172022-11-25 19:01:28 +0000362 fn data_inaccessible_with_diff_key(keyset: &KeySet, device: &str) {
Shikha Panwar27cb7e72022-10-13 20:34:45 +0000363 // This test creates 2 different crypt devices using different keys backed
Andrew Walbrana99b1ce2024-01-16 16:52:28 +0000364 // by same data_device -> Write data on dev1 -> Check the data is visible but not the same
365 // on dev2
Shikha Panwar27cb7e72022-10-13 20:34:45 +0000366 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,
Hung Nguyen109cdfa2024-12-06 11:02:44 -0800376 &LoopConfigOptions { direct_io: true, writable: true, ..Default::default() },
Shikha Panwar27cb7e72022-10-13 20:34:45 +0000377 )
Hung Nguyen109cdfa2024-12-06 11:02:44 -0800378 .unwrap()
379 .path;
Shikha Panwar8e48a172022-11-25 19:01:28 +0000380 let device_diff = device.to_owned() + "_diff";
Shikha Panwar27cb7e72022-10-13 20:34:45 +0000381 scopeguard::defer! {
382 loopdevice::detach(&data_device).unwrap();
Dan Albert04e5dbd2023-05-08 22:49:40 +0000383 let _ignored1 = delete_device(&dm, device);
384 let _ignored2 = delete_device(&dm, &device_diff);
Shikha Panwar27cb7e72022-10-13 20:34:45 +0000385 }
386
Shikha Panwar8e48a172022-11-25 19:01:28 +0000387 let target = DmCryptTargetBuilder::default()
388 .data_device(&data_device, sz)
389 .cipher(keyset.cipher)
390 .key(keyset.key)
391 .build()
392 .unwrap();
Shikha Panwar27cb7e72022-10-13 20:34:45 +0000393 let target2 = DmCryptTargetBuilder::default()
394 .data_device(&data_device, sz)
Shikha Panwar8e48a172022-11-25 19:01:28 +0000395 .cipher(keyset.cipher)
396 .key(keyset.different_key)
Shikha Panwar27cb7e72022-10-13 20:34:45 +0000397 .build()
398 .unwrap();
399
Shikha Panwar8e48a172022-11-25 19:01:28 +0000400 let mut crypt_device = dm.create_crypt_device(device, &target).unwrap();
Shikha Panwar27cb7e72022-10-13 20:34:45 +0000401
402 write_to_dev(&crypt_device, inputimg);
403
404 // Recreate the crypt device again diff key & check if the content is changed
Shikha Panwar8e48a172022-11-25 19:01:28 +0000405 crypt_device = dm.create_crypt_device(&device_diff, &target2).unwrap();
Shikha Panwar27cb7e72022-10-13 20:34:45 +0000406 let crypt = read(crypt_device).unwrap();
407 assert_ne!(inputimg, crypt.as_slice());
408 }
409}