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 |
| 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 Hsieh | 88e5017 | 2021-10-15 13:27:13 -0700 | [diff] [blame] | 25 | //! 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 Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 29 | |
Victor Hsieh | 9978257 | 2022-01-05 15:38:33 -0800 | [diff] [blame^] | 30 | use anyhow::{anyhow, bail, Result}; |
Alan Stokes | e1b6e1c | 2021-10-01 12:44:49 +0100 | [diff] [blame] | 31 | use log::error; |
Victor Hsieh | 9978257 | 2022-01-05 15:38:33 -0800 | [diff] [blame^] | 32 | use protobuf::Message; |
Victor Hsieh | 50d75ac | 2021-09-03 14:46:55 -0700 | [diff] [blame] | 33 | use std::convert::TryInto; |
Victor Hsieh | 9978257 | 2022-01-05 15:38:33 -0800 | [diff] [blame^] | 34 | use std::fs::File; |
Victor Hsieh | d18b975 | 2021-11-09 16:03:34 -0800 | [diff] [blame] | 35 | use std::path::{Path, PathBuf}; |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 36 | use structopt::StructOpt; |
| 37 | |
| 38 | mod auth; |
| 39 | mod common; |
| 40 | mod crypto; |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame] | 41 | mod file; |
Victor Hsieh | f7fc3d3 | 2021-11-22 10:20:33 -0800 | [diff] [blame] | 42 | mod fsstat; |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 43 | mod fsverity; |
| 44 | mod fusefs; |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 45 | |
| 46 | use auth::FakeAuthenticator; |
Victor Hsieh | d18b975 | 2021-11-09 16:03:34 -0800 | [diff] [blame] | 47 | use file::{ |
Victor Hsieh | f393a72 | 2021-12-08 13:04:27 -0800 | [diff] [blame] | 48 | Attr, InMemoryDir, RemoteDirEditor, RemoteFileEditor, RemoteFileReader, RemoteMerkleTreeReader, |
Victor Hsieh | d18b975 | 2021-11-09 16:03:34 -0800 | [diff] [blame] | 49 | }; |
Victor Hsieh | f7fc3d3 | 2021-11-22 10:20:33 -0800 | [diff] [blame] | 50 | use fsstat::RemoteFsStatsReader; |
Victor Hsieh | 6a47e7f | 2021-03-03 15:53:49 -0800 | [diff] [blame] | 51 | use fsverity::{VerifiedFileEditor, VerifiedFileReader}; |
Victor Hsieh | 9978257 | 2022-01-05 15:38:33 -0800 | [diff] [blame^] | 52 | use fsverity_digests_proto::fsverity_digests::FSVerityDigests; |
Victor Hsieh | 4d6b9d4 | 2021-11-08 15:53:49 -0800 | [diff] [blame] | 53 | use fusefs::{AuthFs, AuthFsEntry}; |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 54 | |
| 55 | #[derive(StructOpt)] |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 56 | struct Args { |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 57 | /// Mount point of AuthFS. |
| 58 | #[structopt(parse(from_os_str))] |
| 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. |
| 62 | #[structopt(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 |
| 66 | #[structopt(short = "o")] |
| 67 | extra_options: Option<String>, |
| 68 | |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame] | 69 | /// A read-only remote file with integrity check. Can be multiple. |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 70 | /// |
Victor Hsieh | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 71 | /// 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 Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame] | 73 | #[structopt(long, parse(try_from_str = parse_remote_ro_file_option))] |
| 74 | remote_ro_file: Vec<OptionRemoteRoFile>, |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 75 | |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame] | 76 | /// A read-only remote file without integrity check. Can be multiple. |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 77 | /// |
Victor Hsieh | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 78 | /// 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 Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 82 | |
Victor Hsieh | 6a47e7f | 2021-03-03 15:53:49 -0800 | [diff] [blame] | 83 | /// A new read-writable remote file with integrity check. Can be multiple. |
| 84 | /// |
Victor Hsieh | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 85 | /// 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 Hsieh | 6a47e7f | 2021-03-03 15:53:49 -0800 | [diff] [blame] | 89 | |
Victor Hsieh | d18b975 | 2021-11-09 16:03:34 -0800 | [diff] [blame] | 90 | /// 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 Hsieh | 9978257 | 2022-01-05 15:38:33 -0800 | [diff] [blame^] | 99 | /// 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] | 100 | /// 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^] | 101 | /// 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 Hsieh | d18b975 | 2021-11-09 16:03:34 -0800 | [diff] [blame] | 103 | /// 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 Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 107 | /// 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 Hsieh | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 111 | /// 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 Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 115 | |
Victor Hsieh | 9d0ab62 | 2021-04-26 17:07:02 -0700 | [diff] [blame] | 116 | /// Enable debugging features. |
| 117 | #[structopt(long)] |
| 118 | debug: bool, |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 119 | } |
| 120 | |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame] | 121 | struct OptionRemoteRoFile { |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 122 | /// ID to refer to the remote file. |
Victor Hsieh | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 123 | remote_fd: i32, |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 124 | |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 125 | /// 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 Hsieh | d18b975 | 2021-11-09 16:03:34 -0800 | [diff] [blame] | 130 | struct 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 Hsieh | d18b975 | 2021-11-09 16:03:34 -0800 | [diff] [blame] | 137 | mapping_file_path: PathBuf, |
| 138 | |
Victor Hsieh | 9978257 | 2022-01-05 15:38:33 -0800 | [diff] [blame^] | 139 | prefix: String, |
Victor Hsieh | d18b975 | 2021-11-09 16:03:34 -0800 | [diff] [blame] | 140 | } |
| 141 | |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame] | 142 | fn parse_remote_ro_file_option(option: &str) -> Result<OptionRemoteRoFile> { |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 143 | let strs: Vec<&str> = option.split(':').collect(); |
Victor Hsieh | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 144 | if strs.len() != 2 { |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 145 | bail!("Invalid option: {}", option); |
| 146 | } |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame] | 147 | Ok(OptionRemoteRoFile { |
Victor Hsieh | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 148 | remote_fd: strs[0].parse::<i32>()?, |
| 149 | _certificate_path: PathBuf::from(strs[1]), |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 150 | }) |
| 151 | } |
| 152 | |
Victor Hsieh | d18b975 | 2021-11-09 16:03:34 -0800 | [diff] [blame] | 153 | fn 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 Hsieh | 9978257 | 2022-01-05 15:38:33 -0800 | [diff] [blame^] | 161 | prefix: String::from(strs[2]), |
Victor Hsieh | d18b975 | 2021-11-09 16:03:34 -0800 | [diff] [blame] | 162 | }) |
| 163 | } |
| 164 | |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 165 | fn new_remote_verified_file_entry( |
Victor Hsieh | 2445e33 | 2021-06-04 16:44:53 -0700 | [diff] [blame] | 166 | service: file::VirtFdService, |
Victor Hsieh | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 167 | remote_fd: i32, |
Victor Hsieh | 2445e33 | 2021-06-04 16:44:53 -0700 | [diff] [blame] | 168 | file_size: u64, |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 169 | ) -> Result<AuthFsEntry> { |
Inseob Kim | c0886c2 | 2021-12-13 17:41:24 +0900 | [diff] [blame] | 170 | let signature = service.readFsveritySignature(remote_fd).ok(); |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 171 | |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 172 | let authenticator = FakeAuthenticator::always_succeed(); |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 173 | Ok(AuthFsEntry::VerifiedReadonly { |
Victor Hsieh | 1bcf411 | 2021-03-19 14:26:57 -0700 | [diff] [blame] | 174 | reader: VerifiedFileReader::new( |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 175 | &authenticator, |
Victor Hsieh | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 176 | RemoteFileReader::new(service.clone(), remote_fd), |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 177 | file_size, |
Inseob Kim | c0886c2 | 2021-12-13 17:41:24 +0900 | [diff] [blame] | 178 | signature.as_deref(), |
Victor Hsieh | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 179 | RemoteMerkleTreeReader::new(service.clone(), remote_fd), |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 180 | )?, |
| 181 | file_size, |
Victor Hsieh | 1bcf411 | 2021-03-19 14:26:57 -0700 | [diff] [blame] | 182 | }) |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 183 | } |
| 184 | |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 185 | fn new_remote_unverified_file_entry( |
Victor Hsieh | 2445e33 | 2021-06-04 16:44:53 -0700 | [diff] [blame] | 186 | service: file::VirtFdService, |
Victor Hsieh | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 187 | remote_fd: i32, |
Victor Hsieh | 2445e33 | 2021-06-04 16:44:53 -0700 | [diff] [blame] | 188 | file_size: u64, |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 189 | ) -> Result<AuthFsEntry> { |
Victor Hsieh | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 190 | let reader = RemoteFileReader::new(service, remote_fd); |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 191 | Ok(AuthFsEntry::UnverifiedReadonly { reader, file_size }) |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 192 | } |
| 193 | |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 194 | fn new_remote_new_verified_file_entry( |
Victor Hsieh | 2445e33 | 2021-06-04 16:44:53 -0700 | [diff] [blame] | 195 | service: file::VirtFdService, |
Victor Hsieh | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 196 | remote_fd: i32, |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 197 | ) -> Result<AuthFsEntry> { |
Victor Hsieh | f393a72 | 2021-12-08 13:04:27 -0800 | [diff] [blame] | 198 | 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 Hsieh | 6a47e7f | 2021-03-03 15:53:49 -0800 | [diff] [blame] | 203 | } |
| 204 | |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 205 | fn new_remote_new_verified_dir_entry( |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 206 | service: file::VirtFdService, |
Victor Hsieh | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 207 | remote_fd: i32, |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 208 | ) -> Result<AuthFsEntry> { |
Victor Hsieh | f393a72 | 2021-12-08 13:04:27 -0800 | [diff] [blame] | 209 | let dir = RemoteDirEditor::new(service.clone(), remote_fd); |
| 210 | let attr = Attr::new_dir(service, remote_fd); |
| 211 | Ok(AuthFsEntry::VerifiedNewDirectory { dir, attr }) |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 212 | } |
| 213 | |
Victor Hsieh | f7fc3d3 | 2021-11-22 10:20:33 -0800 | [diff] [blame] | 214 | fn prepare_root_dir_entries( |
| 215 | service: file::VirtFdService, |
| 216 | authfs: &mut AuthFs, |
| 217 | args: &Args, |
| 218 | ) -> Result<()> { |
Victor Hsieh | 88e5017 | 2021-10-15 13:27:13 -0700 | [diff] [blame] | 219 | for config in &args.remote_ro_file { |
Victor Hsieh | 4d6b9d4 | 2021-11-08 15:53:49 -0800 | [diff] [blame] | 220 | authfs.add_entry_at_root_dir( |
Victor Hsieh | 60c2f41 | 2021-11-03 13:02:19 -0700 | [diff] [blame] | 221 | remote_fd_to_path_buf(config.remote_fd), |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 222 | new_remote_verified_file_entry( |
Victor Hsieh | 88e5017 | 2021-10-15 13:27:13 -0700 | [diff] [blame] | 223 | service.clone(), |
Victor Hsieh | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 224 | config.remote_fd, |
| 225 | service.getFileSize(config.remote_fd)?.try_into()?, |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 226 | )?, |
Victor Hsieh | 4d6b9d4 | 2021-11-08 15:53:49 -0800 | [diff] [blame] | 227 | )?; |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 228 | } |
| 229 | |
Victor Hsieh | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 230 | for remote_fd in &args.remote_ro_file_unverified { |
| 231 | let remote_fd = *remote_fd; |
Victor Hsieh | 4d6b9d4 | 2021-11-08 15:53:49 -0800 | [diff] [blame] | 232 | authfs.add_entry_at_root_dir( |
Victor Hsieh | 60c2f41 | 2021-11-03 13:02:19 -0700 | [diff] [blame] | 233 | remote_fd_to_path_buf(remote_fd), |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 234 | new_remote_unverified_file_entry( |
Victor Hsieh | 88e5017 | 2021-10-15 13:27:13 -0700 | [diff] [blame] | 235 | service.clone(), |
Victor Hsieh | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 236 | remote_fd, |
| 237 | service.getFileSize(remote_fd)?.try_into()?, |
Victor Hsieh | 88e5017 | 2021-10-15 13:27:13 -0700 | [diff] [blame] | 238 | )?, |
Victor Hsieh | 4d6b9d4 | 2021-11-08 15:53:49 -0800 | [diff] [blame] | 239 | )?; |
Victor Hsieh | 88e5017 | 2021-10-15 13:27:13 -0700 | [diff] [blame] | 240 | } |
| 241 | |
Victor Hsieh | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 242 | for remote_fd in &args.remote_new_rw_file { |
| 243 | let remote_fd = *remote_fd; |
Victor Hsieh | 4d6b9d4 | 2021-11-08 15:53:49 -0800 | [diff] [blame] | 244 | authfs.add_entry_at_root_dir( |
Victor Hsieh | 60c2f41 | 2021-11-03 13:02:19 -0700 | [diff] [blame] | 245 | remote_fd_to_path_buf(remote_fd), |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 246 | new_remote_new_verified_file_entry(service.clone(), remote_fd)?, |
Victor Hsieh | 4d6b9d4 | 2021-11-08 15:53:49 -0800 | [diff] [blame] | 247 | )?; |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 248 | } |
| 249 | |
Victor Hsieh | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 250 | for remote_fd in &args.remote_new_rw_dir { |
| 251 | let remote_fd = *remote_fd; |
Victor Hsieh | 4d6b9d4 | 2021-11-08 15:53:49 -0800 | [diff] [blame] | 252 | authfs.add_entry_at_root_dir( |
Victor Hsieh | 60c2f41 | 2021-11-03 13:02:19 -0700 | [diff] [blame] | 253 | remote_fd_to_path_buf(remote_fd), |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 254 | new_remote_new_verified_dir_entry(service.clone(), remote_fd)?, |
Victor Hsieh | 4d6b9d4 | 2021-11-08 15:53:49 -0800 | [diff] [blame] | 255 | )?; |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 256 | } |
| 257 | |
Victor Hsieh | d18b975 | 2021-11-09 16:03:34 -0800 | [diff] [blame] | 258 | 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 Hsieh | 9978257 | 2022-01-05 15:38:33 -0800 | [diff] [blame^] | 264 | // 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 Hsieh | d18b975 | 2021-11-09 16:03:34 -0800 | [diff] [blame] | 268 | let file_entry = { |
Victor Hsieh | 9978257 | 2022-01-05 15:38:33 -0800 | [diff] [blame^] | 269 | 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 Hsieh | d18b975 | 2021-11-09 16:03:34 -0800 | [diff] [blame] | 272 | // TODO(205883847): Not all files will be used. Open the remote file lazily. |
Victor Hsieh | d18b975 | 2021-11-09 16:03:34 -0800 | [diff] [blame] | 273 | let remote_file = RemoteFileReader::new_by_path( |
| 274 | service.clone(), |
| 275 | config.remote_dir_fd, |
Victor Hsieh | 9978257 | 2022-01-05 15:38:33 -0800 | [diff] [blame^] | 276 | Path::new(remote_path_str), |
Victor Hsieh | d18b975 | 2021-11-09 16:03:34 -0800 | [diff] [blame] | 277 | )?; |
| 278 | let file_size = service.getFileSize(remote_file.get_remote_fd())?.try_into()?; |
Victor Hsieh | 015bcb5 | 2021-11-17 17:28:01 -0800 | [diff] [blame] | 279 | // TODO(206869687): Switch to VerifiedReadonly |
Victor Hsieh | d18b975 | 2021-11-09 16:03:34 -0800 | [diff] [blame] | 280 | AuthFsEntry::UnverifiedReadonly { reader: remote_file, file_size } |
| 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 | 2442abc | 2021-11-17 13:25:02 -0800 | [diff] [blame] | 294 | let args = Args::from_args_safe()?; |
Victor Hsieh | 9d0ab62 | 2021-04-26 17:07:02 -0700 | [diff] [blame] | 295 | |
| 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 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 | 79f296b | 2021-12-02 15:38:08 -0800 | [diff] [blame] | 305 | fusefs::mount_and_enter_message_loop(authfs, &args.mount_point, &args.extra_options)?; |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 306 | bail!("Unexpected exit after the handler loop") |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 307 | } |
Alan Stokes | e1b6e1c | 2021-10-01 12:44:49 +0100 | [diff] [blame] | 308 | |
| 309 | fn main() { |
| 310 | if let Err(e) = try_main() { |
| 311 | error!("failed with {:?}", e); |
| 312 | std::process::exit(1); |
| 313 | } |
| 314 | } |