blob: a1d09fc0258de7d9807a2f48a989bed256d7f554 [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 Hsiehbf944d72022-03-07 23:46:43 +000026mod common;
Victor Hsieh4dc85c92021-03-15 11:01:23 -070027mod fsverity;
28
Victor Hsieh1a8cd042021-09-03 16:29:45 -070029use anyhow::{bail, Result};
Alan Stokescd359bb2021-10-08 18:22:42 +010030use binder_common::rpc_server::run_rpc_server;
Victor Hsiehb0f5fc82021-10-15 17:24:19 -070031use log::debug;
Victor Hsiehbf944d72022-03-07 23:46:43 +000032use nix::sys::stat::{umask, Mode};
Victor Hsieh42cc7762021-01-25 16:44:19 -080033use std::collections::BTreeMap;
Victor Hsieh42cc7762021-01-25 16:44:19 -080034use std::fs::File;
Victor Hsiehb0f5fc82021-10-15 17:24:19 -070035use std::os::unix::io::FromRawFd;
Victor Hsieh42cc7762021-01-25 16:44:19 -080036
Victor Hsiehb0f5fc82021-10-15 17:24:19 -070037use aidl::{FdConfig, FdService};
Inseob Kimc0886c22021-12-13 17:41:24 +090038use authfs_fsverity_metadata::parse_fsverity_metadata;
Victor Hsieh42cc7762021-01-25 16:44:19 -080039
Victor Hsieh2445e332021-06-04 16:44:53 -070040const RPC_SERVICE_PORT: u32 = 3264; // TODO: support dynamic port for multiple fd_server instances
Victor Hsieh42cc7762021-01-25 16:44:19 -080041
Victor Hsieh42cc7762021-01-25 16:44:19 -080042fn is_fd_valid(fd: i32) -> bool {
43 // SAFETY: a query-only syscall
44 let retval = unsafe { libc::fcntl(fd, libc::F_GETFD) };
45 retval >= 0
46}
47
Victor Hsiehbf944d72022-03-07 23:46:43 +000048fn fd_to_owned<T: FromRawFd>(fd: i32) -> Result<T> {
Victor Hsieh42cc7762021-01-25 16:44:19 -080049 if !is_fd_valid(fd) {
50 bail!("Bad FD: {}", fd);
51 }
52 // SAFETY: The caller is supposed to provide valid FDs to this process.
Victor Hsiehbf944d72022-03-07 23:46:43 +000053 Ok(unsafe { T::from_raw_fd(fd) })
Victor Hsieh42cc7762021-01-25 16:44:19 -080054}
55
Victor Hsieh60acfd32021-02-23 13:08:13 -080056fn parse_arg_ro_fds(arg: &str) -> Result<(i32, FdConfig)> {
Victor Hsieh42cc7762021-01-25 16:44:19 -080057 let result: Result<Vec<i32>, _> = arg.split(':').map(|x| x.parse::<i32>()).collect();
58 let fds = result?;
Inseob Kimc0886c22021-12-13 17:41:24 +090059 if fds.len() > 2 {
Victor Hsieh42cc7762021-01-25 16:44:19 -080060 bail!("Too many options: {}", arg);
61 }
Victor Hsieh42cc7762021-01-25 16:44:19 -080062 Ok((
63 fds[0],
Victor Hsieh60acfd32021-02-23 13:08:13 -080064 FdConfig::Readonly {
Victor Hsiehbf944d72022-03-07 23:46:43 +000065 file: fd_to_owned(fds[0])?,
Inseob Kimc0886c22021-12-13 17:41:24 +090066 // Alternative metadata source, if provided
67 alt_metadata: fds
68 .get(1)
Victor Hsiehbf944d72022-03-07 23:46:43 +000069 .map(|fd| fd_to_owned(*fd))
Inseob Kimc0886c22021-12-13 17:41:24 +090070 .transpose()?
71 .and_then(|f| parse_fsverity_metadata(f).ok()),
Victor Hsieh42cc7762021-01-25 16:44:19 -080072 },
73 ))
74}
75
Victor Hsieh60acfd32021-02-23 13:08:13 -080076fn parse_arg_rw_fds(arg: &str) -> Result<(i32, FdConfig)> {
77 let fd = arg.parse::<i32>()?;
Victor Hsiehbf944d72022-03-07 23:46:43 +000078 let file = fd_to_owned::<File>(fd)?;
Victor Hsieh60acfd32021-02-23 13:08:13 -080079 if file.metadata()?.len() > 0 {
80 bail!("File is expected to be empty");
81 }
82 Ok((fd, FdConfig::ReadWrite(file)))
83}
84
Victor Hsieh559b9272021-11-08 15:15:14 -080085fn parse_arg_ro_dirs(arg: &str) -> Result<(i32, FdConfig)> {
86 let fd = arg.parse::<i32>()?;
Victor Hsiehbf944d72022-03-07 23:46:43 +000087 Ok((fd, FdConfig::InputDir(fd_to_owned(fd)?)))
Victor Hsieh559b9272021-11-08 15:15:14 -080088}
89
Victor Hsieh45636232021-10-15 17:52:51 -070090fn parse_arg_rw_dirs(arg: &str) -> Result<(i32, FdConfig)> {
91 let fd = arg.parse::<i32>()?;
Victor Hsiehbf944d72022-03-07 23:46:43 +000092 Ok((fd, FdConfig::OutputDir(fd_to_owned(fd)?)))
Victor Hsieh45636232021-10-15 17:52:51 -070093}
94
Alan Stokese1b6e1c2021-10-01 12:44:49 +010095struct Args {
96 fd_pool: BTreeMap<i32, FdConfig>,
97 ready_fd: Option<File>,
98}
99
100fn parse_args() -> Result<Args> {
Victor Hsieh42cc7762021-01-25 16:44:19 -0800101 #[rustfmt::skip]
102 let matches = clap::App::new("fd_server")
103 .arg(clap::Arg::with_name("ro-fds")
104 .long("ro-fds")
Victor Hsieh60acfd32021-02-23 13:08:13 -0800105 .multiple(true)
106 .number_of_values(1))
107 .arg(clap::Arg::with_name("rw-fds")
108 .long("rw-fds")
Victor Hsieh42cc7762021-01-25 16:44:19 -0800109 .multiple(true)
110 .number_of_values(1))
Victor Hsieh559b9272021-11-08 15:15:14 -0800111 .arg(clap::Arg::with_name("ro-dirs")
112 .long("ro-dirs")
113 .multiple(true)
114 .number_of_values(1))
Victor Hsieh45636232021-10-15 17:52:51 -0700115 .arg(clap::Arg::with_name("rw-dirs")
116 .long("rw-dirs")
117 .multiple(true)
118 .number_of_values(1))
Alan Stokese1b6e1c2021-10-01 12:44:49 +0100119 .arg(clap::Arg::with_name("ready-fd")
120 .long("ready-fd")
121 .takes_value(true))
Victor Hsieh42cc7762021-01-25 16:44:19 -0800122 .get_matches();
123
124 let mut fd_pool = BTreeMap::new();
125 if let Some(args) = matches.values_of("ro-fds") {
126 for arg in args {
127 let (fd, config) = parse_arg_ro_fds(arg)?;
128 fd_pool.insert(fd, config);
129 }
130 }
Victor Hsieh60acfd32021-02-23 13:08:13 -0800131 if let Some(args) = matches.values_of("rw-fds") {
132 for arg in args {
133 let (fd, config) = parse_arg_rw_fds(arg)?;
134 fd_pool.insert(fd, config);
135 }
136 }
Victor Hsieh559b9272021-11-08 15:15:14 -0800137 if let Some(args) = matches.values_of("ro-dirs") {
138 for arg in args {
139 let (fd, config) = parse_arg_ro_dirs(arg)?;
140 fd_pool.insert(fd, config);
141 }
142 }
Victor Hsieh45636232021-10-15 17:52:51 -0700143 if let Some(args) = matches.values_of("rw-dirs") {
144 for arg in args {
145 let (fd, config) = parse_arg_rw_dirs(arg)?;
146 fd_pool.insert(fd, config);
147 }
148 }
Alan Stokese1b6e1c2021-10-01 12:44:49 +0100149 let ready_fd = if let Some(arg) = matches.value_of("ready-fd") {
150 let fd = arg.parse::<i32>()?;
Victor Hsiehbf944d72022-03-07 23:46:43 +0000151 Some(fd_to_owned(fd)?)
Alan Stokese1b6e1c2021-10-01 12:44:49 +0100152 } else {
153 None
154 };
155 Ok(Args { fd_pool, ready_fd })
Victor Hsieh42cc7762021-01-25 16:44:19 -0800156}
157
158fn main() -> Result<()> {
Victor Hsieh60acfd32021-02-23 13:08:13 -0800159 android_logger::init_once(
160 android_logger::Config::default().with_tag("fd_server").with_min_level(log::Level::Debug),
161 );
162
Alan Stokese1b6e1c2021-10-01 12:44:49 +0100163 let args = parse_args()?;
Victor Hsieh8e285112021-12-10 11:17:26 -0800164
165 // Allow open/create/mkdir from authfs to create with expecting mode. It's possible to still
166 // use a custom mask on creation, then report the actual file mode back to authfs. But there
167 // is no demand now.
168 let old_umask = umask(Mode::empty());
169 debug!("Setting umask to 0 (old: {:03o})", old_umask.bits());
170
Alan Stokescd359bb2021-10-08 18:22:42 +0100171 let service = FdService::new_binder(args.fd_pool).as_binder();
Victor Hsieh1a8cd042021-09-03 16:29:45 -0700172 debug!("fd_server is starting as a rpc service.");
Alan Stokescd359bb2021-10-08 18:22:42 +0100173 let mut ready_fd = args.ready_fd;
174 let retval = run_rpc_server(service, RPC_SERVICE_PORT, || {
175 debug!("fd_server is ready");
176 // Close the ready-fd if we were given one to signal our readiness.
177 drop(ready_fd.take());
178 });
179
Victor Hsieh1a8cd042021-09-03 16:29:45 -0700180 if retval {
181 debug!("RPC server has shut down gracefully");
182 Ok(())
Victor Hsieh2445e332021-06-04 16:44:53 -0700183 } else {
Victor Hsieh1a8cd042021-09-03 16:29:45 -0700184 bail!("Premature termination of RPC server");
Victor Hsieh2445e332021-06-04 16:44:53 -0700185 }
Victor Hsieh42cc7762021-01-25 16:44:19 -0800186}