Victor Hsieh | 42cc776 | 2021-01-25 16:44:19 -0800 | [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 | |
Victor Hsieh | 1a8cd04 | 2021-09-03 16:29:45 -0700 | [diff] [blame] | 17 | //! This program is a constrained file/FD server to serve file requests through a remote binder |
Victor Hsieh | 42cc776 | 2021-01-25 16:44:19 -0800 | [diff] [blame] | 18 | //! service. The file server is not designed to serve arbitrary file paths in the filesystem. On |
| 19 | //! the contrary, the server should be configured to start with already opened FDs, and serve the |
| 20 | //! client's request against the FDs |
| 21 | //! |
| 22 | //! For example, `exec 9</path/to/file fd_server --ro-fds 9` starts the binder service. A client |
| 23 | //! client can then request the content of file 9 by offset and size. |
Victor Hsieh | 42cc776 | 2021-01-25 16:44:19 -0800 | [diff] [blame] | 24 | |
Victor Hsieh | b0f5fc8 | 2021-10-15 17:24:19 -0700 | [diff] [blame] | 25 | mod aidl; |
Victor Hsieh | 4dc85c9 | 2021-03-15 11:01:23 -0700 | [diff] [blame] | 26 | mod fsverity; |
| 27 | |
Victor Hsieh | 1a8cd04 | 2021-09-03 16:29:45 -0700 | [diff] [blame] | 28 | use anyhow::{bail, Result}; |
Alan Stokes | cd359bb | 2021-10-08 18:22:42 +0100 | [diff] [blame] | 29 | use binder_common::rpc_server::run_rpc_server; |
Victor Hsieh | b0f5fc8 | 2021-10-15 17:24:19 -0700 | [diff] [blame] | 30 | use log::debug; |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame^] | 31 | use nix::dir::Dir; |
Victor Hsieh | 42cc776 | 2021-01-25 16:44:19 -0800 | [diff] [blame] | 32 | use std::collections::BTreeMap; |
Victor Hsieh | 42cc776 | 2021-01-25 16:44:19 -0800 | [diff] [blame] | 33 | use std::fs::File; |
Victor Hsieh | b0f5fc8 | 2021-10-15 17:24:19 -0700 | [diff] [blame] | 34 | use std::os::unix::io::FromRawFd; |
Victor Hsieh | 42cc776 | 2021-01-25 16:44:19 -0800 | [diff] [blame] | 35 | |
Victor Hsieh | b0f5fc8 | 2021-10-15 17:24:19 -0700 | [diff] [blame] | 36 | use aidl::{FdConfig, FdService}; |
Victor Hsieh | 42cc776 | 2021-01-25 16:44:19 -0800 | [diff] [blame] | 37 | |
Victor Hsieh | 2445e33 | 2021-06-04 16:44:53 -0700 | [diff] [blame] | 38 | const RPC_SERVICE_PORT: u32 = 3264; // TODO: support dynamic port for multiple fd_server instances |
Victor Hsieh | 42cc776 | 2021-01-25 16:44:19 -0800 | [diff] [blame] | 39 | |
Victor Hsieh | 42cc776 | 2021-01-25 16:44:19 -0800 | [diff] [blame] | 40 | fn is_fd_valid(fd: i32) -> bool { |
| 41 | // SAFETY: a query-only syscall |
| 42 | let retval = unsafe { libc::fcntl(fd, libc::F_GETFD) }; |
| 43 | retval >= 0 |
| 44 | } |
| 45 | |
| 46 | fn fd_to_file(fd: i32) -> Result<File> { |
| 47 | if !is_fd_valid(fd) { |
| 48 | bail!("Bad FD: {}", fd); |
| 49 | } |
| 50 | // SAFETY: The caller is supposed to provide valid FDs to this process. |
| 51 | Ok(unsafe { File::from_raw_fd(fd) }) |
| 52 | } |
| 53 | |
Victor Hsieh | 60acfd3 | 2021-02-23 13:08:13 -0800 | [diff] [blame] | 54 | fn parse_arg_ro_fds(arg: &str) -> Result<(i32, FdConfig)> { |
Victor Hsieh | 42cc776 | 2021-01-25 16:44:19 -0800 | [diff] [blame] | 55 | let result: Result<Vec<i32>, _> = arg.split(':').map(|x| x.parse::<i32>()).collect(); |
| 56 | let fds = result?; |
| 57 | if fds.len() > 3 { |
| 58 | bail!("Too many options: {}", arg); |
| 59 | } |
Victor Hsieh | 42cc776 | 2021-01-25 16:44:19 -0800 | [diff] [blame] | 60 | Ok(( |
| 61 | fds[0], |
Victor Hsieh | 60acfd3 | 2021-02-23 13:08:13 -0800 | [diff] [blame] | 62 | FdConfig::Readonly { |
Victor Hsieh | 42cc776 | 2021-01-25 16:44:19 -0800 | [diff] [blame] | 63 | file: fd_to_file(fds[0])?, |
Victor Hsieh | 60acfd3 | 2021-02-23 13:08:13 -0800 | [diff] [blame] | 64 | // Alternative Merkle tree, if provided |
| 65 | alt_merkle_tree: fds.get(1).map(|fd| fd_to_file(*fd)).transpose()?, |
| 66 | // Alternative signature, if provided |
| 67 | alt_signature: fds.get(2).map(|fd| fd_to_file(*fd)).transpose()?, |
Victor Hsieh | 42cc776 | 2021-01-25 16:44:19 -0800 | [diff] [blame] | 68 | }, |
| 69 | )) |
| 70 | } |
| 71 | |
Victor Hsieh | 60acfd3 | 2021-02-23 13:08:13 -0800 | [diff] [blame] | 72 | fn parse_arg_rw_fds(arg: &str) -> Result<(i32, FdConfig)> { |
| 73 | let fd = arg.parse::<i32>()?; |
| 74 | let file = fd_to_file(fd)?; |
| 75 | if file.metadata()?.len() > 0 { |
| 76 | bail!("File is expected to be empty"); |
| 77 | } |
| 78 | Ok((fd, FdConfig::ReadWrite(file))) |
| 79 | } |
| 80 | |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame^] | 81 | fn parse_arg_rw_dirs(arg: &str) -> Result<(i32, FdConfig)> { |
| 82 | let fd = arg.parse::<i32>()?; |
| 83 | |
| 84 | Ok((fd, FdConfig::OutputDir(Dir::from_fd(fd)?))) |
| 85 | } |
| 86 | |
Alan Stokes | e1b6e1c | 2021-10-01 12:44:49 +0100 | [diff] [blame] | 87 | struct Args { |
| 88 | fd_pool: BTreeMap<i32, FdConfig>, |
| 89 | ready_fd: Option<File>, |
| 90 | } |
| 91 | |
| 92 | fn parse_args() -> Result<Args> { |
Victor Hsieh | 42cc776 | 2021-01-25 16:44:19 -0800 | [diff] [blame] | 93 | #[rustfmt::skip] |
| 94 | let matches = clap::App::new("fd_server") |
| 95 | .arg(clap::Arg::with_name("ro-fds") |
| 96 | .long("ro-fds") |
Victor Hsieh | 60acfd3 | 2021-02-23 13:08:13 -0800 | [diff] [blame] | 97 | .multiple(true) |
| 98 | .number_of_values(1)) |
| 99 | .arg(clap::Arg::with_name("rw-fds") |
| 100 | .long("rw-fds") |
Victor Hsieh | 42cc776 | 2021-01-25 16:44:19 -0800 | [diff] [blame] | 101 | .multiple(true) |
| 102 | .number_of_values(1)) |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame^] | 103 | .arg(clap::Arg::with_name("rw-dirs") |
| 104 | .long("rw-dirs") |
| 105 | .multiple(true) |
| 106 | .number_of_values(1)) |
Alan Stokes | e1b6e1c | 2021-10-01 12:44:49 +0100 | [diff] [blame] | 107 | .arg(clap::Arg::with_name("ready-fd") |
| 108 | .long("ready-fd") |
| 109 | .takes_value(true)) |
Victor Hsieh | 42cc776 | 2021-01-25 16:44:19 -0800 | [diff] [blame] | 110 | .get_matches(); |
| 111 | |
| 112 | let mut fd_pool = BTreeMap::new(); |
| 113 | if let Some(args) = matches.values_of("ro-fds") { |
| 114 | for arg in args { |
| 115 | let (fd, config) = parse_arg_ro_fds(arg)?; |
| 116 | fd_pool.insert(fd, config); |
| 117 | } |
| 118 | } |
Victor Hsieh | 60acfd3 | 2021-02-23 13:08:13 -0800 | [diff] [blame] | 119 | if let Some(args) = matches.values_of("rw-fds") { |
| 120 | for arg in args { |
| 121 | let (fd, config) = parse_arg_rw_fds(arg)?; |
| 122 | fd_pool.insert(fd, config); |
| 123 | } |
| 124 | } |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame^] | 125 | if let Some(args) = matches.values_of("rw-dirs") { |
| 126 | for arg in args { |
| 127 | let (fd, config) = parse_arg_rw_dirs(arg)?; |
| 128 | fd_pool.insert(fd, config); |
| 129 | } |
| 130 | } |
Alan Stokes | e1b6e1c | 2021-10-01 12:44:49 +0100 | [diff] [blame] | 131 | let ready_fd = if let Some(arg) = matches.value_of("ready-fd") { |
| 132 | let fd = arg.parse::<i32>()?; |
| 133 | Some(fd_to_file(fd)?) |
| 134 | } else { |
| 135 | None |
| 136 | }; |
| 137 | Ok(Args { fd_pool, ready_fd }) |
Victor Hsieh | 42cc776 | 2021-01-25 16:44:19 -0800 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | fn main() -> Result<()> { |
Victor Hsieh | 60acfd3 | 2021-02-23 13:08:13 -0800 | [diff] [blame] | 141 | android_logger::init_once( |
| 142 | android_logger::Config::default().with_tag("fd_server").with_min_level(log::Level::Debug), |
| 143 | ); |
| 144 | |
Alan Stokes | e1b6e1c | 2021-10-01 12:44:49 +0100 | [diff] [blame] | 145 | let args = parse_args()?; |
Alan Stokes | cd359bb | 2021-10-08 18:22:42 +0100 | [diff] [blame] | 146 | let service = FdService::new_binder(args.fd_pool).as_binder(); |
Alan Stokes | e1b6e1c | 2021-10-01 12:44:49 +0100 | [diff] [blame] | 147 | |
Victor Hsieh | 1a8cd04 | 2021-09-03 16:29:45 -0700 | [diff] [blame] | 148 | debug!("fd_server is starting as a rpc service."); |
Alan Stokes | cd359bb | 2021-10-08 18:22:42 +0100 | [diff] [blame] | 149 | |
| 150 | let mut ready_fd = args.ready_fd; |
| 151 | let retval = run_rpc_server(service, RPC_SERVICE_PORT, || { |
| 152 | debug!("fd_server is ready"); |
| 153 | // Close the ready-fd if we were given one to signal our readiness. |
| 154 | drop(ready_fd.take()); |
| 155 | }); |
| 156 | |
Victor Hsieh | 1a8cd04 | 2021-09-03 16:29:45 -0700 | [diff] [blame] | 157 | if retval { |
| 158 | debug!("RPC server has shut down gracefully"); |
| 159 | Ok(()) |
Victor Hsieh | 2445e33 | 2021-06-04 16:44:53 -0700 | [diff] [blame] | 160 | } else { |
Victor Hsieh | 1a8cd04 | 2021-09-03 16:29:45 -0700 | [diff] [blame] | 161 | bail!("Premature termination of RPC server"); |
Victor Hsieh | 2445e33 | 2021-06-04 16:44:53 -0700 | [diff] [blame] | 162 | } |
Victor Hsieh | 42cc776 | 2021-01-25 16:44:19 -0800 | [diff] [blame] | 163 | } |