Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 1 | /* |
| 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 |
Victor Hsieh | 5deba52 | 2022-01-10 17:18:40 -0800 | [diff] [blame] | 20 | //! known file hash from trusted party, this filesystem can still verify a (read-only) file even if |
| 21 | //! the host/VM as the blob provider is malicious. With the Merkle tree, each read of file block can |
| 22 | //! be verified individually only when needed. |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 23 | //! |
Victor Hsieh | 5deba52 | 2022-01-10 17:18:40 -0800 | [diff] [blame] | 24 | //! AuthFS only serve files that are specifically configured. Each remote file can be configured to |
| 25 | //! appear as a local file at the mount point. A file configuration may include its remote file |
| 26 | //! identifier and its verification method (e.g. by known digest). |
| 27 | //! |
| 28 | //! AuthFS also support remote directories. A remote directory may be defined by a manifest file, |
| 29 | //! which contains file paths and their corresponding digests. |
| 30 | //! |
| 31 | //! AuthFS can also be configured for write, in which case the remote file server is treated as a |
| 32 | //! (untrusted) storage. The file/directory integrity is maintained in memory in the VM. Currently, |
| 33 | //! the state is not persistent, thus only new file/directory are supported. |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 34 | |
Victor Hsieh | 9978257 | 2022-01-05 15:38:33 -0800 | [diff] [blame] | 35 | use anyhow::{anyhow, bail, Result}; |
Victor Hsieh | b5bcfab | 2022-09-12 13:06:26 -0700 | [diff] [blame] | 36 | use clap::Parser; |
Alan Stokes | e1b6e1c | 2021-10-01 12:44:49 +0100 | [diff] [blame] | 37 | use log::error; |
Victor Hsieh | 9978257 | 2022-01-05 15:38:33 -0800 | [diff] [blame] | 38 | use protobuf::Message; |
Victor Hsieh | 50d75ac | 2021-09-03 14:46:55 -0700 | [diff] [blame] | 39 | use std::convert::TryInto; |
Victor Hsieh | 9978257 | 2022-01-05 15:38:33 -0800 | [diff] [blame] | 40 | use std::fs::File; |
Victor Hsieh | 963d513 | 2022-03-09 21:58:17 +0000 | [diff] [blame] | 41 | use std::num::NonZeroU8; |
Victor Hsieh | d18b975 | 2021-11-09 16:03:34 -0800 | [diff] [blame] | 42 | use std::path::{Path, PathBuf}; |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 43 | |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 44 | mod common; |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame] | 45 | mod file; |
Victor Hsieh | f7fc3d3 | 2021-11-22 10:20:33 -0800 | [diff] [blame] | 46 | mod fsstat; |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 47 | mod fsverity; |
| 48 | mod fusefs; |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 49 | |
Victor Hsieh | e8137e3 | 2022-02-11 22:14:12 +0000 | [diff] [blame] | 50 | use file::{Attr, InMemoryDir, RemoteDirEditor, RemoteFileEditor, RemoteFileReader}; |
Victor Hsieh | f7fc3d3 | 2021-11-22 10:20:33 -0800 | [diff] [blame] | 51 | use fsstat::RemoteFsStatsReader; |
Victor Hsieh | e8137e3 | 2022-02-11 22:14:12 +0000 | [diff] [blame] | 52 | use fsverity::VerifiedFileEditor; |
Victor Hsieh | 9978257 | 2022-01-05 15:38:33 -0800 | [diff] [blame] | 53 | use fsverity_digests_proto::fsverity_digests::FSVerityDigests; |
Victor Hsieh | e8137e3 | 2022-02-11 22:14:12 +0000 | [diff] [blame] | 54 | use fusefs::{AuthFs, AuthFsEntry, LazyVerifiedReadonlyFile}; |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 55 | |
Victor Hsieh | b5bcfab | 2022-09-12 13:06:26 -0700 | [diff] [blame] | 56 | #[derive(Parser)] |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 57 | struct Args { |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 58 | /// Mount point of AuthFS. |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 59 | mount_point: PathBuf, |
| 60 | |
Victor Hsieh | 2445e33 | 2021-06-04 16:44:53 -0700 | [diff] [blame] | 61 | /// CID of the VM where the service runs. |
Victor Hsieh | b5bcfab | 2022-09-12 13:06:26 -0700 | [diff] [blame] | 62 | #[clap(long)] |
Victor Hsieh | 1a8cd04 | 2021-09-03 16:29:45 -0700 | [diff] [blame] | 63 | cid: u32, |
Victor Hsieh | 2445e33 | 2021-06-04 16:44:53 -0700 | [diff] [blame] | 64 | |
Victor Hsieh | 4cc3b79 | 2021-08-04 12:00:04 -0700 | [diff] [blame] | 65 | /// Extra options to FUSE |
Victor Hsieh | b5bcfab | 2022-09-12 13:06:26 -0700 | [diff] [blame] | 66 | #[clap(short = 'o')] |
Victor Hsieh | 4cc3b79 | 2021-08-04 12:00:04 -0700 | [diff] [blame] | 67 | extra_options: Option<String>, |
| 68 | |
Victor Hsieh | 963d513 | 2022-03-09 21:58:17 +0000 | [diff] [blame] | 69 | /// Number of threads to serve FUSE requests. |
Victor Hsieh | b5bcfab | 2022-09-12 13:06:26 -0700 | [diff] [blame] | 70 | #[clap(short = 'j')] |
Victor Hsieh | 963d513 | 2022-03-09 21:58:17 +0000 | [diff] [blame] | 71 | thread_number: Option<NonZeroU8>, |
| 72 | |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame] | 73 | /// A read-only remote file with integrity check. Can be multiple. |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 74 | /// |
Victor Hsieh | 5deba52 | 2022-01-10 17:18:40 -0800 | [diff] [blame] | 75 | /// For example, `--remote-ro-file 5:sha256-1234abcd` tells the filesystem to associate the |
| 76 | /// file $MOUNTPOINT/5 with a remote FD 5, and has a fs-verity digest with sha256 of the hex |
| 77 | /// value 1234abcd. |
Victor Hsieh | b5bcfab | 2022-09-12 13:06:26 -0700 | [diff] [blame] | 78 | #[clap(long, value_parser = parse_remote_ro_file_option)] |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame] | 79 | remote_ro_file: Vec<OptionRemoteRoFile>, |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 80 | |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame] | 81 | /// A read-only remote file without integrity check. Can be multiple. |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 82 | /// |
Victor Hsieh | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 83 | /// For example, `--remote-ro-file-unverified 5` tells the filesystem to associate the file |
| 84 | /// $MOUNTPOINT/5 with a remote FD 5. |
Victor Hsieh | b5bcfab | 2022-09-12 13:06:26 -0700 | [diff] [blame] | 85 | #[clap(long)] |
Victor Hsieh | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 86 | remote_ro_file_unverified: Vec<i32>, |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 87 | |
Victor Hsieh | 6a47e7f | 2021-03-03 15:53:49 -0800 | [diff] [blame] | 88 | /// A new read-writable remote file with integrity check. Can be multiple. |
| 89 | /// |
Victor Hsieh | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 90 | /// For example, `--remote-new-rw-file 5` tells the filesystem to associate the file |
| 91 | /// $MOUNTPOINT/5 with a remote FD 5. |
Victor Hsieh | b5bcfab | 2022-09-12 13:06:26 -0700 | [diff] [blame] | 92 | #[clap(long)] |
Victor Hsieh | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 93 | remote_new_rw_file: Vec<i32>, |
Victor Hsieh | 6a47e7f | 2021-03-03 15:53:49 -0800 | [diff] [blame] | 94 | |
Victor Hsieh | d18b975 | 2021-11-09 16:03:34 -0800 | [diff] [blame] | 95 | /// A read-only directory that represents a remote directory. The directory view is constructed |
| 96 | /// and finalized during the filesystem initialization based on the provided mapping file |
| 97 | /// (which is a serialized protobuf of android.security.fsverity.FSVerityDigests, which |
| 98 | /// essentially provides <file path, fs-verity digest> mappings of exported files). The mapping |
| 99 | /// file is supposed to come from a trusted location in order to provide a trusted view as well |
| 100 | /// as verified access of included files with their fs-verity digest. Not all files on the |
| 101 | /// remote host may be included in the mapping file, so the directory view may be partial. The |
| 102 | /// directory structure won't change throughout the filesystem lifetime. |
| 103 | /// |
Victor Hsieh | 9978257 | 2022-01-05 15:38:33 -0800 | [diff] [blame] | 104 | /// For example, `--remote-ro-dir 5:/path/to/mapping:prefix/` tells the filesystem to |
Victor Hsieh | d18b975 | 2021-11-09 16:03:34 -0800 | [diff] [blame] | 105 | /// construct a directory structure defined in the mapping file at $MOUNTPOINT/5, which may |
Victor Hsieh | 9978257 | 2022-01-05 15:38:33 -0800 | [diff] [blame] | 106 | /// include a file like /5/system/framework/framework.jar. "prefix/" tells the filesystem to |
| 107 | /// strip the path (e.g. "system/") from the mount point to match the expected location of the |
Victor Hsieh | d18b975 | 2021-11-09 16:03:34 -0800 | [diff] [blame] | 108 | /// remote FD (e.g. a directory FD of "/system" in the remote). |
Victor Hsieh | b5bcfab | 2022-09-12 13:06:26 -0700 | [diff] [blame] | 109 | #[clap(long, value_parser = parse_remote_new_ro_dir_option)] |
Victor Hsieh | d18b975 | 2021-11-09 16:03:34 -0800 | [diff] [blame] | 110 | remote_ro_dir: Vec<OptionRemoteRoDir>, |
| 111 | |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 112 | /// A new directory that is assumed empty in the backing filesystem. New files created in this |
| 113 | /// directory are integrity-protected in the same way as --remote-new-verified-file. Can be |
| 114 | /// multiple. |
| 115 | /// |
Victor Hsieh | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 116 | /// For example, `--remote-new-rw-dir 5` tells the filesystem to associate $MOUNTPOINT/5 |
| 117 | /// with a remote dir FD 5. |
Victor Hsieh | b5bcfab | 2022-09-12 13:06:26 -0700 | [diff] [blame] | 118 | #[clap(long)] |
Victor Hsieh | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 119 | remote_new_rw_dir: Vec<i32>, |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 120 | |
Victor Hsieh | 9d0ab62 | 2021-04-26 17:07:02 -0700 | [diff] [blame] | 121 | /// Enable debugging features. |
Victor Hsieh | b5bcfab | 2022-09-12 13:06:26 -0700 | [diff] [blame] | 122 | #[clap(long)] |
Victor Hsieh | 9d0ab62 | 2021-04-26 17:07:02 -0700 | [diff] [blame] | 123 | debug: bool, |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 124 | } |
| 125 | |
Victor Hsieh | b5bcfab | 2022-09-12 13:06:26 -0700 | [diff] [blame] | 126 | #[derive(Clone)] |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame] | 127 | struct OptionRemoteRoFile { |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 128 | /// ID to refer to the remote file. |
Victor Hsieh | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 129 | remote_fd: i32, |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 130 | |
Victor Hsieh | 5deba52 | 2022-01-10 17:18:40 -0800 | [diff] [blame] | 131 | /// Expected fs-verity digest (with sha256) for the remote file. |
| 132 | digest: String, |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 133 | } |
| 134 | |
Victor Hsieh | b5bcfab | 2022-09-12 13:06:26 -0700 | [diff] [blame] | 135 | #[derive(Clone)] |
Victor Hsieh | d18b975 | 2021-11-09 16:03:34 -0800 | [diff] [blame] | 136 | struct OptionRemoteRoDir { |
| 137 | /// ID to refer to the remote dir. |
| 138 | remote_dir_fd: i32, |
| 139 | |
| 140 | /// A mapping file that describes the expecting file/directory structure and integrity metadata |
| 141 | /// in the remote directory. The file contains serialized protobuf of |
| 142 | /// android.security.fsverity.FSVerityDigests. |
Victor Hsieh | d18b975 | 2021-11-09 16:03:34 -0800 | [diff] [blame] | 143 | mapping_file_path: PathBuf, |
| 144 | |
Victor Hsieh | 9978257 | 2022-01-05 15:38:33 -0800 | [diff] [blame] | 145 | prefix: String, |
Victor Hsieh | d18b975 | 2021-11-09 16:03:34 -0800 | [diff] [blame] | 146 | } |
| 147 | |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame] | 148 | fn parse_remote_ro_file_option(option: &str) -> Result<OptionRemoteRoFile> { |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 149 | let strs: Vec<&str> = option.split(':').collect(); |
Victor Hsieh | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 150 | if strs.len() != 2 { |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 151 | bail!("Invalid option: {}", option); |
| 152 | } |
Victor Hsieh | 5deba52 | 2022-01-10 17:18:40 -0800 | [diff] [blame] | 153 | if let Some(digest) = strs[1].strip_prefix("sha256-") { |
| 154 | Ok(OptionRemoteRoFile { remote_fd: strs[0].parse::<i32>()?, digest: String::from(digest) }) |
| 155 | } else { |
| 156 | bail!("Unsupported hash algorithm or invalid format: {}", strs[1]); |
| 157 | } |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 158 | } |
| 159 | |
Victor Hsieh | d18b975 | 2021-11-09 16:03:34 -0800 | [diff] [blame] | 160 | fn parse_remote_new_ro_dir_option(option: &str) -> Result<OptionRemoteRoDir> { |
| 161 | let strs: Vec<&str> = option.split(':').collect(); |
| 162 | if strs.len() != 3 { |
| 163 | bail!("Invalid option: {}", option); |
| 164 | } |
| 165 | Ok(OptionRemoteRoDir { |
| 166 | remote_dir_fd: strs[0].parse::<i32>().unwrap(), |
| 167 | mapping_file_path: PathBuf::from(strs[1]), |
Victor Hsieh | 9978257 | 2022-01-05 15:38:33 -0800 | [diff] [blame] | 168 | prefix: String::from(strs[2]), |
Victor Hsieh | d18b975 | 2021-11-09 16:03:34 -0800 | [diff] [blame] | 169 | }) |
| 170 | } |
| 171 | |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 172 | fn new_remote_verified_file_entry( |
Victor Hsieh | 2445e33 | 2021-06-04 16:44:53 -0700 | [diff] [blame] | 173 | service: file::VirtFdService, |
Victor Hsieh | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 174 | remote_fd: i32, |
Victor Hsieh | 5deba52 | 2022-01-10 17:18:40 -0800 | [diff] [blame] | 175 | expected_digest: &str, |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 176 | ) -> Result<AuthFsEntry> { |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 177 | Ok(AuthFsEntry::VerifiedReadonly { |
Victor Hsieh | e8137e3 | 2022-02-11 22:14:12 +0000 | [diff] [blame] | 178 | reader: LazyVerifiedReadonlyFile::prepare_by_fd( |
| 179 | service, |
| 180 | remote_fd, |
Alice Wang | 0331d94 | 2023-12-01 08:55:59 +0000 | [diff] [blame] | 181 | hex::decode(expected_digest)?, |
Victor Hsieh | e8137e3 | 2022-02-11 22:14:12 +0000 | [diff] [blame] | 182 | ), |
Victor Hsieh | 1bcf411 | 2021-03-19 14:26:57 -0700 | [diff] [blame] | 183 | }) |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 184 | } |
| 185 | |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 186 | fn new_remote_unverified_file_entry( |
Victor Hsieh | 2445e33 | 2021-06-04 16:44:53 -0700 | [diff] [blame] | 187 | service: file::VirtFdService, |
Victor Hsieh | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 188 | remote_fd: i32, |
Victor Hsieh | 2445e33 | 2021-06-04 16:44:53 -0700 | [diff] [blame] | 189 | file_size: u64, |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 190 | ) -> Result<AuthFsEntry> { |
Victor Hsieh | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 191 | let reader = RemoteFileReader::new(service, remote_fd); |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 192 | Ok(AuthFsEntry::UnverifiedReadonly { reader, file_size }) |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 193 | } |
| 194 | |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 195 | fn new_remote_new_verified_file_entry( |
Victor Hsieh | 2445e33 | 2021-06-04 16:44:53 -0700 | [diff] [blame] | 196 | service: file::VirtFdService, |
Victor Hsieh | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 197 | remote_fd: i32, |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 198 | ) -> Result<AuthFsEntry> { |
Victor Hsieh | f393a72 | 2021-12-08 13:04:27 -0800 | [diff] [blame] | 199 | let remote_file = RemoteFileEditor::new(service.clone(), remote_fd); |
| 200 | Ok(AuthFsEntry::VerifiedNew { |
| 201 | editor: VerifiedFileEditor::new(remote_file), |
| 202 | attr: Attr::new_file(service, remote_fd), |
| 203 | }) |
Victor Hsieh | 6a47e7f | 2021-03-03 15:53:49 -0800 | [diff] [blame] | 204 | } |
| 205 | |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 206 | fn new_remote_new_verified_dir_entry( |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 207 | service: file::VirtFdService, |
Victor Hsieh | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 208 | remote_fd: i32, |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 209 | ) -> Result<AuthFsEntry> { |
Victor Hsieh | f393a72 | 2021-12-08 13:04:27 -0800 | [diff] [blame] | 210 | let dir = RemoteDirEditor::new(service.clone(), remote_fd); |
| 211 | let attr = Attr::new_dir(service, remote_fd); |
| 212 | Ok(AuthFsEntry::VerifiedNewDirectory { dir, attr }) |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 213 | } |
| 214 | |
Victor Hsieh | f7fc3d3 | 2021-11-22 10:20:33 -0800 | [diff] [blame] | 215 | fn prepare_root_dir_entries( |
| 216 | service: file::VirtFdService, |
| 217 | authfs: &mut AuthFs, |
| 218 | args: &Args, |
| 219 | ) -> Result<()> { |
Victor Hsieh | 88e5017 | 2021-10-15 13:27:13 -0700 | [diff] [blame] | 220 | for config in &args.remote_ro_file { |
Victor Hsieh | 4d6b9d4 | 2021-11-08 15:53:49 -0800 | [diff] [blame] | 221 | authfs.add_entry_at_root_dir( |
Victor Hsieh | 60c2f41 | 2021-11-03 13:02:19 -0700 | [diff] [blame] | 222 | remote_fd_to_path_buf(config.remote_fd), |
Victor Hsieh | e8137e3 | 2022-02-11 22:14:12 +0000 | [diff] [blame] | 223 | new_remote_verified_file_entry(service.clone(), config.remote_fd, &config.digest)?, |
Victor Hsieh | 4d6b9d4 | 2021-11-08 15:53:49 -0800 | [diff] [blame] | 224 | )?; |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 225 | } |
| 226 | |
Victor Hsieh | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 227 | for remote_fd in &args.remote_ro_file_unverified { |
| 228 | let remote_fd = *remote_fd; |
Victor Hsieh | 4d6b9d4 | 2021-11-08 15:53:49 -0800 | [diff] [blame] | 229 | authfs.add_entry_at_root_dir( |
Victor Hsieh | 60c2f41 | 2021-11-03 13:02:19 -0700 | [diff] [blame] | 230 | remote_fd_to_path_buf(remote_fd), |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 231 | new_remote_unverified_file_entry( |
Victor Hsieh | 88e5017 | 2021-10-15 13:27:13 -0700 | [diff] [blame] | 232 | service.clone(), |
Victor Hsieh | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 233 | remote_fd, |
| 234 | service.getFileSize(remote_fd)?.try_into()?, |
Victor Hsieh | 88e5017 | 2021-10-15 13:27:13 -0700 | [diff] [blame] | 235 | )?, |
Victor Hsieh | 4d6b9d4 | 2021-11-08 15:53:49 -0800 | [diff] [blame] | 236 | )?; |
Victor Hsieh | 88e5017 | 2021-10-15 13:27:13 -0700 | [diff] [blame] | 237 | } |
| 238 | |
Victor Hsieh | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 239 | for remote_fd in &args.remote_new_rw_file { |
| 240 | let remote_fd = *remote_fd; |
Victor Hsieh | 4d6b9d4 | 2021-11-08 15:53:49 -0800 | [diff] [blame] | 241 | authfs.add_entry_at_root_dir( |
Victor Hsieh | 60c2f41 | 2021-11-03 13:02:19 -0700 | [diff] [blame] | 242 | remote_fd_to_path_buf(remote_fd), |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 243 | new_remote_new_verified_file_entry(service.clone(), remote_fd)?, |
Victor Hsieh | 4d6b9d4 | 2021-11-08 15:53:49 -0800 | [diff] [blame] | 244 | )?; |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 245 | } |
| 246 | |
Victor Hsieh | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 247 | for remote_fd in &args.remote_new_rw_dir { |
| 248 | let remote_fd = *remote_fd; |
Victor Hsieh | 4d6b9d4 | 2021-11-08 15:53:49 -0800 | [diff] [blame] | 249 | authfs.add_entry_at_root_dir( |
Victor Hsieh | 60c2f41 | 2021-11-03 13:02:19 -0700 | [diff] [blame] | 250 | remote_fd_to_path_buf(remote_fd), |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 251 | new_remote_new_verified_dir_entry(service.clone(), remote_fd)?, |
Victor Hsieh | 4d6b9d4 | 2021-11-08 15:53:49 -0800 | [diff] [blame] | 252 | )?; |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 253 | } |
| 254 | |
Victor Hsieh | d18b975 | 2021-11-09 16:03:34 -0800 | [diff] [blame] | 255 | for config in &args.remote_ro_dir { |
| 256 | let dir_root_inode = authfs.add_entry_at_root_dir( |
| 257 | remote_fd_to_path_buf(config.remote_dir_fd), |
| 258 | AuthFsEntry::ReadonlyDirectory { dir: InMemoryDir::new() }, |
| 259 | )?; |
| 260 | |
Victor Hsieh | 9978257 | 2022-01-05 15:38:33 -0800 | [diff] [blame] | 261 | // Build the directory tree based on the mapping file. |
| 262 | let mut reader = File::open(&config.mapping_file_path)?; |
| 263 | let proto = FSVerityDigests::parse_from_reader(&mut reader)?; |
Victor Hsieh | 5deba52 | 2022-01-10 17:18:40 -0800 | [diff] [blame] | 264 | for (path_str, digest) in &proto.digests { |
| 265 | if digest.hash_alg != "sha256" { |
| 266 | bail!("Unsupported hash algorithm: {}", digest.hash_alg); |
| 267 | } |
| 268 | |
Victor Hsieh | d18b975 | 2021-11-09 16:03:34 -0800 | [diff] [blame] | 269 | let file_entry = { |
Victor Hsieh | 9978257 | 2022-01-05 15:38:33 -0800 | [diff] [blame] | 270 | let remote_path_str = path_str.strip_prefix(&config.prefix).ok_or_else(|| { |
| 271 | anyhow!("Expect path {} to match prefix {}", path_str, config.prefix) |
| 272 | })?; |
Victor Hsieh | 5deba52 | 2022-01-10 17:18:40 -0800 | [diff] [blame] | 273 | AuthFsEntry::VerifiedReadonly { |
Victor Hsieh | e8137e3 | 2022-02-11 22:14:12 +0000 | [diff] [blame] | 274 | reader: LazyVerifiedReadonlyFile::prepare_by_path( |
| 275 | service.clone(), |
| 276 | config.remote_dir_fd, |
| 277 | PathBuf::from(remote_path_str), |
| 278 | digest.digest.clone(), |
| 279 | ), |
Victor Hsieh | 5deba52 | 2022-01-10 17:18:40 -0800 | [diff] [blame] | 280 | } |
Victor Hsieh | d18b975 | 2021-11-09 16:03:34 -0800 | [diff] [blame] | 281 | }; |
Victor Hsieh | 9978257 | 2022-01-05 15:38:33 -0800 | [diff] [blame] | 282 | authfs.add_entry_at_ro_dir_by_path(dir_root_inode, Path::new(path_str), file_entry)?; |
Victor Hsieh | d18b975 | 2021-11-09 16:03:34 -0800 | [diff] [blame] | 283 | } |
| 284 | } |
| 285 | |
Victor Hsieh | 4d6b9d4 | 2021-11-08 15:53:49 -0800 | [diff] [blame] | 286 | Ok(()) |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 287 | } |
| 288 | |
Victor Hsieh | 60c2f41 | 2021-11-03 13:02:19 -0700 | [diff] [blame] | 289 | fn remote_fd_to_path_buf(fd: i32) -> PathBuf { |
| 290 | PathBuf::from(fd.to_string()) |
| 291 | } |
| 292 | |
Alan Stokes | e1b6e1c | 2021-10-01 12:44:49 +0100 | [diff] [blame] | 293 | fn try_main() -> Result<()> { |
Victor Hsieh | b5bcfab | 2022-09-12 13:06:26 -0700 | [diff] [blame] | 294 | let args = Args::parse(); |
Victor Hsieh | 9d0ab62 | 2021-04-26 17:07:02 -0700 | [diff] [blame] | 295 | |
Jeff Vander Stoep | 57da157 | 2024-01-31 10:52:16 +0100 | [diff] [blame] | 296 | let log_level = if args.debug { log::LevelFilter::Debug } else { log::LevelFilter::Info }; |
Victor Hsieh | 9d0ab62 | 2021-04-26 17:07:02 -0700 | [diff] [blame] | 297 | android_logger::init_once( |
Jeff Vander Stoep | 57da157 | 2024-01-31 10:52:16 +0100 | [diff] [blame] | 298 | android_logger::Config::default().with_tag("authfs").with_max_level(log_level), |
Victor Hsieh | 9d0ab62 | 2021-04-26 17:07:02 -0700 | [diff] [blame] | 299 | ); |
| 300 | |
Victor Hsieh | f7fc3d3 | 2021-11-22 10:20:33 -0800 | [diff] [blame] | 301 | 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 Hsieh | 963d513 | 2022-03-09 21:58:17 +0000 | [diff] [blame] | 305 | fusefs::mount_and_enter_message_loop( |
| 306 | authfs, |
| 307 | &args.mount_point, |
| 308 | &args.extra_options, |
| 309 | args.thread_number, |
| 310 | )?; |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 311 | bail!("Unexpected exit after the handler loop") |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 312 | } |
Alan Stokes | e1b6e1c | 2021-10-01 12:44:49 +0100 | [diff] [blame] | 313 | |
| 314 | fn main() { |
| 315 | if let Err(e) = try_main() { |
| 316 | error!("failed with {:?}", e); |
| 317 | std::process::exit(1); |
| 318 | } |
| 319 | } |