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 | |
Victor Hsieh | 1a8cd04 | 2021-09-03 16:29:45 -0700 | [diff] [blame] | 27 | use anyhow::{bail, Result}; |
Victor Hsieh | 543d199 | 2022-09-14 15:35:51 -0700 | [diff] [blame] | 28 | use clap::Parser; |
Victor Hsieh | b0f5fc8 | 2021-10-15 17:24:19 -0700 | [diff] [blame] | 29 | use log::debug; |
Victor Hsieh | bf944d7 | 2022-03-07 23:46:43 +0000 | [diff] [blame] | 30 | use nix::sys::stat::{umask, Mode}; |
David Brazdil | 671e614 | 2022-11-16 11:47:27 +0000 | [diff] [blame] | 31 | use rpcbinder::RpcServer; |
Jiyong Park | 070b462 | 2024-09-11 08:29:22 +0900 | [diff] [blame^] | 32 | use rustutils::inherited_fd::take_fd_ownership; |
Victor Hsieh | 42cc776 | 2021-01-25 16:44:19 -0800 | [diff] [blame] | 33 | use std::collections::BTreeMap; |
Victor Hsieh | 42cc776 | 2021-01-25 16:44:19 -0800 | [diff] [blame] | 34 | use std::fs::File; |
Jiyong Park | 070b462 | 2024-09-11 08:29:22 +0900 | [diff] [blame^] | 35 | use std::os::unix::io::OwnedFd; |
Victor Hsieh | 42cc776 | 2021-01-25 16:44:19 -0800 | [diff] [blame] | 36 | |
Victor Hsieh | b0f5fc8 | 2021-10-15 17:24:19 -0700 | [diff] [blame] | 37 | use aidl::{FdConfig, FdService}; |
Inseob Kim | c0886c2 | 2021-12-13 17:41:24 +0900 | [diff] [blame] | 38 | use authfs_fsverity_metadata::parse_fsverity_metadata; |
Victor Hsieh | 42cc776 | 2021-01-25 16:44:19 -0800 | [diff] [blame] | 39 | |
David Brazdil | 3238da4 | 2022-11-18 10:04:51 +0000 | [diff] [blame] | 40 | // TODO(b/259920193): support dynamic port for multiple fd_server instances |
| 41 | const RPC_SERVICE_PORT: u32 = 3264; |
Victor Hsieh | 42cc776 | 2021-01-25 16:44:19 -0800 | [diff] [blame] | 42 | |
Victor Hsieh | 60acfd3 | 2021-02-23 13:08:13 -0800 | [diff] [blame] | 43 | fn parse_arg_ro_fds(arg: &str) -> Result<(i32, FdConfig)> { |
Victor Hsieh | 42cc776 | 2021-01-25 16:44:19 -0800 | [diff] [blame] | 44 | let result: Result<Vec<i32>, _> = arg.split(':').map(|x| x.parse::<i32>()).collect(); |
| 45 | let fds = result?; |
Inseob Kim | c0886c2 | 2021-12-13 17:41:24 +0900 | [diff] [blame] | 46 | if fds.len() > 2 { |
Victor Hsieh | 42cc776 | 2021-01-25 16:44:19 -0800 | [diff] [blame] | 47 | bail!("Too many options: {}", arg); |
| 48 | } |
Victor Hsieh | 42cc776 | 2021-01-25 16:44:19 -0800 | [diff] [blame] | 49 | Ok(( |
| 50 | fds[0], |
Victor Hsieh | 60acfd3 | 2021-02-23 13:08:13 -0800 | [diff] [blame] | 51 | FdConfig::Readonly { |
Jiyong Park | 070b462 | 2024-09-11 08:29:22 +0900 | [diff] [blame^] | 52 | file: take_fd_ownership(fds[0])?.into(), |
Inseob Kim | c0886c2 | 2021-12-13 17:41:24 +0900 | [diff] [blame] | 53 | // Alternative metadata source, if provided |
| 54 | alt_metadata: fds |
| 55 | .get(1) |
Jiyong Park | 070b462 | 2024-09-11 08:29:22 +0900 | [diff] [blame^] | 56 | .map(|fd| take_fd_ownership(*fd)) |
Inseob Kim | c0886c2 | 2021-12-13 17:41:24 +0900 | [diff] [blame] | 57 | .transpose()? |
Jiyong Park | 070b462 | 2024-09-11 08:29:22 +0900 | [diff] [blame^] | 58 | .and_then(|f| parse_fsverity_metadata(f.into()).ok()), |
Victor Hsieh | 42cc776 | 2021-01-25 16:44:19 -0800 | [diff] [blame] | 59 | }, |
| 60 | )) |
| 61 | } |
| 62 | |
Victor Hsieh | 543d199 | 2022-09-14 15:35:51 -0700 | [diff] [blame] | 63 | #[derive(Parser)] |
Alan Stokes | e1b6e1c | 2021-10-01 12:44:49 +0100 | [diff] [blame] | 64 | struct Args { |
Victor Hsieh | 543d199 | 2022-09-14 15:35:51 -0700 | [diff] [blame] | 65 | /// Read-only FD of file, with optional FD of corresponding .fsv_meta, joined with a ':'. |
| 66 | /// Example: "1:2", "3". |
| 67 | #[clap(long)] |
| 68 | ro_fds: Vec<String>, |
| 69 | |
| 70 | /// Read-writable FD of file |
| 71 | #[clap(long)] |
| 72 | rw_fds: Vec<i32>, |
| 73 | |
| 74 | /// Read-only FD of directory |
| 75 | #[clap(long)] |
| 76 | ro_dirs: Vec<i32>, |
| 77 | |
| 78 | /// Read-writable FD of directory |
| 79 | #[clap(long)] |
| 80 | rw_dirs: Vec<i32>, |
| 81 | |
| 82 | /// A pipe FD for signaling the other end once ready |
| 83 | #[clap(long)] |
| 84 | ready_fd: Option<i32>, |
Alan Stokes | e1b6e1c | 2021-10-01 12:44:49 +0100 | [diff] [blame] | 85 | } |
| 86 | |
Victor Hsieh | 543d199 | 2022-09-14 15:35:51 -0700 | [diff] [blame] | 87 | /// Convert argument strings and integers to a form that is easier to use and handles ownership. |
| 88 | fn convert_args(args: Args) -> Result<(BTreeMap<i32, FdConfig>, Option<OwnedFd>)> { |
Victor Hsieh | 42cc776 | 2021-01-25 16:44:19 -0800 | [diff] [blame] | 89 | let mut fd_pool = BTreeMap::new(); |
Victor Hsieh | 543d199 | 2022-09-14 15:35:51 -0700 | [diff] [blame] | 90 | for arg in args.ro_fds { |
| 91 | let (fd, config) = parse_arg_ro_fds(&arg)?; |
| 92 | fd_pool.insert(fd, config); |
Victor Hsieh | 42cc776 | 2021-01-25 16:44:19 -0800 | [diff] [blame] | 93 | } |
Victor Hsieh | 543d199 | 2022-09-14 15:35:51 -0700 | [diff] [blame] | 94 | for fd in args.rw_fds { |
Jiyong Park | 070b462 | 2024-09-11 08:29:22 +0900 | [diff] [blame^] | 95 | let file: File = take_fd_ownership(fd)?.into(); |
Victor Hsieh | 543d199 | 2022-09-14 15:35:51 -0700 | [diff] [blame] | 96 | if file.metadata()?.len() > 0 { |
| 97 | bail!("File is expected to be empty"); |
Victor Hsieh | 60acfd3 | 2021-02-23 13:08:13 -0800 | [diff] [blame] | 98 | } |
Victor Hsieh | 543d199 | 2022-09-14 15:35:51 -0700 | [diff] [blame] | 99 | fd_pool.insert(fd, FdConfig::ReadWrite(file)); |
Victor Hsieh | 60acfd3 | 2021-02-23 13:08:13 -0800 | [diff] [blame] | 100 | } |
Victor Hsieh | 543d199 | 2022-09-14 15:35:51 -0700 | [diff] [blame] | 101 | for fd in args.ro_dirs { |
Jiyong Park | 070b462 | 2024-09-11 08:29:22 +0900 | [diff] [blame^] | 102 | fd_pool.insert(fd, FdConfig::InputDir(take_fd_ownership(fd)?)); |
Victor Hsieh | 559b927 | 2021-11-08 15:15:14 -0800 | [diff] [blame] | 103 | } |
Victor Hsieh | 543d199 | 2022-09-14 15:35:51 -0700 | [diff] [blame] | 104 | for fd in args.rw_dirs { |
Jiyong Park | 070b462 | 2024-09-11 08:29:22 +0900 | [diff] [blame^] | 105 | fd_pool.insert(fd, FdConfig::OutputDir(take_fd_ownership(fd)?)); |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 106 | } |
Jiyong Park | 070b462 | 2024-09-11 08:29:22 +0900 | [diff] [blame^] | 107 | let ready_fd = args.ready_fd.map(take_fd_ownership).transpose()?; |
Victor Hsieh | 543d199 | 2022-09-14 15:35:51 -0700 | [diff] [blame] | 108 | Ok((fd_pool, ready_fd)) |
Victor Hsieh | 42cc776 | 2021-01-25 16:44:19 -0800 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | fn main() -> Result<()> { |
Jiyong Park | 070b462 | 2024-09-11 08:29:22 +0900 | [diff] [blame^] | 112 | // SAFETY: This is very early in the process. Nobody has taken ownership of the inherited FDs |
| 113 | // yet. |
| 114 | unsafe { rustutils::inherited_fd::init_once()? }; |
| 115 | |
Victor Hsieh | 60acfd3 | 2021-02-23 13:08:13 -0800 | [diff] [blame] | 116 | android_logger::init_once( |
Jeff Vander Stoep | 57da157 | 2024-01-31 10:52:16 +0100 | [diff] [blame] | 117 | android_logger::Config::default() |
| 118 | .with_tag("fd_server") |
| 119 | .with_max_level(log::LevelFilter::Debug), |
Victor Hsieh | 60acfd3 | 2021-02-23 13:08:13 -0800 | [diff] [blame] | 120 | ); |
| 121 | |
Victor Hsieh | 543d199 | 2022-09-14 15:35:51 -0700 | [diff] [blame] | 122 | let args = Args::parse(); |
| 123 | let (fd_pool, mut ready_fd) = convert_args(args)?; |
Victor Hsieh | 8e28511 | 2021-12-10 11:17:26 -0800 | [diff] [blame] | 124 | |
| 125 | // Allow open/create/mkdir from authfs to create with expecting mode. It's possible to still |
| 126 | // use a custom mask on creation, then report the actual file mode back to authfs. But there |
| 127 | // is no demand now. |
| 128 | let old_umask = umask(Mode::empty()); |
| 129 | debug!("Setting umask to 0 (old: {:03o})", old_umask.bits()); |
| 130 | |
Victor Hsieh | 1a8cd04 | 2021-09-03 16:29:45 -0700 | [diff] [blame] | 131 | debug!("fd_server is starting as a rpc service."); |
David Brazdil | 671e614 | 2022-11-16 11:47:27 +0000 | [diff] [blame] | 132 | let service = FdService::new_binder(fd_pool).as_binder(); |
David Brazdil | 3238da4 | 2022-11-18 10:04:51 +0000 | [diff] [blame] | 133 | // TODO(b/259920193): Only accept connections from the intended guest VM. |
| 134 | let server = RpcServer::new_vsock(service, libc::VMADDR_CID_ANY, RPC_SERVICE_PORT)?; |
David Brazdil | 671e614 | 2022-11-16 11:47:27 +0000 | [diff] [blame] | 135 | debug!("fd_server is ready"); |
Alan Stokes | cd359bb | 2021-10-08 18:22:42 +0100 | [diff] [blame] | 136 | |
David Brazdil | 671e614 | 2022-11-16 11:47:27 +0000 | [diff] [blame] | 137 | // Close the ready-fd if we were given one to signal our readiness. |
| 138 | drop(ready_fd.take()); |
| 139 | |
| 140 | server.join(); |
| 141 | Ok(()) |
Victor Hsieh | 42cc776 | 2021-01-25 16:44:19 -0800 | [diff] [blame] | 142 | } |
Andrew Walbran | da8786d | 2022-12-01 14:54:27 +0000 | [diff] [blame] | 143 | |
| 144 | #[cfg(test)] |
| 145 | mod tests { |
| 146 | use super::*; |
| 147 | use clap::CommandFactory; |
| 148 | |
| 149 | #[test] |
| 150 | fn verify_args() { |
| 151 | // Check that the command parsing has been configured in a valid way. |
| 152 | Args::command().debug_assert(); |
| 153 | } |
| 154 | } |