David Brazdil | 1f53070 | 2022-10-03 12:18:10 +0100 | [diff] [blame] | 1 | // Copyright 2022, The Android Open Source Project |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | //! Android Virtualization Manager |
| 16 | |
| 17 | mod aidl; |
| 18 | mod atom; |
| 19 | mod composite; |
| 20 | mod crosvm; |
Jaewan Kim | c03f661 | 2023-02-20 00:06:26 +0900 | [diff] [blame] | 21 | mod debug_config; |
David Brazdil | 1f53070 | 2022-10-03 12:18:10 +0100 | [diff] [blame] | 22 | mod payload; |
| 23 | mod selinux; |
| 24 | |
David Brazdil | 4b4c510 | 2022-12-19 22:56:20 +0000 | [diff] [blame] | 25 | use crate::aidl::{GLOBAL_SERVICE, VirtualizationService}; |
David Brazdil | 1f53070 | 2022-10-03 12:18:10 +0100 | [diff] [blame] | 26 | use android_system_virtualizationservice::aidl::android::system::virtualizationservice::IVirtualizationService::BnVirtualizationService; |
Alan Stokes | c4d5def | 2023-02-14 17:01:59 +0000 | [diff] [blame] | 27 | use anyhow::{bail, Context, Result}; |
David Brazdil | 4b4c510 | 2022-12-19 22:56:20 +0000 | [diff] [blame] | 28 | use binder::{BinderFeatures, ProcessState}; |
David Brazdil | 1f53070 | 2022-10-03 12:18:10 +0100 | [diff] [blame] | 29 | use lazy_static::lazy_static; |
Jeff Vander Stoep | 57da157 | 2024-01-31 10:52:16 +0100 | [diff] [blame^] | 30 | use log::{info, LevelFilter}; |
David Brazdil | 1f53070 | 2022-10-03 12:18:10 +0100 | [diff] [blame] | 31 | use rpcbinder::{FileDescriptorTransportMode, RpcServer}; |
| 32 | use std::os::unix::io::{FromRawFd, OwnedFd, RawFd}; |
| 33 | use clap::Parser; |
David Brazdil | 161ddda | 2023-01-06 20:29:19 +0000 | [diff] [blame] | 34 | use nix::fcntl::{fcntl, F_GETFD, F_SETFD, FdFlag}; |
David Brazdil | 1f53070 | 2022-10-03 12:18:10 +0100 | [diff] [blame] | 35 | use nix::unistd::{Pid, Uid}; |
| 36 | use std::os::unix::raw::{pid_t, uid_t}; |
| 37 | |
| 38 | const LOG_TAG: &str = "virtmgr"; |
| 39 | |
| 40 | lazy_static! { |
| 41 | static ref PID_PARENT: Pid = Pid::parent(); |
| 42 | static ref UID_CURRENT: Uid = Uid::current(); |
| 43 | } |
| 44 | |
| 45 | fn get_calling_pid() -> pid_t { |
| 46 | // The caller is the parent of this process. |
| 47 | PID_PARENT.as_raw() |
| 48 | } |
| 49 | |
| 50 | fn get_calling_uid() -> uid_t { |
| 51 | // The caller and this process share the same UID. |
| 52 | UID_CURRENT.as_raw() |
| 53 | } |
| 54 | |
| 55 | #[derive(Parser)] |
| 56 | struct Args { |
| 57 | /// File descriptor inherited from the caller to run RpcBinder server on. |
| 58 | /// This should be one end of a socketpair() compatible with RpcBinder's |
| 59 | /// UDS bootstrap transport. |
| 60 | #[clap(long)] |
| 61 | rpc_server_fd: RawFd, |
| 62 | /// File descriptor inherited from the caller to signal RpcBinder server |
| 63 | /// readiness. This should be one end of pipe() and the caller should be |
| 64 | /// waiting for HUP on the other end. |
| 65 | #[clap(long)] |
| 66 | ready_fd: RawFd, |
| 67 | } |
| 68 | |
| 69 | fn take_fd_ownership(raw_fd: RawFd, owned_fds: &mut Vec<RawFd>) -> Result<OwnedFd, anyhow::Error> { |
| 70 | // Basic check that the integer value does correspond to a file descriptor. |
David Brazdil | 161ddda | 2023-01-06 20:29:19 +0000 | [diff] [blame] | 71 | fcntl(raw_fd, F_GETFD).with_context(|| format!("Invalid file descriptor {raw_fd}"))?; |
| 72 | |
| 73 | // The file descriptor had CLOEXEC disabled to be inherited from the parent. |
| 74 | // Re-enable it to make sure it is not accidentally inherited further. |
| 75 | fcntl(raw_fd, F_SETFD(FdFlag::FD_CLOEXEC)) |
| 76 | .with_context(|| format!("Could not set CLOEXEC on file descriptor {raw_fd}"))?; |
David Brazdil | 1f53070 | 2022-10-03 12:18:10 +0100 | [diff] [blame] | 77 | |
| 78 | // Creating OwnedFd for stdio FDs is not safe. |
| 79 | if [libc::STDIN_FILENO, libc::STDOUT_FILENO, libc::STDERR_FILENO].contains(&raw_fd) { |
| 80 | bail!("File descriptor {raw_fd} is standard I/O descriptor"); |
| 81 | } |
| 82 | |
| 83 | // Reject RawFds that already have a corresponding OwnedFd. |
| 84 | if owned_fds.contains(&raw_fd) { |
| 85 | bail!("File descriptor {raw_fd} already owned"); |
| 86 | } |
| 87 | owned_fds.push(raw_fd); |
| 88 | |
Andrew Walbran | b58d1b4 | 2023-07-07 13:54:49 +0100 | [diff] [blame] | 89 | // SAFETY: Initializing OwnedFd for a RawFd provided in cmdline arguments. |
David Brazdil | 1f53070 | 2022-10-03 12:18:10 +0100 | [diff] [blame] | 90 | // We checked that the integer value corresponds to a valid FD and that this |
| 91 | // is the first argument to claim its ownership. |
| 92 | Ok(unsafe { OwnedFd::from_raw_fd(raw_fd) }) |
| 93 | } |
| 94 | |
Alan Stokes | c4d5def | 2023-02-14 17:01:59 +0000 | [diff] [blame] | 95 | fn check_vm_support() -> Result<()> { |
| 96 | if hypervisor_props::is_any_vm_supported()? { |
| 97 | Ok(()) |
| 98 | } else { |
| 99 | // This should never happen, it indicates a misconfigured device where the virt APEX |
| 100 | // is present but VMs are not supported. If it does happen, fail fast to avoid wasting |
| 101 | // resources trying. |
| 102 | bail!("Device doesn't support protected or non-protected VMs") |
| 103 | } |
Alan Stokes | 8d39a9b | 2023-01-10 15:01:00 +0000 | [diff] [blame] | 104 | } |
| 105 | |
David Brazdil | 1f53070 | 2022-10-03 12:18:10 +0100 | [diff] [blame] | 106 | fn main() { |
| 107 | android_logger::init_once( |
| 108 | android_logger::Config::default() |
| 109 | .with_tag(LOG_TAG) |
Jeff Vander Stoep | 57da157 | 2024-01-31 10:52:16 +0100 | [diff] [blame^] | 110 | .with_max_level(LevelFilter::Info) |
| 111 | .with_log_buffer(android_logger::LogId::System), |
David Brazdil | 1f53070 | 2022-10-03 12:18:10 +0100 | [diff] [blame] | 112 | ); |
| 113 | |
Alan Stokes | c4d5def | 2023-02-14 17:01:59 +0000 | [diff] [blame] | 114 | check_vm_support().unwrap(); |
Alan Stokes | 8d39a9b | 2023-01-10 15:01:00 +0000 | [diff] [blame] | 115 | |
David Brazdil | 1f53070 | 2022-10-03 12:18:10 +0100 | [diff] [blame] | 116 | let args = Args::parse(); |
| 117 | |
| 118 | let mut owned_fds = vec![]; |
| 119 | let rpc_server_fd = take_fd_ownership(args.rpc_server_fd, &mut owned_fds) |
| 120 | .expect("Failed to take ownership of rpc_server_fd"); |
| 121 | let ready_fd = take_fd_ownership(args.ready_fd, &mut owned_fds) |
| 122 | .expect("Failed to take ownership of ready_fd"); |
| 123 | |
David Brazdil | 4b4c510 | 2022-12-19 22:56:20 +0000 | [diff] [blame] | 124 | // Start thread pool for kernel Binder connection to VirtualizationServiceInternal. |
| 125 | ProcessState::start_thread_pool(); |
| 126 | |
| 127 | GLOBAL_SERVICE.removeMemlockRlimit().expect("Failed to remove memlock rlimit"); |
| 128 | |
David Brazdil | 1f53070 | 2022-10-03 12:18:10 +0100 | [diff] [blame] | 129 | let service = VirtualizationService::init(); |
| 130 | let service = |
| 131 | BnVirtualizationService::new_binder(service, BinderFeatures::default()).as_binder(); |
| 132 | |
| 133 | let server = RpcServer::new_unix_domain_bootstrap(service, rpc_server_fd) |
| 134 | .expect("Failed to start RpcServer"); |
| 135 | server.set_supported_file_descriptor_transport_modes(&[FileDescriptorTransportMode::Unix]); |
| 136 | |
| 137 | info!("Started VirtualizationService RpcServer. Ready to accept connections"); |
| 138 | |
| 139 | // Signal readiness to the caller by closing our end of the pipe. |
| 140 | drop(ready_fd); |
| 141 | |
| 142 | server.join(); |
| 143 | info!("Shutting down VirtualizationService RpcServer"); |
| 144 | } |