blob: 593fa7402b354dc611b2412fc1defe5300f4ea54 [file] [log] [blame]
Victor Hsieh88ac6ca2020-11-13 15:20:24 -08001/*
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 Walbrancc093862021-03-05 16:59:35 +000030use anyhow::{bail, Context, Result};
Victor Hsieh88ac6ca2020-11-13 15:20:24 -080031use std::collections::BTreeMap;
32use std::fs::File;
33use std::io::Read;
Victor Hsieh6cf75b52021-04-01 12:45:49 -070034use std::path::{Path, PathBuf};
Victor Hsiehf01f3232020-12-11 13:31:31 -080035use std::sync::{Arc, Mutex};
Victor Hsieh88ac6ca2020-11-13 15:20:24 -080036use structopt::StructOpt;
37
38mod auth;
39mod common;
40mod crypto;
Victor Hsieh09e26262021-03-03 16:00:55 -080041mod file;
Victor Hsieh88ac6ca2020-11-13 15:20:24 -080042mod fsverity;
43mod fusefs;
Victor Hsieh88ac6ca2020-11-13 15:20:24 -080044
45use auth::FakeAuthenticator;
Victor Hsieh6a47e7f2021-03-03 15:53:49 -080046use file::{LocalFileReader, RemoteFileEditor, RemoteFileReader, RemoteMerkleTreeReader};
47use fsverity::{VerifiedFileEditor, VerifiedFileReader};
Victor Hsieh88ac6ca2020-11-13 15:20:24 -080048use fusefs::{FileConfig, Inode};
Victor Hsieh88ac6ca2020-11-13 15:20:24 -080049
50#[derive(StructOpt)]
Victor Hsiehf01f3232020-12-11 13:31:31 -080051struct Args {
Victor Hsieh88ac6ca2020-11-13 15:20:24 -080052 /// Mount point of AuthFS.
53 #[structopt(parse(from_os_str))]
54 mount_point: PathBuf,
55
Victor Hsieh2445e332021-06-04 16:44:53 -070056 /// CID of the VM where the service runs.
57 #[structopt(long)]
58 cid: Option<u32>,
59
Victor Hsieh09e26262021-03-03 16:00:55 -080060 /// A read-only remote file with integrity check. Can be multiple.
Victor Hsiehf01f3232020-12-11 13:31:31 -080061 ///
62 /// For example, `--remote-verified-file 5:10:1234:/path/to/cert` tells the filesystem to
63 /// associate entry 5 with a remote file 10 of size 1234 bytes, and need to be verified against
64 /// the /path/to/cert.
Victor Hsieh09e26262021-03-03 16:00:55 -080065 #[structopt(long, parse(try_from_str = parse_remote_ro_file_option))]
66 remote_ro_file: Vec<OptionRemoteRoFile>,
Victor Hsiehf01f3232020-12-11 13:31:31 -080067
Victor Hsieh09e26262021-03-03 16:00:55 -080068 /// A read-only remote file without integrity check. Can be multiple.
Victor Hsiehf01f3232020-12-11 13:31:31 -080069 ///
70 /// For example, `--remote-unverified-file 5:10:1234` tells the filesystem to associate entry 5
71 /// with a remote file 10 of size 1234 bytes.
Victor Hsieh09e26262021-03-03 16:00:55 -080072 #[structopt(long, parse(try_from_str = parse_remote_ro_file_unverified_option))]
73 remote_ro_file_unverified: Vec<OptionRemoteRoFileUnverified>,
Victor Hsiehf01f3232020-12-11 13:31:31 -080074
Victor Hsieh6a47e7f2021-03-03 15:53:49 -080075 /// A new read-writable remote file with integrity check. Can be multiple.
76 ///
77 /// For example, `--remote-new-verified-file 12:34` tells the filesystem to associate entry 12
78 /// with a remote file 34.
79 #[structopt(long, parse(try_from_str = parse_remote_new_rw_file_option))]
80 remote_new_rw_file: Vec<OptionRemoteRwFile>,
81
Victor Hsieh09e26262021-03-03 16:00:55 -080082 /// Debug only. A read-only local file with integrity check. Can be multiple.
83 #[structopt(long, parse(try_from_str = parse_local_file_ro_option))]
84 local_ro_file: Vec<OptionLocalFileRo>,
Victor Hsieh88ac6ca2020-11-13 15:20:24 -080085
Victor Hsieh09e26262021-03-03 16:00:55 -080086 /// Debug only. A read-only local file without integrity check. Can be multiple.
87 #[structopt(long, parse(try_from_str = parse_local_ro_file_unverified_ro_option))]
88 local_ro_file_unverified: Vec<OptionLocalRoFileUnverified>,
Victor Hsieh9d0ab622021-04-26 17:07:02 -070089
90 /// Enable debugging features.
91 #[structopt(long)]
92 debug: bool,
Victor Hsieh88ac6ca2020-11-13 15:20:24 -080093}
94
Victor Hsieh09e26262021-03-03 16:00:55 -080095struct OptionRemoteRoFile {
Victor Hsiehf01f3232020-12-11 13:31:31 -080096 ino: Inode,
97
98 /// ID to refer to the remote file.
99 remote_id: i32,
100
101 /// Expected size of the remote file. Necessary for signature check and Merkle tree
102 /// verification.
103 file_size: u64,
104
105 /// Certificate to verify the authenticity of the file's fs-verity signature.
106 /// TODO(170494765): Implement PKCS#7 signature verification.
107 _certificate_path: PathBuf,
108}
109
Victor Hsieh09e26262021-03-03 16:00:55 -0800110struct OptionRemoteRoFileUnverified {
Victor Hsiehf01f3232020-12-11 13:31:31 -0800111 ino: Inode,
112
113 /// ID to refer to the remote file.
114 remote_id: i32,
115
116 /// Expected size of the remote file.
117 file_size: u64,
118}
119
Victor Hsieh6a47e7f2021-03-03 15:53:49 -0800120struct OptionRemoteRwFile {
121 ino: Inode,
122
123 /// ID to refer to the remote file.
124 remote_id: i32,
125}
126
Victor Hsieh09e26262021-03-03 16:00:55 -0800127struct OptionLocalFileRo {
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800128 ino: Inode,
Victor Hsiehf01f3232020-12-11 13:31:31 -0800129
130 /// Local path of the backing file.
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800131 file_path: PathBuf,
Victor Hsiehf01f3232020-12-11 13:31:31 -0800132
133 /// Local path of the backing file's fs-verity Merkle tree dump.
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800134 merkle_tree_dump_path: PathBuf,
Victor Hsiehf01f3232020-12-11 13:31:31 -0800135
136 /// Local path of fs-verity signature for the backing file.
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800137 signature_path: PathBuf,
Victor Hsiehf01f3232020-12-11 13:31:31 -0800138
139 /// Certificate to verify the authenticity of the file's fs-verity signature.
140 /// TODO(170494765): Implement PKCS#7 signature verification.
141 _certificate_path: PathBuf,
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800142}
143
Victor Hsieh09e26262021-03-03 16:00:55 -0800144struct OptionLocalRoFileUnverified {
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800145 ino: Inode,
Victor Hsiehf01f3232020-12-11 13:31:31 -0800146
147 /// Local path of the backing file.
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800148 file_path: PathBuf,
149}
150
Victor Hsieh09e26262021-03-03 16:00:55 -0800151fn parse_remote_ro_file_option(option: &str) -> Result<OptionRemoteRoFile> {
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800152 let strs: Vec<&str> = option.split(':').collect();
153 if strs.len() != 4 {
154 bail!("Invalid option: {}", option);
155 }
Victor Hsieh09e26262021-03-03 16:00:55 -0800156 Ok(OptionRemoteRoFile {
Victor Hsiehf01f3232020-12-11 13:31:31 -0800157 ino: strs[0].parse::<Inode>()?,
158 remote_id: strs[1].parse::<i32>()?,
159 file_size: strs[2].parse::<u64>()?,
160 _certificate_path: PathBuf::from(strs[3]),
161 })
162}
163
Victor Hsieh09e26262021-03-03 16:00:55 -0800164fn parse_remote_ro_file_unverified_option(option: &str) -> Result<OptionRemoteRoFileUnverified> {
Victor Hsiehf01f3232020-12-11 13:31:31 -0800165 let strs: Vec<&str> = option.split(':').collect();
166 if strs.len() != 3 {
167 bail!("Invalid option: {}", option);
168 }
Victor Hsieh09e26262021-03-03 16:00:55 -0800169 Ok(OptionRemoteRoFileUnverified {
Victor Hsiehf01f3232020-12-11 13:31:31 -0800170 ino: strs[0].parse::<Inode>()?,
171 remote_id: strs[1].parse::<i32>()?,
172 file_size: strs[2].parse::<u64>()?,
173 })
174}
175
Victor Hsieh6a47e7f2021-03-03 15:53:49 -0800176fn parse_remote_new_rw_file_option(option: &str) -> Result<OptionRemoteRwFile> {
177 let strs: Vec<&str> = option.split(':').collect();
178 if strs.len() != 2 {
179 bail!("Invalid option: {}", option);
180 }
181 Ok(OptionRemoteRwFile {
182 ino: strs[0].parse::<Inode>().unwrap(),
183 remote_id: strs[1].parse::<i32>().unwrap(),
184 })
185}
186
Victor Hsieh09e26262021-03-03 16:00:55 -0800187fn parse_local_file_ro_option(option: &str) -> Result<OptionLocalFileRo> {
Victor Hsiehf01f3232020-12-11 13:31:31 -0800188 let strs: Vec<&str> = option.split(':').collect();
189 if strs.len() != 5 {
190 bail!("Invalid option: {}", option);
191 }
Victor Hsieh09e26262021-03-03 16:00:55 -0800192 Ok(OptionLocalFileRo {
Victor Hsiehf01f3232020-12-11 13:31:31 -0800193 ino: strs[0].parse::<Inode>()?,
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800194 file_path: PathBuf::from(strs[1]),
195 merkle_tree_dump_path: PathBuf::from(strs[2]),
196 signature_path: PathBuf::from(strs[3]),
Victor Hsiehf01f3232020-12-11 13:31:31 -0800197 _certificate_path: PathBuf::from(strs[4]),
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800198 })
199}
200
Victor Hsieh09e26262021-03-03 16:00:55 -0800201fn parse_local_ro_file_unverified_ro_option(option: &str) -> Result<OptionLocalRoFileUnverified> {
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800202 let strs: Vec<&str> = option.split(':').collect();
203 if strs.len() != 2 {
204 bail!("Invalid option: {}", option);
205 }
Victor Hsieh09e26262021-03-03 16:00:55 -0800206 Ok(OptionLocalRoFileUnverified {
Victor Hsiehf01f3232020-12-11 13:31:31 -0800207 ino: strs[0].parse::<Inode>()?,
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800208 file_path: PathBuf::from(strs[1]),
209 })
210}
211
Victor Hsieh2445e332021-06-04 16:44:53 -0700212fn new_config_remote_verified_file(
213 service: file::VirtFdService,
214 remote_id: i32,
215 file_size: u64,
216) -> Result<FileConfig> {
Andrew Walbrancc093862021-03-05 16:59:35 +0000217 let signature = service.readFsveritySignature(remote_id).context("Failed to read signature")?;
Victor Hsiehf01f3232020-12-11 13:31:31 -0800218
219 let service = Arc::new(Mutex::new(service));
220 let authenticator = FakeAuthenticator::always_succeed();
Victor Hsieh1bcf4112021-03-19 14:26:57 -0700221 Ok(FileConfig::RemoteVerifiedReadonlyFile {
222 reader: VerifiedFileReader::new(
Victor Hsiehf01f3232020-12-11 13:31:31 -0800223 &authenticator,
Victor Hsieh09e26262021-03-03 16:00:55 -0800224 RemoteFileReader::new(Arc::clone(&service), remote_id),
Victor Hsiehf01f3232020-12-11 13:31:31 -0800225 file_size,
226 signature,
Victor Hsieh09e26262021-03-03 16:00:55 -0800227 RemoteMerkleTreeReader::new(Arc::clone(&service), remote_id),
Victor Hsiehf01f3232020-12-11 13:31:31 -0800228 )?,
229 file_size,
Victor Hsieh1bcf4112021-03-19 14:26:57 -0700230 })
Victor Hsiehf01f3232020-12-11 13:31:31 -0800231}
232
Victor Hsieh2445e332021-06-04 16:44:53 -0700233fn new_config_remote_unverified_file(
234 service: file::VirtFdService,
235 remote_id: i32,
236 file_size: u64,
237) -> Result<FileConfig> {
238 let reader = RemoteFileReader::new(Arc::new(Mutex::new(service)), remote_id);
Victor Hsieh1bcf4112021-03-19 14:26:57 -0700239 Ok(FileConfig::RemoteUnverifiedReadonlyFile { reader, file_size })
Victor Hsiehf01f3232020-12-11 13:31:31 -0800240}
241
Victor Hsieh09e26262021-03-03 16:00:55 -0800242fn new_config_local_ro_file(
Victor Hsieh6cf75b52021-04-01 12:45:49 -0700243 protected_file: &Path,
244 merkle_tree_dump: &Path,
245 signature: &Path,
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800246) -> Result<FileConfig> {
247 let file = File::open(&protected_file)?;
248 let file_size = file.metadata()?.len();
Victor Hsieh09e26262021-03-03 16:00:55 -0800249 let file_reader = LocalFileReader::new(file)?;
250 let merkle_tree_reader = LocalFileReader::new(File::open(merkle_tree_dump)?)?;
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800251 let authenticator = FakeAuthenticator::always_succeed();
252 let mut sig = Vec::new();
253 let _ = File::open(signature)?.read_to_end(&mut sig)?;
Victor Hsieh1bcf4112021-03-19 14:26:57 -0700254 let reader =
Victor Hsieh09e26262021-03-03 16:00:55 -0800255 VerifiedFileReader::new(&authenticator, file_reader, file_size, sig, merkle_tree_reader)?;
Victor Hsieh1bcf4112021-03-19 14:26:57 -0700256 Ok(FileConfig::LocalVerifiedReadonlyFile { reader, file_size })
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800257}
258
Victor Hsieh6cf75b52021-04-01 12:45:49 -0700259fn new_config_local_ro_file_unverified(file_path: &Path) -> Result<FileConfig> {
Victor Hsieh1bcf4112021-03-19 14:26:57 -0700260 let reader = LocalFileReader::new(File::open(file_path)?)?;
261 let file_size = reader.len();
262 Ok(FileConfig::LocalUnverifiedReadonlyFile { reader, file_size })
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800263}
264
Victor Hsieh2445e332021-06-04 16:44:53 -0700265fn new_config_remote_new_verified_file(
266 service: file::VirtFdService,
267 remote_id: i32,
268) -> Result<FileConfig> {
269 let remote_file = RemoteFileEditor::new(Arc::new(Mutex::new(service)), remote_id);
Victor Hsieh1bcf4112021-03-19 14:26:57 -0700270 Ok(FileConfig::RemoteVerifiedNewFile { editor: VerifiedFileEditor::new(remote_file) })
Victor Hsieh6a47e7f2021-03-03 15:53:49 -0800271}
272
Victor Hsiehf01f3232020-12-11 13:31:31 -0800273fn prepare_file_pool(args: &Args) -> Result<BTreeMap<Inode, FileConfig>> {
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800274 let mut file_pool = BTreeMap::new();
275
Victor Hsieh2445e332021-06-04 16:44:53 -0700276 let service = file::get_binder_service(args.cid)?;
277
Victor Hsieh09e26262021-03-03 16:00:55 -0800278 for config in &args.remote_ro_file {
Victor Hsiehf01f3232020-12-11 13:31:31 -0800279 file_pool.insert(
280 config.ino,
Victor Hsieh2445e332021-06-04 16:44:53 -0700281 new_config_remote_verified_file(service.clone(), config.remote_id, config.file_size)?,
Victor Hsiehf01f3232020-12-11 13:31:31 -0800282 );
283 }
284
Victor Hsieh09e26262021-03-03 16:00:55 -0800285 for config in &args.remote_ro_file_unverified {
Victor Hsiehf01f3232020-12-11 13:31:31 -0800286 file_pool.insert(
287 config.ino,
Victor Hsieh2445e332021-06-04 16:44:53 -0700288 new_config_remote_unverified_file(service.clone(), config.remote_id, config.file_size)?,
Victor Hsiehf01f3232020-12-11 13:31:31 -0800289 );
290 }
291
Victor Hsieh6a47e7f2021-03-03 15:53:49 -0800292 for config in &args.remote_new_rw_file {
Victor Hsieh2445e332021-06-04 16:44:53 -0700293 file_pool.insert(
294 config.ino,
295 new_config_remote_new_verified_file(service.clone(), config.remote_id)?,
296 );
Victor Hsieh6a47e7f2021-03-03 15:53:49 -0800297 }
298
Victor Hsieh09e26262021-03-03 16:00:55 -0800299 for config in &args.local_ro_file {
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800300 file_pool.insert(
301 config.ino,
Victor Hsieh09e26262021-03-03 16:00:55 -0800302 new_config_local_ro_file(
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800303 &config.file_path,
304 &config.merkle_tree_dump_path,
305 &config.signature_path,
306 )?,
307 );
308 }
309
Victor Hsieh09e26262021-03-03 16:00:55 -0800310 for config in &args.local_ro_file_unverified {
311 file_pool.insert(config.ino, new_config_local_ro_file_unverified(&config.file_path)?);
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800312 }
313
314 Ok(file_pool)
315}
316
317fn main() -> Result<()> {
Victor Hsiehf01f3232020-12-11 13:31:31 -0800318 let args = Args::from_args();
Victor Hsieh9d0ab622021-04-26 17:07:02 -0700319
320 let log_level = if args.debug { log::Level::Debug } else { log::Level::Info };
321 android_logger::init_once(
322 android_logger::Config::default().with_tag("authfs").with_min_level(log_level),
323 );
324
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800325 let file_pool = prepare_file_pool(&args)?;
326 fusefs::loop_forever(file_pool, &args.mount_point)?;
Victor Hsiehf01f3232020-12-11 13:31:31 -0800327 bail!("Unexpected exit after the handler loop")
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800328}