blob: f5a3cba140dbbebfc8e3b5b488722a5767658937 [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 Hsieh559b9272021-11-08 15:15:14 -080081fn parse_arg_ro_dirs(arg: &str) -> Result<(i32, FdConfig)> {
82 let fd = arg.parse::<i32>()?;
83 Ok((fd, FdConfig::InputDir(Dir::from_fd(fd)?)))
84}
85
Victor Hsieh45636232021-10-15 17:52:51 -070086fn parse_arg_rw_dirs(arg: &str) -> Result<(i32, FdConfig)> {
87 let fd = arg.parse::<i32>()?;
Victor Hsieh45636232021-10-15 17:52:51 -070088 Ok((fd, FdConfig::OutputDir(Dir::from_fd(fd)?)))
89}
90
Alan Stokese1b6e1c2021-10-01 12:44:49 +010091struct Args {
92 fd_pool: BTreeMap<i32, FdConfig>,
93 ready_fd: Option<File>,
94}
95
96fn parse_args() -> Result<Args> {
Victor Hsieh42cc7762021-01-25 16:44:19 -080097 #[rustfmt::skip]
98 let matches = clap::App::new("fd_server")
99 .arg(clap::Arg::with_name("ro-fds")
100 .long("ro-fds")
Victor Hsieh60acfd32021-02-23 13:08:13 -0800101 .multiple(true)
102 .number_of_values(1))
103 .arg(clap::Arg::with_name("rw-fds")
104 .long("rw-fds")
Victor Hsieh42cc7762021-01-25 16:44:19 -0800105 .multiple(true)
106 .number_of_values(1))
Victor Hsieh559b9272021-11-08 15:15:14 -0800107 .arg(clap::Arg::with_name("ro-dirs")
108 .long("ro-dirs")
109 .multiple(true)
110 .number_of_values(1))
Victor Hsieh45636232021-10-15 17:52:51 -0700111 .arg(clap::Arg::with_name("rw-dirs")
112 .long("rw-dirs")
113 .multiple(true)
114 .number_of_values(1))
Alan Stokese1b6e1c2021-10-01 12:44:49 +0100115 .arg(clap::Arg::with_name("ready-fd")
116 .long("ready-fd")
117 .takes_value(true))
Victor Hsieh42cc7762021-01-25 16:44:19 -0800118 .get_matches();
119
120 let mut fd_pool = BTreeMap::new();
121 if let Some(args) = matches.values_of("ro-fds") {
122 for arg in args {
123 let (fd, config) = parse_arg_ro_fds(arg)?;
124 fd_pool.insert(fd, config);
125 }
126 }
Victor Hsieh60acfd32021-02-23 13:08:13 -0800127 if let Some(args) = matches.values_of("rw-fds") {
128 for arg in args {
129 let (fd, config) = parse_arg_rw_fds(arg)?;
130 fd_pool.insert(fd, config);
131 }
132 }
Victor Hsieh559b9272021-11-08 15:15:14 -0800133 if let Some(args) = matches.values_of("ro-dirs") {
134 for arg in args {
135 let (fd, config) = parse_arg_ro_dirs(arg)?;
136 fd_pool.insert(fd, config);
137 }
138 }
Victor Hsieh45636232021-10-15 17:52:51 -0700139 if let Some(args) = matches.values_of("rw-dirs") {
140 for arg in args {
141 let (fd, config) = parse_arg_rw_dirs(arg)?;
142 fd_pool.insert(fd, config);
143 }
144 }
Alan Stokese1b6e1c2021-10-01 12:44:49 +0100145 let ready_fd = if let Some(arg) = matches.value_of("ready-fd") {
146 let fd = arg.parse::<i32>()?;
147 Some(fd_to_file(fd)?)
148 } else {
149 None
150 };
151 Ok(Args { fd_pool, ready_fd })
Victor Hsieh42cc7762021-01-25 16:44:19 -0800152}
153
154fn main() -> Result<()> {
Victor Hsieh60acfd32021-02-23 13:08:13 -0800155 android_logger::init_once(
156 android_logger::Config::default().with_tag("fd_server").with_min_level(log::Level::Debug),
157 );
158
Alan Stokese1b6e1c2021-10-01 12:44:49 +0100159 let args = parse_args()?;
Alan Stokescd359bb2021-10-08 18:22:42 +0100160 let service = FdService::new_binder(args.fd_pool).as_binder();
Alan Stokese1b6e1c2021-10-01 12:44:49 +0100161
Victor Hsieh1a8cd042021-09-03 16:29:45 -0700162 debug!("fd_server is starting as a rpc service.");
Alan Stokescd359bb2021-10-08 18:22:42 +0100163
164 let mut ready_fd = args.ready_fd;
165 let retval = run_rpc_server(service, RPC_SERVICE_PORT, || {
166 debug!("fd_server is ready");
167 // Close the ready-fd if we were given one to signal our readiness.
168 drop(ready_fd.take());
169 });
170
Victor Hsieh1a8cd042021-09-03 16:29:45 -0700171 if retval {
172 debug!("RPC server has shut down gracefully");
173 Ok(())
Victor Hsieh2445e332021-06-04 16:44:53 -0700174 } else {
Victor Hsieh1a8cd042021-09-03 16:29:45 -0700175 bail!("Premature termination of RPC server");
Victor Hsieh2445e332021-06-04 16:44:53 -0700176 }
Victor Hsieh42cc7762021-01-25 16:44:19 -0800177}