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 | 8e28511 | 2021-12-10 11:17:26 -0800 | [diff] [blame] | 31 | use nix::{dir::Dir, sys::stat::umask, sys::stat::Mode}; |
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}; |
Inseob Kim | c0886c2 | 2021-12-13 17:41:24 +0900 | [diff] [blame^] | 37 | use authfs_fsverity_metadata::parse_fsverity_metadata; |
Victor Hsieh | 42cc776 | 2021-01-25 16:44:19 -0800 | [diff] [blame] | 38 | |
Victor Hsieh | 2445e33 | 2021-06-04 16:44:53 -0700 | [diff] [blame] | 39 | 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] | 40 | |
Victor Hsieh | 42cc776 | 2021-01-25 16:44:19 -0800 | [diff] [blame] | 41 | fn is_fd_valid(fd: i32) -> bool { |
| 42 | // SAFETY: a query-only syscall |
| 43 | let retval = unsafe { libc::fcntl(fd, libc::F_GETFD) }; |
| 44 | retval >= 0 |
| 45 | } |
| 46 | |
| 47 | fn fd_to_file(fd: i32) -> Result<File> { |
| 48 | if !is_fd_valid(fd) { |
| 49 | bail!("Bad FD: {}", fd); |
| 50 | } |
| 51 | // SAFETY: The caller is supposed to provide valid FDs to this process. |
| 52 | Ok(unsafe { File::from_raw_fd(fd) }) |
| 53 | } |
| 54 | |
Victor Hsieh | 60acfd3 | 2021-02-23 13:08:13 -0800 | [diff] [blame] | 55 | fn parse_arg_ro_fds(arg: &str) -> Result<(i32, FdConfig)> { |
Victor Hsieh | 42cc776 | 2021-01-25 16:44:19 -0800 | [diff] [blame] | 56 | let result: Result<Vec<i32>, _> = arg.split(':').map(|x| x.parse::<i32>()).collect(); |
| 57 | let fds = result?; |
Inseob Kim | c0886c2 | 2021-12-13 17:41:24 +0900 | [diff] [blame^] | 58 | if fds.len() > 2 { |
Victor Hsieh | 42cc776 | 2021-01-25 16:44:19 -0800 | [diff] [blame] | 59 | bail!("Too many options: {}", arg); |
| 60 | } |
Victor Hsieh | 42cc776 | 2021-01-25 16:44:19 -0800 | [diff] [blame] | 61 | Ok(( |
| 62 | fds[0], |
Victor Hsieh | 60acfd3 | 2021-02-23 13:08:13 -0800 | [diff] [blame] | 63 | FdConfig::Readonly { |
Victor Hsieh | 42cc776 | 2021-01-25 16:44:19 -0800 | [diff] [blame] | 64 | file: fd_to_file(fds[0])?, |
Inseob Kim | c0886c2 | 2021-12-13 17:41:24 +0900 | [diff] [blame^] | 65 | // Alternative metadata source, if provided |
| 66 | alt_metadata: fds |
| 67 | .get(1) |
| 68 | .map(|fd| fd_to_file(*fd)) |
| 69 | .transpose()? |
| 70 | .and_then(|f| parse_fsverity_metadata(f).ok()), |
Victor Hsieh | 42cc776 | 2021-01-25 16:44:19 -0800 | [diff] [blame] | 71 | }, |
| 72 | )) |
| 73 | } |
| 74 | |
Victor Hsieh | 60acfd3 | 2021-02-23 13:08:13 -0800 | [diff] [blame] | 75 | fn parse_arg_rw_fds(arg: &str) -> Result<(i32, FdConfig)> { |
| 76 | let fd = arg.parse::<i32>()?; |
| 77 | let file = fd_to_file(fd)?; |
| 78 | if file.metadata()?.len() > 0 { |
| 79 | bail!("File is expected to be empty"); |
| 80 | } |
| 81 | Ok((fd, FdConfig::ReadWrite(file))) |
| 82 | } |
| 83 | |
Victor Hsieh | 559b927 | 2021-11-08 15:15:14 -0800 | [diff] [blame] | 84 | fn parse_arg_ro_dirs(arg: &str) -> Result<(i32, FdConfig)> { |
| 85 | let fd = arg.parse::<i32>()?; |
| 86 | Ok((fd, FdConfig::InputDir(Dir::from_fd(fd)?))) |
| 87 | } |
| 88 | |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 89 | fn parse_arg_rw_dirs(arg: &str) -> Result<(i32, FdConfig)> { |
| 90 | let fd = arg.parse::<i32>()?; |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 91 | Ok((fd, FdConfig::OutputDir(Dir::from_fd(fd)?))) |
| 92 | } |
| 93 | |
Alan Stokes | e1b6e1c | 2021-10-01 12:44:49 +0100 | [diff] [blame] | 94 | struct Args { |
| 95 | fd_pool: BTreeMap<i32, FdConfig>, |
| 96 | ready_fd: Option<File>, |
| 97 | } |
| 98 | |
| 99 | fn parse_args() -> Result<Args> { |
Victor Hsieh | 42cc776 | 2021-01-25 16:44:19 -0800 | [diff] [blame] | 100 | #[rustfmt::skip] |
| 101 | let matches = clap::App::new("fd_server") |
| 102 | .arg(clap::Arg::with_name("ro-fds") |
| 103 | .long("ro-fds") |
Victor Hsieh | 60acfd3 | 2021-02-23 13:08:13 -0800 | [diff] [blame] | 104 | .multiple(true) |
| 105 | .number_of_values(1)) |
| 106 | .arg(clap::Arg::with_name("rw-fds") |
| 107 | .long("rw-fds") |
Victor Hsieh | 42cc776 | 2021-01-25 16:44:19 -0800 | [diff] [blame] | 108 | .multiple(true) |
| 109 | .number_of_values(1)) |
Victor Hsieh | 559b927 | 2021-11-08 15:15:14 -0800 | [diff] [blame] | 110 | .arg(clap::Arg::with_name("ro-dirs") |
| 111 | .long("ro-dirs") |
| 112 | .multiple(true) |
| 113 | .number_of_values(1)) |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 114 | .arg(clap::Arg::with_name("rw-dirs") |
| 115 | .long("rw-dirs") |
| 116 | .multiple(true) |
| 117 | .number_of_values(1)) |
Alan Stokes | e1b6e1c | 2021-10-01 12:44:49 +0100 | [diff] [blame] | 118 | .arg(clap::Arg::with_name("ready-fd") |
| 119 | .long("ready-fd") |
| 120 | .takes_value(true)) |
Victor Hsieh | 42cc776 | 2021-01-25 16:44:19 -0800 | [diff] [blame] | 121 | .get_matches(); |
| 122 | |
| 123 | let mut fd_pool = BTreeMap::new(); |
| 124 | if let Some(args) = matches.values_of("ro-fds") { |
| 125 | for arg in args { |
| 126 | let (fd, config) = parse_arg_ro_fds(arg)?; |
| 127 | fd_pool.insert(fd, config); |
| 128 | } |
| 129 | } |
Victor Hsieh | 60acfd3 | 2021-02-23 13:08:13 -0800 | [diff] [blame] | 130 | if let Some(args) = matches.values_of("rw-fds") { |
| 131 | for arg in args { |
| 132 | let (fd, config) = parse_arg_rw_fds(arg)?; |
| 133 | fd_pool.insert(fd, config); |
| 134 | } |
| 135 | } |
Victor Hsieh | 559b927 | 2021-11-08 15:15:14 -0800 | [diff] [blame] | 136 | if let Some(args) = matches.values_of("ro-dirs") { |
| 137 | for arg in args { |
| 138 | let (fd, config) = parse_arg_ro_dirs(arg)?; |
| 139 | fd_pool.insert(fd, config); |
| 140 | } |
| 141 | } |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 142 | if let Some(args) = matches.values_of("rw-dirs") { |
| 143 | for arg in args { |
| 144 | let (fd, config) = parse_arg_rw_dirs(arg)?; |
| 145 | fd_pool.insert(fd, config); |
| 146 | } |
| 147 | } |
Alan Stokes | e1b6e1c | 2021-10-01 12:44:49 +0100 | [diff] [blame] | 148 | let ready_fd = if let Some(arg) = matches.value_of("ready-fd") { |
| 149 | let fd = arg.parse::<i32>()?; |
| 150 | Some(fd_to_file(fd)?) |
| 151 | } else { |
| 152 | None |
| 153 | }; |
| 154 | Ok(Args { fd_pool, ready_fd }) |
Victor Hsieh | 42cc776 | 2021-01-25 16:44:19 -0800 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | fn main() -> Result<()> { |
Victor Hsieh | 60acfd3 | 2021-02-23 13:08:13 -0800 | [diff] [blame] | 158 | android_logger::init_once( |
| 159 | android_logger::Config::default().with_tag("fd_server").with_min_level(log::Level::Debug), |
| 160 | ); |
| 161 | |
Alan Stokes | e1b6e1c | 2021-10-01 12:44:49 +0100 | [diff] [blame] | 162 | let args = parse_args()?; |
Victor Hsieh | 8e28511 | 2021-12-10 11:17:26 -0800 | [diff] [blame] | 163 | |
| 164 | // Allow open/create/mkdir from authfs to create with expecting mode. It's possible to still |
| 165 | // use a custom mask on creation, then report the actual file mode back to authfs. But there |
| 166 | // is no demand now. |
| 167 | let old_umask = umask(Mode::empty()); |
| 168 | debug!("Setting umask to 0 (old: {:03o})", old_umask.bits()); |
| 169 | |
Alan Stokes | cd359bb | 2021-10-08 18:22:42 +0100 | [diff] [blame] | 170 | let service = FdService::new_binder(args.fd_pool).as_binder(); |
Victor Hsieh | 1a8cd04 | 2021-09-03 16:29:45 -0700 | [diff] [blame] | 171 | debug!("fd_server is starting as a rpc service."); |
Alan Stokes | cd359bb | 2021-10-08 18:22:42 +0100 | [diff] [blame] | 172 | let mut ready_fd = args.ready_fd; |
| 173 | let retval = run_rpc_server(service, RPC_SERVICE_PORT, || { |
| 174 | debug!("fd_server is ready"); |
| 175 | // Close the ready-fd if we were given one to signal our readiness. |
| 176 | drop(ready_fd.take()); |
| 177 | }); |
| 178 | |
Victor Hsieh | 1a8cd04 | 2021-09-03 16:29:45 -0700 | [diff] [blame] | 179 | if retval { |
| 180 | debug!("RPC server has shut down gracefully"); |
| 181 | Ok(()) |
Victor Hsieh | 2445e33 | 2021-06-04 16:44:53 -0700 | [diff] [blame] | 182 | } else { |
Victor Hsieh | 1a8cd04 | 2021-09-03 16:29:45 -0700 | [diff] [blame] | 183 | bail!("Premature termination of RPC server"); |
Victor Hsieh | 2445e33 | 2021-06-04 16:44:53 -0700 | [diff] [blame] | 184 | } |
Victor Hsieh | 42cc776 | 2021-01-25 16:44:19 -0800 | [diff] [blame] | 185 | } |