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 | |
| 29 | use crate::util::*; |
| 30 | |
Jiyong Park | 0553ff2 | 2021-07-15 12:25:36 +0900 | [diff] [blame] | 31 | use anyhow::{Context, Result}; |
Jiyong Park | 3c327d2 | 2021-06-08 20:51:54 +0900 | [diff] [blame] | 32 | use data_model::DataInit; |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 33 | use std::fs::{File, OpenOptions}; |
| 34 | use std::io::Write; |
| 35 | use std::mem::size_of; |
| 36 | use std::os::unix::io::AsRawFd; |
| 37 | use std::path::{Path, PathBuf}; |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 38 | |
| 39 | mod sys; |
| 40 | mod verity; |
| 41 | use sys::*; |
| 42 | pub use verity::*; |
| 43 | |
| 44 | 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] | 45 | nix::ioctl_readwrite!(_dm_dev_suspend, DM_IOCTL, Cmd::DM_DEV_SUSPEND, DmIoctl); |
| 46 | 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] | 47 | #[cfg(test)] |
| 48 | 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] | 49 | |
| 50 | fn dm_dev_create(dm: &DeviceMapper, ioctl: *mut DmIoctl) -> Result<i32> { |
| 51 | // SAFETY: `ioctl` is copied into the kernel. It modifies the state in the kernel, not the |
| 52 | // state of this process in any way. |
| 53 | Ok(unsafe { _dm_dev_create(dm.0.as_raw_fd(), ioctl) }?) |
| 54 | } |
| 55 | |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 56 | fn dm_dev_suspend(dm: &DeviceMapper, ioctl: *mut DmIoctl) -> Result<i32> { |
| 57 | // SAFETY: `ioctl` is copied into the kernel. It modifies the state in the kernel, not the |
| 58 | // state of this process in any way. |
| 59 | Ok(unsafe { _dm_dev_suspend(dm.0.as_raw_fd(), ioctl) }?) |
| 60 | } |
| 61 | |
| 62 | fn dm_table_load(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_table_load(dm.0.as_raw_fd(), ioctl) }?) |
| 66 | } |
| 67 | |
Jiyong Park | 99a35b8 | 2021-06-07 10:13:44 +0900 | [diff] [blame] | 68 | #[cfg(test)] |
| 69 | fn dm_dev_remove(dm: &DeviceMapper, ioctl: *mut DmIoctl) -> Result<i32> { |
| 70 | // SAFETY: `ioctl` is copied into the kernel. It modifies the state in the kernel, not the |
| 71 | // state of this process in any way. |
| 72 | Ok(unsafe { _dm_dev_remove(dm.0.as_raw_fd(), ioctl) }?) |
| 73 | } |
| 74 | |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 75 | // `DmTargetSpec` is the header of the data structure for a device-mapper target. When doing the |
| 76 | // ioctl, one of more `DmTargetSpec` (and its body) are appened to the `DmIoctl` struct. |
| 77 | #[repr(C)] |
Jiyong Park | 3c327d2 | 2021-06-08 20:51:54 +0900 | [diff] [blame] | 78 | #[derive(Copy, Clone)] |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 79 | struct DmTargetSpec { |
| 80 | sector_start: u64, |
| 81 | length: u64, // number of 512 sectors |
| 82 | status: i32, |
| 83 | next: u32, |
| 84 | target_type: [u8; DM_MAX_TYPE_NAME], |
| 85 | } |
| 86 | |
Jiyong Park | 3c327d2 | 2021-06-08 20:51:54 +0900 | [diff] [blame] | 87 | // SAFETY: C struct is safe to be initialized from raw data |
| 88 | unsafe impl DataInit for DmTargetSpec {} |
| 89 | |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 90 | impl DmTargetSpec { |
| 91 | fn new(target_type: &str) -> Result<Self> { |
Jiyong Park | 3c327d2 | 2021-06-08 20:51:54 +0900 | [diff] [blame] | 92 | // safe because the size of the array is the same as the size of the struct |
| 93 | 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] | 94 | spec.target_type.as_mut().write_all(target_type.as_bytes())?; |
| 95 | Ok(spec) |
| 96 | } |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | impl DmIoctl { |
| 100 | fn new(name: &str) -> Result<DmIoctl> { |
Jiyong Park | 3c327d2 | 2021-06-08 20:51:54 +0900 | [diff] [blame] | 101 | // safe because the size of the array is the same as the size of the struct |
| 102 | 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] | 103 | data.version[0] = DM_VERSION_MAJOR; |
| 104 | data.version[1] = DM_VERSION_MINOR; |
| 105 | data.version[2] = DM_VERSION_PATCHLEVEL; |
| 106 | data.data_size = size_of::<Self>() as u32; |
| 107 | data.data_start = 0; |
| 108 | data.name.as_mut().write_all(name.as_bytes())?; |
| 109 | Ok(data) |
| 110 | } |
| 111 | |
| 112 | fn set_uuid(&mut self, uuid: &str) -> Result<()> { |
| 113 | let mut dst = self.uuid.as_mut(); |
| 114 | dst.fill(0); |
| 115 | dst.write_all(uuid.as_bytes())?; |
| 116 | Ok(()) |
| 117 | } |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | /// `DeviceMapper` is the entry point for the device mapper framework. It essentially is a file |
| 121 | /// handle to "/dev/mapper/control". |
| 122 | pub struct DeviceMapper(File); |
| 123 | |
Jiyong Park | 5f0ebea | 2021-06-07 12:53:35 +0900 | [diff] [blame] | 124 | #[cfg(not(target_os = "android"))] |
| 125 | const MAPPER_CONTROL: &str = "/dev/mapper/control"; |
| 126 | #[cfg(not(target_os = "android"))] |
| 127 | const MAPPER_DEV_ROOT: &str = "/dev/mapper"; |
| 128 | |
| 129 | #[cfg(target_os = "android")] |
| 130 | const MAPPER_CONTROL: &str = "/dev/device-mapper"; |
| 131 | #[cfg(target_os = "android")] |
| 132 | const MAPPER_DEV_ROOT: &str = "/dev/block/mapper"; |
| 133 | |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 134 | impl DeviceMapper { |
| 135 | /// Constructs a new `DeviceMapper` entrypoint. This is essentially the same as opening |
| 136 | /// "/dev/mapper/control". |
| 137 | pub fn new() -> Result<DeviceMapper> { |
Jiyong Park | 0553ff2 | 2021-07-15 12:25:36 +0900 | [diff] [blame] | 138 | let f = OpenOptions::new() |
| 139 | .read(true) |
| 140 | .write(true) |
| 141 | .open(MAPPER_CONTROL) |
| 142 | .context(format!("failed to open {}", MAPPER_CONTROL))?; |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 143 | Ok(DeviceMapper(f)) |
| 144 | } |
| 145 | |
| 146 | /// Creates a device mapper device and configure it according to the `target` specification. |
| 147 | /// The path to the generated device is "/dev/mapper/<name>". |
| 148 | pub fn create_device(&self, name: &str, target: &DmVerityTarget) -> Result<PathBuf> { |
| 149 | // Step 1: create an empty device |
Chris Wailes | 68c39f8 | 2021-07-27 16:03:44 -0700 | [diff] [blame] | 150 | let mut data = DmIoctl::new(name)?; |
Jiyong Park | f02061f | 2021-06-07 09:44:44 +0900 | [diff] [blame] | 151 | data.set_uuid(&uuid()?)?; |
Chris Wailes | 68c39f8 | 2021-07-27 16:03:44 -0700 | [diff] [blame] | 152 | dm_dev_create(self, &mut data) |
Jiyong Park | 0553ff2 | 2021-07-15 12:25:36 +0900 | [diff] [blame] | 153 | .context(format!("failed to create an empty device with name {}", &name))?; |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 154 | |
| 155 | // Step 2: load table onto the device |
Jiyong Park | 3c327d2 | 2021-06-08 20:51:54 +0900 | [diff] [blame] | 156 | let payload_size = size_of::<DmIoctl>() + target.as_slice().len(); |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 157 | |
Chris Wailes | 68c39f8 | 2021-07-27 16:03:44 -0700 | [diff] [blame] | 158 | let mut data = DmIoctl::new(name)?; |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 159 | data.data_size = payload_size as u32; |
| 160 | data.data_start = size_of::<DmIoctl>() as u32; |
| 161 | data.target_count = 1; |
| 162 | data.flags |= Flag::DM_READONLY_FLAG; |
| 163 | |
| 164 | let mut payload = Vec::with_capacity(payload_size); |
Jiyong Park | 3c327d2 | 2021-06-08 20:51:54 +0900 | [diff] [blame] | 165 | payload.extend_from_slice(data.as_slice()); |
| 166 | payload.extend_from_slice(target.as_slice()); |
Chris Wailes | 68c39f8 | 2021-07-27 16:03:44 -0700 | [diff] [blame] | 167 | dm_table_load(self, payload.as_mut_ptr() as *mut DmIoctl) |
Jiyong Park | 0553ff2 | 2021-07-15 12:25:36 +0900 | [diff] [blame] | 168 | .context("failed to load table")?; |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 169 | |
| 170 | // Step 3: activate the device (note: the term 'suspend' might be misleading, but it |
| 171 | // actually activates the table. See include/uapi/linux/dm-ioctl.h |
Chris Wailes | 68c39f8 | 2021-07-27 16:03:44 -0700 | [diff] [blame] | 172 | let mut data = DmIoctl::new(name)?; |
| 173 | dm_dev_suspend(self, &mut data).context("failed to activate")?; |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 174 | |
| 175 | // 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] | 176 | let path = Path::new(MAPPER_DEV_ROOT).join(&name); |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 177 | wait_for_path(&path)?; |
| 178 | Ok(path) |
| 179 | } |
| 180 | |
| 181 | /// Removes a mapper device |
Jiyong Park | 99a35b8 | 2021-06-07 10:13:44 +0900 | [diff] [blame] | 182 | #[cfg(test)] |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 183 | pub fn delete_device_deferred(&self, name: &str) -> Result<()> { |
Chris Wailes | 68c39f8 | 2021-07-27 16:03:44 -0700 | [diff] [blame] | 184 | let mut data = DmIoctl::new(name)?; |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 185 | data.flags |= Flag::DM_DEFERRED_REMOVE; |
Chris Wailes | 68c39f8 | 2021-07-27 16:03:44 -0700 | [diff] [blame] | 186 | dm_dev_remove(self, &mut data) |
Jiyong Park | 0553ff2 | 2021-07-15 12:25:36 +0900 | [diff] [blame] | 187 | .context(format!("failed to remove device with name {}", &name))?; |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 188 | Ok(()) |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | /// Used to derive a UUID that uniquely identifies a device mapper device when creating it. |
Jiyong Park | f02061f | 2021-06-07 09:44:44 +0900 | [diff] [blame] | 193 | fn uuid() -> Result<String> { |
| 194 | use std::time::{SystemTime, UNIX_EPOCH}; |
| 195 | use uuid::v1::{Context, Timestamp}; |
| 196 | use uuid::Uuid; |
| 197 | |
| 198 | let context = Context::new(0); |
| 199 | let now = SystemTime::now().duration_since(UNIX_EPOCH)?; |
| 200 | let ts = Timestamp::from_unix(&context, now.as_secs(), now.subsec_nanos()); |
| 201 | let uuid = Uuid::new_v1(ts, "apkver".as_bytes())?; |
| 202 | Ok(String::from(uuid.to_hyphenated().encode_lower(&mut Uuid::encode_buffer()))) |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 203 | } |