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