Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 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 | //! This crate implements AuthFS, a FUSE-based, non-generic filesystem where file access is |
| 18 | //! authenticated. This filesystem assumes the underlying layer is not trusted, e.g. file may be |
| 19 | //! provided by an untrusted host/VM, so that the content can't be simply trusted. However, with a |
Victor Hsieh | 5deba52 | 2022-01-10 17:18:40 -0800 | [diff] [blame^] | 20 | //! known file hash from trusted party, this filesystem can still verify a (read-only) file even if |
| 21 | //! the host/VM as the blob provider is malicious. With the Merkle tree, each read of file block can |
| 22 | //! be verified individually only when needed. |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 23 | //! |
Victor Hsieh | 5deba52 | 2022-01-10 17:18:40 -0800 | [diff] [blame^] | 24 | //! AuthFS only serve files that are specifically configured. Each remote file can be configured to |
| 25 | //! appear as a local file at the mount point. A file configuration may include its remote file |
| 26 | //! identifier and its verification method (e.g. by known digest). |
| 27 | //! |
| 28 | //! AuthFS also support remote directories. A remote directory may be defined by a manifest file, |
| 29 | //! which contains file paths and their corresponding digests. |
| 30 | //! |
| 31 | //! AuthFS can also be configured for write, in which case the remote file server is treated as a |
| 32 | //! (untrusted) storage. The file/directory integrity is maintained in memory in the VM. Currently, |
| 33 | //! the state is not persistent, thus only new file/directory are supported. |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 34 | |
Victor Hsieh | 9978257 | 2022-01-05 15:38:33 -0800 | [diff] [blame] | 35 | use anyhow::{anyhow, bail, Result}; |
Alan Stokes | e1b6e1c | 2021-10-01 12:44:49 +0100 | [diff] [blame] | 36 | use log::error; |
Victor Hsieh | 9978257 | 2022-01-05 15:38:33 -0800 | [diff] [blame] | 37 | use protobuf::Message; |
Victor Hsieh | 50d75ac | 2021-09-03 14:46:55 -0700 | [diff] [blame] | 38 | use std::convert::TryInto; |
Victor Hsieh | 9978257 | 2022-01-05 15:38:33 -0800 | [diff] [blame] | 39 | use std::fs::File; |
Victor Hsieh | d18b975 | 2021-11-09 16:03:34 -0800 | [diff] [blame] | 40 | use std::path::{Path, PathBuf}; |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 41 | use structopt::StructOpt; |
| 42 | |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 43 | mod common; |
| 44 | mod crypto; |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame] | 45 | mod file; |
Victor Hsieh | f7fc3d3 | 2021-11-22 10:20:33 -0800 | [diff] [blame] | 46 | mod fsstat; |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 47 | mod fsverity; |
| 48 | mod fusefs; |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 49 | |
Victor Hsieh | d18b975 | 2021-11-09 16:03:34 -0800 | [diff] [blame] | 50 | use file::{ |
Victor Hsieh | 35dfa1e | 2022-01-12 17:03:35 -0800 | [diff] [blame] | 51 | Attr, EagerChunkReader, InMemoryDir, RemoteDirEditor, RemoteFileEditor, RemoteFileReader, |
| 52 | RemoteMerkleTreeReader, |
Victor Hsieh | d18b975 | 2021-11-09 16:03:34 -0800 | [diff] [blame] | 53 | }; |
Victor Hsieh | f7fc3d3 | 2021-11-22 10:20:33 -0800 | [diff] [blame] | 54 | use fsstat::RemoteFsStatsReader; |
Victor Hsieh | 35dfa1e | 2022-01-12 17:03:35 -0800 | [diff] [blame] | 55 | use fsverity::{merkle_tree_size, VerifiedFileEditor, VerifiedFileReader}; |
Victor Hsieh | 9978257 | 2022-01-05 15:38:33 -0800 | [diff] [blame] | 56 | use fsverity_digests_proto::fsverity_digests::FSVerityDigests; |
Victor Hsieh | 4d6b9d4 | 2021-11-08 15:53:49 -0800 | [diff] [blame] | 57 | use fusefs::{AuthFs, AuthFsEntry}; |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 58 | |
| 59 | #[derive(StructOpt)] |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 60 | struct Args { |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 61 | /// Mount point of AuthFS. |
| 62 | #[structopt(parse(from_os_str))] |
| 63 | mount_point: PathBuf, |
| 64 | |
Victor Hsieh | 2445e33 | 2021-06-04 16:44:53 -0700 | [diff] [blame] | 65 | /// CID of the VM where the service runs. |
| 66 | #[structopt(long)] |
Victor Hsieh | 1a8cd04 | 2021-09-03 16:29:45 -0700 | [diff] [blame] | 67 | cid: u32, |
Victor Hsieh | 2445e33 | 2021-06-04 16:44:53 -0700 | [diff] [blame] | 68 | |
Victor Hsieh | 4cc3b79 | 2021-08-04 12:00:04 -0700 | [diff] [blame] | 69 | /// Extra options to FUSE |
| 70 | #[structopt(short = "o")] |
| 71 | extra_options: Option<String>, |
| 72 | |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame] | 73 | /// A read-only remote file with integrity check. Can be multiple. |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 74 | /// |
Victor Hsieh | 5deba52 | 2022-01-10 17:18:40 -0800 | [diff] [blame^] | 75 | /// For example, `--remote-ro-file 5:sha256-1234abcd` tells the filesystem to associate the |
| 76 | /// file $MOUNTPOINT/5 with a remote FD 5, and has a fs-verity digest with sha256 of the hex |
| 77 | /// value 1234abcd. |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame] | 78 | #[structopt(long, parse(try_from_str = parse_remote_ro_file_option))] |
| 79 | remote_ro_file: Vec<OptionRemoteRoFile>, |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 80 | |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame] | 81 | /// A read-only remote file without integrity check. Can be multiple. |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 82 | /// |
Victor Hsieh | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 83 | /// For example, `--remote-ro-file-unverified 5` tells the filesystem to associate the file |
| 84 | /// $MOUNTPOINT/5 with a remote FD 5. |
| 85 | #[structopt(long)] |
| 86 | remote_ro_file_unverified: Vec<i32>, |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 87 | |
Victor Hsieh | 6a47e7f | 2021-03-03 15:53:49 -0800 | [diff] [blame] | 88 | /// A new read-writable remote file with integrity check. Can be multiple. |
| 89 | /// |
Victor Hsieh | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 90 | /// For example, `--remote-new-rw-file 5` tells the filesystem to associate the file |
| 91 | /// $MOUNTPOINT/5 with a remote FD 5. |
| 92 | #[structopt(long)] |
| 93 | remote_new_rw_file: Vec<i32>, |
Victor Hsieh | 6a47e7f | 2021-03-03 15:53:49 -0800 | [diff] [blame] | 94 | |
Victor Hsieh | d18b975 | 2021-11-09 16:03:34 -0800 | [diff] [blame] | 95 | /// A read-only directory that represents a remote directory. The directory view is constructed |
| 96 | /// and finalized during the filesystem initialization based on the provided mapping file |
| 97 | /// (which is a serialized protobuf of android.security.fsverity.FSVerityDigests, which |
| 98 | /// essentially provides <file path, fs-verity digest> mappings of exported files). The mapping |
| 99 | /// file is supposed to come from a trusted location in order to provide a trusted view as well |
| 100 | /// as verified access of included files with their fs-verity digest. Not all files on the |
| 101 | /// remote host may be included in the mapping file, so the directory view may be partial. The |
| 102 | /// directory structure won't change throughout the filesystem lifetime. |
| 103 | /// |
Victor Hsieh | 9978257 | 2022-01-05 15:38:33 -0800 | [diff] [blame] | 104 | /// For example, `--remote-ro-dir 5:/path/to/mapping:prefix/` tells the filesystem to |
Victor Hsieh | d18b975 | 2021-11-09 16:03:34 -0800 | [diff] [blame] | 105 | /// construct a directory structure defined in the mapping file at $MOUNTPOINT/5, which may |
Victor Hsieh | 9978257 | 2022-01-05 15:38:33 -0800 | [diff] [blame] | 106 | /// include a file like /5/system/framework/framework.jar. "prefix/" tells the filesystem to |
| 107 | /// strip the path (e.g. "system/") from the mount point to match the expected location of the |
Victor Hsieh | d18b975 | 2021-11-09 16:03:34 -0800 | [diff] [blame] | 108 | /// remote FD (e.g. a directory FD of "/system" in the remote). |
| 109 | #[structopt(long, parse(try_from_str = parse_remote_new_ro_dir_option))] |
| 110 | remote_ro_dir: Vec<OptionRemoteRoDir>, |
| 111 | |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 112 | /// A new directory that is assumed empty in the backing filesystem. New files created in this |
| 113 | /// directory are integrity-protected in the same way as --remote-new-verified-file. Can be |
| 114 | /// multiple. |
| 115 | /// |
Victor Hsieh | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 116 | /// For example, `--remote-new-rw-dir 5` tells the filesystem to associate $MOUNTPOINT/5 |
| 117 | /// with a remote dir FD 5. |
| 118 | #[structopt(long)] |
| 119 | remote_new_rw_dir: Vec<i32>, |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 120 | |
Victor Hsieh | 9d0ab62 | 2021-04-26 17:07:02 -0700 | [diff] [blame] | 121 | /// Enable debugging features. |
| 122 | #[structopt(long)] |
| 123 | debug: bool, |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 124 | } |
| 125 | |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame] | 126 | struct OptionRemoteRoFile { |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 127 | /// ID to refer to the remote file. |
Victor Hsieh | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 128 | remote_fd: i32, |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 129 | |
Victor Hsieh | 5deba52 | 2022-01-10 17:18:40 -0800 | [diff] [blame^] | 130 | /// Expected fs-verity digest (with sha256) for the remote file. |
| 131 | digest: String, |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 132 | } |
| 133 | |
Victor Hsieh | d18b975 | 2021-11-09 16:03:34 -0800 | [diff] [blame] | 134 | struct OptionRemoteRoDir { |
| 135 | /// ID to refer to the remote dir. |
| 136 | remote_dir_fd: i32, |
| 137 | |
| 138 | /// A mapping file that describes the expecting file/directory structure and integrity metadata |
| 139 | /// in the remote directory. The file contains serialized protobuf of |
| 140 | /// android.security.fsverity.FSVerityDigests. |
Victor Hsieh | d18b975 | 2021-11-09 16:03:34 -0800 | [diff] [blame] | 141 | mapping_file_path: PathBuf, |
| 142 | |
Victor Hsieh | 9978257 | 2022-01-05 15:38:33 -0800 | [diff] [blame] | 143 | prefix: String, |
Victor Hsieh | d18b975 | 2021-11-09 16:03:34 -0800 | [diff] [blame] | 144 | } |
| 145 | |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame] | 146 | fn parse_remote_ro_file_option(option: &str) -> Result<OptionRemoteRoFile> { |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 147 | let strs: Vec<&str> = option.split(':').collect(); |
Victor Hsieh | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 148 | if strs.len() != 2 { |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 149 | bail!("Invalid option: {}", option); |
| 150 | } |
Victor Hsieh | 5deba52 | 2022-01-10 17:18:40 -0800 | [diff] [blame^] | 151 | if let Some(digest) = strs[1].strip_prefix("sha256-") { |
| 152 | Ok(OptionRemoteRoFile { remote_fd: strs[0].parse::<i32>()?, digest: String::from(digest) }) |
| 153 | } else { |
| 154 | bail!("Unsupported hash algorithm or invalid format: {}", strs[1]); |
| 155 | } |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 156 | } |
| 157 | |
Victor Hsieh | d18b975 | 2021-11-09 16:03:34 -0800 | [diff] [blame] | 158 | fn parse_remote_new_ro_dir_option(option: &str) -> Result<OptionRemoteRoDir> { |
| 159 | let strs: Vec<&str> = option.split(':').collect(); |
| 160 | if strs.len() != 3 { |
| 161 | bail!("Invalid option: {}", option); |
| 162 | } |
| 163 | Ok(OptionRemoteRoDir { |
| 164 | remote_dir_fd: strs[0].parse::<i32>().unwrap(), |
| 165 | mapping_file_path: PathBuf::from(strs[1]), |
Victor Hsieh | 9978257 | 2022-01-05 15:38:33 -0800 | [diff] [blame] | 166 | prefix: String::from(strs[2]), |
Victor Hsieh | d18b975 | 2021-11-09 16:03:34 -0800 | [diff] [blame] | 167 | }) |
| 168 | } |
| 169 | |
Victor Hsieh | 5deba52 | 2022-01-10 17:18:40 -0800 | [diff] [blame^] | 170 | fn from_hex_string(s: &str) -> Result<Vec<u8>> { |
| 171 | if s.len() % 2 == 1 { |
| 172 | bail!("Incomplete hex string: {}", s); |
| 173 | } else { |
| 174 | let results = (0..s.len()) |
| 175 | .step_by(2) |
| 176 | .map(|i| { |
| 177 | u8::from_str_radix(&s[i..i + 2], 16) |
| 178 | .map_err(|e| anyhow!("Cannot parse hex {}: {}", &s[i..i + 2], e)) |
| 179 | }) |
| 180 | .collect::<Result<Vec<_>>>(); |
| 181 | Ok(results?) |
| 182 | } |
| 183 | } |
| 184 | |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 185 | fn new_remote_verified_file_entry( |
Victor Hsieh | 2445e33 | 2021-06-04 16:44:53 -0700 | [diff] [blame] | 186 | service: file::VirtFdService, |
Victor Hsieh | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 187 | remote_fd: i32, |
Victor Hsieh | 5deba52 | 2022-01-10 17:18:40 -0800 | [diff] [blame^] | 188 | expected_digest: &str, |
Victor Hsieh | 2445e33 | 2021-06-04 16:44:53 -0700 | [diff] [blame] | 189 | file_size: u64, |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 190 | ) -> Result<AuthFsEntry> { |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 191 | Ok(AuthFsEntry::VerifiedReadonly { |
Victor Hsieh | 1bcf411 | 2021-03-19 14:26:57 -0700 | [diff] [blame] | 192 | reader: VerifiedFileReader::new( |
Victor Hsieh | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 193 | RemoteFileReader::new(service.clone(), remote_fd), |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 194 | file_size, |
Victor Hsieh | 5deba52 | 2022-01-10 17:18:40 -0800 | [diff] [blame^] | 195 | &from_hex_string(expected_digest)?, |
Victor Hsieh | 35dfa1e | 2022-01-12 17:03:35 -0800 | [diff] [blame] | 196 | EagerChunkReader::new( |
| 197 | RemoteMerkleTreeReader::new(service.clone(), remote_fd), |
| 198 | merkle_tree_size(file_size), |
| 199 | )?, |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 200 | )?, |
| 201 | file_size, |
Victor Hsieh | 1bcf411 | 2021-03-19 14:26:57 -0700 | [diff] [blame] | 202 | }) |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 203 | } |
| 204 | |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 205 | fn new_remote_unverified_file_entry( |
Victor Hsieh | 2445e33 | 2021-06-04 16:44:53 -0700 | [diff] [blame] | 206 | service: file::VirtFdService, |
Victor Hsieh | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 207 | remote_fd: i32, |
Victor Hsieh | 2445e33 | 2021-06-04 16:44:53 -0700 | [diff] [blame] | 208 | file_size: u64, |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 209 | ) -> Result<AuthFsEntry> { |
Victor Hsieh | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 210 | let reader = RemoteFileReader::new(service, remote_fd); |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 211 | Ok(AuthFsEntry::UnverifiedReadonly { reader, file_size }) |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 212 | } |
| 213 | |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 214 | fn new_remote_new_verified_file_entry( |
Victor Hsieh | 2445e33 | 2021-06-04 16:44:53 -0700 | [diff] [blame] | 215 | service: file::VirtFdService, |
Victor Hsieh | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 216 | remote_fd: i32, |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 217 | ) -> Result<AuthFsEntry> { |
Victor Hsieh | f393a72 | 2021-12-08 13:04:27 -0800 | [diff] [blame] | 218 | let remote_file = RemoteFileEditor::new(service.clone(), remote_fd); |
| 219 | Ok(AuthFsEntry::VerifiedNew { |
| 220 | editor: VerifiedFileEditor::new(remote_file), |
| 221 | attr: Attr::new_file(service, remote_fd), |
| 222 | }) |
Victor Hsieh | 6a47e7f | 2021-03-03 15:53:49 -0800 | [diff] [blame] | 223 | } |
| 224 | |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 225 | fn new_remote_new_verified_dir_entry( |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 226 | service: file::VirtFdService, |
Victor Hsieh | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 227 | remote_fd: i32, |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 228 | ) -> Result<AuthFsEntry> { |
Victor Hsieh | f393a72 | 2021-12-08 13:04:27 -0800 | [diff] [blame] | 229 | let dir = RemoteDirEditor::new(service.clone(), remote_fd); |
| 230 | let attr = Attr::new_dir(service, remote_fd); |
| 231 | Ok(AuthFsEntry::VerifiedNewDirectory { dir, attr }) |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 232 | } |
| 233 | |
Victor Hsieh | f7fc3d3 | 2021-11-22 10:20:33 -0800 | [diff] [blame] | 234 | fn prepare_root_dir_entries( |
| 235 | service: file::VirtFdService, |
| 236 | authfs: &mut AuthFs, |
| 237 | args: &Args, |
| 238 | ) -> Result<()> { |
Victor Hsieh | 88e5017 | 2021-10-15 13:27:13 -0700 | [diff] [blame] | 239 | for config in &args.remote_ro_file { |
Victor Hsieh | 4d6b9d4 | 2021-11-08 15:53:49 -0800 | [diff] [blame] | 240 | authfs.add_entry_at_root_dir( |
Victor Hsieh | 60c2f41 | 2021-11-03 13:02:19 -0700 | [diff] [blame] | 241 | remote_fd_to_path_buf(config.remote_fd), |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 242 | new_remote_verified_file_entry( |
Victor Hsieh | 88e5017 | 2021-10-15 13:27:13 -0700 | [diff] [blame] | 243 | service.clone(), |
Victor Hsieh | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 244 | config.remote_fd, |
Victor Hsieh | 5deba52 | 2022-01-10 17:18:40 -0800 | [diff] [blame^] | 245 | &config.digest, |
Victor Hsieh | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 246 | service.getFileSize(config.remote_fd)?.try_into()?, |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 247 | )?, |
Victor Hsieh | 4d6b9d4 | 2021-11-08 15:53:49 -0800 | [diff] [blame] | 248 | )?; |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 249 | } |
| 250 | |
Victor Hsieh | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 251 | for remote_fd in &args.remote_ro_file_unverified { |
| 252 | let remote_fd = *remote_fd; |
Victor Hsieh | 4d6b9d4 | 2021-11-08 15:53:49 -0800 | [diff] [blame] | 253 | authfs.add_entry_at_root_dir( |
Victor Hsieh | 60c2f41 | 2021-11-03 13:02:19 -0700 | [diff] [blame] | 254 | remote_fd_to_path_buf(remote_fd), |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 255 | new_remote_unverified_file_entry( |
Victor Hsieh | 88e5017 | 2021-10-15 13:27:13 -0700 | [diff] [blame] | 256 | service.clone(), |
Victor Hsieh | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 257 | remote_fd, |
| 258 | service.getFileSize(remote_fd)?.try_into()?, |
Victor Hsieh | 88e5017 | 2021-10-15 13:27:13 -0700 | [diff] [blame] | 259 | )?, |
Victor Hsieh | 4d6b9d4 | 2021-11-08 15:53:49 -0800 | [diff] [blame] | 260 | )?; |
Victor Hsieh | 88e5017 | 2021-10-15 13:27:13 -0700 | [diff] [blame] | 261 | } |
| 262 | |
Victor Hsieh | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 263 | for remote_fd in &args.remote_new_rw_file { |
| 264 | let remote_fd = *remote_fd; |
Victor Hsieh | 4d6b9d4 | 2021-11-08 15:53:49 -0800 | [diff] [blame] | 265 | authfs.add_entry_at_root_dir( |
Victor Hsieh | 60c2f41 | 2021-11-03 13:02:19 -0700 | [diff] [blame] | 266 | remote_fd_to_path_buf(remote_fd), |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 267 | new_remote_new_verified_file_entry(service.clone(), remote_fd)?, |
Victor Hsieh | 4d6b9d4 | 2021-11-08 15:53:49 -0800 | [diff] [blame] | 268 | )?; |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 269 | } |
| 270 | |
Victor Hsieh | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 271 | for remote_fd in &args.remote_new_rw_dir { |
| 272 | let remote_fd = *remote_fd; |
Victor Hsieh | 4d6b9d4 | 2021-11-08 15:53:49 -0800 | [diff] [blame] | 273 | authfs.add_entry_at_root_dir( |
Victor Hsieh | 60c2f41 | 2021-11-03 13:02:19 -0700 | [diff] [blame] | 274 | remote_fd_to_path_buf(remote_fd), |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 275 | new_remote_new_verified_dir_entry(service.clone(), remote_fd)?, |
Victor Hsieh | 4d6b9d4 | 2021-11-08 15:53:49 -0800 | [diff] [blame] | 276 | )?; |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 277 | } |
| 278 | |
Victor Hsieh | d18b975 | 2021-11-09 16:03:34 -0800 | [diff] [blame] | 279 | for config in &args.remote_ro_dir { |
| 280 | let dir_root_inode = authfs.add_entry_at_root_dir( |
| 281 | remote_fd_to_path_buf(config.remote_dir_fd), |
| 282 | AuthFsEntry::ReadonlyDirectory { dir: InMemoryDir::new() }, |
| 283 | )?; |
| 284 | |
Victor Hsieh | 9978257 | 2022-01-05 15:38:33 -0800 | [diff] [blame] | 285 | // Build the directory tree based on the mapping file. |
| 286 | let mut reader = File::open(&config.mapping_file_path)?; |
| 287 | let proto = FSVerityDigests::parse_from_reader(&mut reader)?; |
Victor Hsieh | 5deba52 | 2022-01-10 17:18:40 -0800 | [diff] [blame^] | 288 | for (path_str, digest) in &proto.digests { |
| 289 | if digest.hash_alg != "sha256" { |
| 290 | bail!("Unsupported hash algorithm: {}", digest.hash_alg); |
| 291 | } |
| 292 | |
Victor Hsieh | d18b975 | 2021-11-09 16:03:34 -0800 | [diff] [blame] | 293 | let file_entry = { |
Victor Hsieh | 9978257 | 2022-01-05 15:38:33 -0800 | [diff] [blame] | 294 | let remote_path_str = path_str.strip_prefix(&config.prefix).ok_or_else(|| { |
| 295 | anyhow!("Expect path {} to match prefix {}", path_str, config.prefix) |
| 296 | })?; |
Victor Hsieh | d18b975 | 2021-11-09 16:03:34 -0800 | [diff] [blame] | 297 | // TODO(205883847): Not all files will be used. Open the remote file lazily. |
Victor Hsieh | d18b975 | 2021-11-09 16:03:34 -0800 | [diff] [blame] | 298 | let remote_file = RemoteFileReader::new_by_path( |
| 299 | service.clone(), |
| 300 | config.remote_dir_fd, |
Victor Hsieh | 9978257 | 2022-01-05 15:38:33 -0800 | [diff] [blame] | 301 | Path::new(remote_path_str), |
Victor Hsieh | d18b975 | 2021-11-09 16:03:34 -0800 | [diff] [blame] | 302 | )?; |
Victor Hsieh | 5deba52 | 2022-01-10 17:18:40 -0800 | [diff] [blame^] | 303 | let remote_fd = remote_file.get_remote_fd(); |
| 304 | let file_size = service.getFileSize(remote_fd)?.try_into()?; |
| 305 | AuthFsEntry::VerifiedReadonly { |
| 306 | reader: VerifiedFileReader::new( |
| 307 | remote_file, |
| 308 | file_size, |
| 309 | &digest.digest, |
| 310 | EagerChunkReader::new( |
| 311 | RemoteMerkleTreeReader::new(service.clone(), remote_fd), |
| 312 | merkle_tree_size(file_size), |
| 313 | )?, |
| 314 | )?, |
| 315 | file_size, |
| 316 | } |
Victor Hsieh | d18b975 | 2021-11-09 16:03:34 -0800 | [diff] [blame] | 317 | }; |
Victor Hsieh | 9978257 | 2022-01-05 15:38:33 -0800 | [diff] [blame] | 318 | authfs.add_entry_at_ro_dir_by_path(dir_root_inode, Path::new(path_str), file_entry)?; |
Victor Hsieh | d18b975 | 2021-11-09 16:03:34 -0800 | [diff] [blame] | 319 | } |
| 320 | } |
| 321 | |
Victor Hsieh | 4d6b9d4 | 2021-11-08 15:53:49 -0800 | [diff] [blame] | 322 | Ok(()) |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 323 | } |
| 324 | |
Victor Hsieh | 60c2f41 | 2021-11-03 13:02:19 -0700 | [diff] [blame] | 325 | fn remote_fd_to_path_buf(fd: i32) -> PathBuf { |
| 326 | PathBuf::from(fd.to_string()) |
| 327 | } |
| 328 | |
Alan Stokes | e1b6e1c | 2021-10-01 12:44:49 +0100 | [diff] [blame] | 329 | fn try_main() -> Result<()> { |
Victor Hsieh | 2442abc | 2021-11-17 13:25:02 -0800 | [diff] [blame] | 330 | let args = Args::from_args_safe()?; |
Victor Hsieh | 9d0ab62 | 2021-04-26 17:07:02 -0700 | [diff] [blame] | 331 | |
| 332 | let log_level = if args.debug { log::Level::Debug } else { log::Level::Info }; |
| 333 | android_logger::init_once( |
| 334 | android_logger::Config::default().with_tag("authfs").with_min_level(log_level), |
| 335 | ); |
| 336 | |
Victor Hsieh | f7fc3d3 | 2021-11-22 10:20:33 -0800 | [diff] [blame] | 337 | let service = file::get_rpc_binder_service(args.cid)?; |
| 338 | let mut authfs = AuthFs::new(RemoteFsStatsReader::new(service.clone())); |
| 339 | prepare_root_dir_entries(service, &mut authfs, &args)?; |
| 340 | |
Victor Hsieh | 79f296b | 2021-12-02 15:38:08 -0800 | [diff] [blame] | 341 | fusefs::mount_and_enter_message_loop(authfs, &args.mount_point, &args.extra_options)?; |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 342 | bail!("Unexpected exit after the handler loop") |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 343 | } |
Alan Stokes | e1b6e1c | 2021-10-01 12:44:49 +0100 | [diff] [blame] | 344 | |
| 345 | fn main() { |
| 346 | if let Err(e) = try_main() { |
| 347 | error!("failed with {:?}", e); |
| 348 | std::process::exit(1); |
| 349 | } |
| 350 | } |
Victor Hsieh | 5deba52 | 2022-01-10 17:18:40 -0800 | [diff] [blame^] | 351 | |
| 352 | #[cfg(test)] |
| 353 | mod tests { |
| 354 | use super::*; |
| 355 | |
| 356 | #[test] |
| 357 | fn parse_hex_string() { |
| 358 | assert_eq!(from_hex_string("deadbeef").unwrap(), vec![0xde, 0xad, 0xbe, 0xef]); |
| 359 | assert_eq!(from_hex_string("DEADBEEF").unwrap(), vec![0xde, 0xad, 0xbe, 0xef]); |
| 360 | assert_eq!(from_hex_string("").unwrap(), Vec::<u8>::new()); |
| 361 | |
| 362 | assert!(from_hex_string("deadbee").is_err()); |
| 363 | assert!(from_hex_string("X").is_err()); |
| 364 | } |
| 365 | } |