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 | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame^] | 47 | use fusefs::{AuthFsEntry, 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 | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 65 | /// For example, `--remote-ro-file 5:/path/to/cert` tells the filesystem to associate the |
| 66 | /// file $MOUNTPOINT/5 with a remote FD 5, 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 | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 72 | /// For example, `--remote-ro-file-unverified 5` tells the filesystem to associate the file |
| 73 | /// $MOUNTPOINT/5 with a remote FD 5. |
| 74 | #[structopt(long)] |
| 75 | remote_ro_file_unverified: Vec<i32>, |
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 | /// |
Victor Hsieh | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 79 | /// For example, `--remote-new-rw-file 5` tells the filesystem to associate the file |
| 80 | /// $MOUNTPOINT/5 with a remote FD 5. |
| 81 | #[structopt(long)] |
| 82 | remote_new_rw_file: Vec<i32>, |
Victor Hsieh | 6a47e7f | 2021-03-03 15:53:49 -0800 | [diff] [blame] | 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 | /// |
Victor Hsieh | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 88 | /// For example, `--remote-new-rw-dir 5` tells the filesystem to associate $MOUNTPOINT/5 |
| 89 | /// with a remote dir FD 5. |
| 90 | #[structopt(long)] |
| 91 | remote_new_rw_dir: Vec<i32>, |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 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 | /// ID to refer to the remote file. |
Victor Hsieh | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 100 | remote_fd: i32, |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 101 | |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 102 | /// Certificate to verify the authenticity of the file's fs-verity signature. |
| 103 | /// TODO(170494765): Implement PKCS#7 signature verification. |
| 104 | _certificate_path: PathBuf, |
| 105 | } |
| 106 | |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame] | 107 | fn parse_remote_ro_file_option(option: &str) -> Result<OptionRemoteRoFile> { |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 108 | let strs: Vec<&str> = option.split(':').collect(); |
Victor Hsieh | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 109 | if strs.len() != 2 { |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 110 | bail!("Invalid option: {}", option); |
| 111 | } |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame] | 112 | Ok(OptionRemoteRoFile { |
Victor Hsieh | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 113 | remote_fd: strs[0].parse::<i32>()?, |
| 114 | _certificate_path: PathBuf::from(strs[1]), |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 115 | }) |
| 116 | } |
| 117 | |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame^] | 118 | fn new_remote_verified_file_entry( |
Victor Hsieh | 2445e33 | 2021-06-04 16:44:53 -0700 | [diff] [blame] | 119 | service: file::VirtFdService, |
Victor Hsieh | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 120 | remote_fd: i32, |
Victor Hsieh | 2445e33 | 2021-06-04 16:44:53 -0700 | [diff] [blame] | 121 | file_size: u64, |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame^] | 122 | ) -> Result<AuthFsEntry> { |
Victor Hsieh | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 123 | let signature = service.readFsveritySignature(remote_fd).context("Failed to read signature")?; |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 124 | |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 125 | let authenticator = FakeAuthenticator::always_succeed(); |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame^] | 126 | Ok(AuthFsEntry::VerifiedReadonly { |
Victor Hsieh | 1bcf411 | 2021-03-19 14:26:57 -0700 | [diff] [blame] | 127 | reader: VerifiedFileReader::new( |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 128 | &authenticator, |
Victor Hsieh | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 129 | RemoteFileReader::new(service.clone(), remote_fd), |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 130 | file_size, |
| 131 | signature, |
Victor Hsieh | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 132 | RemoteMerkleTreeReader::new(service.clone(), remote_fd), |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 133 | )?, |
| 134 | file_size, |
Victor Hsieh | 1bcf411 | 2021-03-19 14:26:57 -0700 | [diff] [blame] | 135 | }) |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 136 | } |
| 137 | |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame^] | 138 | fn new_remote_unverified_file_entry( |
Victor Hsieh | 2445e33 | 2021-06-04 16:44:53 -0700 | [diff] [blame] | 139 | service: file::VirtFdService, |
Victor Hsieh | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 140 | remote_fd: i32, |
Victor Hsieh | 2445e33 | 2021-06-04 16:44:53 -0700 | [diff] [blame] | 141 | file_size: u64, |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame^] | 142 | ) -> Result<AuthFsEntry> { |
Victor Hsieh | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 143 | let reader = RemoteFileReader::new(service, remote_fd); |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame^] | 144 | Ok(AuthFsEntry::UnverifiedReadonly { reader, file_size }) |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 145 | } |
| 146 | |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame^] | 147 | fn new_remote_new_verified_file_entry( |
Victor Hsieh | 2445e33 | 2021-06-04 16:44:53 -0700 | [diff] [blame] | 148 | service: file::VirtFdService, |
Victor Hsieh | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 149 | remote_fd: i32, |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame^] | 150 | ) -> Result<AuthFsEntry> { |
Victor Hsieh | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 151 | let remote_file = RemoteFileEditor::new(service, remote_fd); |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame^] | 152 | Ok(AuthFsEntry::VerifiedNew { editor: VerifiedFileEditor::new(remote_file) }) |
Victor Hsieh | 6a47e7f | 2021-03-03 15:53:49 -0800 | [diff] [blame] | 153 | } |
| 154 | |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame^] | 155 | fn new_remote_new_verified_dir_entry( |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 156 | service: file::VirtFdService, |
Victor Hsieh | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 157 | remote_fd: i32, |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame^] | 158 | ) -> Result<AuthFsEntry> { |
Victor Hsieh | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 159 | let dir = RemoteDirEditor::new(service, remote_fd); |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame^] | 160 | Ok(AuthFsEntry::VerifiedNewDirectory { dir }) |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame^] | 163 | fn prepare_root_dir_entries(args: &Args) -> Result<BTreeMap<Inode, AuthFsEntry>> { |
| 164 | let mut root_entries = BTreeMap::new(); |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 165 | |
Victor Hsieh | 88e5017 | 2021-10-15 13:27:13 -0700 | [diff] [blame] | 166 | let service = file::get_rpc_binder_service(args.cid)?; |
Victor Hsieh | 2445e33 | 2021-06-04 16:44:53 -0700 | [diff] [blame] | 167 | |
Victor Hsieh | 88e5017 | 2021-10-15 13:27:13 -0700 | [diff] [blame] | 168 | for config in &args.remote_ro_file { |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame^] | 169 | root_entries.insert( |
Victor Hsieh | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 170 | config.remote_fd.try_into()?, |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame^] | 171 | new_remote_verified_file_entry( |
Victor Hsieh | 88e5017 | 2021-10-15 13:27:13 -0700 | [diff] [blame] | 172 | service.clone(), |
Victor Hsieh | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 173 | config.remote_fd, |
| 174 | service.getFileSize(config.remote_fd)?.try_into()?, |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 175 | )?, |
| 176 | ); |
| 177 | } |
| 178 | |
Victor Hsieh | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 179 | for remote_fd in &args.remote_ro_file_unverified { |
| 180 | let remote_fd = *remote_fd; |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame^] | 181 | root_entries.insert( |
Victor Hsieh | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 182 | remote_fd.try_into()?, |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame^] | 183 | new_remote_unverified_file_entry( |
Victor Hsieh | 88e5017 | 2021-10-15 13:27:13 -0700 | [diff] [blame] | 184 | service.clone(), |
Victor Hsieh | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 185 | remote_fd, |
| 186 | service.getFileSize(remote_fd)?.try_into()?, |
Victor Hsieh | 88e5017 | 2021-10-15 13:27:13 -0700 | [diff] [blame] | 187 | )?, |
| 188 | ); |
| 189 | } |
| 190 | |
Victor Hsieh | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 191 | for remote_fd in &args.remote_new_rw_file { |
| 192 | let remote_fd = *remote_fd; |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame^] | 193 | root_entries.insert( |
Victor Hsieh | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 194 | remote_fd.try_into()?, |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame^] | 195 | new_remote_new_verified_file_entry(service.clone(), remote_fd)?, |
Victor Hsieh | 88e5017 | 2021-10-15 13:27:13 -0700 | [diff] [blame] | 196 | ); |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 197 | } |
| 198 | |
Victor Hsieh | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 199 | for remote_fd in &args.remote_new_rw_dir { |
| 200 | let remote_fd = *remote_fd; |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame^] | 201 | root_entries.insert( |
Victor Hsieh | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 202 | remote_fd.try_into()?, |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame^] | 203 | new_remote_new_verified_dir_entry(service.clone(), remote_fd)?, |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 204 | ); |
| 205 | } |
| 206 | |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame^] | 207 | Ok(root_entries) |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 208 | } |
| 209 | |
Alan Stokes | e1b6e1c | 2021-10-01 12:44:49 +0100 | [diff] [blame] | 210 | fn try_main() -> Result<()> { |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 211 | let args = Args::from_args(); |
Victor Hsieh | 9d0ab62 | 2021-04-26 17:07:02 -0700 | [diff] [blame] | 212 | |
| 213 | let log_level = if args.debug { log::Level::Debug } else { log::Level::Info }; |
| 214 | android_logger::init_once( |
| 215 | android_logger::Config::default().with_tag("authfs").with_min_level(log_level), |
| 216 | ); |
| 217 | |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame^] | 218 | let root_entries = prepare_root_dir_entries(&args)?; |
| 219 | fusefs::loop_forever(root_entries, &args.mount_point, &args.extra_options)?; |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 220 | bail!("Unexpected exit after the handler loop") |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 221 | } |
Alan Stokes | e1b6e1c | 2021-10-01 12:44:49 +0100 | [diff] [blame] | 222 | |
| 223 | fn main() { |
| 224 | if let Err(e) = try_main() { |
| 225 | error!("failed with {:?}", e); |
| 226 | std::process::exit(1); |
| 227 | } |
| 228 | } |