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}; |
| 31 | use std::sync::atomic::{AtomicUsize, Ordering}; |
| 32 | |
Victor Hsieh | 045f1e6 | 2021-08-03 12:04:34 -0700 | [diff] [blame] | 33 | use authfs_aidl_interface::aidl::com::android::virt::fs::AuthFsConfig::AuthFsConfig; |
| 34 | use authfs_aidl_interface::aidl::com::android::virt::fs::IAuthFs::IAuthFs; |
| 35 | use authfs_aidl_interface::aidl::com::android::virt::fs::IAuthFsService::{ |
Alice Wang | 15ae7b9 | 2022-10-24 14:36:59 +0000 | [diff] [blame] | 36 | BnAuthFsService, IAuthFsService, AUTHFS_SERVICE_SOCKET_NAME, |
Victor Hsieh | 045f1e6 | 2021-08-03 12:04:34 -0700 | [diff] [blame] | 37 | }; |
Alice Wang | 15ae7b9 | 2022-10-24 14:36:59 +0000 | [diff] [blame] | 38 | use binder::{self, BinderFeatures, ExceptionCode, Interface, Status, Strong}; |
Victor Hsieh | 045f1e6 | 2021-08-03 12:04:34 -0700 | [diff] [blame] | 39 | |
Victor Hsieh | 8bb67b6 | 2021-08-04 12:10:58 -0700 | [diff] [blame] | 40 | const SERVICE_ROOT: &str = "/data/misc/authfs"; |
Victor Hsieh | 045f1e6 | 2021-08-03 12:04:34 -0700 | [diff] [blame] | 41 | |
| 42 | /// Implementation of `IAuthFsService`. |
| 43 | pub struct AuthFsService { |
| 44 | serial_number: AtomicUsize, |
| 45 | debuggable: bool, |
| 46 | } |
| 47 | |
| 48 | impl Interface for AuthFsService {} |
| 49 | |
| 50 | impl IAuthFsService for AuthFsService { |
| 51 | fn mount(&self, config: &AuthFsConfig) -> binder::Result<Strong<dyn IAuthFs>> { |
| 52 | self.validate(config)?; |
| 53 | |
| 54 | let mountpoint = self.get_next_mount_point(); |
| 55 | |
| 56 | // The directory is supposed to be deleted when `AuthFs` is dropped. |
| 57 | create_dir(&mountpoint).map_err(|e| { |
Andrew Walbran | dcf9d58 | 2022-08-03 11:25:24 +0000 | [diff] [blame] | 58 | Status::new_service_specific_error_str( |
| 59 | -1, |
| 60 | Some(format!("Cannot create mount directory {:?}: {:?}", &mountpoint, e)), |
Victor Hsieh | 045f1e6 | 2021-08-03 12:04:34 -0700 | [diff] [blame] | 61 | ) |
| 62 | })?; |
| 63 | |
| 64 | authfs::AuthFs::mount_and_wait(mountpoint, config, self.debuggable).map_err(|e| { |
Andrew Walbran | dcf9d58 | 2022-08-03 11:25:24 +0000 | [diff] [blame] | 65 | Status::new_service_specific_error_str( |
| 66 | -1, |
| 67 | Some(format!("mount_and_wait failed: {:?}", e)), |
Victor Hsieh | 045f1e6 | 2021-08-03 12:04:34 -0700 | [diff] [blame] | 68 | ) |
| 69 | }) |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | impl AuthFsService { |
| 74 | fn new_binder(debuggable: bool) -> Strong<dyn IAuthFsService> { |
| 75 | let service = AuthFsService { serial_number: AtomicUsize::new(1), debuggable }; |
| 76 | BnAuthFsService::new_binder(service, BinderFeatures::default()) |
| 77 | } |
| 78 | |
| 79 | fn validate(&self, config: &AuthFsConfig) -> binder::Result<()> { |
| 80 | if config.port < 0 { |
Andrew Walbran | dcf9d58 | 2022-08-03 11:25:24 +0000 | [diff] [blame] | 81 | return Err(Status::new_exception_str( |
Victor Hsieh | 045f1e6 | 2021-08-03 12:04:34 -0700 | [diff] [blame] | 82 | ExceptionCode::ILLEGAL_ARGUMENT, |
Andrew Walbran | dcf9d58 | 2022-08-03 11:25:24 +0000 | [diff] [blame] | 83 | Some(format!("Invalid port: {}", config.port)), |
Victor Hsieh | 045f1e6 | 2021-08-03 12:04:34 -0700 | [diff] [blame] | 84 | )); |
| 85 | } |
| 86 | Ok(()) |
| 87 | } |
| 88 | |
| 89 | fn get_next_mount_point(&self) -> OsString { |
| 90 | let previous = self.serial_number.fetch_add(1, Ordering::Relaxed); |
| 91 | OsString::from(format!("{}/{}", SERVICE_ROOT, previous)) |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | fn clean_up_working_directory() -> Result<()> { |
| 96 | for entry in read_dir(SERVICE_ROOT)? { |
| 97 | let entry = entry?; |
| 98 | let path = entry.path(); |
| 99 | if path.is_dir() { |
| 100 | remove_dir_all(path)?; |
| 101 | } else if path.is_file() { |
| 102 | remove_file(path)?; |
| 103 | } else { |
| 104 | bail!("Unrecognized path type: {:?}", path); |
| 105 | } |
| 106 | } |
| 107 | Ok(()) |
| 108 | } |
| 109 | |
Charisee | 484cead | 2023-11-30 04:24:10 +0000 | [diff] [blame] | 110 | #[allow(clippy::eq_op)] |
Alan Stokes | e1b6e1c | 2021-10-01 12:44:49 +0100 | [diff] [blame] | 111 | fn try_main() -> Result<()> { |
Jiyong Park | 7d67171 | 2024-09-11 08:29:22 +0900 | [diff] [blame] | 112 | // SAFETY: nobody has taken ownership of the inherited FDs yet. |
| 113 | unsafe { rustutils::inherited_fd::init_once()? }; |
| 114 | |
Victor Hsieh | 045f1e6 | 2021-08-03 12:04:34 -0700 | [diff] [blame] | 115 | let debuggable = env!("TARGET_BUILD_VARIANT") != "user"; |
Jeff Vander Stoep | 57da157 | 2024-01-31 10:52:16 +0100 | [diff] [blame] | 116 | let log_level = if debuggable { log::LevelFilter::Trace } else { log::LevelFilter::Info }; |
Victor Hsieh | 045f1e6 | 2021-08-03 12:04:34 -0700 | [diff] [blame] | 117 | android_logger::init_once( |
Jeff Vander Stoep | 57da157 | 2024-01-31 10:52:16 +0100 | [diff] [blame] | 118 | android_logger::Config::default().with_tag("authfs_service").with_max_level(log_level), |
Victor Hsieh | 045f1e6 | 2021-08-03 12:04:34 -0700 | [diff] [blame] | 119 | ); |
| 120 | |
| 121 | clean_up_working_directory()?; |
| 122 | |
Jiyong Park | 7d67171 | 2024-09-11 08:29:22 +0900 | [diff] [blame] | 123 | let socket_fd = android_get_control_socket(AUTHFS_SERVICE_SOCKET_NAME)?; |
Victor Hsieh | 045f1e6 | 2021-08-03 12:04:34 -0700 | [diff] [blame] | 124 | let service = AuthFsService::new_binder(debuggable).as_binder(); |
Alice Wang | 15ae7b9 | 2022-10-24 14:36:59 +0000 | [diff] [blame] | 125 | debug!("{} is starting as a rpc service.", AUTHFS_SERVICE_SOCKET_NAME); |
Alice Wang | fd222fd | 2023-05-25 09:37:38 +0000 | [diff] [blame] | 126 | let server = RpcServer::new_bound_socket(service, socket_fd)?; |
David Brazdil | 671e614 | 2022-11-16 11:47:27 +0000 | [diff] [blame] | 127 | info!("The RPC server '{}' is running.", AUTHFS_SERVICE_SOCKET_NAME); |
| 128 | server.join(); |
| 129 | info!("The RPC server at '{}' has shut down gracefully.", AUTHFS_SERVICE_SOCKET_NAME); |
| 130 | Ok(()) |
Victor Hsieh | 045f1e6 | 2021-08-03 12:04:34 -0700 | [diff] [blame] | 131 | } |
Alan Stokes | e1b6e1c | 2021-10-01 12:44:49 +0100 | [diff] [blame] | 132 | |
| 133 | fn main() { |
| 134 | if let Err(e) = try_main() { |
| 135 | error!("failed with {:?}", e); |
| 136 | std::process::exit(1); |
| 137 | } |
| 138 | } |