blob: b30195a13c9b28682ef24a040f4cd0edfa959b73 [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 Hsieh09e26262021-03-03 16:00:55 -080056 /// A read-only remote file with integrity check. Can be multiple.
Victor Hsiehf01f3232020-12-11 13:31:31 -080057 ///
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 Hsieh09e26262021-03-03 16:00:55 -080061 #[structopt(long, parse(try_from_str = parse_remote_ro_file_option))]
62 remote_ro_file: Vec<OptionRemoteRoFile>,
Victor Hsiehf01f3232020-12-11 13:31:31 -080063
Victor Hsieh09e26262021-03-03 16:00:55 -080064 /// A read-only remote file without integrity check. Can be multiple.
Victor Hsiehf01f3232020-12-11 13:31:31 -080065 ///
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 Hsieh09e26262021-03-03 16:00:55 -080068 #[structopt(long, parse(try_from_str = parse_remote_ro_file_unverified_option))]
69 remote_ro_file_unverified: Vec<OptionRemoteRoFileUnverified>,
Victor Hsiehf01f3232020-12-11 13:31:31 -080070
Victor Hsieh6a47e7f2021-03-03 15:53:49 -080071 /// A new read-writable remote file with integrity check. Can be multiple.
72 ///
73 /// For example, `--remote-new-verified-file 12:34` tells the filesystem to associate entry 12
74 /// with a remote file 34.
75 #[structopt(long, parse(try_from_str = parse_remote_new_rw_file_option))]
76 remote_new_rw_file: Vec<OptionRemoteRwFile>,
77
Victor Hsieh09e26262021-03-03 16:00:55 -080078 /// Debug only. A read-only local file with integrity check. Can be multiple.
79 #[structopt(long, parse(try_from_str = parse_local_file_ro_option))]
80 local_ro_file: Vec<OptionLocalFileRo>,
Victor Hsieh88ac6ca2020-11-13 15:20:24 -080081
Victor Hsieh09e26262021-03-03 16:00:55 -080082 /// Debug only. A read-only local file without integrity check. Can be multiple.
83 #[structopt(long, parse(try_from_str = parse_local_ro_file_unverified_ro_option))]
84 local_ro_file_unverified: Vec<OptionLocalRoFileUnverified>,
Victor Hsieh9d0ab622021-04-26 17:07:02 -070085
86 /// Enable debugging features.
87 #[structopt(long)]
88 debug: bool,
Victor Hsieh88ac6ca2020-11-13 15:20:24 -080089}
90
Victor Hsieh09e26262021-03-03 16:00:55 -080091struct OptionRemoteRoFile {
Victor Hsiehf01f3232020-12-11 13:31:31 -080092 ino: Inode,
93
94 /// ID to refer to the remote file.
95 remote_id: i32,
96
97 /// Expected size of the remote file. Necessary for signature check and Merkle tree
98 /// verification.
99 file_size: u64,
100
101 /// Certificate to verify the authenticity of the file's fs-verity signature.
102 /// TODO(170494765): Implement PKCS#7 signature verification.
103 _certificate_path: PathBuf,
104}
105
Victor Hsieh09e26262021-03-03 16:00:55 -0800106struct OptionRemoteRoFileUnverified {
Victor Hsiehf01f3232020-12-11 13:31:31 -0800107 ino: Inode,
108
109 /// ID to refer to the remote file.
110 remote_id: i32,
111
112 /// Expected size of the remote file.
113 file_size: u64,
114}
115
Victor Hsieh6a47e7f2021-03-03 15:53:49 -0800116struct OptionRemoteRwFile {
117 ino: Inode,
118
119 /// ID to refer to the remote file.
120 remote_id: i32,
121}
122
Victor Hsieh09e26262021-03-03 16:00:55 -0800123struct OptionLocalFileRo {
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800124 ino: Inode,
Victor Hsiehf01f3232020-12-11 13:31:31 -0800125
126 /// Local path of the backing file.
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800127 file_path: PathBuf,
Victor Hsiehf01f3232020-12-11 13:31:31 -0800128
129 /// Local path of the backing file's fs-verity Merkle tree dump.
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800130 merkle_tree_dump_path: PathBuf,
Victor Hsiehf01f3232020-12-11 13:31:31 -0800131
132 /// Local path of fs-verity signature for the backing file.
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800133 signature_path: PathBuf,
Victor Hsiehf01f3232020-12-11 13:31:31 -0800134
135 /// Certificate to verify the authenticity of the file's fs-verity signature.
136 /// TODO(170494765): Implement PKCS#7 signature verification.
137 _certificate_path: PathBuf,
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800138}
139
Victor Hsieh09e26262021-03-03 16:00:55 -0800140struct OptionLocalRoFileUnverified {
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800141 ino: Inode,
Victor Hsiehf01f3232020-12-11 13:31:31 -0800142
143 /// Local path of the backing file.
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800144 file_path: PathBuf,
145}
146
Victor Hsieh09e26262021-03-03 16:00:55 -0800147fn parse_remote_ro_file_option(option: &str) -> Result<OptionRemoteRoFile> {
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800148 let strs: Vec<&str> = option.split(':').collect();
149 if strs.len() != 4 {
150 bail!("Invalid option: {}", option);
151 }
Victor Hsieh09e26262021-03-03 16:00:55 -0800152 Ok(OptionRemoteRoFile {
Victor Hsiehf01f3232020-12-11 13:31:31 -0800153 ino: strs[0].parse::<Inode>()?,
154 remote_id: strs[1].parse::<i32>()?,
155 file_size: strs[2].parse::<u64>()?,
156 _certificate_path: PathBuf::from(strs[3]),
157 })
158}
159
Victor Hsieh09e26262021-03-03 16:00:55 -0800160fn parse_remote_ro_file_unverified_option(option: &str) -> Result<OptionRemoteRoFileUnverified> {
Victor Hsiehf01f3232020-12-11 13:31:31 -0800161 let strs: Vec<&str> = option.split(':').collect();
162 if strs.len() != 3 {
163 bail!("Invalid option: {}", option);
164 }
Victor Hsieh09e26262021-03-03 16:00:55 -0800165 Ok(OptionRemoteRoFileUnverified {
Victor Hsiehf01f3232020-12-11 13:31:31 -0800166 ino: strs[0].parse::<Inode>()?,
167 remote_id: strs[1].parse::<i32>()?,
168 file_size: strs[2].parse::<u64>()?,
169 })
170}
171
Victor Hsieh6a47e7f2021-03-03 15:53:49 -0800172fn parse_remote_new_rw_file_option(option: &str) -> Result<OptionRemoteRwFile> {
173 let strs: Vec<&str> = option.split(':').collect();
174 if strs.len() != 2 {
175 bail!("Invalid option: {}", option);
176 }
177 Ok(OptionRemoteRwFile {
178 ino: strs[0].parse::<Inode>().unwrap(),
179 remote_id: strs[1].parse::<i32>().unwrap(),
180 })
181}
182
Victor Hsieh09e26262021-03-03 16:00:55 -0800183fn parse_local_file_ro_option(option: &str) -> Result<OptionLocalFileRo> {
Victor Hsiehf01f3232020-12-11 13:31:31 -0800184 let strs: Vec<&str> = option.split(':').collect();
185 if strs.len() != 5 {
186 bail!("Invalid option: {}", option);
187 }
Victor Hsieh09e26262021-03-03 16:00:55 -0800188 Ok(OptionLocalFileRo {
Victor Hsiehf01f3232020-12-11 13:31:31 -0800189 ino: strs[0].parse::<Inode>()?,
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800190 file_path: PathBuf::from(strs[1]),
191 merkle_tree_dump_path: PathBuf::from(strs[2]),
192 signature_path: PathBuf::from(strs[3]),
Victor Hsiehf01f3232020-12-11 13:31:31 -0800193 _certificate_path: PathBuf::from(strs[4]),
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800194 })
195}
196
Victor Hsieh09e26262021-03-03 16:00:55 -0800197fn parse_local_ro_file_unverified_ro_option(option: &str) -> Result<OptionLocalRoFileUnverified> {
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800198 let strs: Vec<&str> = option.split(':').collect();
199 if strs.len() != 2 {
200 bail!("Invalid option: {}", option);
201 }
Victor Hsieh09e26262021-03-03 16:00:55 -0800202 Ok(OptionLocalRoFileUnverified {
Victor Hsiehf01f3232020-12-11 13:31:31 -0800203 ino: strs[0].parse::<Inode>()?,
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800204 file_path: PathBuf::from(strs[1]),
205 })
206}
207
Victor Hsiehf01f3232020-12-11 13:31:31 -0800208fn new_config_remote_verified_file(remote_id: i32, file_size: u64) -> Result<FileConfig> {
Victor Hsieh09e26262021-03-03 16:00:55 -0800209 let service = file::get_local_binder();
Andrew Walbrancc093862021-03-05 16:59:35 +0000210 let signature = service.readFsveritySignature(remote_id).context("Failed to read signature")?;
Victor Hsiehf01f3232020-12-11 13:31:31 -0800211
212 let service = Arc::new(Mutex::new(service));
213 let authenticator = FakeAuthenticator::always_succeed();
Victor Hsieh1bcf4112021-03-19 14:26:57 -0700214 Ok(FileConfig::RemoteVerifiedReadonlyFile {
215 reader: VerifiedFileReader::new(
Victor Hsiehf01f3232020-12-11 13:31:31 -0800216 &authenticator,
Victor Hsieh09e26262021-03-03 16:00:55 -0800217 RemoteFileReader::new(Arc::clone(&service), remote_id),
Victor Hsiehf01f3232020-12-11 13:31:31 -0800218 file_size,
219 signature,
Victor Hsieh09e26262021-03-03 16:00:55 -0800220 RemoteMerkleTreeReader::new(Arc::clone(&service), remote_id),
Victor Hsiehf01f3232020-12-11 13:31:31 -0800221 )?,
222 file_size,
Victor Hsieh1bcf4112021-03-19 14:26:57 -0700223 })
Victor Hsiehf01f3232020-12-11 13:31:31 -0800224}
225
226fn new_config_remote_unverified_file(remote_id: i32, file_size: u64) -> Result<FileConfig> {
Victor Hsieh1bcf4112021-03-19 14:26:57 -0700227 let reader = RemoteFileReader::new(Arc::new(Mutex::new(file::get_local_binder())), remote_id);
228 Ok(FileConfig::RemoteUnverifiedReadonlyFile { reader, file_size })
Victor Hsiehf01f3232020-12-11 13:31:31 -0800229}
230
Victor Hsieh09e26262021-03-03 16:00:55 -0800231fn new_config_local_ro_file(
Victor Hsieh6cf75b52021-04-01 12:45:49 -0700232 protected_file: &Path,
233 merkle_tree_dump: &Path,
234 signature: &Path,
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800235) -> Result<FileConfig> {
236 let file = File::open(&protected_file)?;
237 let file_size = file.metadata()?.len();
Victor Hsieh09e26262021-03-03 16:00:55 -0800238 let file_reader = LocalFileReader::new(file)?;
239 let merkle_tree_reader = LocalFileReader::new(File::open(merkle_tree_dump)?)?;
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800240 let authenticator = FakeAuthenticator::always_succeed();
241 let mut sig = Vec::new();
242 let _ = File::open(signature)?.read_to_end(&mut sig)?;
Victor Hsieh1bcf4112021-03-19 14:26:57 -0700243 let reader =
Victor Hsieh09e26262021-03-03 16:00:55 -0800244 VerifiedFileReader::new(&authenticator, file_reader, file_size, sig, merkle_tree_reader)?;
Victor Hsieh1bcf4112021-03-19 14:26:57 -0700245 Ok(FileConfig::LocalVerifiedReadonlyFile { reader, file_size })
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800246}
247
Victor Hsieh6cf75b52021-04-01 12:45:49 -0700248fn new_config_local_ro_file_unverified(file_path: &Path) -> Result<FileConfig> {
Victor Hsieh1bcf4112021-03-19 14:26:57 -0700249 let reader = LocalFileReader::new(File::open(file_path)?)?;
250 let file_size = reader.len();
251 Ok(FileConfig::LocalUnverifiedReadonlyFile { reader, file_size })
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800252}
253
Victor Hsieh6a47e7f2021-03-03 15:53:49 -0800254fn new_config_remote_new_verified_file(remote_id: i32) -> Result<FileConfig> {
255 let remote_file =
256 RemoteFileEditor::new(Arc::new(Mutex::new(file::get_local_binder())), remote_id);
Victor Hsieh1bcf4112021-03-19 14:26:57 -0700257 Ok(FileConfig::RemoteVerifiedNewFile { editor: VerifiedFileEditor::new(remote_file) })
Victor Hsieh6a47e7f2021-03-03 15:53:49 -0800258}
259
Victor Hsiehf01f3232020-12-11 13:31:31 -0800260fn prepare_file_pool(args: &Args) -> Result<BTreeMap<Inode, FileConfig>> {
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800261 let mut file_pool = BTreeMap::new();
262
Victor Hsieh09e26262021-03-03 16:00:55 -0800263 for config in &args.remote_ro_file {
Victor Hsiehf01f3232020-12-11 13:31:31 -0800264 file_pool.insert(
265 config.ino,
266 new_config_remote_verified_file(config.remote_id, config.file_size)?,
267 );
268 }
269
Victor Hsieh09e26262021-03-03 16:00:55 -0800270 for config in &args.remote_ro_file_unverified {
Victor Hsiehf01f3232020-12-11 13:31:31 -0800271 file_pool.insert(
272 config.ino,
273 new_config_remote_unverified_file(config.remote_id, config.file_size)?,
274 );
275 }
276
Victor Hsieh6a47e7f2021-03-03 15:53:49 -0800277 for config in &args.remote_new_rw_file {
278 file_pool.insert(config.ino, new_config_remote_new_verified_file(config.remote_id)?);
279 }
280
Victor Hsieh09e26262021-03-03 16:00:55 -0800281 for config in &args.local_ro_file {
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800282 file_pool.insert(
283 config.ino,
Victor Hsieh09e26262021-03-03 16:00:55 -0800284 new_config_local_ro_file(
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800285 &config.file_path,
286 &config.merkle_tree_dump_path,
287 &config.signature_path,
288 )?,
289 );
290 }
291
Victor Hsieh09e26262021-03-03 16:00:55 -0800292 for config in &args.local_ro_file_unverified {
293 file_pool.insert(config.ino, new_config_local_ro_file_unverified(&config.file_path)?);
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800294 }
295
296 Ok(file_pool)
297}
298
299fn main() -> Result<()> {
Victor Hsiehf01f3232020-12-11 13:31:31 -0800300 let args = Args::from_args();
Victor Hsieh9d0ab622021-04-26 17:07:02 -0700301
302 let log_level = if args.debug { log::Level::Debug } else { log::Level::Info };
303 android_logger::init_once(
304 android_logger::Config::default().with_tag("authfs").with_min_level(log_level),
305 );
306
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800307 let file_pool = prepare_file_pool(&args)?;
308 fusefs::loop_forever(file_pool, &args.mount_point)?;
Victor Hsiehf01f3232020-12-11 13:31:31 -0800309 bail!("Unexpected exit after the handler loop")
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800310}