blob: bbcd49fe135d95d59b43463a413960664dcbc639 [file] [log] [blame]
Victor Hsieh42cc7762021-01-25 16:44:19 -08001/*
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 Hsieh1a8cd042021-09-03 16:29:45 -070017//! This program is a constrained file/FD server to serve file requests through a remote binder
Victor Hsieh42cc7762021-01-25 16:44:19 -080018//! 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 Hsieh42cc7762021-01-25 16:44:19 -080024
Victor Hsiehb0f5fc82021-10-15 17:24:19 -070025mod aidl;
Victor Hsieh4dc85c92021-03-15 11:01:23 -070026mod fsverity;
27
Victor Hsieh1a8cd042021-09-03 16:29:45 -070028use anyhow::{bail, Result};
Alan Stokescd359bb2021-10-08 18:22:42 +010029use binder_common::rpc_server::run_rpc_server;
Victor Hsiehb0f5fc82021-10-15 17:24:19 -070030use log::debug;
Victor Hsieh45636232021-10-15 17:52:51 -070031use nix::dir::Dir;
Victor Hsieh42cc7762021-01-25 16:44:19 -080032use std::collections::BTreeMap;
Victor Hsieh42cc7762021-01-25 16:44:19 -080033use std::fs::File;
Victor Hsiehb0f5fc82021-10-15 17:24:19 -070034use std::os::unix::io::FromRawFd;
Victor Hsieh42cc7762021-01-25 16:44:19 -080035
Victor Hsiehb0f5fc82021-10-15 17:24:19 -070036use aidl::{FdConfig, FdService};
Victor Hsieh42cc7762021-01-25 16:44:19 -080037
Victor Hsieh2445e332021-06-04 16:44:53 -070038const RPC_SERVICE_PORT: u32 = 3264; // TODO: support dynamic port for multiple fd_server instances
Victor Hsieh42cc7762021-01-25 16:44:19 -080039
Victor Hsieh42cc7762021-01-25 16:44:19 -080040fn 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
46fn 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 Hsieh60acfd32021-02-23 13:08:13 -080054fn parse_arg_ro_fds(arg: &str) -> Result<(i32, FdConfig)> {
Victor Hsieh42cc7762021-01-25 16:44:19 -080055 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 Hsieh42cc7762021-01-25 16:44:19 -080060 Ok((
61 fds[0],
Victor Hsieh60acfd32021-02-23 13:08:13 -080062 FdConfig::Readonly {
Victor Hsieh42cc7762021-01-25 16:44:19 -080063 file: fd_to_file(fds[0])?,
Victor Hsieh60acfd32021-02-23 13:08:13 -080064 // 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 Hsieh42cc7762021-01-25 16:44:19 -080068 },
69 ))
70}
71
Victor Hsieh60acfd32021-02-23 13:08:13 -080072fn 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 Hsieh45636232021-10-15 17:52:51 -070081fn 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 Stokese1b6e1c2021-10-01 12:44:49 +010087struct Args {
88 fd_pool: BTreeMap<i32, FdConfig>,
89 ready_fd: Option<File>,
90}
91
92fn parse_args() -> Result<Args> {
Victor Hsieh42cc7762021-01-25 16:44:19 -080093 #[rustfmt::skip]
94 let matches = clap::App::new("fd_server")
95 .arg(clap::Arg::with_name("ro-fds")
96 .long("ro-fds")
Victor Hsieh60acfd32021-02-23 13:08:13 -080097 .multiple(true)
98 .number_of_values(1))
99 .arg(clap::Arg::with_name("rw-fds")
100 .long("rw-fds")
Victor Hsieh42cc7762021-01-25 16:44:19 -0800101 .multiple(true)
102 .number_of_values(1))
Victor Hsieh45636232021-10-15 17:52:51 -0700103 .arg(clap::Arg::with_name("rw-dirs")
104 .long("rw-dirs")
105 .multiple(true)
106 .number_of_values(1))
Alan Stokese1b6e1c2021-10-01 12:44:49 +0100107 .arg(clap::Arg::with_name("ready-fd")
108 .long("ready-fd")
109 .takes_value(true))
Victor Hsieh42cc7762021-01-25 16:44:19 -0800110 .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 Hsieh60acfd32021-02-23 13:08:13 -0800119 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 Hsieh45636232021-10-15 17:52:51 -0700125 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 Stokese1b6e1c2021-10-01 12:44:49 +0100131 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 Hsieh42cc7762021-01-25 16:44:19 -0800138}
139
140fn main() -> Result<()> {
Victor Hsieh60acfd32021-02-23 13:08:13 -0800141 android_logger::init_once(
142 android_logger::Config::default().with_tag("fd_server").with_min_level(log::Level::Debug),
143 );
144
Alan Stokese1b6e1c2021-10-01 12:44:49 +0100145 let args = parse_args()?;
Alan Stokescd359bb2021-10-08 18:22:42 +0100146 let service = FdService::new_binder(args.fd_pool).as_binder();
Alan Stokese1b6e1c2021-10-01 12:44:49 +0100147
Victor Hsieh1a8cd042021-09-03 16:29:45 -0700148 debug!("fd_server is starting as a rpc service.");
Alan Stokescd359bb2021-10-08 18:22:42 +0100149
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 Hsieh1a8cd042021-09-03 16:29:45 -0700157 if retval {
158 debug!("RPC server has shut down gracefully");
159 Ok(())
Victor Hsieh2445e332021-06-04 16:44:53 -0700160 } else {
Victor Hsieh1a8cd042021-09-03 16:29:45 -0700161 bail!("Premature termination of RPC server");
Victor Hsieh2445e332021-06-04 16:44:53 -0700162 }
Victor Hsieh42cc7762021-01-25 16:44:19 -0800163}