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 | |
Andrew Walbran | cc09386 | 2021-03-05 16:59:35 +0000 | [diff] [blame] | 30 | use anyhow::{bail, Context, Result}; |
Alan Stokes | e1b6e1c | 2021-10-01 12:44:49 +0100 | [diff] [blame] | 31 | use log::error; |
Victor Hsieh | 50d75ac | 2021-09-03 14:46:55 -0700 | [diff] [blame] | 32 | use std::convert::TryInto; |
Victor Hsieh | d18b975 | 2021-11-09 16:03:34 -0800 | [diff] [blame] | 33 | use std::path::{Path, PathBuf}; |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 34 | use structopt::StructOpt; |
| 35 | |
| 36 | mod auth; |
| 37 | mod common; |
| 38 | mod crypto; |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame] | 39 | mod file; |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 40 | mod fsverity; |
| 41 | mod fusefs; |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 42 | |
| 43 | use auth::FakeAuthenticator; |
Victor Hsieh | d18b975 | 2021-11-09 16:03:34 -0800 | [diff] [blame] | 44 | use file::{ |
| 45 | InMemoryDir, RemoteDirEditor, RemoteFileEditor, RemoteFileReader, RemoteMerkleTreeReader, |
| 46 | }; |
Victor Hsieh | 6a47e7f | 2021-03-03 15:53:49 -0800 | [diff] [blame] | 47 | use fsverity::{VerifiedFileEditor, VerifiedFileReader}; |
Victor Hsieh | 4d6b9d4 | 2021-11-08 15:53:49 -0800 | [diff] [blame] | 48 | use fusefs::{AuthFs, AuthFsEntry}; |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 49 | |
| 50 | #[derive(StructOpt)] |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 51 | struct Args { |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 52 | /// Mount point of AuthFS. |
| 53 | #[structopt(parse(from_os_str))] |
| 54 | mount_point: PathBuf, |
| 55 | |
Victor Hsieh | 2445e33 | 2021-06-04 16:44:53 -0700 | [diff] [blame] | 56 | /// CID of the VM where the service runs. |
| 57 | #[structopt(long)] |
Victor Hsieh | 1a8cd04 | 2021-09-03 16:29:45 -0700 | [diff] [blame] | 58 | cid: u32, |
Victor Hsieh | 2445e33 | 2021-06-04 16:44:53 -0700 | [diff] [blame] | 59 | |
Victor Hsieh | 4cc3b79 | 2021-08-04 12:00:04 -0700 | [diff] [blame] | 60 | /// Extra options to FUSE |
| 61 | #[structopt(short = "o")] |
| 62 | extra_options: Option<String>, |
| 63 | |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame] | 64 | /// A read-only remote file with integrity check. Can be multiple. |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 65 | /// |
Victor Hsieh | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 66 | /// For example, `--remote-ro-file 5:/path/to/cert` tells the filesystem to associate the |
| 67 | /// 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] | 68 | #[structopt(long, parse(try_from_str = parse_remote_ro_file_option))] |
| 69 | remote_ro_file: Vec<OptionRemoteRoFile>, |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 70 | |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame] | 71 | /// A read-only remote file without integrity check. Can be multiple. |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 72 | /// |
Victor Hsieh | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 73 | /// For example, `--remote-ro-file-unverified 5` tells the filesystem to associate the file |
| 74 | /// $MOUNTPOINT/5 with a remote FD 5. |
| 75 | #[structopt(long)] |
| 76 | remote_ro_file_unverified: Vec<i32>, |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 77 | |
Victor Hsieh | 6a47e7f | 2021-03-03 15:53:49 -0800 | [diff] [blame] | 78 | /// A new read-writable remote file with integrity check. Can be multiple. |
| 79 | /// |
Victor Hsieh | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 80 | /// For example, `--remote-new-rw-file 5` tells the filesystem to associate the file |
| 81 | /// $MOUNTPOINT/5 with a remote FD 5. |
| 82 | #[structopt(long)] |
| 83 | remote_new_rw_file: Vec<i32>, |
Victor Hsieh | 6a47e7f | 2021-03-03 15:53:49 -0800 | [diff] [blame] | 84 | |
Victor Hsieh | d18b975 | 2021-11-09 16:03:34 -0800 | [diff] [blame] | 85 | /// A read-only directory that represents a remote directory. The directory view is constructed |
| 86 | /// and finalized during the filesystem initialization based on the provided mapping file |
| 87 | /// (which is a serialized protobuf of android.security.fsverity.FSVerityDigests, which |
| 88 | /// essentially provides <file path, fs-verity digest> mappings of exported files). The mapping |
| 89 | /// file is supposed to come from a trusted location in order to provide a trusted view as well |
| 90 | /// as verified access of included files with their fs-verity digest. Not all files on the |
| 91 | /// remote host may be included in the mapping file, so the directory view may be partial. The |
| 92 | /// directory structure won't change throughout the filesystem lifetime. |
| 93 | /// |
| 94 | /// For example, `--remote-ro-dir 5:/path/to/mapping:/prefix/` tells the filesystem to |
| 95 | /// construct a directory structure defined in the mapping file at $MOUNTPOINT/5, which may |
| 96 | /// include a file like /5/system/framework/framework.jar. "/prefix/" tells the filesystem to |
| 97 | /// strip the path (e.g. "/system/") from the mount point to match the expected location of the |
| 98 | /// remote FD (e.g. a directory FD of "/system" in the remote). |
| 99 | #[structopt(long, parse(try_from_str = parse_remote_new_ro_dir_option))] |
| 100 | remote_ro_dir: Vec<OptionRemoteRoDir>, |
| 101 | |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 102 | /// A new directory that is assumed empty in the backing filesystem. New files created in this |
| 103 | /// directory are integrity-protected in the same way as --remote-new-verified-file. Can be |
| 104 | /// multiple. |
| 105 | /// |
Victor Hsieh | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 106 | /// For example, `--remote-new-rw-dir 5` tells the filesystem to associate $MOUNTPOINT/5 |
| 107 | /// with a remote dir FD 5. |
| 108 | #[structopt(long)] |
| 109 | remote_new_rw_dir: Vec<i32>, |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 110 | |
Victor Hsieh | 9d0ab62 | 2021-04-26 17:07:02 -0700 | [diff] [blame] | 111 | /// Enable debugging features. |
| 112 | #[structopt(long)] |
| 113 | debug: bool, |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 114 | } |
| 115 | |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame] | 116 | struct OptionRemoteRoFile { |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 117 | /// ID to refer to the remote file. |
Victor Hsieh | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 118 | remote_fd: i32, |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 119 | |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 120 | /// Certificate to verify the authenticity of the file's fs-verity signature. |
| 121 | /// TODO(170494765): Implement PKCS#7 signature verification. |
| 122 | _certificate_path: PathBuf, |
| 123 | } |
| 124 | |
Victor Hsieh | d18b975 | 2021-11-09 16:03:34 -0800 | [diff] [blame] | 125 | struct OptionRemoteRoDir { |
| 126 | /// ID to refer to the remote dir. |
| 127 | remote_dir_fd: i32, |
| 128 | |
| 129 | /// A mapping file that describes the expecting file/directory structure and integrity metadata |
| 130 | /// in the remote directory. The file contains serialized protobuf of |
| 131 | /// android.security.fsverity.FSVerityDigests. |
Victor Hsieh | 015bcb5 | 2021-11-17 17:28:01 -0800 | [diff] [blame] | 132 | /// TODO(206869687): Really use the file when it's generated. |
Victor Hsieh | d18b975 | 2021-11-09 16:03:34 -0800 | [diff] [blame] | 133 | #[allow(dead_code)] |
| 134 | mapping_file_path: PathBuf, |
| 135 | |
| 136 | prefix: PathBuf, |
| 137 | } |
| 138 | |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame] | 139 | fn parse_remote_ro_file_option(option: &str) -> Result<OptionRemoteRoFile> { |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 140 | let strs: Vec<&str> = option.split(':').collect(); |
Victor Hsieh | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 141 | if strs.len() != 2 { |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 142 | bail!("Invalid option: {}", option); |
| 143 | } |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame] | 144 | Ok(OptionRemoteRoFile { |
Victor Hsieh | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 145 | remote_fd: strs[0].parse::<i32>()?, |
| 146 | _certificate_path: PathBuf::from(strs[1]), |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 147 | }) |
| 148 | } |
| 149 | |
Victor Hsieh | d18b975 | 2021-11-09 16:03:34 -0800 | [diff] [blame] | 150 | fn parse_remote_new_ro_dir_option(option: &str) -> Result<OptionRemoteRoDir> { |
| 151 | let strs: Vec<&str> = option.split(':').collect(); |
| 152 | if strs.len() != 3 { |
| 153 | bail!("Invalid option: {}", option); |
| 154 | } |
| 155 | Ok(OptionRemoteRoDir { |
| 156 | remote_dir_fd: strs[0].parse::<i32>().unwrap(), |
| 157 | mapping_file_path: PathBuf::from(strs[1]), |
| 158 | prefix: PathBuf::from(strs[2]), |
| 159 | }) |
| 160 | } |
| 161 | |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 162 | fn new_remote_verified_file_entry( |
Victor Hsieh | 2445e33 | 2021-06-04 16:44:53 -0700 | [diff] [blame] | 163 | service: file::VirtFdService, |
Victor Hsieh | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 164 | remote_fd: i32, |
Victor Hsieh | 2445e33 | 2021-06-04 16:44:53 -0700 | [diff] [blame] | 165 | file_size: u64, |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 166 | ) -> Result<AuthFsEntry> { |
Victor Hsieh | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 167 | let signature = service.readFsveritySignature(remote_fd).context("Failed to read signature")?; |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 168 | |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 169 | let authenticator = FakeAuthenticator::always_succeed(); |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 170 | Ok(AuthFsEntry::VerifiedReadonly { |
Victor Hsieh | 1bcf411 | 2021-03-19 14:26:57 -0700 | [diff] [blame] | 171 | reader: VerifiedFileReader::new( |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 172 | &authenticator, |
Victor Hsieh | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 173 | RemoteFileReader::new(service.clone(), remote_fd), |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 174 | file_size, |
| 175 | signature, |
Victor Hsieh | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 176 | RemoteMerkleTreeReader::new(service.clone(), remote_fd), |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 177 | )?, |
| 178 | file_size, |
Victor Hsieh | 1bcf411 | 2021-03-19 14:26:57 -0700 | [diff] [blame] | 179 | }) |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 180 | } |
| 181 | |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 182 | fn new_remote_unverified_file_entry( |
Victor Hsieh | 2445e33 | 2021-06-04 16:44:53 -0700 | [diff] [blame] | 183 | service: file::VirtFdService, |
Victor Hsieh | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 184 | remote_fd: i32, |
Victor Hsieh | 2445e33 | 2021-06-04 16:44:53 -0700 | [diff] [blame] | 185 | file_size: u64, |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 186 | ) -> Result<AuthFsEntry> { |
Victor Hsieh | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 187 | let reader = RemoteFileReader::new(service, remote_fd); |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 188 | Ok(AuthFsEntry::UnverifiedReadonly { reader, file_size }) |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 189 | } |
| 190 | |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 191 | fn new_remote_new_verified_file_entry( |
Victor Hsieh | 2445e33 | 2021-06-04 16:44:53 -0700 | [diff] [blame] | 192 | service: file::VirtFdService, |
Victor Hsieh | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 193 | remote_fd: i32, |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 194 | ) -> Result<AuthFsEntry> { |
Victor Hsieh | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 195 | let remote_file = RemoteFileEditor::new(service, remote_fd); |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 196 | Ok(AuthFsEntry::VerifiedNew { editor: VerifiedFileEditor::new(remote_file) }) |
Victor Hsieh | 6a47e7f | 2021-03-03 15:53:49 -0800 | [diff] [blame] | 197 | } |
| 198 | |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 199 | fn new_remote_new_verified_dir_entry( |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 200 | service: file::VirtFdService, |
Victor Hsieh | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 201 | remote_fd: i32, |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 202 | ) -> Result<AuthFsEntry> { |
Victor Hsieh | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 203 | let dir = RemoteDirEditor::new(service, remote_fd); |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 204 | Ok(AuthFsEntry::VerifiedNewDirectory { dir }) |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 205 | } |
| 206 | |
Victor Hsieh | 4d6b9d4 | 2021-11-08 15:53:49 -0800 | [diff] [blame] | 207 | fn prepare_root_dir_entries(authfs: &mut AuthFs, args: &Args) -> Result<()> { |
Victor Hsieh | 88e5017 | 2021-10-15 13:27:13 -0700 | [diff] [blame] | 208 | let service = file::get_rpc_binder_service(args.cid)?; |
Victor Hsieh | 2445e33 | 2021-06-04 16:44:53 -0700 | [diff] [blame] | 209 | |
Victor Hsieh | 88e5017 | 2021-10-15 13:27:13 -0700 | [diff] [blame] | 210 | for config in &args.remote_ro_file { |
Victor Hsieh | 4d6b9d4 | 2021-11-08 15:53:49 -0800 | [diff] [blame] | 211 | authfs.add_entry_at_root_dir( |
Victor Hsieh | 60c2f41 | 2021-11-03 13:02:19 -0700 | [diff] [blame] | 212 | remote_fd_to_path_buf(config.remote_fd), |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 213 | new_remote_verified_file_entry( |
Victor Hsieh | 88e5017 | 2021-10-15 13:27:13 -0700 | [diff] [blame] | 214 | service.clone(), |
Victor Hsieh | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 215 | config.remote_fd, |
| 216 | service.getFileSize(config.remote_fd)?.try_into()?, |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 217 | )?, |
Victor Hsieh | 4d6b9d4 | 2021-11-08 15:53:49 -0800 | [diff] [blame] | 218 | )?; |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 219 | } |
| 220 | |
Victor Hsieh | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 221 | for remote_fd in &args.remote_ro_file_unverified { |
| 222 | let remote_fd = *remote_fd; |
Victor Hsieh | 4d6b9d4 | 2021-11-08 15:53:49 -0800 | [diff] [blame] | 223 | authfs.add_entry_at_root_dir( |
Victor Hsieh | 60c2f41 | 2021-11-03 13:02:19 -0700 | [diff] [blame] | 224 | remote_fd_to_path_buf(remote_fd), |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 225 | new_remote_unverified_file_entry( |
Victor Hsieh | 88e5017 | 2021-10-15 13:27:13 -0700 | [diff] [blame] | 226 | service.clone(), |
Victor Hsieh | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 227 | remote_fd, |
| 228 | service.getFileSize(remote_fd)?.try_into()?, |
Victor Hsieh | 88e5017 | 2021-10-15 13:27:13 -0700 | [diff] [blame] | 229 | )?, |
Victor Hsieh | 4d6b9d4 | 2021-11-08 15:53:49 -0800 | [diff] [blame] | 230 | )?; |
Victor Hsieh | 88e5017 | 2021-10-15 13:27:13 -0700 | [diff] [blame] | 231 | } |
| 232 | |
Victor Hsieh | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 233 | for remote_fd in &args.remote_new_rw_file { |
| 234 | let remote_fd = *remote_fd; |
Victor Hsieh | 4d6b9d4 | 2021-11-08 15:53:49 -0800 | [diff] [blame] | 235 | authfs.add_entry_at_root_dir( |
Victor Hsieh | 60c2f41 | 2021-11-03 13:02:19 -0700 | [diff] [blame] | 236 | remote_fd_to_path_buf(remote_fd), |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 237 | new_remote_new_verified_file_entry(service.clone(), remote_fd)?, |
Victor Hsieh | 4d6b9d4 | 2021-11-08 15:53:49 -0800 | [diff] [blame] | 238 | )?; |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 239 | } |
| 240 | |
Victor Hsieh | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 241 | for remote_fd in &args.remote_new_rw_dir { |
| 242 | let remote_fd = *remote_fd; |
Victor Hsieh | 4d6b9d4 | 2021-11-08 15:53:49 -0800 | [diff] [blame] | 243 | authfs.add_entry_at_root_dir( |
Victor Hsieh | 60c2f41 | 2021-11-03 13:02:19 -0700 | [diff] [blame] | 244 | remote_fd_to_path_buf(remote_fd), |
Victor Hsieh | 26cea2f | 2021-11-03 10:28:33 -0700 | [diff] [blame] | 245 | new_remote_new_verified_dir_entry(service.clone(), remote_fd)?, |
Victor Hsieh | 4d6b9d4 | 2021-11-08 15:53:49 -0800 | [diff] [blame] | 246 | )?; |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 247 | } |
| 248 | |
Victor Hsieh | d18b975 | 2021-11-09 16:03:34 -0800 | [diff] [blame] | 249 | for config in &args.remote_ro_dir { |
| 250 | let dir_root_inode = authfs.add_entry_at_root_dir( |
| 251 | remote_fd_to_path_buf(config.remote_dir_fd), |
| 252 | AuthFsEntry::ReadonlyDirectory { dir: InMemoryDir::new() }, |
| 253 | )?; |
| 254 | |
Victor Hsieh | 015bcb5 | 2021-11-17 17:28:01 -0800 | [diff] [blame] | 255 | // TODO(206869687): Read actual path from config.mapping_file_path when it's generated. |
Victor Hsieh | d18b975 | 2021-11-09 16:03:34 -0800 | [diff] [blame] | 256 | let paths = vec![ |
Victor Hsieh | 8032b18 | 2021-11-16 14:26:37 -0800 | [diff] [blame] | 257 | Path::new("/system/framework/com.android.location.provider.jar"), |
| 258 | Path::new("/system/framework/ethernet-service.jar"), |
| 259 | Path::new("/system/framework/ext.jar"), |
| 260 | Path::new("/system/framework/framework-graphics.jar"), |
Victor Hsieh | d18b975 | 2021-11-09 16:03:34 -0800 | [diff] [blame] | 261 | Path::new("/system/framework/framework.jar"), |
Victor Hsieh | 8032b18 | 2021-11-16 14:26:37 -0800 | [diff] [blame] | 262 | Path::new("/system/framework/ims-common.jar"), |
Victor Hsieh | d18b975 | 2021-11-09 16:03:34 -0800 | [diff] [blame] | 263 | Path::new("/system/framework/services.jar"), |
Victor Hsieh | 8032b18 | 2021-11-16 14:26:37 -0800 | [diff] [blame] | 264 | Path::new("/system/framework/telephony-common.jar"), |
| 265 | Path::new("/system/framework/voip-common.jar"), |
| 266 | Path::new("/system/etc/boot-image.prof"), |
| 267 | Path::new("/system/etc/dirty-image-objects"), |
Victor Hsieh | d18b975 | 2021-11-09 16:03:34 -0800 | [diff] [blame] | 268 | ]; |
| 269 | |
| 270 | for path in &paths { |
| 271 | let file_entry = { |
| 272 | // TODO(205883847): Not all files will be used. Open the remote file lazily. |
| 273 | let related_path = path.strip_prefix(&config.prefix)?; |
| 274 | let remote_file = RemoteFileReader::new_by_path( |
| 275 | service.clone(), |
| 276 | config.remote_dir_fd, |
| 277 | related_path, |
| 278 | )?; |
| 279 | let file_size = service.getFileSize(remote_file.get_remote_fd())?.try_into()?; |
Victor Hsieh | 015bcb5 | 2021-11-17 17:28:01 -0800 | [diff] [blame] | 280 | // TODO(206869687): Switch to VerifiedReadonly |
Victor Hsieh | d18b975 | 2021-11-09 16:03:34 -0800 | [diff] [blame] | 281 | AuthFsEntry::UnverifiedReadonly { reader: remote_file, file_size } |
| 282 | }; |
| 283 | authfs.add_entry_at_ro_dir_by_path( |
| 284 | dir_root_inode, |
| 285 | path.strip_prefix("/")?, |
| 286 | file_entry, |
| 287 | )?; |
| 288 | } |
| 289 | } |
| 290 | |
Victor Hsieh | 4d6b9d4 | 2021-11-08 15:53:49 -0800 | [diff] [blame] | 291 | Ok(()) |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 292 | } |
| 293 | |
Victor Hsieh | 60c2f41 | 2021-11-03 13:02:19 -0700 | [diff] [blame] | 294 | fn remote_fd_to_path_buf(fd: i32) -> PathBuf { |
| 295 | PathBuf::from(fd.to_string()) |
| 296 | } |
| 297 | |
Alan Stokes | e1b6e1c | 2021-10-01 12:44:49 +0100 | [diff] [blame] | 298 | fn try_main() -> Result<()> { |
Victor Hsieh | 2442abc | 2021-11-17 13:25:02 -0800 | [diff] [blame] | 299 | let args = Args::from_args_safe()?; |
Victor Hsieh | 9d0ab62 | 2021-04-26 17:07:02 -0700 | [diff] [blame] | 300 | |
| 301 | let log_level = if args.debug { log::Level::Debug } else { log::Level::Info }; |
| 302 | android_logger::init_once( |
| 303 | android_logger::Config::default().with_tag("authfs").with_min_level(log_level), |
| 304 | ); |
| 305 | |
Victor Hsieh | 4d6b9d4 | 2021-11-08 15:53:49 -0800 | [diff] [blame] | 306 | let mut authfs = AuthFs::new(); |
| 307 | prepare_root_dir_entries(&mut authfs, &args)?; |
| 308 | fusefs::loop_forever(authfs, &args.mount_point, &args.extra_options)?; |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 309 | bail!("Unexpected exit after the handler loop") |
Victor Hsieh | 88ac6ca | 2020-11-13 15:20:24 -0800 | [diff] [blame] | 310 | } |
Alan Stokes | e1b6e1c | 2021-10-01 12:44:49 +0100 | [diff] [blame] | 311 | |
| 312 | fn main() { |
| 313 | if let Err(e) = try_main() { |
| 314 | error!("failed with {:?}", e); |
| 315 | std::process::exit(1); |
| 316 | } |
| 317 | } |