diff options
Diffstat (limited to 'src/http')
| -rw-r--r-- | src/http/auth.rs | 28 | ||||
| -rw-r--r-- | src/http/mod.rs | 7 | ||||
| -rw-r--r-- | src/http/routes.rs | 5 |
3 files changed, 27 insertions, 13 deletions
diff --git a/src/http/auth.rs b/src/http/auth.rs index 3d05cab..8c56c1f 100644 --- a/src/http/auth.rs +++ b/src/http/auth.rs @@ -27,20 +27,26 @@ fn sessions() -> &'static Mutex<HashMap<String, bool>> { SESSIONS.get_or_init(|| Mutex::new(HashMap::new())) } -/// Random hex token from the system RNG (`/dev/urandom` through our hash). -fn random_hex() -> String { +/// Random hex token from the system RNG, or `None` when there is no usable one. +/// +/// The old version read `/dev/urandom` into a zeroed buffer, ignored every +/// error, and hashed the result anyway (§6 m-13): a failed read produced a +/// session id derived from a buffer of zeros plus the clock and a counter, +/// which is guessable by anyone who can guess roughly when the process started +/// — and nothing anywhere said the RNG had failed. A session id is the whole +/// credential now (C-1), so "no RNG" is a refusal, not a weaker token. +fn random_hex() -> Option<String> { + use std::io::Read; let mut buf = [0u8; 32]; - if let Ok(mut f) = std::fs::File::open("/dev/urandom") { - use std::io::Read; - let _ = f.read_exact(&mut buf); - } - // Mix in a counter and the clock so two calls in the same instant differ. + let mut f = std::fs::File::open("/dev/urandom").ok()?; + f.read_exact(&mut buf).ok()?; + // Mixed in regardless, so two calls in the same instant still differ. let nanos = std::time::SystemTime::now() .duration_since(std::time::UNIX_EPOCH) .map(|d| d.as_nanos()) .unwrap_or(0); let salt = format!("{:?}", (buf, nanos, std::process::id(), counter())); - hash::hex(salt.as_bytes()) + Some(hash::hex(salt.as_bytes())) } fn counter() -> u64 { @@ -50,12 +56,12 @@ fn counter() -> u64 { /// Mint an anonymous session: enough for a CSRF token, not enough to see a /// single page when a password is set. -pub fn new_session() -> String { - let sid = random_hex(); +pub fn new_session() -> Option<String> { + let sid = random_hex()?; if let Ok(mut s) = sessions().lock() { s.insert(sid.clone(), false); } - sid + Some(sid) } pub fn is_valid_session(sid: &str) -> bool { diff --git a/src/http/mod.rs b/src/http/mod.rs index 07024d8..24da43f 100644 --- a/src/http/mod.rs +++ b/src/http/mod.rs @@ -238,7 +238,12 @@ fn dispatch(state: &State, req: &Req) -> Res { // through a sid that a successful login marked authenticated. let (sid, new_session) = match req.cookie("sid") { Some(s) if auth::is_valid_session(&s) => (s, false), - _ => (auth::new_session(), true), + _ => match auth::new_session() { + Some(s) => (s, true), + // No system RNG means no unguessable session id. Refuse rather than + // hand out a token someone else could compute. + None => return Res::text(500, "500: could not mint a session\n".into()), + }, }; let gated = routes::gate(state, req, &sid); if let Some(mut res) = gated { diff --git a/src/http/routes.rs b/src/http/routes.rs index 288f002..f475d3a 100644 --- a/src/http/routes.rs +++ b/src/http/routes.rs @@ -52,7 +52,10 @@ pub fn login(state: &State, req: &Req) -> Res { if let Some(old) = req.cookie("sid") { auth::drop_session(&old); } - let sid = auth::new_session(); + let sid = match auth::new_session() { + Some(sid) => sid, + None => return page_to_res(Login { page, error: "server: no entropy for a session".into() }, 500), + }; auth::authenticate(&sid); let mut res = Res::redirect("/"); // `serve` turns this into the `Set-Cookie: sid=…` header. |
