Victor Hsieh | 045f1e6 | 2021-08-03 12:04:34 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2021 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 | use anyhow::{bail, Context, Result}; |
| 18 | use log::{debug, error, warn}; |
| 19 | use nix::mount::{umount2, MntFlags}; |
| 20 | use nix::sys::statfs::{statfs, FsType}; |
| 21 | use shared_child::SharedChild; |
| 22 | use std::ffi::{OsStr, OsString}; |
| 23 | use std::fs::{remove_dir, OpenOptions}; |
| 24 | use std::path::PathBuf; |
| 25 | use std::process::Command; |
| 26 | use std::thread::sleep; |
| 27 | use std::time::{Duration, Instant}; |
| 28 | |
Victor Hsieh | 015bcb5 | 2021-11-17 17:28:01 -0800 | [diff] [blame] | 29 | use authfs_aidl_interface::aidl::com::android::virt::fs::AuthFsConfig::{ |
| 30 | AuthFsConfig, InputDirFdAnnotation::InputDirFdAnnotation, InputFdAnnotation::InputFdAnnotation, |
| 31 | OutputDirFdAnnotation::OutputDirFdAnnotation, OutputFdAnnotation::OutputFdAnnotation, |
Victor Hsieh | 045f1e6 | 2021-08-03 12:04:34 -0700 | [diff] [blame] | 32 | }; |
Victor Hsieh | 015bcb5 | 2021-11-17 17:28:01 -0800 | [diff] [blame] | 33 | use authfs_aidl_interface::aidl::com::android::virt::fs::IAuthFs::{BnAuthFs, IAuthFs}; |
Victor Hsieh | 045f1e6 | 2021-08-03 12:04:34 -0700 | [diff] [blame] | 34 | use authfs_aidl_interface::binder::{ |
| 35 | self, BinderFeatures, ExceptionCode, Interface, ParcelFileDescriptor, Strong, |
| 36 | }; |
Alan Stokes | 3189af0 | 2021-09-30 17:51:19 +0100 | [diff] [blame] | 37 | use binder_common::new_binder_exception; |
Victor Hsieh | 045f1e6 | 2021-08-03 12:04:34 -0700 | [diff] [blame] | 38 | |
| 39 | const AUTHFS_BIN: &str = "/system/bin/authfs"; |
| 40 | const AUTHFS_SETUP_POLL_INTERVAL_MS: Duration = Duration::from_millis(50); |
| 41 | const AUTHFS_SETUP_TIMEOUT_SEC: Duration = Duration::from_secs(10); |
| 42 | const FUSE_SUPER_MAGIC: FsType = FsType(0x65735546); |
| 43 | |
| 44 | /// An `AuthFs` instance is supposed to be backed by an `authfs` process. When the lifetime of the |
| 45 | /// instance is over, it should leave no trace on the system: the process should be terminated, the |
| 46 | /// FUSE should be unmounted, and the mount directory should be deleted. |
| 47 | pub struct AuthFs { |
| 48 | mountpoint: OsString, |
| 49 | process: SharedChild, |
| 50 | } |
| 51 | |
| 52 | impl Interface for AuthFs {} |
| 53 | |
| 54 | impl IAuthFs for AuthFs { |
| 55 | fn openFile( |
| 56 | &self, |
Victor Hsieh | ebb1d90 | 2021-08-06 13:00:18 -0700 | [diff] [blame] | 57 | remote_fd_name: i32, |
Victor Hsieh | 045f1e6 | 2021-08-03 12:04:34 -0700 | [diff] [blame] | 58 | writable: bool, |
| 59 | ) -> binder::Result<ParcelFileDescriptor> { |
| 60 | let mut path = PathBuf::from(&self.mountpoint); |
| 61 | path.push(remote_fd_name.to_string()); |
| 62 | let file = OpenOptions::new().read(true).write(writable).open(&path).map_err(|e| { |
| 63 | new_binder_exception( |
| 64 | ExceptionCode::SERVICE_SPECIFIC, |
| 65 | format!("failed to open {:?} on authfs: {}", &path, e), |
| 66 | ) |
| 67 | })?; |
| 68 | Ok(ParcelFileDescriptor::new(file)) |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | impl AuthFs { |
| 73 | /// Mount an authfs at `mountpoint` with specified FD annotations. |
| 74 | pub fn mount_and_wait( |
| 75 | mountpoint: OsString, |
| 76 | config: &AuthFsConfig, |
| 77 | debuggable: bool, |
| 78 | ) -> Result<Strong<dyn IAuthFs>> { |
| 79 | let child = run_authfs( |
| 80 | &mountpoint, |
| 81 | &config.inputFdAnnotations, |
| 82 | &config.outputFdAnnotations, |
Victor Hsieh | 015bcb5 | 2021-11-17 17:28:01 -0800 | [diff] [blame] | 83 | &config.inputDirFdAnnotations, |
| 84 | &config.outputDirFdAnnotations, |
Victor Hsieh | 045f1e6 | 2021-08-03 12:04:34 -0700 | [diff] [blame] | 85 | debuggable, |
| 86 | )?; |
Alan Stokes | e1b6e1c | 2021-10-01 12:44:49 +0100 | [diff] [blame] | 87 | wait_until_authfs_ready(&child, &mountpoint).map_err(|e| { |
| 88 | match child.wait() { |
| 89 | Ok(status) => debug!("Wait for authfs: {}", status), |
| 90 | Err(e) => warn!("Failed to wait for child: {}", e), |
| 91 | } |
Victor Hsieh | 045f1e6 | 2021-08-03 12:04:34 -0700 | [diff] [blame] | 92 | e |
| 93 | })?; |
| 94 | |
| 95 | let authfs = AuthFs { mountpoint, process: child }; |
| 96 | Ok(BnAuthFs::new_binder(authfs, BinderFeatures::default())) |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | impl Drop for AuthFs { |
| 101 | /// On drop, try to erase all the traces for this authfs mount. |
| 102 | fn drop(&mut self) { |
| 103 | debug!("Dropping AuthFs instance at mountpoint {:?}", &self.mountpoint); |
| 104 | if let Err(e) = self.process.kill() { |
| 105 | error!("Failed to kill authfs: {}", e); |
| 106 | } |
| 107 | match self.process.wait() { |
| 108 | Ok(status) => debug!("authfs exit code: {}", status), |
| 109 | Err(e) => warn!("Failed to wait for authfs: {}", e), |
| 110 | } |
| 111 | // The client may still hold the file descriptors that refer to this filesystem. Use |
| 112 | // MNT_DETACH to detach the mountpoint, and automatically unmount when there is no more |
| 113 | // reference. |
| 114 | if let Err(e) = umount2(self.mountpoint.as_os_str(), MntFlags::MNT_DETACH) { |
| 115 | error!("Failed to umount authfs at {:?}: {}", &self.mountpoint, e) |
| 116 | } |
| 117 | |
| 118 | if let Err(e) = remove_dir(&self.mountpoint) { |
| 119 | error!("Failed to clean up mount directory {:?}: {}", &self.mountpoint, e) |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | fn run_authfs( |
| 125 | mountpoint: &OsStr, |
Victor Hsieh | 015bcb5 | 2021-11-17 17:28:01 -0800 | [diff] [blame] | 126 | in_file_fds: &[InputFdAnnotation], |
| 127 | out_file_fds: &[OutputFdAnnotation], |
| 128 | in_dir_fds: &[InputDirFdAnnotation], |
| 129 | out_dir_fds: &[OutputDirFdAnnotation], |
Victor Hsieh | 045f1e6 | 2021-08-03 12:04:34 -0700 | [diff] [blame] | 130 | debuggable: bool, |
| 131 | ) -> Result<SharedChild> { |
| 132 | let mut args = vec![mountpoint.to_owned(), OsString::from("--cid=2")]; |
Victor Hsieh | 8bb67b6 | 2021-08-04 12:10:58 -0700 | [diff] [blame] | 133 | args.push(OsString::from("-o")); |
| 134 | args.push(OsString::from("fscontext=u:object_r:authfs_fuse:s0")); |
Victor Hsieh | 015bcb5 | 2021-11-17 17:28:01 -0800 | [diff] [blame] | 135 | for conf in in_file_fds { |
Victor Hsieh | 045f1e6 | 2021-08-03 12:04:34 -0700 | [diff] [blame] | 136 | // TODO(b/185178698): Many input files need to be signed and verified. |
| 137 | // or can we use debug cert for now, which is better than nothing? |
| 138 | args.push(OsString::from("--remote-ro-file-unverified")); |
Victor Hsieh | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 139 | args.push(OsString::from(conf.fd.to_string())); |
Victor Hsieh | 045f1e6 | 2021-08-03 12:04:34 -0700 | [diff] [blame] | 140 | } |
Victor Hsieh | 015bcb5 | 2021-11-17 17:28:01 -0800 | [diff] [blame] | 141 | for conf in out_file_fds { |
Victor Hsieh | 045f1e6 | 2021-08-03 12:04:34 -0700 | [diff] [blame] | 142 | args.push(OsString::from("--remote-new-rw-file")); |
Victor Hsieh | b3588ce | 2021-11-02 15:02:32 -0700 | [diff] [blame] | 143 | args.push(OsString::from(conf.fd.to_string())); |
Victor Hsieh | 045f1e6 | 2021-08-03 12:04:34 -0700 | [diff] [blame] | 144 | } |
Victor Hsieh | 015bcb5 | 2021-11-17 17:28:01 -0800 | [diff] [blame] | 145 | for conf in in_dir_fds { |
| 146 | args.push(OsString::from("--remote-ro-dir")); |
| 147 | // TODO(206869687): Replace /dev/null with the real path when possible. |
| 148 | args.push(OsString::from(format!("{}:{}:{}", conf.fd, conf.manifestPath, conf.prefix))); |
| 149 | } |
| 150 | for conf in out_dir_fds { |
| 151 | args.push(OsString::from("--remote-new-rw-dir")); |
| 152 | args.push(OsString::from(conf.fd.to_string())); |
| 153 | } |
Victor Hsieh | 045f1e6 | 2021-08-03 12:04:34 -0700 | [diff] [blame] | 154 | if debuggable { |
| 155 | args.push(OsString::from("--debug")); |
| 156 | } |
| 157 | |
| 158 | let mut command = Command::new(AUTHFS_BIN); |
| 159 | command.args(&args); |
Victor Hsieh | 015bcb5 | 2021-11-17 17:28:01 -0800 | [diff] [blame] | 160 | debug!("Spawn authfs: {:?}", command); |
Victor Hsieh | 045f1e6 | 2021-08-03 12:04:34 -0700 | [diff] [blame] | 161 | SharedChild::spawn(&mut command).context("Spawn authfs") |
| 162 | } |
| 163 | |
Alan Stokes | e1b6e1c | 2021-10-01 12:44:49 +0100 | [diff] [blame] | 164 | fn wait_until_authfs_ready(child: &SharedChild, mountpoint: &OsStr) -> Result<()> { |
Victor Hsieh | 045f1e6 | 2021-08-03 12:04:34 -0700 | [diff] [blame] | 165 | let start_time = Instant::now(); |
| 166 | loop { |
| 167 | if is_fuse(mountpoint)? { |
| 168 | break; |
| 169 | } |
Alan Stokes | e1b6e1c | 2021-10-01 12:44:49 +0100 | [diff] [blame] | 170 | if let Some(exit_status) = child.try_wait()? { |
| 171 | // If the child has exited, we will never become ready. |
| 172 | bail!("Child has exited: {}", exit_status); |
| 173 | } |
Victor Hsieh | 045f1e6 | 2021-08-03 12:04:34 -0700 | [diff] [blame] | 174 | if start_time.elapsed() > AUTHFS_SETUP_TIMEOUT_SEC { |
Alan Stokes | e1b6e1c | 2021-10-01 12:44:49 +0100 | [diff] [blame] | 175 | let _ = child.kill(); |
Victor Hsieh | 045f1e6 | 2021-08-03 12:04:34 -0700 | [diff] [blame] | 176 | bail!("Time out mounting authfs"); |
| 177 | } |
| 178 | sleep(AUTHFS_SETUP_POLL_INTERVAL_MS); |
| 179 | } |
| 180 | Ok(()) |
| 181 | } |
| 182 | |
| 183 | fn is_fuse(path: &OsStr) -> Result<bool> { |
| 184 | Ok(statfs(path)?.filesystem_type() == FUSE_SUPER_MAGIC) |
| 185 | } |