blob: 0db73e9ba741e78dbb2d8cb52f0b3a2b49cb98a7 [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;
34use std::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 Hsieh88ac6ca2020-11-13 15:20:24 -080085}
86
Victor Hsieh09e26262021-03-03 16:00:55 -080087struct OptionRemoteRoFile {
Victor Hsiehf01f3232020-12-11 13:31:31 -080088 ino: Inode,
89
90 /// ID to refer to the remote file.
91 remote_id: i32,
92
93 /// Expected size of the remote file. Necessary for signature check and Merkle tree
94 /// verification.
95 file_size: u64,
96
97 /// Certificate to verify the authenticity of the file's fs-verity signature.
98 /// TODO(170494765): Implement PKCS#7 signature verification.
99 _certificate_path: PathBuf,
100}
101
Victor Hsieh09e26262021-03-03 16:00:55 -0800102struct OptionRemoteRoFileUnverified {
Victor Hsiehf01f3232020-12-11 13:31:31 -0800103 ino: Inode,
104
105 /// ID to refer to the remote file.
106 remote_id: i32,
107
108 /// Expected size of the remote file.
109 file_size: u64,
110}
111
Victor Hsieh6a47e7f2021-03-03 15:53:49 -0800112struct OptionRemoteRwFile {
113 ino: Inode,
114
115 /// ID to refer to the remote file.
116 remote_id: i32,
117}
118
Victor Hsieh09e26262021-03-03 16:00:55 -0800119struct OptionLocalFileRo {
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800120 ino: Inode,
Victor Hsiehf01f3232020-12-11 13:31:31 -0800121
122 /// Local path of the backing file.
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800123 file_path: PathBuf,
Victor Hsiehf01f3232020-12-11 13:31:31 -0800124
125 /// Local path of the backing file's fs-verity Merkle tree dump.
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800126 merkle_tree_dump_path: PathBuf,
Victor Hsiehf01f3232020-12-11 13:31:31 -0800127
128 /// Local path of fs-verity signature for the backing file.
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800129 signature_path: PathBuf,
Victor Hsiehf01f3232020-12-11 13:31:31 -0800130
131 /// Certificate to verify the authenticity of the file's fs-verity signature.
132 /// TODO(170494765): Implement PKCS#7 signature verification.
133 _certificate_path: PathBuf,
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800134}
135
Victor Hsieh09e26262021-03-03 16:00:55 -0800136struct OptionLocalRoFileUnverified {
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800137 ino: Inode,
Victor Hsiehf01f3232020-12-11 13:31:31 -0800138
139 /// Local path of the backing file.
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800140 file_path: PathBuf,
141}
142
Victor Hsieh09e26262021-03-03 16:00:55 -0800143fn parse_remote_ro_file_option(option: &str) -> Result<OptionRemoteRoFile> {
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800144 let strs: Vec<&str> = option.split(':').collect();
145 if strs.len() != 4 {
146 bail!("Invalid option: {}", option);
147 }
Victor Hsieh09e26262021-03-03 16:00:55 -0800148 Ok(OptionRemoteRoFile {
Victor Hsiehf01f3232020-12-11 13:31:31 -0800149 ino: strs[0].parse::<Inode>()?,
150 remote_id: strs[1].parse::<i32>()?,
151 file_size: strs[2].parse::<u64>()?,
152 _certificate_path: PathBuf::from(strs[3]),
153 })
154}
155
Victor Hsieh09e26262021-03-03 16:00:55 -0800156fn parse_remote_ro_file_unverified_option(option: &str) -> Result<OptionRemoteRoFileUnverified> {
Victor Hsiehf01f3232020-12-11 13:31:31 -0800157 let strs: Vec<&str> = option.split(':').collect();
158 if strs.len() != 3 {
159 bail!("Invalid option: {}", option);
160 }
Victor Hsieh09e26262021-03-03 16:00:55 -0800161 Ok(OptionRemoteRoFileUnverified {
Victor Hsiehf01f3232020-12-11 13:31:31 -0800162 ino: strs[0].parse::<Inode>()?,
163 remote_id: strs[1].parse::<i32>()?,
164 file_size: strs[2].parse::<u64>()?,
165 })
166}
167
Victor Hsieh6a47e7f2021-03-03 15:53:49 -0800168fn parse_remote_new_rw_file_option(option: &str) -> Result<OptionRemoteRwFile> {
169 let strs: Vec<&str> = option.split(':').collect();
170 if strs.len() != 2 {
171 bail!("Invalid option: {}", option);
172 }
173 Ok(OptionRemoteRwFile {
174 ino: strs[0].parse::<Inode>().unwrap(),
175 remote_id: strs[1].parse::<i32>().unwrap(),
176 })
177}
178
Victor Hsieh09e26262021-03-03 16:00:55 -0800179fn parse_local_file_ro_option(option: &str) -> Result<OptionLocalFileRo> {
Victor Hsiehf01f3232020-12-11 13:31:31 -0800180 let strs: Vec<&str> = option.split(':').collect();
181 if strs.len() != 5 {
182 bail!("Invalid option: {}", option);
183 }
Victor Hsieh09e26262021-03-03 16:00:55 -0800184 Ok(OptionLocalFileRo {
Victor Hsiehf01f3232020-12-11 13:31:31 -0800185 ino: strs[0].parse::<Inode>()?,
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800186 file_path: PathBuf::from(strs[1]),
187 merkle_tree_dump_path: PathBuf::from(strs[2]),
188 signature_path: PathBuf::from(strs[3]),
Victor Hsiehf01f3232020-12-11 13:31:31 -0800189 _certificate_path: PathBuf::from(strs[4]),
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800190 })
191}
192
Victor Hsieh09e26262021-03-03 16:00:55 -0800193fn parse_local_ro_file_unverified_ro_option(option: &str) -> Result<OptionLocalRoFileUnverified> {
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800194 let strs: Vec<&str> = option.split(':').collect();
195 if strs.len() != 2 {
196 bail!("Invalid option: {}", option);
197 }
Victor Hsieh09e26262021-03-03 16:00:55 -0800198 Ok(OptionLocalRoFileUnverified {
Victor Hsiehf01f3232020-12-11 13:31:31 -0800199 ino: strs[0].parse::<Inode>()?,
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800200 file_path: PathBuf::from(strs[1]),
201 })
202}
203
Victor Hsiehf01f3232020-12-11 13:31:31 -0800204fn new_config_remote_verified_file(remote_id: i32, file_size: u64) -> Result<FileConfig> {
Victor Hsieh09e26262021-03-03 16:00:55 -0800205 let service = file::get_local_binder();
Andrew Walbrancc093862021-03-05 16:59:35 +0000206 let signature = service.readFsveritySignature(remote_id).context("Failed to read signature")?;
Victor Hsiehf01f3232020-12-11 13:31:31 -0800207
208 let service = Arc::new(Mutex::new(service));
209 let authenticator = FakeAuthenticator::always_succeed();
Victor Hsieh1bcf4112021-03-19 14:26:57 -0700210 Ok(FileConfig::RemoteVerifiedReadonlyFile {
211 reader: VerifiedFileReader::new(
Victor Hsiehf01f3232020-12-11 13:31:31 -0800212 &authenticator,
Victor Hsieh09e26262021-03-03 16:00:55 -0800213 RemoteFileReader::new(Arc::clone(&service), remote_id),
Victor Hsiehf01f3232020-12-11 13:31:31 -0800214 file_size,
215 signature,
Victor Hsieh09e26262021-03-03 16:00:55 -0800216 RemoteMerkleTreeReader::new(Arc::clone(&service), remote_id),
Victor Hsiehf01f3232020-12-11 13:31:31 -0800217 )?,
218 file_size,
Victor Hsieh1bcf4112021-03-19 14:26:57 -0700219 })
Victor Hsiehf01f3232020-12-11 13:31:31 -0800220}
221
222fn new_config_remote_unverified_file(remote_id: i32, file_size: u64) -> Result<FileConfig> {
Victor Hsieh1bcf4112021-03-19 14:26:57 -0700223 let reader = RemoteFileReader::new(Arc::new(Mutex::new(file::get_local_binder())), remote_id);
224 Ok(FileConfig::RemoteUnverifiedReadonlyFile { reader, file_size })
Victor Hsiehf01f3232020-12-11 13:31:31 -0800225}
226
Victor Hsieh09e26262021-03-03 16:00:55 -0800227fn new_config_local_ro_file(
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800228 protected_file: &PathBuf,
229 merkle_tree_dump: &PathBuf,
230 signature: &PathBuf,
231) -> Result<FileConfig> {
232 let file = File::open(&protected_file)?;
233 let file_size = file.metadata()?.len();
Victor Hsieh09e26262021-03-03 16:00:55 -0800234 let file_reader = LocalFileReader::new(file)?;
235 let merkle_tree_reader = LocalFileReader::new(File::open(merkle_tree_dump)?)?;
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800236 let authenticator = FakeAuthenticator::always_succeed();
237 let mut sig = Vec::new();
238 let _ = File::open(signature)?.read_to_end(&mut sig)?;
Victor Hsieh1bcf4112021-03-19 14:26:57 -0700239 let reader =
Victor Hsieh09e26262021-03-03 16:00:55 -0800240 VerifiedFileReader::new(&authenticator, file_reader, file_size, sig, merkle_tree_reader)?;
Victor Hsieh1bcf4112021-03-19 14:26:57 -0700241 Ok(FileConfig::LocalVerifiedReadonlyFile { reader, file_size })
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800242}
243
Victor Hsieh09e26262021-03-03 16:00:55 -0800244fn new_config_local_ro_file_unverified(file_path: &PathBuf) -> Result<FileConfig> {
Victor Hsieh1bcf4112021-03-19 14:26:57 -0700245 let reader = LocalFileReader::new(File::open(file_path)?)?;
246 let file_size = reader.len();
247 Ok(FileConfig::LocalUnverifiedReadonlyFile { reader, file_size })
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800248}
249
Victor Hsieh6a47e7f2021-03-03 15:53:49 -0800250fn new_config_remote_new_verified_file(remote_id: i32) -> Result<FileConfig> {
251 let remote_file =
252 RemoteFileEditor::new(Arc::new(Mutex::new(file::get_local_binder())), remote_id);
Victor Hsieh1bcf4112021-03-19 14:26:57 -0700253 Ok(FileConfig::RemoteVerifiedNewFile { editor: VerifiedFileEditor::new(remote_file) })
Victor Hsieh6a47e7f2021-03-03 15:53:49 -0800254}
255
Victor Hsiehf01f3232020-12-11 13:31:31 -0800256fn prepare_file_pool(args: &Args) -> Result<BTreeMap<Inode, FileConfig>> {
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800257 let mut file_pool = BTreeMap::new();
258
Victor Hsieh09e26262021-03-03 16:00:55 -0800259 for config in &args.remote_ro_file {
Victor Hsiehf01f3232020-12-11 13:31:31 -0800260 file_pool.insert(
261 config.ino,
262 new_config_remote_verified_file(config.remote_id, config.file_size)?,
263 );
264 }
265
Victor Hsieh09e26262021-03-03 16:00:55 -0800266 for config in &args.remote_ro_file_unverified {
Victor Hsiehf01f3232020-12-11 13:31:31 -0800267 file_pool.insert(
268 config.ino,
269 new_config_remote_unverified_file(config.remote_id, config.file_size)?,
270 );
271 }
272
Victor Hsieh6a47e7f2021-03-03 15:53:49 -0800273 for config in &args.remote_new_rw_file {
274 file_pool.insert(config.ino, new_config_remote_new_verified_file(config.remote_id)?);
275 }
276
Victor Hsieh09e26262021-03-03 16:00:55 -0800277 for config in &args.local_ro_file {
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800278 file_pool.insert(
279 config.ino,
Victor Hsieh09e26262021-03-03 16:00:55 -0800280 new_config_local_ro_file(
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800281 &config.file_path,
282 &config.merkle_tree_dump_path,
283 &config.signature_path,
284 )?,
285 );
286 }
287
Victor Hsieh09e26262021-03-03 16:00:55 -0800288 for config in &args.local_ro_file_unverified {
289 file_pool.insert(config.ino, new_config_local_ro_file_unverified(&config.file_path)?);
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800290 }
291
292 Ok(file_pool)
293}
294
295fn main() -> Result<()> {
Victor Hsiehf01f3232020-12-11 13:31:31 -0800296 let args = Args::from_args();
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800297 let file_pool = prepare_file_pool(&args)?;
298 fusefs::loop_forever(file_pool, &args.mount_point)?;
Victor Hsiehf01f3232020-12-11 13:31:31 -0800299 bail!("Unexpected exit after the handler loop")
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800300}