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 | //! AuthFsService facilitates authfs mounting (which is a privileged operation) for the client. The |
| 18 | //! client will provide an `AuthFsConfig` which includes the backend address (only port for now) and |
| 19 | //! the filesystem configuration. It is up to the client to ensure the backend server is running. On |
| 20 | //! a successful mount, the client receives an `IAuthFs`, and through the binder object, the client |
| 21 | //! is able to retrieve "remote file descriptors". |
| 22 | |
| 23 | mod authfs; |
Victor Hsieh | 045f1e6 | 2021-08-03 12:04:34 -0700 | [diff] [blame] | 24 | |
Alice Wang | 15ae7b9 | 2022-10-24 14:36:59 +0000 | [diff] [blame] | 25 | use anyhow::{bail, Result}; |
Victor Hsieh | 045f1e6 | 2021-08-03 12:04:34 -0700 | [diff] [blame] | 26 | use log::*; |
David Brazdil | 671e614 | 2022-11-16 11:47:27 +0000 | [diff] [blame] | 27 | use rpcbinder::RpcServer; |
Alice Wang | fd222fd | 2023-05-25 09:37:38 +0000 | [diff] [blame^] | 28 | use rustutils::sockets::android_get_control_socket; |
Victor Hsieh | 045f1e6 | 2021-08-03 12:04:34 -0700 | [diff] [blame] | 29 | use std::ffi::OsString; |
| 30 | use std::fs::{create_dir, read_dir, remove_dir_all, remove_file}; |
Alice Wang | fd222fd | 2023-05-25 09:37:38 +0000 | [diff] [blame^] | 31 | use std::os::unix::io::{FromRawFd, OwnedFd}; |
Victor Hsieh | 045f1e6 | 2021-08-03 12:04:34 -0700 | [diff] [blame] | 32 | use std::sync::atomic::{AtomicUsize, Ordering}; |
| 33 | |
Victor Hsieh | 045f1e6 | 2021-08-03 12:04:34 -0700 | [diff] [blame] | 34 | use authfs_aidl_interface::aidl::com::android::virt::fs::AuthFsConfig::AuthFsConfig; |
| 35 | use authfs_aidl_interface::aidl::com::android::virt::fs::IAuthFs::IAuthFs; |
| 36 | use authfs_aidl_interface::aidl::com::android::virt::fs::IAuthFsService::{ |
Alice Wang | 15ae7b9 | 2022-10-24 14:36:59 +0000 | [diff] [blame] | 37 | BnAuthFsService, IAuthFsService, AUTHFS_SERVICE_SOCKET_NAME, |
Victor Hsieh | 045f1e6 | 2021-08-03 12:04:34 -0700 | [diff] [blame] | 38 | }; |
Alice Wang | 15ae7b9 | 2022-10-24 14:36:59 +0000 | [diff] [blame] | 39 | use binder::{self, BinderFeatures, ExceptionCode, Interface, Status, Strong}; |
Victor Hsieh | 045f1e6 | 2021-08-03 12:04:34 -0700 | [diff] [blame] | 40 | |
Victor Hsieh | 8bb67b6 | 2021-08-04 12:10:58 -0700 | [diff] [blame] | 41 | const SERVICE_ROOT: &str = "/data/misc/authfs"; |
Victor Hsieh | 045f1e6 | 2021-08-03 12:04:34 -0700 | [diff] [blame] | 42 | |
| 43 | /// Implementation of `IAuthFsService`. |
| 44 | pub struct AuthFsService { |
| 45 | serial_number: AtomicUsize, |
| 46 | debuggable: bool, |
| 47 | } |
| 48 | |
| 49 | impl Interface for AuthFsService {} |
| 50 | |
| 51 | impl IAuthFsService for AuthFsService { |
| 52 | fn mount(&self, config: &AuthFsConfig) -> binder::Result<Strong<dyn IAuthFs>> { |
| 53 | self.validate(config)?; |
| 54 | |
| 55 | let mountpoint = self.get_next_mount_point(); |
| 56 | |
| 57 | // The directory is supposed to be deleted when `AuthFs` is dropped. |
| 58 | create_dir(&mountpoint).map_err(|e| { |
Andrew Walbran | dcf9d58 | 2022-08-03 11:25:24 +0000 | [diff] [blame] | 59 | Status::new_service_specific_error_str( |
| 60 | -1, |
| 61 | Some(format!("Cannot create mount directory {:?}: {:?}", &mountpoint, e)), |
Victor Hsieh | 045f1e6 | 2021-08-03 12:04:34 -0700 | [diff] [blame] | 62 | ) |
| 63 | })?; |
| 64 | |
| 65 | authfs::AuthFs::mount_and_wait(mountpoint, config, self.debuggable).map_err(|e| { |
Andrew Walbran | dcf9d58 | 2022-08-03 11:25:24 +0000 | [diff] [blame] | 66 | Status::new_service_specific_error_str( |
| 67 | -1, |
| 68 | Some(format!("mount_and_wait failed: {:?}", e)), |
Victor Hsieh | 045f1e6 | 2021-08-03 12:04:34 -0700 | [diff] [blame] | 69 | ) |
| 70 | }) |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | impl AuthFsService { |
| 75 | fn new_binder(debuggable: bool) -> Strong<dyn IAuthFsService> { |
| 76 | let service = AuthFsService { serial_number: AtomicUsize::new(1), debuggable }; |
| 77 | BnAuthFsService::new_binder(service, BinderFeatures::default()) |
| 78 | } |
| 79 | |
| 80 | fn validate(&self, config: &AuthFsConfig) -> binder::Result<()> { |
| 81 | if config.port < 0 { |
Andrew Walbran | dcf9d58 | 2022-08-03 11:25:24 +0000 | [diff] [blame] | 82 | return Err(Status::new_exception_str( |
Victor Hsieh | 045f1e6 | 2021-08-03 12:04:34 -0700 | [diff] [blame] | 83 | ExceptionCode::ILLEGAL_ARGUMENT, |
Andrew Walbran | dcf9d58 | 2022-08-03 11:25:24 +0000 | [diff] [blame] | 84 | Some(format!("Invalid port: {}", config.port)), |
Victor Hsieh | 045f1e6 | 2021-08-03 12:04:34 -0700 | [diff] [blame] | 85 | )); |
| 86 | } |
| 87 | Ok(()) |
| 88 | } |
| 89 | |
| 90 | fn get_next_mount_point(&self) -> OsString { |
| 91 | let previous = self.serial_number.fetch_add(1, Ordering::Relaxed); |
| 92 | OsString::from(format!("{}/{}", SERVICE_ROOT, previous)) |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | fn clean_up_working_directory() -> Result<()> { |
| 97 | for entry in read_dir(SERVICE_ROOT)? { |
| 98 | let entry = entry?; |
| 99 | let path = entry.path(); |
| 100 | if path.is_dir() { |
| 101 | remove_dir_all(path)?; |
| 102 | } else if path.is_file() { |
| 103 | remove_file(path)?; |
| 104 | } else { |
| 105 | bail!("Unrecognized path type: {:?}", path); |
| 106 | } |
| 107 | } |
| 108 | Ok(()) |
| 109 | } |
| 110 | |
Alice Wang | fd222fd | 2023-05-25 09:37:38 +0000 | [diff] [blame^] | 111 | /// Prepares a socket file descriptor for the authfs service. |
| 112 | /// |
| 113 | /// # Safety requirement |
| 114 | /// |
| 115 | /// The caller must ensure that this function is the only place that claims ownership |
| 116 | /// of the file descriptor and it is called only once. |
| 117 | unsafe fn prepare_authfs_service_socket() -> Result<OwnedFd> { |
| 118 | let raw_fd = android_get_control_socket(AUTHFS_SERVICE_SOCKET_NAME)?; |
| 119 | |
| 120 | // Creating OwnedFd for stdio FDs is not safe. |
| 121 | if [libc::STDIN_FILENO, libc::STDOUT_FILENO, libc::STDERR_FILENO].contains(&raw_fd) { |
| 122 | bail!("File descriptor {raw_fd} is standard I/O descriptor"); |
| 123 | } |
| 124 | // SAFETY: Initializing OwnedFd for a RawFd created by the init. |
| 125 | // We checked that the integer value corresponds to a valid FD and that the caller |
| 126 | // ensures that this is the only place to claim its ownership. |
| 127 | Ok(unsafe { OwnedFd::from_raw_fd(raw_fd) }) |
| 128 | } |
| 129 | |
Alan Stokes | e1b6e1c | 2021-10-01 12:44:49 +0100 | [diff] [blame] | 130 | fn try_main() -> Result<()> { |
Victor Hsieh | 045f1e6 | 2021-08-03 12:04:34 -0700 | [diff] [blame] | 131 | let debuggable = env!("TARGET_BUILD_VARIANT") != "user"; |
| 132 | let log_level = if debuggable { log::Level::Trace } else { log::Level::Info }; |
| 133 | android_logger::init_once( |
| 134 | android_logger::Config::default().with_tag("authfs_service").with_min_level(log_level), |
| 135 | ); |
| 136 | |
| 137 | clean_up_working_directory()?; |
| 138 | |
Alice Wang | fd222fd | 2023-05-25 09:37:38 +0000 | [diff] [blame^] | 139 | // SAFETY: This is the only place we take the ownership of the fd of the authfs service. |
| 140 | let socket_fd = unsafe { prepare_authfs_service_socket()? }; |
Victor Hsieh | 045f1e6 | 2021-08-03 12:04:34 -0700 | [diff] [blame] | 141 | let service = AuthFsService::new_binder(debuggable).as_binder(); |
Alice Wang | 15ae7b9 | 2022-10-24 14:36:59 +0000 | [diff] [blame] | 142 | debug!("{} is starting as a rpc service.", AUTHFS_SERVICE_SOCKET_NAME); |
Alice Wang | fd222fd | 2023-05-25 09:37:38 +0000 | [diff] [blame^] | 143 | let server = RpcServer::new_bound_socket(service, socket_fd)?; |
David Brazdil | 671e614 | 2022-11-16 11:47:27 +0000 | [diff] [blame] | 144 | info!("The RPC server '{}' is running.", AUTHFS_SERVICE_SOCKET_NAME); |
| 145 | server.join(); |
| 146 | info!("The RPC server at '{}' has shut down gracefully.", AUTHFS_SERVICE_SOCKET_NAME); |
| 147 | Ok(()) |
Victor Hsieh | 045f1e6 | 2021-08-03 12:04:34 -0700 | [diff] [blame] | 148 | } |
Alan Stokes | e1b6e1c | 2021-10-01 12:44:49 +0100 | [diff] [blame] | 149 | |
| 150 | fn main() { |
| 151 | if let Err(e) = try_main() { |
| 152 | error!("failed with {:?}", e); |
| 153 | std::process::exit(1); |
| 154 | } |
| 155 | } |