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 |
Victor Hsieh | 88e5017 | 2021-10-15 13:27:13 -0700 | [diff] [blame] | 25 | //! source (e.g. remote file server), verification method (e.g. certificate for fs-verity |
| 26 | //! verification, or no verification if expected to mount over dm-verity), and file ID. Regardless |
| 27 | //! of the actual file name, the exposed file names through AuthFS are currently integer, e.g. |
| 28 | //! /mountpoint/42. |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 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 | 88e5017 | 2021-10-15 13:27:13 -0700 | [diff] [blame] | 34 | use std::path::PathBuf; |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 35 | use structopt::StructOpt; |
| 36 | |
| 37 | mod auth; |
| 38 | mod common; |
| 39 | mod crypto; |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame] | 40 | mod file; |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 41 | mod fsverity; |
| 42 | mod fusefs; |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 43 | |
| 44 | use auth::FakeAuthenticator; |
Victor Hsieh | 88e5017 | 2021-10-15 13:27:13 -0700 | [diff] [blame] | 45 | use file::{RemoteFileEditor, RemoteFileReader, RemoteMerkleTreeReader}; |
Victor Hsieh | 6a47e7f | 2021-03-03 15:53:49 -0800 | [diff] [blame] | 46 | use fsverity::{VerifiedFileEditor, VerifiedFileReader}; |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 47 | use fusefs::{FileConfig, Inode}; |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 48 | |
| 49 | #[derive(StructOpt)] |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 50 | struct Args { |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 51 | /// Mount point of AuthFS. |
| 52 | #[structopt(parse(from_os_str))] |
| 53 | mount_point: PathBuf, |
| 54 | |
Victor Hsieh | 2445e33 | 2021-06-04 16:44:53 -0700 | [diff] [blame] | 55 | /// CID of the VM where the service runs. |
| 56 | #[structopt(long)] |
Victor Hsieh | 1a8cd04 | 2021-09-03 16:29:45 -0700 | [diff] [blame] | 57 | cid: u32, |
Victor Hsieh | 2445e33 | 2021-06-04 16:44:53 -0700 | [diff] [blame] | 58 | |
Victor Hsieh | 4cc3b79 | 2021-08-04 12:00:04 -0700 | [diff] [blame] | 59 | /// Extra options to FUSE |
| 60 | #[structopt(short = "o")] |
| 61 | extra_options: Option<String>, |
| 62 | |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame] | 63 | /// A read-only remote file with integrity check. Can be multiple. |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 64 | /// |
Victor Hsieh | 50d75ac | 2021-09-03 14:46:55 -0700 | [diff] [blame] | 65 | /// For example, `--remote-verified-file 5:10:/path/to/cert` tells the filesystem to associate |
| 66 | /// 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] | 67 | #[structopt(long, parse(try_from_str = parse_remote_ro_file_option))] |
| 68 | remote_ro_file: Vec<OptionRemoteRoFile>, |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 69 | |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame] | 70 | /// A read-only remote file without integrity check. Can be multiple. |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 71 | /// |
Victor Hsieh | 50d75ac | 2021-09-03 14:46:55 -0700 | [diff] [blame] | 72 | /// For example, `--remote-unverified-file 5:10` tells the filesystem to associate entry 5 |
| 73 | /// with a remote file 10. |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame] | 74 | #[structopt(long, parse(try_from_str = parse_remote_ro_file_unverified_option))] |
| 75 | remote_ro_file_unverified: Vec<OptionRemoteRoFileUnverified>, |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 76 | |
Victor Hsieh | 6a47e7f | 2021-03-03 15:53:49 -0800 | [diff] [blame] | 77 | /// A new read-writable remote file with integrity check. Can be multiple. |
| 78 | /// |
| 79 | /// For example, `--remote-new-verified-file 12:34` tells the filesystem to associate entry 12 |
| 80 | /// with a remote file 34. |
| 81 | #[structopt(long, parse(try_from_str = parse_remote_new_rw_file_option))] |
| 82 | remote_new_rw_file: Vec<OptionRemoteRwFile>, |
| 83 | |
Victor Hsieh | 9d0ab62 | 2021-04-26 17:07:02 -0700 | [diff] [blame] | 84 | /// Enable debugging features. |
| 85 | #[structopt(long)] |
| 86 | debug: bool, |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 87 | } |
| 88 | |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame] | 89 | struct OptionRemoteRoFile { |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 90 | ino: Inode, |
| 91 | |
| 92 | /// ID to refer to the remote file. |
| 93 | remote_id: i32, |
| 94 | |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 95 | /// Certificate to verify the authenticity of the file's fs-verity signature. |
| 96 | /// TODO(170494765): Implement PKCS#7 signature verification. |
| 97 | _certificate_path: PathBuf, |
| 98 | } |
| 99 | |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame] | 100 | struct OptionRemoteRoFileUnverified { |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 101 | ino: Inode, |
| 102 | |
| 103 | /// ID to refer to the remote file. |
| 104 | remote_id: i32, |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 105 | } |
| 106 | |
Victor Hsieh | 6a47e7f | 2021-03-03 15:53:49 -0800 | [diff] [blame] | 107 | struct OptionRemoteRwFile { |
| 108 | ino: Inode, |
| 109 | |
| 110 | /// ID to refer to the remote file. |
| 111 | remote_id: i32, |
| 112 | } |
| 113 | |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame] | 114 | fn parse_remote_ro_file_option(option: &str) -> Result<OptionRemoteRoFile> { |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 115 | let strs: Vec<&str> = option.split(':').collect(); |
Victor Hsieh | 50d75ac | 2021-09-03 14:46:55 -0700 | [diff] [blame] | 116 | if strs.len() != 3 { |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 117 | bail!("Invalid option: {}", option); |
| 118 | } |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame] | 119 | Ok(OptionRemoteRoFile { |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 120 | ino: strs[0].parse::<Inode>()?, |
| 121 | remote_id: strs[1].parse::<i32>()?, |
Victor Hsieh | 50d75ac | 2021-09-03 14:46:55 -0700 | [diff] [blame] | 122 | _certificate_path: PathBuf::from(strs[2]), |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 123 | }) |
| 124 | } |
| 125 | |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame] | 126 | fn parse_remote_ro_file_unverified_option(option: &str) -> Result<OptionRemoteRoFileUnverified> { |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 127 | let strs: Vec<&str> = option.split(':').collect(); |
Victor Hsieh | 50d75ac | 2021-09-03 14:46:55 -0700 | [diff] [blame] | 128 | if strs.len() != 2 { |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 129 | bail!("Invalid option: {}", option); |
| 130 | } |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame] | 131 | Ok(OptionRemoteRoFileUnverified { |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 132 | ino: strs[0].parse::<Inode>()?, |
| 133 | remote_id: strs[1].parse::<i32>()?, |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 134 | }) |
| 135 | } |
| 136 | |
Victor Hsieh | 6a47e7f | 2021-03-03 15:53:49 -0800 | [diff] [blame] | 137 | fn parse_remote_new_rw_file_option(option: &str) -> Result<OptionRemoteRwFile> { |
| 138 | let strs: Vec<&str> = option.split(':').collect(); |
| 139 | if strs.len() != 2 { |
| 140 | bail!("Invalid option: {}", option); |
| 141 | } |
| 142 | Ok(OptionRemoteRwFile { |
| 143 | ino: strs[0].parse::<Inode>().unwrap(), |
| 144 | remote_id: strs[1].parse::<i32>().unwrap(), |
| 145 | }) |
| 146 | } |
| 147 | |
Victor Hsieh | 2445e33 | 2021-06-04 16:44:53 -0700 | [diff] [blame] | 148 | fn new_config_remote_verified_file( |
| 149 | service: file::VirtFdService, |
| 150 | remote_id: i32, |
| 151 | file_size: u64, |
| 152 | ) -> Result<FileConfig> { |
Andrew Walbran | cc09386 | 2021-03-05 16:59:35 +0000 | [diff] [blame] | 153 | let signature = service.readFsveritySignature(remote_id).context("Failed to read signature")?; |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 154 | |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 155 | let authenticator = FakeAuthenticator::always_succeed(); |
Victor Hsieh | 88e5017 | 2021-10-15 13:27:13 -0700 | [diff] [blame] | 156 | Ok(FileConfig::VerifiedReadonly { |
Victor Hsieh | 1bcf411 | 2021-03-19 14:26:57 -0700 | [diff] [blame] | 157 | reader: VerifiedFileReader::new( |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 158 | &authenticator, |
Victor Hsieh | c3d45b1 | 2021-06-30 09:16:41 -0700 | [diff] [blame] | 159 | RemoteFileReader::new(service.clone(), remote_id), |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 160 | file_size, |
| 161 | signature, |
Victor Hsieh | c3d45b1 | 2021-06-30 09:16:41 -0700 | [diff] [blame] | 162 | RemoteMerkleTreeReader::new(service.clone(), remote_id), |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 163 | )?, |
| 164 | file_size, |
Victor Hsieh | 1bcf411 | 2021-03-19 14:26:57 -0700 | [diff] [blame] | 165 | }) |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 166 | } |
| 167 | |
Victor Hsieh | 2445e33 | 2021-06-04 16:44:53 -0700 | [diff] [blame] | 168 | fn new_config_remote_unverified_file( |
| 169 | service: file::VirtFdService, |
| 170 | remote_id: i32, |
| 171 | file_size: u64, |
| 172 | ) -> Result<FileConfig> { |
Victor Hsieh | c3d45b1 | 2021-06-30 09:16:41 -0700 | [diff] [blame] | 173 | let reader = RemoteFileReader::new(service, remote_id); |
Victor Hsieh | 88e5017 | 2021-10-15 13:27:13 -0700 | [diff] [blame] | 174 | Ok(FileConfig::UnverifiedReadonly { reader, file_size }) |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 175 | } |
| 176 | |
Victor Hsieh | 2445e33 | 2021-06-04 16:44:53 -0700 | [diff] [blame] | 177 | fn new_config_remote_new_verified_file( |
| 178 | service: file::VirtFdService, |
| 179 | remote_id: i32, |
| 180 | ) -> Result<FileConfig> { |
Victor Hsieh | c3d45b1 | 2021-06-30 09:16:41 -0700 | [diff] [blame] | 181 | let remote_file = RemoteFileEditor::new(service, remote_id); |
Victor Hsieh | 88e5017 | 2021-10-15 13:27:13 -0700 | [diff] [blame] | 182 | Ok(FileConfig::VerifiedNew { editor: VerifiedFileEditor::new(remote_file) }) |
Victor Hsieh | 6a47e7f | 2021-03-03 15:53:49 -0800 | [diff] [blame] | 183 | } |
| 184 | |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 185 | fn prepare_file_pool(args: &Args) -> Result<BTreeMap<Inode, FileConfig>> { |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 186 | let mut file_pool = BTreeMap::new(); |
| 187 | |
Victor Hsieh | 88e5017 | 2021-10-15 13:27:13 -0700 | [diff] [blame] | 188 | let service = file::get_rpc_binder_service(args.cid)?; |
Victor Hsieh | 2445e33 | 2021-06-04 16:44:53 -0700 | [diff] [blame] | 189 | |
Victor Hsieh | 88e5017 | 2021-10-15 13:27:13 -0700 | [diff] [blame] | 190 | for config in &args.remote_ro_file { |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 191 | file_pool.insert( |
| 192 | config.ino, |
Victor Hsieh | 88e5017 | 2021-10-15 13:27:13 -0700 | [diff] [blame] | 193 | new_config_remote_verified_file( |
| 194 | service.clone(), |
| 195 | config.remote_id, |
| 196 | service.getFileSize(config.remote_id)?.try_into()?, |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 197 | )?, |
| 198 | ); |
| 199 | } |
| 200 | |
Victor Hsieh | 88e5017 | 2021-10-15 13:27:13 -0700 | [diff] [blame] | 201 | for config in &args.remote_ro_file_unverified { |
| 202 | file_pool.insert( |
| 203 | config.ino, |
| 204 | new_config_remote_unverified_file( |
| 205 | service.clone(), |
| 206 | config.remote_id, |
| 207 | service.getFileSize(config.remote_id)?.try_into()?, |
| 208 | )?, |
| 209 | ); |
| 210 | } |
| 211 | |
| 212 | for config in &args.remote_new_rw_file { |
| 213 | file_pool.insert( |
| 214 | config.ino, |
| 215 | new_config_remote_new_verified_file(service.clone(), config.remote_id)?, |
| 216 | ); |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | Ok(file_pool) |
| 220 | } |
| 221 | |
Alan Stokes | e1b6e1c | 2021-10-01 12:44:49 +0100 | [diff] [blame] | 222 | fn try_main() -> Result<()> { |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 223 | let args = Args::from_args(); |
Victor Hsieh | 9d0ab62 | 2021-04-26 17:07:02 -0700 | [diff] [blame] | 224 | |
| 225 | let log_level = if args.debug { log::Level::Debug } else { log::Level::Info }; |
| 226 | android_logger::init_once( |
| 227 | android_logger::Config::default().with_tag("authfs").with_min_level(log_level), |
| 228 | ); |
| 229 | |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 230 | let file_pool = prepare_file_pool(&args)?; |
Victor Hsieh | 4cc3b79 | 2021-08-04 12:00:04 -0700 | [diff] [blame] | 231 | fusefs::loop_forever(file_pool, &args.mount_point, &args.extra_options)?; |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 232 | bail!("Unexpected exit after the handler loop") |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 233 | } |
Alan Stokes | e1b6e1c | 2021-10-01 12:44:49 +0100 | [diff] [blame] | 234 | |
| 235 | fn main() { |
| 236 | if let Err(e) = try_main() { |
| 237 | error!("failed with {:?}", e); |
| 238 | std::process::exit(1); |
| 239 | } |
| 240 | } |