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; |
| 32 | use std::fs::File; |
| 33 | use std::io::Read; |
| 34 | use std::path::PathBuf; |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 35 | use std::sync::{Arc, Mutex}; |
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 | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame^] | 46 | use file::{LocalFileReader, RemoteFileReader, RemoteMerkleTreeReader}; |
| 47 | use fsverity::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 | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame^] | 56 | /// A read-only remote file with integrity check. Can be multiple. |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 57 | /// |
| 58 | /// For example, `--remote-verified-file 5:10:1234:/path/to/cert` tells the filesystem to |
| 59 | /// associate entry 5 with a remote file 10 of size 1234 bytes, and need to be verified against |
| 60 | /// the /path/to/cert. |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame^] | 61 | #[structopt(long, parse(try_from_str = parse_remote_ro_file_option))] |
| 62 | remote_ro_file: Vec<OptionRemoteRoFile>, |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 63 | |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame^] | 64 | /// A read-only remote file without integrity check. Can be multiple. |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 65 | /// |
| 66 | /// For example, `--remote-unverified-file 5:10:1234` tells the filesystem to associate entry 5 |
| 67 | /// with a remote file 10 of size 1234 bytes. |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame^] | 68 | #[structopt(long, parse(try_from_str = parse_remote_ro_file_unverified_option))] |
| 69 | remote_ro_file_unverified: Vec<OptionRemoteRoFileUnverified>, |
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 | /// Debug only. A read-only local file with integrity check. Can be multiple. |
| 72 | #[structopt(long, parse(try_from_str = parse_local_file_ro_option))] |
| 73 | local_ro_file: Vec<OptionLocalFileRo>, |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 74 | |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame^] | 75 | /// Debug only. A read-only local file without integrity check. Can be multiple. |
| 76 | #[structopt(long, parse(try_from_str = parse_local_ro_file_unverified_ro_option))] |
| 77 | local_ro_file_unverified: Vec<OptionLocalRoFileUnverified>, |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 78 | } |
| 79 | |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame^] | 80 | struct OptionRemoteRoFile { |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 81 | ino: Inode, |
| 82 | |
| 83 | /// ID to refer to the remote file. |
| 84 | remote_id: i32, |
| 85 | |
| 86 | /// Expected size of the remote file. Necessary for signature check and Merkle tree |
| 87 | /// verification. |
| 88 | file_size: u64, |
| 89 | |
| 90 | /// Certificate to verify the authenticity of the file's fs-verity signature. |
| 91 | /// TODO(170494765): Implement PKCS#7 signature verification. |
| 92 | _certificate_path: PathBuf, |
| 93 | } |
| 94 | |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame^] | 95 | struct OptionRemoteRoFileUnverified { |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 96 | ino: Inode, |
| 97 | |
| 98 | /// ID to refer to the remote file. |
| 99 | remote_id: i32, |
| 100 | |
| 101 | /// Expected size of the remote file. |
| 102 | file_size: u64, |
| 103 | } |
| 104 | |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame^] | 105 | struct OptionLocalFileRo { |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 106 | ino: Inode, |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 107 | |
| 108 | /// Local path of the backing file. |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 109 | file_path: PathBuf, |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 110 | |
| 111 | /// Local path of the backing file's fs-verity Merkle tree dump. |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 112 | merkle_tree_dump_path: PathBuf, |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 113 | |
| 114 | /// Local path of fs-verity signature for the backing file. |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 115 | signature_path: PathBuf, |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 116 | |
| 117 | /// Certificate to verify the authenticity of the file's fs-verity signature. |
| 118 | /// TODO(170494765): Implement PKCS#7 signature verification. |
| 119 | _certificate_path: PathBuf, |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 120 | } |
| 121 | |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame^] | 122 | struct OptionLocalRoFileUnverified { |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 123 | ino: Inode, |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 124 | |
| 125 | /// Local path of the backing file. |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 126 | file_path: PathBuf, |
| 127 | } |
| 128 | |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame^] | 129 | fn parse_remote_ro_file_option(option: &str) -> Result<OptionRemoteRoFile> { |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 130 | let strs: Vec<&str> = option.split(':').collect(); |
| 131 | if strs.len() != 4 { |
| 132 | bail!("Invalid option: {}", option); |
| 133 | } |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame^] | 134 | Ok(OptionRemoteRoFile { |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 135 | ino: strs[0].parse::<Inode>()?, |
| 136 | remote_id: strs[1].parse::<i32>()?, |
| 137 | file_size: strs[2].parse::<u64>()?, |
| 138 | _certificate_path: PathBuf::from(strs[3]), |
| 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(); |
| 144 | if strs.len() != 3 { |
| 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>()?, |
| 150 | file_size: strs[2].parse::<u64>()?, |
| 151 | }) |
| 152 | } |
| 153 | |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame^] | 154 | fn parse_local_file_ro_option(option: &str) -> Result<OptionLocalFileRo> { |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 155 | let strs: Vec<&str> = option.split(':').collect(); |
| 156 | if strs.len() != 5 { |
| 157 | bail!("Invalid option: {}", option); |
| 158 | } |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame^] | 159 | Ok(OptionLocalFileRo { |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 160 | ino: strs[0].parse::<Inode>()?, |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 161 | file_path: PathBuf::from(strs[1]), |
| 162 | merkle_tree_dump_path: PathBuf::from(strs[2]), |
| 163 | signature_path: PathBuf::from(strs[3]), |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 164 | _certificate_path: PathBuf::from(strs[4]), |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 165 | }) |
| 166 | } |
| 167 | |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame^] | 168 | fn parse_local_ro_file_unverified_ro_option(option: &str) -> Result<OptionLocalRoFileUnverified> { |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 169 | let strs: Vec<&str> = option.split(':').collect(); |
| 170 | if strs.len() != 2 { |
| 171 | bail!("Invalid option: {}", option); |
| 172 | } |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame^] | 173 | Ok(OptionLocalRoFileUnverified { |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 174 | ino: strs[0].parse::<Inode>()?, |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 175 | file_path: PathBuf::from(strs[1]), |
| 176 | }) |
| 177 | } |
| 178 | |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 179 | fn new_config_remote_verified_file(remote_id: i32, file_size: u64) -> Result<FileConfig> { |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame^] | 180 | let service = file::get_local_binder(); |
Andrew Walbran | cc09386 | 2021-03-05 16:59:35 +0000 | [diff] [blame] | 181 | let signature = service.readFsveritySignature(remote_id).context("Failed to read signature")?; |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 182 | |
| 183 | let service = Arc::new(Mutex::new(service)); |
| 184 | let authenticator = FakeAuthenticator::always_succeed(); |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame^] | 185 | Ok(FileConfig::RemoteVerifiedReadonlyFile( |
| 186 | VerifiedFileReader::new( |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 187 | &authenticator, |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame^] | 188 | RemoteFileReader::new(Arc::clone(&service), remote_id), |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 189 | file_size, |
| 190 | signature, |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame^] | 191 | RemoteMerkleTreeReader::new(Arc::clone(&service), remote_id), |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 192 | )?, |
| 193 | file_size, |
| 194 | )) |
| 195 | } |
| 196 | |
| 197 | fn new_config_remote_unverified_file(remote_id: i32, file_size: u64) -> Result<FileConfig> { |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame^] | 198 | let file_reader = |
| 199 | RemoteFileReader::new(Arc::new(Mutex::new(file::get_local_binder())), remote_id); |
| 200 | Ok(FileConfig::RemoteUnverifiedReadonlyFile(file_reader, file_size)) |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 201 | } |
| 202 | |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame^] | 203 | fn new_config_local_ro_file( |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 204 | protected_file: &PathBuf, |
| 205 | merkle_tree_dump: &PathBuf, |
| 206 | signature: &PathBuf, |
| 207 | ) -> Result<FileConfig> { |
| 208 | let file = File::open(&protected_file)?; |
| 209 | let file_size = file.metadata()?.len(); |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame^] | 210 | let file_reader = LocalFileReader::new(file)?; |
| 211 | let merkle_tree_reader = LocalFileReader::new(File::open(merkle_tree_dump)?)?; |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 212 | let authenticator = FakeAuthenticator::always_succeed(); |
| 213 | let mut sig = Vec::new(); |
| 214 | let _ = File::open(signature)?.read_to_end(&mut sig)?; |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame^] | 215 | let file_reader = |
| 216 | VerifiedFileReader::new(&authenticator, file_reader, file_size, sig, merkle_tree_reader)?; |
| 217 | Ok(FileConfig::LocalVerifiedReadonlyFile(file_reader, file_size)) |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 218 | } |
| 219 | |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame^] | 220 | fn new_config_local_ro_file_unverified(file_path: &PathBuf) -> Result<FileConfig> { |
| 221 | let file_reader = LocalFileReader::new(File::open(file_path)?)?; |
Victor Hsieh | fa4477a | 2021-02-08 10:51:50 -0800 | [diff] [blame] | 222 | let file_size = file_reader.len(); |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame^] | 223 | Ok(FileConfig::LocalUnverifiedReadonlyFile(file_reader, file_size)) |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 224 | } |
| 225 | |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 226 | fn prepare_file_pool(args: &Args) -> Result<BTreeMap<Inode, FileConfig>> { |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 227 | let mut file_pool = BTreeMap::new(); |
| 228 | |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame^] | 229 | for config in &args.remote_ro_file { |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 230 | file_pool.insert( |
| 231 | config.ino, |
| 232 | new_config_remote_verified_file(config.remote_id, config.file_size)?, |
| 233 | ); |
| 234 | } |
| 235 | |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame^] | 236 | for config in &args.remote_ro_file_unverified { |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 237 | file_pool.insert( |
| 238 | config.ino, |
| 239 | new_config_remote_unverified_file(config.remote_id, config.file_size)?, |
| 240 | ); |
| 241 | } |
| 242 | |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame^] | 243 | for config in &args.local_ro_file { |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 244 | file_pool.insert( |
| 245 | config.ino, |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame^] | 246 | new_config_local_ro_file( |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 247 | &config.file_path, |
| 248 | &config.merkle_tree_dump_path, |
| 249 | &config.signature_path, |
| 250 | )?, |
| 251 | ); |
| 252 | } |
| 253 | |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame^] | 254 | for config in &args.local_ro_file_unverified { |
| 255 | 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] | 256 | } |
| 257 | |
| 258 | Ok(file_pool) |
| 259 | } |
| 260 | |
| 261 | fn main() -> Result<()> { |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 262 | let args = Args::from_args(); |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 263 | let file_pool = prepare_file_pool(&args)?; |
| 264 | fusefs::loop_forever(file_pool, &args.mount_point)?; |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 265 | bail!("Unexpected exit after the handler loop") |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 266 | } |