blob: e004b815c916ddc283eca7395d9618cf23f13096 [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;
Victor Hsieh50d75ac2021-09-03 14:46:55 -070032use std::convert::TryInto;
Victor Hsieh88ac6ca2020-11-13 15:20:24 -080033use std::fs::File;
34use std::io::Read;
Victor Hsieh6cf75b52021-04-01 12:45:49 -070035use std::path::{Path, PathBuf};
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 Hsieh4cc3b792021-08-04 12:00:04 -070060 /// Extra options to FUSE
61 #[structopt(short = "o")]
62 extra_options: Option<String>,
63
Victor Hsieh09e26262021-03-03 16:00:55 -080064 /// A read-only remote file with integrity check. Can be multiple.
Victor Hsiehf01f3232020-12-11 13:31:31 -080065 ///
Victor Hsieh50d75ac2021-09-03 14:46:55 -070066 /// For example, `--remote-verified-file 5:10:/path/to/cert` tells the filesystem to associate
67 /// entry 5 with a remote file 10, and need to be verified against the /path/to/cert.
Victor Hsieh09e26262021-03-03 16:00:55 -080068 #[structopt(long, parse(try_from_str = parse_remote_ro_file_option))]
69 remote_ro_file: Vec<OptionRemoteRoFile>,
Victor Hsiehf01f3232020-12-11 13:31:31 -080070
Victor Hsieh09e26262021-03-03 16:00:55 -080071 /// A read-only remote file without integrity check. Can be multiple.
Victor Hsiehf01f3232020-12-11 13:31:31 -080072 ///
Victor Hsieh50d75ac2021-09-03 14:46:55 -070073 /// For example, `--remote-unverified-file 5:10` tells the filesystem to associate entry 5
74 /// with a remote file 10.
Victor Hsieh09e26262021-03-03 16:00:55 -080075 #[structopt(long, parse(try_from_str = parse_remote_ro_file_unverified_option))]
76 remote_ro_file_unverified: Vec<OptionRemoteRoFileUnverified>,
Victor Hsiehf01f3232020-12-11 13:31:31 -080077
Victor Hsieh6a47e7f2021-03-03 15:53:49 -080078 /// A new read-writable remote file with integrity check. Can be multiple.
79 ///
80 /// For example, `--remote-new-verified-file 12:34` tells the filesystem to associate entry 12
81 /// with a remote file 34.
82 #[structopt(long, parse(try_from_str = parse_remote_new_rw_file_option))]
83 remote_new_rw_file: Vec<OptionRemoteRwFile>,
84
Victor Hsieh09e26262021-03-03 16:00:55 -080085 /// Debug only. A read-only local file with integrity check. Can be multiple.
86 #[structopt(long, parse(try_from_str = parse_local_file_ro_option))]
87 local_ro_file: Vec<OptionLocalFileRo>,
Victor Hsieh88ac6ca2020-11-13 15:20:24 -080088
Victor Hsieh09e26262021-03-03 16:00:55 -080089 /// Debug only. A read-only local file without integrity check. Can be multiple.
90 #[structopt(long, parse(try_from_str = parse_local_ro_file_unverified_ro_option))]
91 local_ro_file_unverified: Vec<OptionLocalRoFileUnverified>,
Victor Hsieh9d0ab622021-04-26 17:07:02 -070092
93 /// Enable debugging features.
94 #[structopt(long)]
95 debug: bool,
Victor Hsieh88ac6ca2020-11-13 15:20:24 -080096}
97
Victor Hsieh9ab13e52021-06-29 09:23:29 -070098impl Args {
99 fn has_remote_files(&self) -> bool {
100 !self.remote_ro_file.is_empty()
101 || !self.remote_ro_file_unverified.is_empty()
102 || !self.remote_new_rw_file.is_empty()
103 }
104}
105
Victor Hsieh09e26262021-03-03 16:00:55 -0800106struct OptionRemoteRoFile {
Victor Hsiehf01f3232020-12-11 13:31:31 -0800107 ino: Inode,
108
109 /// ID to refer to the remote file.
110 remote_id: i32,
111
Victor Hsiehf01f3232020-12-11 13:31:31 -0800112 /// Certificate to verify the authenticity of the file's fs-verity signature.
113 /// TODO(170494765): Implement PKCS#7 signature verification.
114 _certificate_path: PathBuf,
115}
116
Victor Hsieh09e26262021-03-03 16:00:55 -0800117struct OptionRemoteRoFileUnverified {
Victor Hsiehf01f3232020-12-11 13:31:31 -0800118 ino: Inode,
119
120 /// ID to refer to the remote file.
121 remote_id: i32,
Victor Hsiehf01f3232020-12-11 13:31:31 -0800122}
123
Victor Hsieh6a47e7f2021-03-03 15:53:49 -0800124struct OptionRemoteRwFile {
125 ino: Inode,
126
127 /// ID to refer to the remote file.
128 remote_id: i32,
129}
130
Victor Hsieh09e26262021-03-03 16:00:55 -0800131struct OptionLocalFileRo {
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800132 ino: Inode,
Victor Hsiehf01f3232020-12-11 13:31:31 -0800133
134 /// Local path of the backing file.
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800135 file_path: PathBuf,
Victor Hsiehf01f3232020-12-11 13:31:31 -0800136
137 /// Local path of the backing file's fs-verity Merkle tree dump.
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800138 merkle_tree_dump_path: PathBuf,
Victor Hsiehf01f3232020-12-11 13:31:31 -0800139
140 /// Local path of fs-verity signature for the backing file.
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800141 signature_path: PathBuf,
Victor Hsiehf01f3232020-12-11 13:31:31 -0800142
143 /// Certificate to verify the authenticity of the file's fs-verity signature.
144 /// TODO(170494765): Implement PKCS#7 signature verification.
145 _certificate_path: PathBuf,
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800146}
147
Victor Hsieh09e26262021-03-03 16:00:55 -0800148struct OptionLocalRoFileUnverified {
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800149 ino: Inode,
Victor Hsiehf01f3232020-12-11 13:31:31 -0800150
151 /// Local path of the backing file.
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800152 file_path: PathBuf,
153}
154
Victor Hsieh09e26262021-03-03 16:00:55 -0800155fn parse_remote_ro_file_option(option: &str) -> Result<OptionRemoteRoFile> {
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800156 let strs: Vec<&str> = option.split(':').collect();
Victor Hsieh50d75ac2021-09-03 14:46:55 -0700157 if strs.len() != 3 {
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800158 bail!("Invalid option: {}", option);
159 }
Victor Hsieh09e26262021-03-03 16:00:55 -0800160 Ok(OptionRemoteRoFile {
Victor Hsiehf01f3232020-12-11 13:31:31 -0800161 ino: strs[0].parse::<Inode>()?,
162 remote_id: strs[1].parse::<i32>()?,
Victor Hsieh50d75ac2021-09-03 14:46:55 -0700163 _certificate_path: PathBuf::from(strs[2]),
Victor Hsiehf01f3232020-12-11 13:31:31 -0800164 })
165}
166
Victor Hsieh09e26262021-03-03 16:00:55 -0800167fn parse_remote_ro_file_unverified_option(option: &str) -> Result<OptionRemoteRoFileUnverified> {
Victor Hsiehf01f3232020-12-11 13:31:31 -0800168 let strs: Vec<&str> = option.split(':').collect();
Victor Hsieh50d75ac2021-09-03 14:46:55 -0700169 if strs.len() != 2 {
Victor Hsiehf01f3232020-12-11 13:31:31 -0800170 bail!("Invalid option: {}", option);
171 }
Victor Hsieh09e26262021-03-03 16:00:55 -0800172 Ok(OptionRemoteRoFileUnverified {
Victor Hsiehf01f3232020-12-11 13:31:31 -0800173 ino: strs[0].parse::<Inode>()?,
174 remote_id: strs[1].parse::<i32>()?,
Victor Hsiehf01f3232020-12-11 13:31:31 -0800175 })
176}
177
Victor Hsieh6a47e7f2021-03-03 15:53:49 -0800178fn parse_remote_new_rw_file_option(option: &str) -> Result<OptionRemoteRwFile> {
179 let strs: Vec<&str> = option.split(':').collect();
180 if strs.len() != 2 {
181 bail!("Invalid option: {}", option);
182 }
183 Ok(OptionRemoteRwFile {
184 ino: strs[0].parse::<Inode>().unwrap(),
185 remote_id: strs[1].parse::<i32>().unwrap(),
186 })
187}
188
Victor Hsieh09e26262021-03-03 16:00:55 -0800189fn parse_local_file_ro_option(option: &str) -> Result<OptionLocalFileRo> {
Victor Hsiehf01f3232020-12-11 13:31:31 -0800190 let strs: Vec<&str> = option.split(':').collect();
191 if strs.len() != 5 {
192 bail!("Invalid option: {}", option);
193 }
Victor Hsieh09e26262021-03-03 16:00:55 -0800194 Ok(OptionLocalFileRo {
Victor Hsiehf01f3232020-12-11 13:31:31 -0800195 ino: strs[0].parse::<Inode>()?,
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800196 file_path: PathBuf::from(strs[1]),
197 merkle_tree_dump_path: PathBuf::from(strs[2]),
198 signature_path: PathBuf::from(strs[3]),
Victor Hsiehf01f3232020-12-11 13:31:31 -0800199 _certificate_path: PathBuf::from(strs[4]),
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800200 })
201}
202
Victor Hsieh09e26262021-03-03 16:00:55 -0800203fn parse_local_ro_file_unverified_ro_option(option: &str) -> Result<OptionLocalRoFileUnverified> {
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800204 let strs: Vec<&str> = option.split(':').collect();
205 if strs.len() != 2 {
206 bail!("Invalid option: {}", option);
207 }
Victor Hsieh09e26262021-03-03 16:00:55 -0800208 Ok(OptionLocalRoFileUnverified {
Victor Hsiehf01f3232020-12-11 13:31:31 -0800209 ino: strs[0].parse::<Inode>()?,
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800210 file_path: PathBuf::from(strs[1]),
211 })
212}
213
Victor Hsieh2445e332021-06-04 16:44:53 -0700214fn new_config_remote_verified_file(
215 service: file::VirtFdService,
216 remote_id: i32,
217 file_size: u64,
218) -> Result<FileConfig> {
Andrew Walbrancc093862021-03-05 16:59:35 +0000219 let signature = service.readFsveritySignature(remote_id).context("Failed to read signature")?;
Victor Hsiehf01f3232020-12-11 13:31:31 -0800220
Victor Hsiehf01f3232020-12-11 13:31:31 -0800221 let authenticator = FakeAuthenticator::always_succeed();
Chris Wailes68c39f82021-07-27 16:03:44 -0700222 Ok(FileConfig::RemoteVerifiedReadonly {
Victor Hsieh1bcf4112021-03-19 14:26:57 -0700223 reader: VerifiedFileReader::new(
Victor Hsiehf01f3232020-12-11 13:31:31 -0800224 &authenticator,
Victor Hsiehc3d45b12021-06-30 09:16:41 -0700225 RemoteFileReader::new(service.clone(), remote_id),
Victor Hsiehf01f3232020-12-11 13:31:31 -0800226 file_size,
227 signature,
Victor Hsiehc3d45b12021-06-30 09:16:41 -0700228 RemoteMerkleTreeReader::new(service.clone(), remote_id),
Victor Hsiehf01f3232020-12-11 13:31:31 -0800229 )?,
230 file_size,
Victor Hsieh1bcf4112021-03-19 14:26:57 -0700231 })
Victor Hsiehf01f3232020-12-11 13:31:31 -0800232}
233
Victor Hsieh2445e332021-06-04 16:44:53 -0700234fn new_config_remote_unverified_file(
235 service: file::VirtFdService,
236 remote_id: i32,
237 file_size: u64,
238) -> Result<FileConfig> {
Victor Hsiehc3d45b12021-06-30 09:16:41 -0700239 let reader = RemoteFileReader::new(service, remote_id);
Chris Wailes68c39f82021-07-27 16:03:44 -0700240 Ok(FileConfig::RemoteUnverifiedReadonly { reader, file_size })
Victor Hsiehf01f3232020-12-11 13:31:31 -0800241}
242
Victor Hsieh09e26262021-03-03 16:00:55 -0800243fn new_config_local_ro_file(
Victor Hsieh6cf75b52021-04-01 12:45:49 -0700244 protected_file: &Path,
245 merkle_tree_dump: &Path,
246 signature: &Path,
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800247) -> Result<FileConfig> {
248 let file = File::open(&protected_file)?;
249 let file_size = file.metadata()?.len();
Victor Hsieh09e26262021-03-03 16:00:55 -0800250 let file_reader = LocalFileReader::new(file)?;
251 let merkle_tree_reader = LocalFileReader::new(File::open(merkle_tree_dump)?)?;
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800252 let authenticator = FakeAuthenticator::always_succeed();
253 let mut sig = Vec::new();
254 let _ = File::open(signature)?.read_to_end(&mut sig)?;
Victor Hsieh1bcf4112021-03-19 14:26:57 -0700255 let reader =
Victor Hsieh09e26262021-03-03 16:00:55 -0800256 VerifiedFileReader::new(&authenticator, file_reader, file_size, sig, merkle_tree_reader)?;
Chris Wailes68c39f82021-07-27 16:03:44 -0700257 Ok(FileConfig::LocalVerifiedReadonly { reader, file_size })
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800258}
259
Victor Hsieh6cf75b52021-04-01 12:45:49 -0700260fn new_config_local_ro_file_unverified(file_path: &Path) -> Result<FileConfig> {
Victor Hsieh1bcf4112021-03-19 14:26:57 -0700261 let reader = LocalFileReader::new(File::open(file_path)?)?;
262 let file_size = reader.len();
Chris Wailes68c39f82021-07-27 16:03:44 -0700263 Ok(FileConfig::LocalUnverifiedReadonly { reader, file_size })
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800264}
265
Victor Hsieh2445e332021-06-04 16:44:53 -0700266fn new_config_remote_new_verified_file(
267 service: file::VirtFdService,
268 remote_id: i32,
269) -> Result<FileConfig> {
Victor Hsiehc3d45b12021-06-30 09:16:41 -0700270 let remote_file = RemoteFileEditor::new(service, remote_id);
Chris Wailes68c39f82021-07-27 16:03:44 -0700271 Ok(FileConfig::RemoteVerifiedNew { editor: VerifiedFileEditor::new(remote_file) })
Victor Hsieh6a47e7f2021-03-03 15:53:49 -0800272}
273
Victor Hsiehf01f3232020-12-11 13:31:31 -0800274fn prepare_file_pool(args: &Args) -> Result<BTreeMap<Inode, FileConfig>> {
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800275 let mut file_pool = BTreeMap::new();
276
Victor Hsieh9ab13e52021-06-29 09:23:29 -0700277 if args.has_remote_files() {
278 let service = file::get_binder_service(args.cid)?;
Victor Hsieh2445e332021-06-04 16:44:53 -0700279
Victor Hsieh9ab13e52021-06-29 09:23:29 -0700280 for config in &args.remote_ro_file {
281 file_pool.insert(
282 config.ino,
283 new_config_remote_verified_file(
284 service.clone(),
285 config.remote_id,
Victor Hsieh50d75ac2021-09-03 14:46:55 -0700286 service.getFileSize(config.remote_id)?.try_into()?,
Victor Hsieh9ab13e52021-06-29 09:23:29 -0700287 )?,
288 );
289 }
Victor Hsiehf01f3232020-12-11 13:31:31 -0800290
Victor Hsieh9ab13e52021-06-29 09:23:29 -0700291 for config in &args.remote_ro_file_unverified {
292 file_pool.insert(
293 config.ino,
294 new_config_remote_unverified_file(
295 service.clone(),
296 config.remote_id,
Victor Hsieh50d75ac2021-09-03 14:46:55 -0700297 service.getFileSize(config.remote_id)?.try_into()?,
Victor Hsieh9ab13e52021-06-29 09:23:29 -0700298 )?,
299 );
300 }
Victor Hsiehf01f3232020-12-11 13:31:31 -0800301
Victor Hsieh9ab13e52021-06-29 09:23:29 -0700302 for config in &args.remote_new_rw_file {
303 file_pool.insert(
304 config.ino,
305 new_config_remote_new_verified_file(service.clone(), config.remote_id)?,
306 );
307 }
Victor Hsieh6a47e7f2021-03-03 15:53:49 -0800308 }
309
Victor Hsieh09e26262021-03-03 16:00:55 -0800310 for config in &args.local_ro_file {
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800311 file_pool.insert(
312 config.ino,
Victor Hsieh09e26262021-03-03 16:00:55 -0800313 new_config_local_ro_file(
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800314 &config.file_path,
315 &config.merkle_tree_dump_path,
316 &config.signature_path,
317 )?,
318 );
319 }
320
Victor Hsieh09e26262021-03-03 16:00:55 -0800321 for config in &args.local_ro_file_unverified {
322 file_pool.insert(config.ino, new_config_local_ro_file_unverified(&config.file_path)?);
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800323 }
324
325 Ok(file_pool)
326}
327
328fn main() -> Result<()> {
Victor Hsiehf01f3232020-12-11 13:31:31 -0800329 let args = Args::from_args();
Victor Hsieh9d0ab622021-04-26 17:07:02 -0700330
331 let log_level = if args.debug { log::Level::Debug } else { log::Level::Info };
332 android_logger::init_once(
333 android_logger::Config::default().with_tag("authfs").with_min_level(log_level),
334 );
335
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800336 let file_pool = prepare_file_pool(&args)?;
Victor Hsieh4cc3b792021-08-04 12:00:04 -0700337 fusefs::loop_forever(file_pool, &args.mount_point, &args.extra_options)?;
Victor Hsiehf01f3232020-12-11 13:31:31 -0800338 bail!("Unexpected exit after the handler loop")
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800339}