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 | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame^] | 45 | use file::{RemoteDirEditor, 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 | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame^] | 84 | /// A new directory that is assumed empty in the backing filesystem. New files created in this |
| 85 | /// directory are integrity-protected in the same way as --remote-new-verified-file. Can be |
| 86 | /// multiple. |
| 87 | /// |
| 88 | /// For example, `--remote-new-verified-dir 12:34` tells the filesystem to associate entry 12 |
| 89 | /// with a remote dir FD 34. |
| 90 | #[structopt(long, parse(try_from_str = parse_remote_new_rw_dir_option))] |
| 91 | remote_new_rw_dir: Vec<OptionRemoteRwDir>, |
| 92 | |
Victor Hsieh | 9d0ab62 | 2021-04-26 17:07:02 -0700 | [diff] [blame] | 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 | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame] | 98 | struct OptionRemoteRoFile { |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 99 | ino: Inode, |
| 100 | |
| 101 | /// ID to refer to the remote file. |
| 102 | remote_id: i32, |
| 103 | |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 104 | /// Certificate to verify the authenticity of the file's fs-verity signature. |
| 105 | /// TODO(170494765): Implement PKCS#7 signature verification. |
| 106 | _certificate_path: PathBuf, |
| 107 | } |
| 108 | |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame] | 109 | struct OptionRemoteRoFileUnverified { |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 110 | ino: Inode, |
| 111 | |
| 112 | /// ID to refer to the remote file. |
| 113 | remote_id: i32, |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 114 | } |
| 115 | |
Victor Hsieh | 6a47e7f | 2021-03-03 15:53:49 -0800 | [diff] [blame] | 116 | struct OptionRemoteRwFile { |
| 117 | ino: Inode, |
| 118 | |
| 119 | /// ID to refer to the remote file. |
| 120 | remote_id: i32, |
| 121 | } |
| 122 | |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame^] | 123 | struct OptionRemoteRwDir { |
| 124 | ino: Inode, |
| 125 | |
| 126 | /// ID to refer to the remote dir. |
| 127 | remote_id: i32, |
| 128 | } |
| 129 | |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame] | 130 | fn parse_remote_ro_file_option(option: &str) -> Result<OptionRemoteRoFile> { |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 131 | let strs: Vec<&str> = option.split(':').collect(); |
Victor Hsieh | 50d75ac | 2021-09-03 14:46:55 -0700 | [diff] [blame] | 132 | if strs.len() != 3 { |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 133 | bail!("Invalid option: {}", option); |
| 134 | } |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame] | 135 | Ok(OptionRemoteRoFile { |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 136 | ino: strs[0].parse::<Inode>()?, |
| 137 | remote_id: strs[1].parse::<i32>()?, |
Victor Hsieh | 50d75ac | 2021-09-03 14:46:55 -0700 | [diff] [blame] | 138 | _certificate_path: PathBuf::from(strs[2]), |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 139 | }) |
| 140 | } |
| 141 | |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame] | 142 | fn parse_remote_ro_file_unverified_option(option: &str) -> Result<OptionRemoteRoFileUnverified> { |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 143 | let strs: Vec<&str> = option.split(':').collect(); |
Victor Hsieh | 50d75ac | 2021-09-03 14:46:55 -0700 | [diff] [blame] | 144 | if strs.len() != 2 { |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 145 | bail!("Invalid option: {}", option); |
| 146 | } |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame] | 147 | Ok(OptionRemoteRoFileUnverified { |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 148 | ino: strs[0].parse::<Inode>()?, |
| 149 | remote_id: strs[1].parse::<i32>()?, |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 150 | }) |
| 151 | } |
| 152 | |
Victor Hsieh | 6a47e7f | 2021-03-03 15:53:49 -0800 | [diff] [blame] | 153 | fn parse_remote_new_rw_file_option(option: &str) -> Result<OptionRemoteRwFile> { |
| 154 | let strs: Vec<&str> = option.split(':').collect(); |
| 155 | if strs.len() != 2 { |
| 156 | bail!("Invalid option: {}", option); |
| 157 | } |
| 158 | Ok(OptionRemoteRwFile { |
| 159 | ino: strs[0].parse::<Inode>().unwrap(), |
| 160 | remote_id: strs[1].parse::<i32>().unwrap(), |
| 161 | }) |
| 162 | } |
| 163 | |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame^] | 164 | fn parse_remote_new_rw_dir_option(option: &str) -> Result<OptionRemoteRwDir> { |
| 165 | let strs: Vec<&str> = option.split(':').collect(); |
| 166 | if strs.len() != 2 { |
| 167 | bail!("Invalid option: {}", option); |
| 168 | } |
| 169 | Ok(OptionRemoteRwDir { |
| 170 | ino: strs[0].parse::<Inode>().unwrap(), |
| 171 | remote_id: strs[1].parse::<i32>().unwrap(), |
| 172 | }) |
| 173 | } |
| 174 | |
Victor Hsieh | 2445e33 | 2021-06-04 16:44:53 -0700 | [diff] [blame] | 175 | fn new_config_remote_verified_file( |
| 176 | service: file::VirtFdService, |
| 177 | remote_id: i32, |
| 178 | file_size: u64, |
| 179 | ) -> Result<FileConfig> { |
Andrew Walbran | cc09386 | 2021-03-05 16:59:35 +0000 | [diff] [blame] | 180 | let signature = service.readFsveritySignature(remote_id).context("Failed to read signature")?; |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 181 | |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 182 | let authenticator = FakeAuthenticator::always_succeed(); |
Victor Hsieh | 88e5017 | 2021-10-15 13:27:13 -0700 | [diff] [blame] | 183 | Ok(FileConfig::VerifiedReadonly { |
Victor Hsieh | 1bcf411 | 2021-03-19 14:26:57 -0700 | [diff] [blame] | 184 | reader: VerifiedFileReader::new( |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 185 | &authenticator, |
Victor Hsieh | c3d45b1 | 2021-06-30 09:16:41 -0700 | [diff] [blame] | 186 | RemoteFileReader::new(service.clone(), remote_id), |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 187 | file_size, |
| 188 | signature, |
Victor Hsieh | c3d45b1 | 2021-06-30 09:16:41 -0700 | [diff] [blame] | 189 | RemoteMerkleTreeReader::new(service.clone(), remote_id), |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 190 | )?, |
| 191 | file_size, |
Victor Hsieh | 1bcf411 | 2021-03-19 14:26:57 -0700 | [diff] [blame] | 192 | }) |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 193 | } |
| 194 | |
Victor Hsieh | 2445e33 | 2021-06-04 16:44:53 -0700 | [diff] [blame] | 195 | fn new_config_remote_unverified_file( |
| 196 | service: file::VirtFdService, |
| 197 | remote_id: i32, |
| 198 | file_size: u64, |
| 199 | ) -> Result<FileConfig> { |
Victor Hsieh | c3d45b1 | 2021-06-30 09:16:41 -0700 | [diff] [blame] | 200 | let reader = RemoteFileReader::new(service, remote_id); |
Victor Hsieh | 88e5017 | 2021-10-15 13:27:13 -0700 | [diff] [blame] | 201 | Ok(FileConfig::UnverifiedReadonly { reader, file_size }) |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 202 | } |
| 203 | |
Victor Hsieh | 2445e33 | 2021-06-04 16:44:53 -0700 | [diff] [blame] | 204 | fn new_config_remote_new_verified_file( |
| 205 | service: file::VirtFdService, |
| 206 | remote_id: i32, |
| 207 | ) -> Result<FileConfig> { |
Victor Hsieh | c3d45b1 | 2021-06-30 09:16:41 -0700 | [diff] [blame] | 208 | let remote_file = RemoteFileEditor::new(service, remote_id); |
Victor Hsieh | 88e5017 | 2021-10-15 13:27:13 -0700 | [diff] [blame] | 209 | Ok(FileConfig::VerifiedNew { editor: VerifiedFileEditor::new(remote_file) }) |
Victor Hsieh | 6a47e7f | 2021-03-03 15:53:49 -0800 | [diff] [blame] | 210 | } |
| 211 | |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame^] | 212 | fn new_config_remote_new_verified_dir( |
| 213 | service: file::VirtFdService, |
| 214 | remote_id: i32, |
| 215 | ) -> Result<FileConfig> { |
| 216 | let dir = RemoteDirEditor::new(service, remote_id); |
| 217 | Ok(FileConfig::VerifiedNewDirectory { dir }) |
| 218 | } |
| 219 | |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 220 | fn prepare_file_pool(args: &Args) -> Result<BTreeMap<Inode, FileConfig>> { |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 221 | let mut file_pool = BTreeMap::new(); |
| 222 | |
Victor Hsieh | 88e5017 | 2021-10-15 13:27:13 -0700 | [diff] [blame] | 223 | let service = file::get_rpc_binder_service(args.cid)?; |
Victor Hsieh | 2445e33 | 2021-06-04 16:44:53 -0700 | [diff] [blame] | 224 | |
Victor Hsieh | 88e5017 | 2021-10-15 13:27:13 -0700 | [diff] [blame] | 225 | for config in &args.remote_ro_file { |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 226 | file_pool.insert( |
| 227 | config.ino, |
Victor Hsieh | 88e5017 | 2021-10-15 13:27:13 -0700 | [diff] [blame] | 228 | new_config_remote_verified_file( |
| 229 | service.clone(), |
| 230 | config.remote_id, |
| 231 | service.getFileSize(config.remote_id)?.try_into()?, |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 232 | )?, |
| 233 | ); |
| 234 | } |
| 235 | |
Victor Hsieh | 88e5017 | 2021-10-15 13:27:13 -0700 | [diff] [blame] | 236 | for config in &args.remote_ro_file_unverified { |
| 237 | file_pool.insert( |
| 238 | config.ino, |
| 239 | new_config_remote_unverified_file( |
| 240 | service.clone(), |
| 241 | config.remote_id, |
| 242 | service.getFileSize(config.remote_id)?.try_into()?, |
| 243 | )?, |
| 244 | ); |
| 245 | } |
| 246 | |
| 247 | for config in &args.remote_new_rw_file { |
| 248 | file_pool.insert( |
| 249 | config.ino, |
| 250 | new_config_remote_new_verified_file(service.clone(), config.remote_id)?, |
| 251 | ); |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 252 | } |
| 253 | |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame^] | 254 | for config in &args.remote_new_rw_dir { |
| 255 | file_pool.insert( |
| 256 | config.ino, |
| 257 | new_config_remote_new_verified_dir(service.clone(), config.remote_id)?, |
| 258 | ); |
| 259 | } |
| 260 | |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 261 | Ok(file_pool) |
| 262 | } |
| 263 | |
Alan Stokes | e1b6e1c | 2021-10-01 12:44:49 +0100 | [diff] [blame] | 264 | fn try_main() -> Result<()> { |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 265 | let args = Args::from_args(); |
Victor Hsieh | 9d0ab62 | 2021-04-26 17:07:02 -0700 | [diff] [blame] | 266 | |
| 267 | let log_level = if args.debug { log::Level::Debug } else { log::Level::Info }; |
| 268 | android_logger::init_once( |
| 269 | android_logger::Config::default().with_tag("authfs").with_min_level(log_level), |
| 270 | ); |
| 271 | |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 272 | let file_pool = prepare_file_pool(&args)?; |
Victor Hsieh | 4cc3b79 | 2021-08-04 12:00:04 -0700 | [diff] [blame] | 273 | fusefs::loop_forever(file_pool, &args.mount_point, &args.extra_options)?; |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 274 | bail!("Unexpected exit after the handler loop") |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 275 | } |
Alan Stokes | e1b6e1c | 2021-10-01 12:44:49 +0100 | [diff] [blame] | 276 | |
| 277 | fn main() { |
| 278 | if let Err(e) = try_main() { |
| 279 | error!("failed with {:?}", e); |
| 280 | std::process::exit(1); |
| 281 | } |
| 282 | } |