Victor Hsieh | 4dc85c9 | 2021-03-15 11:01:23 -0700 | [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 | use nix::ioctl_readwrite; |
| 18 | use std::io; |
| 19 | |
| 20 | // Constants/values from uapi/linux/fsverity.h |
| 21 | const FS_VERITY_METADATA_TYPE_MERKLE_TREE: u64 = 1; |
| 22 | const FS_VERITY_METADATA_TYPE_SIGNATURE: u64 = 3; |
| 23 | const FS_IOCTL_MAGIC: u8 = b'f'; |
| 24 | const FS_IOCTL_READ_VERITY_METADATA: u8 = 135; |
| 25 | |
| 26 | #[repr(C)] |
| 27 | pub struct fsverity_read_metadata_arg { |
| 28 | metadata_type: u64, |
| 29 | offset: u64, |
| 30 | length: u64, |
| 31 | buf_ptr: u64, |
| 32 | __reserved: u64, |
| 33 | } |
| 34 | |
| 35 | ioctl_readwrite!( |
| 36 | read_verity_metadata, |
| 37 | FS_IOCTL_MAGIC, |
| 38 | FS_IOCTL_READ_VERITY_METADATA, |
| 39 | fsverity_read_metadata_arg |
| 40 | ); |
| 41 | |
| 42 | fn read_metadata(fd: i32, metadata_type: u64, offset: u64, buf: &mut [u8]) -> io::Result<usize> { |
| 43 | let mut arg = fsverity_read_metadata_arg { |
| 44 | metadata_type, |
| 45 | offset, |
| 46 | length: buf.len() as u64, |
| 47 | buf_ptr: buf.as_mut_ptr() as u64, |
| 48 | __reserved: 0, |
| 49 | }; |
Joel Galenson | f81b058 | 2021-08-16 13:13:30 -0700 | [diff] [blame] | 50 | Ok(unsafe { read_verity_metadata(fd, &mut arg) }? as usize) |
Victor Hsieh | 4dc85c9 | 2021-03-15 11:01:23 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | /// Read the raw Merkle tree from the fd, if it exists. The API semantics is similar to a regular |
| 54 | /// pread(2), and may not return full requested buffer. |
| 55 | pub fn read_merkle_tree(fd: i32, offset: u64, buf: &mut [u8]) -> io::Result<usize> { |
| 56 | read_metadata(fd, FS_VERITY_METADATA_TYPE_MERKLE_TREE, offset, buf) |
| 57 | } |
| 58 | |
| 59 | /// Read the fs-verity signature from the fd (if exists). The returned signature should be complete. |
| 60 | pub fn read_signature(fd: i32, buf: &mut [u8]) -> io::Result<usize> { |
| 61 | read_metadata(fd, FS_VERITY_METADATA_TYPE_SIGNATURE, 0 /* offset */, buf) |
| 62 | } |