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