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 | 414ea89 | 2022-10-12 13:45:52 +0000 | [diff] [blame] | 41 | /// Expose util functions |
| 42 | pub mod util; |
| 43 | /// Exposes the DmVerityTarget & related builder |
| 44 | pub mod verity; |
Shikha Panwar | b278b1c | 2022-10-14 12:38:32 +0000 | [diff] [blame] | 45 | // Expose loopdevice |
| 46 | pub mod loopdevice; |
Shikha Panwar | 414ea89 | 2022-10-12 13:45:52 +0000 | [diff] [blame] | 47 | |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 48 | mod sys; |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 49 | use sys::*; |
Shikha Panwar | 414ea89 | 2022-10-12 13:45:52 +0000 | [diff] [blame] | 50 | use util::*; |
| 51 | use verity::*; |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 52 | |
| 53 | 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] | 54 | nix::ioctl_readwrite!(_dm_dev_suspend, DM_IOCTL, Cmd::DM_DEV_SUSPEND, DmIoctl); |
| 55 | 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] | 56 | 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] | 57 | |
Shikha Panwar | 414ea89 | 2022-10-12 13:45:52 +0000 | [diff] [blame] | 58 | /// Create a new (mapper) device |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 59 | fn dm_dev_create(dm: &DeviceMapper, ioctl: *mut DmIoctl) -> Result<i32> { |
| 60 | // SAFETY: `ioctl` is copied into the kernel. It modifies the state in the kernel, not the |
| 61 | // state of this process in any way. |
| 62 | Ok(unsafe { _dm_dev_create(dm.0.as_raw_fd(), ioctl) }?) |
| 63 | } |
| 64 | |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 65 | fn dm_dev_suspend(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_suspend(dm.0.as_raw_fd(), ioctl) }?) |
| 69 | } |
| 70 | |
| 71 | fn dm_table_load(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_table_load(dm.0.as_raw_fd(), ioctl) }?) |
| 75 | } |
| 76 | |
Jiyong Park | 99a35b8 | 2021-06-07 10:13:44 +0900 | [diff] [blame] | 77 | fn dm_dev_remove(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_dev_remove(dm.0.as_raw_fd(), ioctl) }?) |
| 81 | } |
| 82 | |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 83 | // `DmTargetSpec` is the header of the data structure for a device-mapper target. When doing the |
| 84 | // ioctl, one of more `DmTargetSpec` (and its body) are appened to the `DmIoctl` struct. |
| 85 | #[repr(C)] |
Jiyong Park | 3c327d2 | 2021-06-08 20:51:54 +0900 | [diff] [blame] | 86 | #[derive(Copy, Clone)] |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 87 | struct DmTargetSpec { |
| 88 | sector_start: u64, |
| 89 | length: u64, // number of 512 sectors |
| 90 | status: i32, |
| 91 | next: u32, |
| 92 | target_type: [u8; DM_MAX_TYPE_NAME], |
| 93 | } |
| 94 | |
Jiyong Park | 3c327d2 | 2021-06-08 20:51:54 +0900 | [diff] [blame] | 95 | // SAFETY: C struct is safe to be initialized from raw data |
| 96 | unsafe impl DataInit for DmTargetSpec {} |
| 97 | |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 98 | impl DmTargetSpec { |
| 99 | fn new(target_type: &str) -> Result<Self> { |
Jiyong Park | 3c327d2 | 2021-06-08 20:51:54 +0900 | [diff] [blame] | 100 | // safe because the size of the array is the same as the size of the struct |
| 101 | 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] | 102 | spec.target_type.as_mut().write_all(target_type.as_bytes())?; |
| 103 | Ok(spec) |
| 104 | } |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | impl DmIoctl { |
| 108 | fn new(name: &str) -> Result<DmIoctl> { |
Jiyong Park | 3c327d2 | 2021-06-08 20:51:54 +0900 | [diff] [blame] | 109 | // safe because the size of the array is the same as the size of the struct |
| 110 | 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] | 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 | |
| 154 | /// Creates a device mapper device and configure it according to the `target` specification. |
| 155 | /// The path to the generated device is "/dev/mapper/<name>". |
Shikha Panwar | 414ea89 | 2022-10-12 13:45:52 +0000 | [diff] [blame] | 156 | pub fn create_verity_device(&self, name: &str, target: &DmVerityTarget) -> Result<PathBuf> { |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 157 | // Step 1: create an empty device |
Chris Wailes | 68c39f8 | 2021-07-27 16:03:44 -0700 | [diff] [blame] | 158 | let mut data = DmIoctl::new(name)?; |
Shikha Panwar | 414ea89 | 2022-10-12 13:45:52 +0000 | [diff] [blame] | 159 | data.set_uuid(&uuid("apkver".as_bytes())?)?; |
Chris Wailes | 68c39f8 | 2021-07-27 16:03:44 -0700 | [diff] [blame] | 160 | dm_dev_create(self, &mut data) |
Jiyong Park | 0553ff2 | 2021-07-15 12:25:36 +0900 | [diff] [blame] | 161 | .context(format!("failed to create an empty device with name {}", &name))?; |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 162 | |
| 163 | // Step 2: load table onto the device |
Jiyong Park | 3c327d2 | 2021-06-08 20:51:54 +0900 | [diff] [blame] | 164 | let payload_size = size_of::<DmIoctl>() + target.as_slice().len(); |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 165 | |
Chris Wailes | 68c39f8 | 2021-07-27 16:03:44 -0700 | [diff] [blame] | 166 | let mut data = DmIoctl::new(name)?; |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 167 | data.data_size = payload_size as u32; |
| 168 | data.data_start = size_of::<DmIoctl>() as u32; |
| 169 | data.target_count = 1; |
| 170 | data.flags |= Flag::DM_READONLY_FLAG; |
| 171 | |
| 172 | let mut payload = Vec::with_capacity(payload_size); |
Jiyong Park | 3c327d2 | 2021-06-08 20:51:54 +0900 | [diff] [blame] | 173 | payload.extend_from_slice(data.as_slice()); |
| 174 | payload.extend_from_slice(target.as_slice()); |
Chris Wailes | 68c39f8 | 2021-07-27 16:03:44 -0700 | [diff] [blame] | 175 | dm_table_load(self, payload.as_mut_ptr() as *mut DmIoctl) |
Jiyong Park | 0553ff2 | 2021-07-15 12:25:36 +0900 | [diff] [blame] | 176 | .context("failed to load table")?; |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 177 | |
| 178 | // Step 3: activate the device (note: the term 'suspend' might be misleading, but it |
| 179 | // actually activates the table. See include/uapi/linux/dm-ioctl.h |
Chris Wailes | 68c39f8 | 2021-07-27 16:03:44 -0700 | [diff] [blame] | 180 | let mut data = DmIoctl::new(name)?; |
| 181 | dm_dev_suspend(self, &mut data).context("failed to activate")?; |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 182 | |
| 183 | // 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] | 184 | let path = Path::new(MAPPER_DEV_ROOT).join(&name); |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 185 | wait_for_path(&path)?; |
| 186 | Ok(path) |
| 187 | } |
| 188 | |
| 189 | /// Removes a mapper device |
| 190 | pub fn delete_device_deferred(&self, name: &str) -> Result<()> { |
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.flags |= Flag::DM_DEFERRED_REMOVE; |
Chris Wailes | 68c39f8 | 2021-07-27 16:03:44 -0700 | [diff] [blame] | 193 | dm_dev_remove(self, &mut data) |
Jiyong Park | 0553ff2 | 2021-07-15 12:25:36 +0900 | [diff] [blame] | 194 | .context(format!("failed to remove device with name {}", &name))?; |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 195 | Ok(()) |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | /// 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] | 200 | fn uuid(node_id: &[u8]) -> Result<String> { |
Jiyong Park | f02061f | 2021-06-07 09:44:44 +0900 | [diff] [blame] | 201 | use std::time::{SystemTime, UNIX_EPOCH}; |
| 202 | use uuid::v1::{Context, Timestamp}; |
| 203 | use uuid::Uuid; |
| 204 | |
| 205 | let context = Context::new(0); |
| 206 | let now = SystemTime::now().duration_since(UNIX_EPOCH)?; |
| 207 | let ts = Timestamp::from_unix(&context, now.as_secs(), now.subsec_nanos()); |
Shikha Panwar | 414ea89 | 2022-10-12 13:45:52 +0000 | [diff] [blame] | 208 | let uuid = Uuid::new_v1(ts, node_id)?; |
Jiyong Park | f02061f | 2021-06-07 09:44:44 +0900 | [diff] [blame] | 209 | Ok(String::from(uuid.to_hyphenated().encode_lower(&mut Uuid::encode_buffer()))) |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 210 | } |