blob: f8b0c63d795ee08f6bf5a16956ff73d1a09abaa2 [file] [log] [blame]
Paul Crowley9da969e2022-09-16 23:42:24 +00001// Copyright (C) 2022 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! FIPS compliant random number conditioner. Reads from /dev/hw_random
16//! and applies the NIST SP 800-90A CTR DRBG strategy to provide
17//! pseudorandom bytes to clients which connect to a socket provided
18//! by init.
19
20mod conditioner;
21mod cutils_socket;
22mod drbg;
23
24use std::{
25 convert::Infallible,
Paul Crowley021cf552022-09-28 18:37:43 +000026 fs::remove_file,
Paul Crowley9da969e2022-09-16 23:42:24 +000027 io::ErrorKind,
Paul Crowley021cf552022-09-28 18:37:43 +000028 os::unix::net::UnixListener,
Paul Crowley9da969e2022-09-16 23:42:24 +000029 path::{Path, PathBuf},
30};
31
Paul Crowley021cf552022-09-28 18:37:43 +000032use anyhow::{ensure, Context, Result};
Paul Crowley9da969e2022-09-16 23:42:24 +000033use clap::Parser;
Paul Crowley021cf552022-09-28 18:37:43 +000034use log::{error, info, Level};
35use nix::sys::signal;
Paul Crowley9da969e2022-09-16 23:42:24 +000036use tokio::{io::AsyncWriteExt, net::UnixListener as TokioUnixListener};
37
Paul Crowley021cf552022-09-28 18:37:43 +000038use crate::conditioner::ConditionerBuilder;
Paul Crowley9da969e2022-09-16 23:42:24 +000039
Andrew Walbran13335de2022-12-01 15:05:39 +000040#[derive(Debug, Parser)]
Paul Crowley9da969e2022-09-16 23:42:24 +000041struct Cli {
42 #[clap(long, default_value = "/dev/hw_random")]
43 source: PathBuf,
44 #[clap(long)]
45 socket: Option<PathBuf>,
46}
47
Paul Crowley021cf552022-09-28 18:37:43 +000048fn configure_logging() -> Result<()> {
49 ensure!(
50 logger::init(
51 logger::Config::default().with_tag_on_device("prng_seeder").with_min_level(Level::Info)
52 ),
53 "log configuration failed"
54 );
55 Ok(())
Paul Crowley9da969e2022-09-16 23:42:24 +000056}
57
58fn get_socket(path: &Path) -> Result<UnixListener> {
59 if let Err(e) = remove_file(path) {
60 if e.kind() != ErrorKind::NotFound {
Paul Crowley021cf552022-09-28 18:37:43 +000061 return Err(e).context(format!("Removing old socket: {}", path.display()));
Paul Crowley9da969e2022-09-16 23:42:24 +000062 }
63 } else {
Paul Crowley021cf552022-09-28 18:37:43 +000064 info!("Deleted old {}", path.display());
Paul Crowley9da969e2022-09-16 23:42:24 +000065 }
Paul Crowley021cf552022-09-28 18:37:43 +000066 UnixListener::bind(path)
67 .with_context(|| format!("In get_socket: binding socket to {}", path.display()))
Paul Crowley9da969e2022-09-16 23:42:24 +000068}
69
Paul Crowley021cf552022-09-28 18:37:43 +000070fn setup() -> Result<(ConditionerBuilder, UnixListener)> {
71 configure_logging()?;
72 let cli = Cli::try_parse()?;
Andrew Walbranc7687332023-07-21 17:26:09 +010073 // SAFETY: Nothing else sets the signal handler, so either it was set here or it is the default.
Paul Crowley021cf552022-09-28 18:37:43 +000074 unsafe { signal::signal(signal::Signal::SIGPIPE, signal::SigHandler::SigIgn) }
75 .context("In setup, setting SIGPIPE to SIG_IGN")?;
76
77 let listener = match cli.socket {
78 Some(path) => get_socket(path.as_path())?,
79 None => cutils_socket::android_get_control_socket("prng_seeder")
80 .context("In setup, calling android_get_control_socket")?,
81 };
82 let hwrng = std::fs::File::open(&cli.source)
83 .with_context(|| format!("Unable to open hwrng {}", cli.source.display()))?;
84 let cb = ConditionerBuilder::new(hwrng)?;
85 Ok((cb, listener))
86}
87
88async fn listen_loop(cb: ConditionerBuilder, listener: UnixListener) -> Result<Infallible> {
89 let mut conditioner = cb.build();
90 listener.set_nonblocking(true).context("In listen_loop, on set_nonblocking")?;
91 let listener = TokioUnixListener::from_std(listener).context("In listen_loop, on from_std")?;
92 info!("Starting listen loop");
Paul Crowley9da969e2022-09-16 23:42:24 +000093 loop {
94 match listener.accept().await {
95 Ok((mut stream, _)) => {
96 let new_bytes = conditioner.request()?;
97 tokio::spawn(async move {
98 if let Err(e) = stream.write_all(&new_bytes).await {
99 error!("Request failed: {}", e);
100 }
101 });
102 conditioner.reseed_if_necessary().await?;
103 }
104 Err(e) if e.kind() == ErrorKind::Interrupted => {}
Paul Crowley021cf552022-09-28 18:37:43 +0000105 Err(e) => return Err(e).context("accept on socket failed"),
Paul Crowley9da969e2022-09-16 23:42:24 +0000106 }
107 }
108}
109
Paul Crowley021cf552022-09-28 18:37:43 +0000110fn run() -> Result<Infallible> {
111 let (cb, listener) = match setup() {
112 Ok(t) => t,
113 Err(e) => {
114 // If setup fails, just hang forever. That way init doesn't respawn us.
115 error!("Hanging forever because setup failed: {:?}", e);
116 // Logs are sometimes mysteriously not being logged, so print too
117 println!("prng_seeder: Hanging forever because setup failed: {:?}", e);
118 loop {
119 std::thread::park();
120 error!("std::thread::park() finished unexpectedly, re-parking thread");
121 }
122 }
Paul Crowley9da969e2022-09-16 23:42:24 +0000123 };
Paul Crowley9da969e2022-09-16 23:42:24 +0000124
125 tokio::runtime::Builder::new_current_thread()
126 .enable_all()
Paul Crowley021cf552022-09-28 18:37:43 +0000127 .build()
128 .context("In run, building reactor")?
129 .block_on(async { listen_loop(cb, listener).await })
Paul Crowley9da969e2022-09-16 23:42:24 +0000130}
131
132fn main() {
Paul Crowley021cf552022-09-28 18:37:43 +0000133 let e = run();
134 error!("Launch terminated: {:?}", e);
135 // Logs are sometimes mysteriously not being logged, so print too
136 println!("prng_seeder: launch terminated: {:?}", e);
Paul Crowley9da969e2022-09-16 23:42:24 +0000137 std::process::exit(-1);
138}
Andrew Walbran13335de2022-12-01 15:05:39 +0000139
140#[cfg(test)]
141mod tests {
142 use super::*;
143 use clap::CommandFactory;
144
145 #[test]
146 fn verify_cli() {
147 Cli::command().debug_assert();
148 }
149}