blob: 0fa3db7dbd155240626c79afcd163dfceea6e2fe [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
Victor Hsieh99782572022-01-05 15:38:33 -080030use anyhow::{anyhow, bail, Result};
Alan Stokese1b6e1c2021-10-01 12:44:49 +010031use log::error;
Victor Hsieh99782572022-01-05 15:38:33 -080032use protobuf::Message;
Victor Hsieh50d75ac2021-09-03 14:46:55 -070033use std::convert::TryInto;
Victor Hsieh99782572022-01-05 15:38:33 -080034use std::fs::File;
Victor Hsiehd18b9752021-11-09 16:03:34 -080035use 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 Hsiehf7fc3d32021-11-22 10:20:33 -080042mod fsstat;
Victor Hsieh88ac6ca2020-11-13 15:20:24 -080043mod fsverity;
44mod fusefs;
Victor Hsieh88ac6ca2020-11-13 15:20:24 -080045
46use auth::FakeAuthenticator;
Victor Hsiehd18b9752021-11-09 16:03:34 -080047use file::{
Victor Hsiehf393a722021-12-08 13:04:27 -080048 Attr, InMemoryDir, RemoteDirEditor, RemoteFileEditor, RemoteFileReader, RemoteMerkleTreeReader,
Victor Hsiehd18b9752021-11-09 16:03:34 -080049};
Victor Hsiehf7fc3d32021-11-22 10:20:33 -080050use fsstat::RemoteFsStatsReader;
Victor Hsieh6a47e7f2021-03-03 15:53:49 -080051use fsverity::{VerifiedFileEditor, VerifiedFileReader};
Victor Hsieh99782572022-01-05 15:38:33 -080052use fsverity_digests_proto::fsverity_digests::FSVerityDigests;
Victor Hsieh4d6b9d42021-11-08 15:53:49 -080053use fusefs::{AuthFs, AuthFsEntry};
Victor Hsieh88ac6ca2020-11-13 15:20:24 -080054
55#[derive(StructOpt)]
Victor Hsiehf01f3232020-12-11 13:31:31 -080056struct Args {
Victor Hsieh88ac6ca2020-11-13 15:20:24 -080057 /// Mount point of AuthFS.
58 #[structopt(parse(from_os_str))]
59 mount_point: PathBuf,
60
Victor Hsieh2445e332021-06-04 16:44:53 -070061 /// CID of the VM where the service runs.
62 #[structopt(long)]
Victor Hsieh1a8cd042021-09-03 16:29:45 -070063 cid: u32,
Victor Hsieh2445e332021-06-04 16:44:53 -070064
Victor Hsieh4cc3b792021-08-04 12:00:04 -070065 /// Extra options to FUSE
66 #[structopt(short = "o")]
67 extra_options: Option<String>,
68
Victor Hsieh09e26262021-03-03 16:00:55 -080069 /// A read-only remote file with integrity check. Can be multiple.
Victor Hsiehf01f3232020-12-11 13:31:31 -080070 ///
Victor Hsiehb3588ce2021-11-02 15:02:32 -070071 /// For example, `--remote-ro-file 5:/path/to/cert` tells the filesystem to associate the
72 /// file $MOUNTPOINT/5 with a remote FD 5, and need to be verified against the /path/to/cert.
Victor Hsieh09e26262021-03-03 16:00:55 -080073 #[structopt(long, parse(try_from_str = parse_remote_ro_file_option))]
74 remote_ro_file: Vec<OptionRemoteRoFile>,
Victor Hsiehf01f3232020-12-11 13:31:31 -080075
Victor Hsieh09e26262021-03-03 16:00:55 -080076 /// A read-only remote file without integrity check. Can be multiple.
Victor Hsiehf01f3232020-12-11 13:31:31 -080077 ///
Victor Hsiehb3588ce2021-11-02 15:02:32 -070078 /// For example, `--remote-ro-file-unverified 5` tells the filesystem to associate the file
79 /// $MOUNTPOINT/5 with a remote FD 5.
80 #[structopt(long)]
81 remote_ro_file_unverified: Vec<i32>,
Victor Hsiehf01f3232020-12-11 13:31:31 -080082
Victor Hsieh6a47e7f2021-03-03 15:53:49 -080083 /// A new read-writable remote file with integrity check. Can be multiple.
84 ///
Victor Hsiehb3588ce2021-11-02 15:02:32 -070085 /// For example, `--remote-new-rw-file 5` tells the filesystem to associate the file
86 /// $MOUNTPOINT/5 with a remote FD 5.
87 #[structopt(long)]
88 remote_new_rw_file: Vec<i32>,
Victor Hsieh6a47e7f2021-03-03 15:53:49 -080089
Victor Hsiehd18b9752021-11-09 16:03:34 -080090 /// A read-only directory that represents a remote directory. The directory view is constructed
91 /// and finalized during the filesystem initialization based on the provided mapping file
92 /// (which is a serialized protobuf of android.security.fsverity.FSVerityDigests, which
93 /// essentially provides <file path, fs-verity digest> mappings of exported files). The mapping
94 /// file is supposed to come from a trusted location in order to provide a trusted view as well
95 /// as verified access of included files with their fs-verity digest. Not all files on the
96 /// remote host may be included in the mapping file, so the directory view may be partial. The
97 /// directory structure won't change throughout the filesystem lifetime.
98 ///
Victor Hsieh99782572022-01-05 15:38:33 -080099 /// For example, `--remote-ro-dir 5:/path/to/mapping:prefix/` tells the filesystem to
Victor Hsiehd18b9752021-11-09 16:03:34 -0800100 /// construct a directory structure defined in the mapping file at $MOUNTPOINT/5, which may
Victor Hsieh99782572022-01-05 15:38:33 -0800101 /// include a file like /5/system/framework/framework.jar. "prefix/" tells the filesystem to
102 /// strip the path (e.g. "system/") from the mount point to match the expected location of the
Victor Hsiehd18b9752021-11-09 16:03:34 -0800103 /// remote FD (e.g. a directory FD of "/system" in the remote).
104 #[structopt(long, parse(try_from_str = parse_remote_new_ro_dir_option))]
105 remote_ro_dir: Vec<OptionRemoteRoDir>,
106
Victor Hsieh45636232021-10-15 17:52:51 -0700107 /// A new directory that is assumed empty in the backing filesystem. New files created in this
108 /// directory are integrity-protected in the same way as --remote-new-verified-file. Can be
109 /// multiple.
110 ///
Victor Hsiehb3588ce2021-11-02 15:02:32 -0700111 /// For example, `--remote-new-rw-dir 5` tells the filesystem to associate $MOUNTPOINT/5
112 /// with a remote dir FD 5.
113 #[structopt(long)]
114 remote_new_rw_dir: Vec<i32>,
Victor Hsieh45636232021-10-15 17:52:51 -0700115
Victor Hsieh9d0ab622021-04-26 17:07:02 -0700116 /// Enable debugging features.
117 #[structopt(long)]
118 debug: bool,
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800119}
120
Victor Hsieh09e26262021-03-03 16:00:55 -0800121struct OptionRemoteRoFile {
Victor Hsiehf01f3232020-12-11 13:31:31 -0800122 /// ID to refer to the remote file.
Victor Hsiehb3588ce2021-11-02 15:02:32 -0700123 remote_fd: i32,
Victor Hsiehf01f3232020-12-11 13:31:31 -0800124
Victor Hsiehf01f3232020-12-11 13:31:31 -0800125 /// Certificate to verify the authenticity of the file's fs-verity signature.
126 /// TODO(170494765): Implement PKCS#7 signature verification.
127 _certificate_path: PathBuf,
128}
129
Victor Hsiehd18b9752021-11-09 16:03:34 -0800130struct OptionRemoteRoDir {
131 /// ID to refer to the remote dir.
132 remote_dir_fd: i32,
133
134 /// A mapping file that describes the expecting file/directory structure and integrity metadata
135 /// in the remote directory. The file contains serialized protobuf of
136 /// android.security.fsverity.FSVerityDigests.
Victor Hsiehd18b9752021-11-09 16:03:34 -0800137 mapping_file_path: PathBuf,
138
Victor Hsieh99782572022-01-05 15:38:33 -0800139 prefix: String,
Victor Hsiehd18b9752021-11-09 16:03:34 -0800140}
141
Victor Hsieh09e26262021-03-03 16:00:55 -0800142fn parse_remote_ro_file_option(option: &str) -> Result<OptionRemoteRoFile> {
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800143 let strs: Vec<&str> = option.split(':').collect();
Victor Hsiehb3588ce2021-11-02 15:02:32 -0700144 if strs.len() != 2 {
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800145 bail!("Invalid option: {}", option);
146 }
Victor Hsieh09e26262021-03-03 16:00:55 -0800147 Ok(OptionRemoteRoFile {
Victor Hsiehb3588ce2021-11-02 15:02:32 -0700148 remote_fd: strs[0].parse::<i32>()?,
149 _certificate_path: PathBuf::from(strs[1]),
Victor Hsieh45636232021-10-15 17:52:51 -0700150 })
151}
152
Victor Hsiehd18b9752021-11-09 16:03:34 -0800153fn parse_remote_new_ro_dir_option(option: &str) -> Result<OptionRemoteRoDir> {
154 let strs: Vec<&str> = option.split(':').collect();
155 if strs.len() != 3 {
156 bail!("Invalid option: {}", option);
157 }
158 Ok(OptionRemoteRoDir {
159 remote_dir_fd: strs[0].parse::<i32>().unwrap(),
160 mapping_file_path: PathBuf::from(strs[1]),
Victor Hsieh99782572022-01-05 15:38:33 -0800161 prefix: String::from(strs[2]),
Victor Hsiehd18b9752021-11-09 16:03:34 -0800162 })
163}
164
Victor Hsieh26cea2f2021-11-03 10:28:33 -0700165fn new_remote_verified_file_entry(
Victor Hsieh2445e332021-06-04 16:44:53 -0700166 service: file::VirtFdService,
Victor Hsiehb3588ce2021-11-02 15:02:32 -0700167 remote_fd: i32,
Victor Hsieh2445e332021-06-04 16:44:53 -0700168 file_size: u64,
Victor Hsieh26cea2f2021-11-03 10:28:33 -0700169) -> Result<AuthFsEntry> {
Inseob Kimc0886c22021-12-13 17:41:24 +0900170 let signature = service.readFsveritySignature(remote_fd).ok();
Victor Hsiehf01f3232020-12-11 13:31:31 -0800171
Victor Hsiehf01f3232020-12-11 13:31:31 -0800172 let authenticator = FakeAuthenticator::always_succeed();
Victor Hsieh26cea2f2021-11-03 10:28:33 -0700173 Ok(AuthFsEntry::VerifiedReadonly {
Victor Hsieh1bcf4112021-03-19 14:26:57 -0700174 reader: VerifiedFileReader::new(
Victor Hsiehf01f3232020-12-11 13:31:31 -0800175 &authenticator,
Victor Hsiehb3588ce2021-11-02 15:02:32 -0700176 RemoteFileReader::new(service.clone(), remote_fd),
Victor Hsiehf01f3232020-12-11 13:31:31 -0800177 file_size,
Inseob Kimc0886c22021-12-13 17:41:24 +0900178 signature.as_deref(),
Victor Hsiehb3588ce2021-11-02 15:02:32 -0700179 RemoteMerkleTreeReader::new(service.clone(), remote_fd),
Victor Hsiehf01f3232020-12-11 13:31:31 -0800180 )?,
181 file_size,
Victor Hsieh1bcf4112021-03-19 14:26:57 -0700182 })
Victor Hsiehf01f3232020-12-11 13:31:31 -0800183}
184
Victor Hsieh26cea2f2021-11-03 10:28:33 -0700185fn new_remote_unverified_file_entry(
Victor Hsieh2445e332021-06-04 16:44:53 -0700186 service: file::VirtFdService,
Victor Hsiehb3588ce2021-11-02 15:02:32 -0700187 remote_fd: i32,
Victor Hsieh2445e332021-06-04 16:44:53 -0700188 file_size: u64,
Victor Hsieh26cea2f2021-11-03 10:28:33 -0700189) -> Result<AuthFsEntry> {
Victor Hsiehb3588ce2021-11-02 15:02:32 -0700190 let reader = RemoteFileReader::new(service, remote_fd);
Victor Hsieh26cea2f2021-11-03 10:28:33 -0700191 Ok(AuthFsEntry::UnverifiedReadonly { reader, file_size })
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800192}
193
Victor Hsieh26cea2f2021-11-03 10:28:33 -0700194fn new_remote_new_verified_file_entry(
Victor Hsieh2445e332021-06-04 16:44:53 -0700195 service: file::VirtFdService,
Victor Hsiehb3588ce2021-11-02 15:02:32 -0700196 remote_fd: i32,
Victor Hsieh26cea2f2021-11-03 10:28:33 -0700197) -> Result<AuthFsEntry> {
Victor Hsiehf393a722021-12-08 13:04:27 -0800198 let remote_file = RemoteFileEditor::new(service.clone(), remote_fd);
199 Ok(AuthFsEntry::VerifiedNew {
200 editor: VerifiedFileEditor::new(remote_file),
201 attr: Attr::new_file(service, remote_fd),
202 })
Victor Hsieh6a47e7f2021-03-03 15:53:49 -0800203}
204
Victor Hsieh26cea2f2021-11-03 10:28:33 -0700205fn new_remote_new_verified_dir_entry(
Victor Hsieh45636232021-10-15 17:52:51 -0700206 service: file::VirtFdService,
Victor Hsiehb3588ce2021-11-02 15:02:32 -0700207 remote_fd: i32,
Victor Hsieh26cea2f2021-11-03 10:28:33 -0700208) -> Result<AuthFsEntry> {
Victor Hsiehf393a722021-12-08 13:04:27 -0800209 let dir = RemoteDirEditor::new(service.clone(), remote_fd);
210 let attr = Attr::new_dir(service, remote_fd);
211 Ok(AuthFsEntry::VerifiedNewDirectory { dir, attr })
Victor Hsieh45636232021-10-15 17:52:51 -0700212}
213
Victor Hsiehf7fc3d32021-11-22 10:20:33 -0800214fn prepare_root_dir_entries(
215 service: file::VirtFdService,
216 authfs: &mut AuthFs,
217 args: &Args,
218) -> Result<()> {
Victor Hsieh88e50172021-10-15 13:27:13 -0700219 for config in &args.remote_ro_file {
Victor Hsieh4d6b9d42021-11-08 15:53:49 -0800220 authfs.add_entry_at_root_dir(
Victor Hsieh60c2f412021-11-03 13:02:19 -0700221 remote_fd_to_path_buf(config.remote_fd),
Victor Hsieh26cea2f2021-11-03 10:28:33 -0700222 new_remote_verified_file_entry(
Victor Hsieh88e50172021-10-15 13:27:13 -0700223 service.clone(),
Victor Hsiehb3588ce2021-11-02 15:02:32 -0700224 config.remote_fd,
225 service.getFileSize(config.remote_fd)?.try_into()?,
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800226 )?,
Victor Hsieh4d6b9d42021-11-08 15:53:49 -0800227 )?;
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800228 }
229
Victor Hsiehb3588ce2021-11-02 15:02:32 -0700230 for remote_fd in &args.remote_ro_file_unverified {
231 let remote_fd = *remote_fd;
Victor Hsieh4d6b9d42021-11-08 15:53:49 -0800232 authfs.add_entry_at_root_dir(
Victor Hsieh60c2f412021-11-03 13:02:19 -0700233 remote_fd_to_path_buf(remote_fd),
Victor Hsieh26cea2f2021-11-03 10:28:33 -0700234 new_remote_unverified_file_entry(
Victor Hsieh88e50172021-10-15 13:27:13 -0700235 service.clone(),
Victor Hsiehb3588ce2021-11-02 15:02:32 -0700236 remote_fd,
237 service.getFileSize(remote_fd)?.try_into()?,
Victor Hsieh88e50172021-10-15 13:27:13 -0700238 )?,
Victor Hsieh4d6b9d42021-11-08 15:53:49 -0800239 )?;
Victor Hsieh88e50172021-10-15 13:27:13 -0700240 }
241
Victor Hsiehb3588ce2021-11-02 15:02:32 -0700242 for remote_fd in &args.remote_new_rw_file {
243 let remote_fd = *remote_fd;
Victor Hsieh4d6b9d42021-11-08 15:53:49 -0800244 authfs.add_entry_at_root_dir(
Victor Hsieh60c2f412021-11-03 13:02:19 -0700245 remote_fd_to_path_buf(remote_fd),
Victor Hsieh26cea2f2021-11-03 10:28:33 -0700246 new_remote_new_verified_file_entry(service.clone(), remote_fd)?,
Victor Hsieh4d6b9d42021-11-08 15:53:49 -0800247 )?;
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800248 }
249
Victor Hsiehb3588ce2021-11-02 15:02:32 -0700250 for remote_fd in &args.remote_new_rw_dir {
251 let remote_fd = *remote_fd;
Victor Hsieh4d6b9d42021-11-08 15:53:49 -0800252 authfs.add_entry_at_root_dir(
Victor Hsieh60c2f412021-11-03 13:02:19 -0700253 remote_fd_to_path_buf(remote_fd),
Victor Hsieh26cea2f2021-11-03 10:28:33 -0700254 new_remote_new_verified_dir_entry(service.clone(), remote_fd)?,
Victor Hsieh4d6b9d42021-11-08 15:53:49 -0800255 )?;
Victor Hsieh45636232021-10-15 17:52:51 -0700256 }
257
Victor Hsiehd18b9752021-11-09 16:03:34 -0800258 for config in &args.remote_ro_dir {
259 let dir_root_inode = authfs.add_entry_at_root_dir(
260 remote_fd_to_path_buf(config.remote_dir_fd),
261 AuthFsEntry::ReadonlyDirectory { dir: InMemoryDir::new() },
262 )?;
263
Victor Hsieh99782572022-01-05 15:38:33 -0800264 // Build the directory tree based on the mapping file.
265 let mut reader = File::open(&config.mapping_file_path)?;
266 let proto = FSVerityDigests::parse_from_reader(&mut reader)?;
267 for path_str in proto.digests.keys() {
Victor Hsiehd18b9752021-11-09 16:03:34 -0800268 let file_entry = {
Victor Hsieh99782572022-01-05 15:38:33 -0800269 let remote_path_str = path_str.strip_prefix(&config.prefix).ok_or_else(|| {
270 anyhow!("Expect path {} to match prefix {}", path_str, config.prefix)
271 })?;
Victor Hsiehd18b9752021-11-09 16:03:34 -0800272 // TODO(205883847): Not all files will be used. Open the remote file lazily.
Victor Hsiehd18b9752021-11-09 16:03:34 -0800273 let remote_file = RemoteFileReader::new_by_path(
274 service.clone(),
275 config.remote_dir_fd,
Victor Hsieh99782572022-01-05 15:38:33 -0800276 Path::new(remote_path_str),
Victor Hsiehd18b9752021-11-09 16:03:34 -0800277 )?;
278 let file_size = service.getFileSize(remote_file.get_remote_fd())?.try_into()?;
Victor Hsieh015bcb52021-11-17 17:28:01 -0800279 // TODO(206869687): Switch to VerifiedReadonly
Victor Hsiehd18b9752021-11-09 16:03:34 -0800280 AuthFsEntry::UnverifiedReadonly { reader: remote_file, file_size }
281 };
Victor Hsieh99782572022-01-05 15:38:33 -0800282 authfs.add_entry_at_ro_dir_by_path(dir_root_inode, Path::new(path_str), file_entry)?;
Victor Hsiehd18b9752021-11-09 16:03:34 -0800283 }
284 }
285
Victor Hsieh4d6b9d42021-11-08 15:53:49 -0800286 Ok(())
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800287}
288
Victor Hsieh60c2f412021-11-03 13:02:19 -0700289fn remote_fd_to_path_buf(fd: i32) -> PathBuf {
290 PathBuf::from(fd.to_string())
291}
292
Alan Stokese1b6e1c2021-10-01 12:44:49 +0100293fn try_main() -> Result<()> {
Victor Hsieh2442abc2021-11-17 13:25:02 -0800294 let args = Args::from_args_safe()?;
Victor Hsieh9d0ab622021-04-26 17:07:02 -0700295
296 let log_level = if args.debug { log::Level::Debug } else { log::Level::Info };
297 android_logger::init_once(
298 android_logger::Config::default().with_tag("authfs").with_min_level(log_level),
299 );
300
Victor Hsiehf7fc3d32021-11-22 10:20:33 -0800301 let service = file::get_rpc_binder_service(args.cid)?;
302 let mut authfs = AuthFs::new(RemoteFsStatsReader::new(service.clone()));
303 prepare_root_dir_entries(service, &mut authfs, &args)?;
304
Victor Hsieh79f296b2021-12-02 15:38:08 -0800305 fusefs::mount_and_enter_message_loop(authfs, &args.mount_point, &args.extra_options)?;
Victor Hsiehf01f3232020-12-11 13:31:31 -0800306 bail!("Unexpected exit after the handler loop")
Victor Hsieh88ac6ca2020-11-13 15:20:24 -0800307}
Alan Stokese1b6e1c2021-10-01 12:44:49 +0100308
309fn main() {
310 if let Err(e) = try_main() {
311 error!("failed with {:?}", e);
312 std::process::exit(1);
313 }
314}