From e19ba9973f6e7051036a9e91c36d2a4aa4ce986c Mon Sep 17 00:00:00 2001 From: tunnell Date: Mon, 20 Apr 2026 08:15:32 -0500 Subject: [PATCH] Use constant-time compare for GAM auth tokens GAM auth tokens are 128-bit capabilities bound to their owning process. The previous `==` compare on `[u32; 4]` short-circuits on the first mismatching element, leaking prefix-length information through timing. Switch to `subtle::ConstantTimeEq`, which is already present in the dep graph via ed25519-dalek and used elsewhere in the crypto stack. Signed-off-by: tunnell Assisted-by: Claude Code (Opus 4.7) --- Cargo.lock | 1 + services/gam/Cargo.toml | 1 + services/gam/src/tokens.rs | 7 +++++-- 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a06f9bbd6..c6d9453ab 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2688,6 +2688,7 @@ dependencies = [ "qrcode", "rkyv 0.8.8", "sha2", + "subtle", "trng", "tts-frontend", "utralib 0.1.27", diff --git a/services/gam/Cargo.toml b/services/gam/Cargo.toml index 5bb857282..110961ff7 100644 --- a/services/gam/Cargo.toml +++ b/services/gam/Cargo.toml @@ -46,6 +46,7 @@ miniz_oxide = "0.4.4" hex = { version = "0.4.3", default-features = false, features = ["alloc"] } sha2 = { version = "0.10.8" } digest = "0.9.0" +subtle = { version = "2.6", default-features = false } utralib = { version = "0.1.27", optional = true, default-features = false } diff --git a/services/gam/src/tokens.rs b/services/gam/src/tokens.rs index ddead7aea..638947ae4 100644 --- a/services/gam/src/tokens.rs +++ b/services/gam/src/tokens.rs @@ -3,6 +3,7 @@ use std::cell::RefCell; #[cfg(feature = "bao1x")] use bao1x_hal_service::trng; use gam::{EXPECTED_APP_CONTEXTS, EXPECTED_BOOT_CONTEXTS}; +use subtle::ConstantTimeEq; /* Authentication tokens to the GAM are created on a first-come, first-serve basis, @@ -117,7 +118,9 @@ impl<'a> TokenManager { } pub(crate) fn is_token_valid(&self, token: [u32; 4]) -> bool { - self.tokens.iter().find(|&namedtoken| namedtoken.token == token).is_some() + // Constant-time comparison: tokens are 128-bit capabilities; a + // non-CT compare leaks prefix information through timing. + self.tokens.iter().any(|namedtoken| bool::from(namedtoken.token.ct_eq(&token))) } pub(crate) fn find_token(&self, name: &str) -> Option<[u32; 4]> { @@ -131,7 +134,7 @@ impl<'a> TokenManager { pub(crate) fn lookup_name(&self, token: &[u32; 4]) -> Option { for entry in self.tokens.iter() { - if entry.token == *token { + if bool::from(entry.token.ct_eq(token)) { return Some(entry.name.to_string()); } }