blob: a6956e2e79fac8e826a5f83481468bc175425b80 [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
Victor Hsieh88e50172021-10-15 13:27:13 -070025//! 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 Hsieh88ac6ca2020-11-13 15:20:24 -080029
Andrew Walbrancc093862021-03-05 16:59:35 +000030use anyhow::{bail, Context, Result};
Alan Stokese1b6e1c2021-10-01 12:44:49 +010031use log::error;
Victor Hsieh88ac6ca2020-11-13 15:20:24 -080032use std::collections::BTreeMap;
Victor Hsieh50d75ac2021-09-03 14:46:55 -070033use std::convert::TryInto;
Victor Hsieh88e50172021-10-15 13:27:13 -070034use std::path::PathBuf;
Victor Hsieh88ac6ca2020-11-13 15:20:24 -080035use structopt::StructOpt;
36
37mod auth;
38mod common;
39mod crypto;
Victor Hsieh09e26262021-03-03 16:00:55 -080040mod file;
Victor Hsieh88ac6ca2020-11-13 15:20:24 -080041mod fsverity;
42mod fusefs;
Victor Hsieh88ac6ca2020-11-13 15:20:24 -080043
44use auth::FakeAuthenticator;
Victor Hsieh88e50172021-10-15 13:27:13 -070045use file::{RemoteFileEditor, RemoteFileReader, RemoteMerkleTreeReader};
Victor Hsieh6a47e7f2021-03-03 15:53:49 -080046use fsverity::{VerifiedFileEditor, VerifiedFileReader};
Victor Hsieh88ac6ca2020-11-13 15:20:24 -080047use fusefs::{FileConfig, Inode};
Victor Hsieh88ac6ca2020-11-13 15:20:24 -080048
49#[derive(StructOpt)]
Victor Hsiehf01f3232020-12-11 13:31:31 -080050struct Args {
Victor Hsieh88ac6ca2020-11-13 15:20:24 -080051 /// Mount point of AuthFS.
52 #[structopt(parse(from_os_str))]
53 mount_point: PathBuf,
54
Victor Hsieh2445e332021-06-04 16:44:53 -070055 /// CID of the VM where the service runs.
56 #[structopt(long)]
Victor Hsieh1a8cd042021-09-03 16:29:45 -070057 cid: u32,
Victor Hsieh2445e332021-06-04 16:44:53 -070058
Victor Hsieh4cc3b792021-08-04 12:00:04 -070059 /// Extra options to FUSE
60 #[structopt(short = "o")]
61 extra_options: Option<String>,
62
Victor Hsieh09e26262021-03-03 16:00:55 -080063 /// A read-only remote file with integrity check. Can be multiple.
Victor Hsiehf01f3232020-12-11 13:31:31 -080064 ///
Victor Hsieh50d75ac2021-09-03 14:46:55 -070065 /// 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 Hsieh09e26262021-03-03 16:00:55 -080067 #[structopt(long, parse(try_from_str = parse_remote_ro_file_option))]
68 remote_ro_file: Vec<OptionRemoteRoFile>,
Victor Hsiehf01f3232020-12-11 13:31:31 -080069
Victor Hsieh09e26262021-03-03 16:00:55 -080070 /// A read-only remote file without integrity check. Can be multiple.
Victor Hsiehf01f3232020-12-11 13:31:31 -080071 ///
Victor Hsieh50d75ac2021-09-03 14:46:55 -070072 /// For example, `--remote-unverified-file 5:10` tells the filesystem to associate entry 5
73 /// with a remote file 10.
Victor Hsieh09e26262021-03-03 16:00:55 -080074 #[structopt(long, parse(try_from_str = parse_remote_ro_file_unverified_option))]
75 remote_ro_file_unverified: Vec<OptionRemoteRoFileUnverified>,
Victor Hsiehf01f3232020-12-11 13:31:31 -080076
Victor Hsieh6a47e7f2021-03-03 15:53:49 -080077 /// 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 Hsieh9d0ab622021-04-26 17:07:02 -070084 /// Enable debugging features.
85 #[structopt(long)]
86 debug: bool,
Victor Hsieh88ac6ca2020-11-13 15:20:24 -080087}
88
Victor Hsieh09e26262021-03-03 16:00:55 -080089struct OptionRemoteRoFile {
Victor Hsiehf01f3232020-12-11 13:31:31 -080090 ino: Inode,
91
92 /// ID to refer to the remote file.
93 remote_id: i32,
94
Victor Hsiehf01f3232020-12-11 13:31:31 -080095 /// 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 Hsieh09e26262021-03-03 16:00:55 -0800100struct OptionRemoteRoFileUnverified {
Victor Hsiehf01f3232020-12-11 13:31:31 -0800101 ino: Inode,
102
103 /// ID to refer to the remote file.
104 remote_id: i32,
Victor Hsiehf01f3232020-12-11 13:31:31 -0800105}
106
Victor Hsieh6a47e7f2021-03-03 15:53:49 -0800107struct OptionRemoteRwFile {
108 ino: Inode,
109
110 /// ID to refer to the remote file.
111 remote_id: i32,
112}
113
Victor Hsieh09e26262021-03-03 16:00:55 -0800114fn parse_remote_ro_file_option(option: &str) -> Result<OptionRemoteRoFile> {
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800115 let strs: Vec<&str> = option.split(':').collect();
Victor Hsieh50d75ac2021-09-03 14:46:55 -0700116 if strs.len() != 3 {
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800117 bail!("Invalid option: {}", option);
118 }
Victor Hsieh09e26262021-03-03 16:00:55 -0800119 Ok(OptionRemoteRoFile {
Victor Hsiehf01f3232020-12-11 13:31:31 -0800120 ino: strs[0].parse::<Inode>()?,
121 remote_id: strs[1].parse::<i32>()?,
Victor Hsieh50d75ac2021-09-03 14:46:55 -0700122 _certificate_path: PathBuf::from(strs[2]),
Victor Hsiehf01f3232020-12-11 13:31:31 -0800123 })
124}
125
Victor Hsieh09e26262021-03-03 16:00:55 -0800126fn parse_remote_ro_file_unverified_option(option: &str) -> Result<OptionRemoteRoFileUnverified> {
Victor Hsiehf01f3232020-12-11 13:31:31 -0800127 let strs: Vec<&str> = option.split(':').collect();
Victor Hsieh50d75ac2021-09-03 14:46:55 -0700128 if strs.len() != 2 {
Victor Hsiehf01f3232020-12-11 13:31:31 -0800129 bail!("Invalid option: {}", option);
130 }
Victor Hsieh09e26262021-03-03 16:00:55 -0800131 Ok(OptionRemoteRoFileUnverified {
Victor Hsiehf01f3232020-12-11 13:31:31 -0800132 ino: strs[0].parse::<Inode>()?,
133 remote_id: strs[1].parse::<i32>()?,
Victor Hsiehf01f3232020-12-11 13:31:31 -0800134 })
135}
136
Victor Hsieh6a47e7f2021-03-03 15:53:49 -0800137fn 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 Hsieh2445e332021-06-04 16:44:53 -0700148fn new_config_remote_verified_file(
149 service: file::VirtFdService,
150 remote_id: i32,
151 file_size: u64,
152) -> Result<FileConfig> {
Andrew Walbrancc093862021-03-05 16:59:35 +0000153 let signature = service.readFsveritySignature(remote_id).context("Failed to read signature")?;
Victor Hsiehf01f3232020-12-11 13:31:31 -0800154
Victor Hsiehf01f3232020-12-11 13:31:31 -0800155 let authenticator = FakeAuthenticator::always_succeed();
Victor Hsieh88e50172021-10-15 13:27:13 -0700156 Ok(FileConfig::VerifiedReadonly {
Victor Hsieh1bcf4112021-03-19 14:26:57 -0700157 reader: VerifiedFileReader::new(
Victor Hsiehf01f3232020-12-11 13:31:31 -0800158 &authenticator,
Victor Hsiehc3d45b12021-06-30 09:16:41 -0700159 RemoteFileReader::new(service.clone(), remote_id),
Victor Hsiehf01f3232020-12-11 13:31:31 -0800160 file_size,
161 signature,
Victor Hsiehc3d45b12021-06-30 09:16:41 -0700162 RemoteMerkleTreeReader::new(service.clone(), remote_id),
Victor Hsiehf01f3232020-12-11 13:31:31 -0800163 )?,
164 file_size,
Victor Hsieh1bcf4112021-03-19 14:26:57 -0700165 })
Victor Hsiehf01f3232020-12-11 13:31:31 -0800166}
167
Victor Hsieh2445e332021-06-04 16:44:53 -0700168fn new_config_remote_unverified_file(
169 service: file::VirtFdService,
170 remote_id: i32,
171 file_size: u64,
172) -> Result<FileConfig> {
Victor Hsiehc3d45b12021-06-30 09:16:41 -0700173 let reader = RemoteFileReader::new(service, remote_id);
Victor Hsieh88e50172021-10-15 13:27:13 -0700174 Ok(FileConfig::UnverifiedReadonly { reader, file_size })
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800175}
176
Victor Hsieh2445e332021-06-04 16:44:53 -0700177fn new_config_remote_new_verified_file(
178 service: file::VirtFdService,
179 remote_id: i32,
180) -> Result<FileConfig> {
Victor Hsiehc3d45b12021-06-30 09:16:41 -0700181 let remote_file = RemoteFileEditor::new(service, remote_id);
Victor Hsieh88e50172021-10-15 13:27:13 -0700182 Ok(FileConfig::VerifiedNew { editor: VerifiedFileEditor::new(remote_file) })
Victor Hsieh6a47e7f2021-03-03 15:53:49 -0800183}
184
Victor Hsiehf01f3232020-12-11 13:31:31 -0800185fn prepare_file_pool(args: &Args) -> Result<BTreeMap<Inode, FileConfig>> {
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800186 let mut file_pool = BTreeMap::new();
187
Victor Hsieh88e50172021-10-15 13:27:13 -0700188 let service = file::get_rpc_binder_service(args.cid)?;
Victor Hsieh2445e332021-06-04 16:44:53 -0700189
Victor Hsieh88e50172021-10-15 13:27:13 -0700190 for config in &args.remote_ro_file {
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800191 file_pool.insert(
192 config.ino,
Victor Hsieh88e50172021-10-15 13:27:13 -0700193 new_config_remote_verified_file(
194 service.clone(),
195 config.remote_id,
196 service.getFileSize(config.remote_id)?.try_into()?,
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800197 )?,
198 );
199 }
200
Victor Hsieh88e50172021-10-15 13:27:13 -0700201 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 Hsieh88ac6ca2020-11-13 15:20:24 -0800217 }
218
219 Ok(file_pool)
220}
221
Alan Stokese1b6e1c2021-10-01 12:44:49 +0100222fn try_main() -> Result<()> {
Victor Hsiehf01f3232020-12-11 13:31:31 -0800223 let args = Args::from_args();
Victor Hsieh9d0ab622021-04-26 17:07:02 -0700224
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 Hsieh88ac6ca2020-11-13 15:20:24 -0800230 let file_pool = prepare_file_pool(&args)?;
Victor Hsieh4cc3b792021-08-04 12:00:04 -0700231 fusefs::loop_forever(file_pool, &args.mount_point, &args.extra_options)?;
Victor Hsiehf01f3232020-12-11 13:31:31 -0800232 bail!("Unexpected exit after the handler loop")
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800233}
Alan Stokese1b6e1c2021-10-01 12:44:49 +0100234
235fn main() {
236 if let Err(e) = try_main() {
237 error!("failed with {:?}", e);
238 std::process::exit(1);
239 }
240}