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 |
| 20 | //! public key from a trusted party, this filesystem can still verify a (read-only) file signed by |
| 21 | //! the trusted party even if the host/VM as the blob provider is malicious. With the Merkle tree, |
| 22 | //! each read of file block can be verified individually only when needed. |
| 23 | //! |
| 24 | //! AuthFS only serve files that are specifically configured. A file configuration may include the |
| 25 | //! source (e.g. local file or remote file server), verification method (e.g. certificate for |
| 26 | //! fs-verity verification, or no verification if expected to mount over dm-verity), and file ID. |
| 27 | //! Regardless of the actual file name, the exposed file names through AuthFS are currently integer, |
| 28 | //! e.g. /mountpoint/42. |
| 29 | |
Andrew Walbran | cc09386 | 2021-03-05 16:59:35 +0000 | [diff] [blame] | 30 | use anyhow::{bail, Context, Result}; |
Alan Stokes | e1b6e1c | 2021-10-01 12:44:49 +0100 | [diff] [blame^] | 31 | use log::error; |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 32 | use std::collections::BTreeMap; |
Victor Hsieh | 50d75ac | 2021-09-03 14:46:55 -0700 | [diff] [blame] | 33 | use std::convert::TryInto; |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 34 | use std::fs::File; |
| 35 | use std::io::Read; |
Victor Hsieh | 6cf75b5 | 2021-04-01 12:45:49 -0700 | [diff] [blame] | 36 | use std::path::{Path, PathBuf}; |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 37 | use structopt::StructOpt; |
| 38 | |
| 39 | mod auth; |
| 40 | mod common; |
| 41 | mod crypto; |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame] | 42 | mod file; |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 43 | mod fsverity; |
| 44 | mod fusefs; |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 45 | |
| 46 | use auth::FakeAuthenticator; |
Victor Hsieh | 6a47e7f | 2021-03-03 15:53:49 -0800 | [diff] [blame] | 47 | use file::{LocalFileReader, RemoteFileEditor, RemoteFileReader, RemoteMerkleTreeReader}; |
| 48 | use fsverity::{VerifiedFileEditor, VerifiedFileReader}; |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 49 | use fusefs::{FileConfig, Inode}; |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 50 | |
| 51 | #[derive(StructOpt)] |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 52 | struct Args { |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 53 | /// Mount point of AuthFS. |
| 54 | #[structopt(parse(from_os_str))] |
| 55 | mount_point: PathBuf, |
| 56 | |
Victor Hsieh | 2445e33 | 2021-06-04 16:44:53 -0700 | [diff] [blame] | 57 | /// CID of the VM where the service runs. |
| 58 | #[structopt(long)] |
Victor Hsieh | 1a8cd04 | 2021-09-03 16:29:45 -0700 | [diff] [blame] | 59 | cid: u32, |
Victor Hsieh | 2445e33 | 2021-06-04 16:44:53 -0700 | [diff] [blame] | 60 | |
Victor Hsieh | 4cc3b79 | 2021-08-04 12:00:04 -0700 | [diff] [blame] | 61 | /// Extra options to FUSE |
| 62 | #[structopt(short = "o")] |
| 63 | extra_options: Option<String>, |
| 64 | |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame] | 65 | /// A read-only remote file with integrity check. Can be multiple. |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 66 | /// |
Victor Hsieh | 50d75ac | 2021-09-03 14:46:55 -0700 | [diff] [blame] | 67 | /// For example, `--remote-verified-file 5:10:/path/to/cert` tells the filesystem to associate |
| 68 | /// entry 5 with a remote file 10, and need to be verified against the /path/to/cert. |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame] | 69 | #[structopt(long, parse(try_from_str = parse_remote_ro_file_option))] |
| 70 | remote_ro_file: Vec<OptionRemoteRoFile>, |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 71 | |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame] | 72 | /// A read-only remote file without integrity check. Can be multiple. |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 73 | /// |
Victor Hsieh | 50d75ac | 2021-09-03 14:46:55 -0700 | [diff] [blame] | 74 | /// For example, `--remote-unverified-file 5:10` tells the filesystem to associate entry 5 |
| 75 | /// with a remote file 10. |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame] | 76 | #[structopt(long, parse(try_from_str = parse_remote_ro_file_unverified_option))] |
| 77 | remote_ro_file_unverified: Vec<OptionRemoteRoFileUnverified>, |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 78 | |
Victor Hsieh | 6a47e7f | 2021-03-03 15:53:49 -0800 | [diff] [blame] | 79 | /// A new read-writable remote file with integrity check. Can be multiple. |
| 80 | /// |
| 81 | /// For example, `--remote-new-verified-file 12:34` tells the filesystem to associate entry 12 |
| 82 | /// with a remote file 34. |
| 83 | #[structopt(long, parse(try_from_str = parse_remote_new_rw_file_option))] |
| 84 | remote_new_rw_file: Vec<OptionRemoteRwFile>, |
| 85 | |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame] | 86 | /// Debug only. A read-only local file with integrity check. Can be multiple. |
| 87 | #[structopt(long, parse(try_from_str = parse_local_file_ro_option))] |
| 88 | local_ro_file: Vec<OptionLocalFileRo>, |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 89 | |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame] | 90 | /// Debug only. A read-only local file without integrity check. Can be multiple. |
| 91 | #[structopt(long, parse(try_from_str = parse_local_ro_file_unverified_ro_option))] |
| 92 | local_ro_file_unverified: Vec<OptionLocalRoFileUnverified>, |
Victor Hsieh | 9d0ab62 | 2021-04-26 17:07:02 -0700 | [diff] [blame] | 93 | |
| 94 | /// Enable debugging features. |
| 95 | #[structopt(long)] |
| 96 | debug: bool, |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 97 | } |
| 98 | |
Victor Hsieh | 9ab13e5 | 2021-06-29 09:23:29 -0700 | [diff] [blame] | 99 | impl Args { |
| 100 | fn has_remote_files(&self) -> bool { |
| 101 | !self.remote_ro_file.is_empty() |
| 102 | || !self.remote_ro_file_unverified.is_empty() |
| 103 | || !self.remote_new_rw_file.is_empty() |
| 104 | } |
| 105 | } |
| 106 | |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame] | 107 | struct OptionRemoteRoFile { |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 108 | ino: Inode, |
| 109 | |
| 110 | /// ID to refer to the remote file. |
| 111 | remote_id: i32, |
| 112 | |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 113 | /// Certificate to verify the authenticity of the file's fs-verity signature. |
| 114 | /// TODO(170494765): Implement PKCS#7 signature verification. |
| 115 | _certificate_path: PathBuf, |
| 116 | } |
| 117 | |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame] | 118 | struct OptionRemoteRoFileUnverified { |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 119 | ino: Inode, |
| 120 | |
| 121 | /// ID to refer to the remote file. |
| 122 | remote_id: i32, |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 123 | } |
| 124 | |
Victor Hsieh | 6a47e7f | 2021-03-03 15:53:49 -0800 | [diff] [blame] | 125 | struct OptionRemoteRwFile { |
| 126 | ino: Inode, |
| 127 | |
| 128 | /// ID to refer to the remote file. |
| 129 | remote_id: i32, |
| 130 | } |
| 131 | |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame] | 132 | struct OptionLocalFileRo { |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 133 | ino: Inode, |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 134 | |
| 135 | /// Local path of the backing file. |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 136 | file_path: PathBuf, |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 137 | |
| 138 | /// Local path of the backing file's fs-verity Merkle tree dump. |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 139 | merkle_tree_dump_path: PathBuf, |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 140 | |
| 141 | /// Local path of fs-verity signature for the backing file. |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 142 | signature_path: PathBuf, |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 143 | |
| 144 | /// Certificate to verify the authenticity of the file's fs-verity signature. |
| 145 | /// TODO(170494765): Implement PKCS#7 signature verification. |
| 146 | _certificate_path: PathBuf, |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 147 | } |
| 148 | |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame] | 149 | struct OptionLocalRoFileUnverified { |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 150 | ino: Inode, |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 151 | |
| 152 | /// Local path of the backing file. |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 153 | file_path: PathBuf, |
| 154 | } |
| 155 | |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame] | 156 | fn parse_remote_ro_file_option(option: &str) -> Result<OptionRemoteRoFile> { |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 157 | let strs: Vec<&str> = option.split(':').collect(); |
Victor Hsieh | 50d75ac | 2021-09-03 14:46:55 -0700 | [diff] [blame] | 158 | if strs.len() != 3 { |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 159 | bail!("Invalid option: {}", option); |
| 160 | } |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame] | 161 | Ok(OptionRemoteRoFile { |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 162 | ino: strs[0].parse::<Inode>()?, |
| 163 | remote_id: strs[1].parse::<i32>()?, |
Victor Hsieh | 50d75ac | 2021-09-03 14:46:55 -0700 | [diff] [blame] | 164 | _certificate_path: PathBuf::from(strs[2]), |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 165 | }) |
| 166 | } |
| 167 | |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame] | 168 | fn parse_remote_ro_file_unverified_option(option: &str) -> Result<OptionRemoteRoFileUnverified> { |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 169 | let strs: Vec<&str> = option.split(':').collect(); |
Victor Hsieh | 50d75ac | 2021-09-03 14:46:55 -0700 | [diff] [blame] | 170 | if strs.len() != 2 { |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 171 | bail!("Invalid option: {}", option); |
| 172 | } |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame] | 173 | Ok(OptionRemoteRoFileUnverified { |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 174 | ino: strs[0].parse::<Inode>()?, |
| 175 | remote_id: strs[1].parse::<i32>()?, |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 176 | }) |
| 177 | } |
| 178 | |
Victor Hsieh | 6a47e7f | 2021-03-03 15:53:49 -0800 | [diff] [blame] | 179 | fn parse_remote_new_rw_file_option(option: &str) -> Result<OptionRemoteRwFile> { |
| 180 | let strs: Vec<&str> = option.split(':').collect(); |
| 181 | if strs.len() != 2 { |
| 182 | bail!("Invalid option: {}", option); |
| 183 | } |
| 184 | Ok(OptionRemoteRwFile { |
| 185 | ino: strs[0].parse::<Inode>().unwrap(), |
| 186 | remote_id: strs[1].parse::<i32>().unwrap(), |
| 187 | }) |
| 188 | } |
| 189 | |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame] | 190 | fn parse_local_file_ro_option(option: &str) -> Result<OptionLocalFileRo> { |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 191 | let strs: Vec<&str> = option.split(':').collect(); |
| 192 | if strs.len() != 5 { |
| 193 | bail!("Invalid option: {}", option); |
| 194 | } |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame] | 195 | Ok(OptionLocalFileRo { |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 196 | ino: strs[0].parse::<Inode>()?, |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 197 | file_path: PathBuf::from(strs[1]), |
| 198 | merkle_tree_dump_path: PathBuf::from(strs[2]), |
| 199 | signature_path: PathBuf::from(strs[3]), |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 200 | _certificate_path: PathBuf::from(strs[4]), |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 201 | }) |
| 202 | } |
| 203 | |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame] | 204 | fn parse_local_ro_file_unverified_ro_option(option: &str) -> Result<OptionLocalRoFileUnverified> { |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 205 | let strs: Vec<&str> = option.split(':').collect(); |
| 206 | if strs.len() != 2 { |
| 207 | bail!("Invalid option: {}", option); |
| 208 | } |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame] | 209 | Ok(OptionLocalRoFileUnverified { |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 210 | ino: strs[0].parse::<Inode>()?, |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 211 | file_path: PathBuf::from(strs[1]), |
| 212 | }) |
| 213 | } |
| 214 | |
Victor Hsieh | 2445e33 | 2021-06-04 16:44:53 -0700 | [diff] [blame] | 215 | fn new_config_remote_verified_file( |
| 216 | service: file::VirtFdService, |
| 217 | remote_id: i32, |
| 218 | file_size: u64, |
| 219 | ) -> Result<FileConfig> { |
Andrew Walbran | cc09386 | 2021-03-05 16:59:35 +0000 | [diff] [blame] | 220 | let signature = service.readFsveritySignature(remote_id).context("Failed to read signature")?; |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 221 | |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 222 | let authenticator = FakeAuthenticator::always_succeed(); |
Chris Wailes | 68c39f8 | 2021-07-27 16:03:44 -0700 | [diff] [blame] | 223 | Ok(FileConfig::RemoteVerifiedReadonly { |
Victor Hsieh | 1bcf411 | 2021-03-19 14:26:57 -0700 | [diff] [blame] | 224 | reader: VerifiedFileReader::new( |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 225 | &authenticator, |
Victor Hsieh | c3d45b1 | 2021-06-30 09:16:41 -0700 | [diff] [blame] | 226 | RemoteFileReader::new(service.clone(), remote_id), |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 227 | file_size, |
| 228 | signature, |
Victor Hsieh | c3d45b1 | 2021-06-30 09:16:41 -0700 | [diff] [blame] | 229 | RemoteMerkleTreeReader::new(service.clone(), remote_id), |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 230 | )?, |
| 231 | file_size, |
Victor Hsieh | 1bcf411 | 2021-03-19 14:26:57 -0700 | [diff] [blame] | 232 | }) |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 233 | } |
| 234 | |
Victor Hsieh | 2445e33 | 2021-06-04 16:44:53 -0700 | [diff] [blame] | 235 | fn new_config_remote_unverified_file( |
| 236 | service: file::VirtFdService, |
| 237 | remote_id: i32, |
| 238 | file_size: u64, |
| 239 | ) -> Result<FileConfig> { |
Victor Hsieh | c3d45b1 | 2021-06-30 09:16:41 -0700 | [diff] [blame] | 240 | let reader = RemoteFileReader::new(service, remote_id); |
Chris Wailes | 68c39f8 | 2021-07-27 16:03:44 -0700 | [diff] [blame] | 241 | Ok(FileConfig::RemoteUnverifiedReadonly { reader, file_size }) |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 242 | } |
| 243 | |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame] | 244 | fn new_config_local_ro_file( |
Victor Hsieh | 6cf75b5 | 2021-04-01 12:45:49 -0700 | [diff] [blame] | 245 | protected_file: &Path, |
| 246 | merkle_tree_dump: &Path, |
| 247 | signature: &Path, |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 248 | ) -> Result<FileConfig> { |
| 249 | let file = File::open(&protected_file)?; |
| 250 | let file_size = file.metadata()?.len(); |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame] | 251 | let file_reader = LocalFileReader::new(file)?; |
| 252 | let merkle_tree_reader = LocalFileReader::new(File::open(merkle_tree_dump)?)?; |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 253 | let authenticator = FakeAuthenticator::always_succeed(); |
| 254 | let mut sig = Vec::new(); |
| 255 | let _ = File::open(signature)?.read_to_end(&mut sig)?; |
Victor Hsieh | 1bcf411 | 2021-03-19 14:26:57 -0700 | [diff] [blame] | 256 | let reader = |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame] | 257 | VerifiedFileReader::new(&authenticator, file_reader, file_size, sig, merkle_tree_reader)?; |
Chris Wailes | 68c39f8 | 2021-07-27 16:03:44 -0700 | [diff] [blame] | 258 | Ok(FileConfig::LocalVerifiedReadonly { reader, file_size }) |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 259 | } |
| 260 | |
Victor Hsieh | 6cf75b5 | 2021-04-01 12:45:49 -0700 | [diff] [blame] | 261 | fn new_config_local_ro_file_unverified(file_path: &Path) -> Result<FileConfig> { |
Victor Hsieh | 1bcf411 | 2021-03-19 14:26:57 -0700 | [diff] [blame] | 262 | let reader = LocalFileReader::new(File::open(file_path)?)?; |
| 263 | let file_size = reader.len(); |
Chris Wailes | 68c39f8 | 2021-07-27 16:03:44 -0700 | [diff] [blame] | 264 | Ok(FileConfig::LocalUnverifiedReadonly { reader, file_size }) |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 265 | } |
| 266 | |
Victor Hsieh | 2445e33 | 2021-06-04 16:44:53 -0700 | [diff] [blame] | 267 | fn new_config_remote_new_verified_file( |
| 268 | service: file::VirtFdService, |
| 269 | remote_id: i32, |
| 270 | ) -> Result<FileConfig> { |
Victor Hsieh | c3d45b1 | 2021-06-30 09:16:41 -0700 | [diff] [blame] | 271 | let remote_file = RemoteFileEditor::new(service, remote_id); |
Chris Wailes | 68c39f8 | 2021-07-27 16:03:44 -0700 | [diff] [blame] | 272 | Ok(FileConfig::RemoteVerifiedNew { editor: VerifiedFileEditor::new(remote_file) }) |
Victor Hsieh | 6a47e7f | 2021-03-03 15:53:49 -0800 | [diff] [blame] | 273 | } |
| 274 | |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 275 | fn prepare_file_pool(args: &Args) -> Result<BTreeMap<Inode, FileConfig>> { |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 276 | let mut file_pool = BTreeMap::new(); |
| 277 | |
Victor Hsieh | 9ab13e5 | 2021-06-29 09:23:29 -0700 | [diff] [blame] | 278 | if args.has_remote_files() { |
Victor Hsieh | 1a8cd04 | 2021-09-03 16:29:45 -0700 | [diff] [blame] | 279 | let service = file::get_rpc_binder_service(args.cid)?; |
Victor Hsieh | 2445e33 | 2021-06-04 16:44:53 -0700 | [diff] [blame] | 280 | |
Victor Hsieh | 9ab13e5 | 2021-06-29 09:23:29 -0700 | [diff] [blame] | 281 | for config in &args.remote_ro_file { |
| 282 | file_pool.insert( |
| 283 | config.ino, |
| 284 | new_config_remote_verified_file( |
| 285 | service.clone(), |
| 286 | config.remote_id, |
Victor Hsieh | 50d75ac | 2021-09-03 14:46:55 -0700 | [diff] [blame] | 287 | service.getFileSize(config.remote_id)?.try_into()?, |
Victor Hsieh | 9ab13e5 | 2021-06-29 09:23:29 -0700 | [diff] [blame] | 288 | )?, |
| 289 | ); |
| 290 | } |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 291 | |
Victor Hsieh | 9ab13e5 | 2021-06-29 09:23:29 -0700 | [diff] [blame] | 292 | for config in &args.remote_ro_file_unverified { |
| 293 | file_pool.insert( |
| 294 | config.ino, |
| 295 | new_config_remote_unverified_file( |
| 296 | service.clone(), |
| 297 | config.remote_id, |
Victor Hsieh | 50d75ac | 2021-09-03 14:46:55 -0700 | [diff] [blame] | 298 | service.getFileSize(config.remote_id)?.try_into()?, |
Victor Hsieh | 9ab13e5 | 2021-06-29 09:23:29 -0700 | [diff] [blame] | 299 | )?, |
| 300 | ); |
| 301 | } |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 302 | |
Victor Hsieh | 9ab13e5 | 2021-06-29 09:23:29 -0700 | [diff] [blame] | 303 | for config in &args.remote_new_rw_file { |
| 304 | file_pool.insert( |
| 305 | config.ino, |
| 306 | new_config_remote_new_verified_file(service.clone(), config.remote_id)?, |
| 307 | ); |
| 308 | } |
Victor Hsieh | 6a47e7f | 2021-03-03 15:53:49 -0800 | [diff] [blame] | 309 | } |
| 310 | |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame] | 311 | for config in &args.local_ro_file { |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 312 | file_pool.insert( |
| 313 | config.ino, |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame] | 314 | new_config_local_ro_file( |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 315 | &config.file_path, |
| 316 | &config.merkle_tree_dump_path, |
| 317 | &config.signature_path, |
| 318 | )?, |
| 319 | ); |
| 320 | } |
| 321 | |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame] | 322 | for config in &args.local_ro_file_unverified { |
| 323 | file_pool.insert(config.ino, new_config_local_ro_file_unverified(&config.file_path)?); |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | Ok(file_pool) |
| 327 | } |
| 328 | |
Alan Stokes | e1b6e1c | 2021-10-01 12:44:49 +0100 | [diff] [blame^] | 329 | fn try_main() -> Result<()> { |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 330 | let args = Args::from_args(); |
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 | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 337 | let file_pool = prepare_file_pool(&args)?; |
Victor Hsieh | 4cc3b79 | 2021-08-04 12:00:04 -0700 | [diff] [blame] | 338 | fusefs::loop_forever(file_pool, &args.mount_point, &args.extra_options)?; |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 339 | bail!("Unexpected exit after the handler loop") |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 340 | } |
Alan Stokes | e1b6e1c | 2021-10-01 12:44:49 +0100 | [diff] [blame^] | 341 | |
| 342 | fn main() { |
| 343 | if let Err(e) = try_main() { |
| 344 | error!("failed with {:?}", e); |
| 345 | std::process::exit(1); |
| 346 | } |
| 347 | } |