From d5b68c0c216c6545013089163ed0fd1ed0b071a7 Mon Sep 17 00:00:00 2001 From: yoriswind Date: Fri, 20 Nov 2026 18:55:33 -0800 Subject: [PATCH] openpgp: exercise per-key check budgets with small inputs --- crates/openpgp/src/keys.rs | 32 ++++++++++++- crates/openpgp/src/keys_tests.rs | 78 +++++++++++++++++++++++--------- 2 files changed, 88 insertions(+), 22 deletions(-) diff --git a/crates/openpgp/src/keys.rs b/crates/openpgp/src/keys.rs index c3b083162e..720abd63af 100644 --- a/crates/openpgp/src/keys.rs +++ b/crates/openpgp/src/keys.rs @@ -27,6 +27,36 @@ const MAX_USER_ID: usize = 4 * 1024; /// checked, so a real key needs a handful). Room for two copies of the largest key kept, merged: /// two self-signatures for each User ID, and two with a back-signature each for each subkey. const MAX_CHECKS: usize = 512; + +#[cfg(not(test))] +pub(super) fn max_checks() -> usize { + MAX_CHECKS +} + +#[cfg(test)] +std::thread_local! { + static TEST_MAX_CHECKS: std::cell::Cell = const { std::cell::Cell::new(MAX_CHECKS) }; +} + +#[cfg(test)] +pub(super) fn max_checks() -> usize { + TEST_MAX_CHECKS.with(std::cell::Cell::get) +} + +#[cfg(test)] +pub(super) fn with_max_checks(limit: usize, run: impl FnOnce() -> T) -> T { + struct Restore(usize); + + impl Drop for Restore { + fn drop(&mut self) { + TEST_MAX_CHECKS.with(|value| value.set(self.0)); + } + } + + let _restore = Restore(TEST_MAX_CHECKS.with(|value| value.replace(limit))); + run() +} + /// How many packets one block is read for before it is given up on. const MAX_PACKETS: usize = 4_096; @@ -298,7 +328,7 @@ impl PublicKey { return None; } let primary = &key.primary_key; - let mut local = Budget::new(MAX_CHECKS); + let mut local = Budget::new(max_checks()); let mut revocations = Budget::new(MAX_REVOCATION_CHECKS); let spend = |budget: &mut Budget, total: &mut Budget| budget.spend() && total.spend(); diff --git a/crates/openpgp/src/keys_tests.rs b/crates/openpgp/src/keys_tests.rs index 3cc53b0bcd..7418d159e1 100644 --- a/crates/openpgp/src/keys_tests.rs +++ b/crates/openpgp/src/keys_tests.rs @@ -7,7 +7,7 @@ use pgp::ser::Serialize; use pgp::types::{Duration, Password, Tag}; use crate::fixtures::*; -use crate::keys::Budget; +use crate::keys::{Budget, max_checks, with_max_checks}; use crate::{CantCheck, Fingerprint, KeyRing, NotVerified, Outcome, PublicKey, verify}; const ALICE: &str = "Alice "; @@ -255,26 +255,62 @@ fn a_copy_with_junk_subkey_bindings_from_the_future_never_pushes_out_the_real_on fn a_copy_that_costs_more_checks_than_a_key_may_is_refused_not_cut_short() { let alice = secret(ALICE, Make { subkey: true, ..Make::default() }); let mallory = secret("Mallory ", Make { seed: 6, ..Make::default() }); - let mut copy = SignedPublicKey::from(alice.clone()); - // Junk User IDs with junk certifications before the subkey: checking them all would leave - // no checks for the subkey's binding. - for n in 0..5 { - let id = pgp::packet::UserId::from_str(Default::default(), format!("Alice {n} ")).unwrap(); - let sigs = (0..110) - .map(|i| { - forged(&alice, &mallory, SignatureType::CertPositive, made(&alice) + i, vec![]) - .sign_certification(&mallory.primary_key, ©.primary_key, &Password::empty(), Tag::UserId, &id) - .unwrap() - }) - .collect(); - copy.details.users.push(pgp::types::SignedUser::new(id, sigs)); - } - let junk = copy.to_bytes().unwrap(); - assert!(PublicKey::parse(&junk).is_err(), "a key not checked whole is no key"); - let mut keys = KeyRing::new(); - keys.insert(public(&alice)); - offer(&mut keys, &junk); - assert!(matches!(outcome(&signed_at(&alice, made(&alice) + 10), ALICE, &keys), Outcome::Verified(_))); + assert_eq!(max_checks(), 512); + let real = public(&alice); + let message = signed_at(&alice, made(&alice) + 10); + let nine_checks = with_max_checks(8, || { + assert_eq!(max_checks(), 8); + let mut copy = SignedPublicKey::from(alice.clone()); + assert_eq!(copy.details.users.len(), 1); + assert_eq!(copy.public_subkeys.len(), 1); + // Junk User IDs with junk certifications before the subkey: checking them all would leave + // no checks for the subkey's binding. Five users with two forged certs need 13 slots, + // including the real user's certification, the subkey binding and its back-signature. + for n in 0..5 { + let id = pgp::packet::UserId::from_str(Default::default(), format!("Alice {n} ")).unwrap(); + let sigs = (0..2) + .map(|i| { + forged(&alice, &mallory, SignatureType::CertPositive, made(&alice) + i, vec![]) + .sign_certification(&mallory.primary_key, ©.primary_key, &Password::empty(), Tag::UserId, &id) + .unwrap() + }) + .collect(); + copy.details.users.push(pgp::types::SignedUser::new(id, sigs)); + } + let junk = copy.to_bytes().unwrap(); + assert!(PublicKey::parse(&junk).is_err(), "a key not checked whole is no key"); + let mut keys = KeyRing::new(); + keys.insert(public(&alice)); + offer(&mut keys, &junk); + assert!(matches!(outcome(&signed_at(&alice, made(&alice) + 10), ALICE, &keys), Outcome::Verified(_))); + + // Reuse one actual forged packet per junk user: five unsuccessful checks plus the + // genuine key's three checks must still reach and verify the signing subkey. + let mut exact = SignedPublicKey::from(alice.clone()); + for user in copy.details.users.iter().skip(1) { + assert_eq!(user.signatures.len(), 2); + exact.details.users.push(pgp::types::SignedUser::new(user.id.clone(), vec![user.signatures[0].clone()])); + } + let accepted = PublicKey::parse(&exact.to_bytes().unwrap()).unwrap(); + assert_eq!(accepted.len(), 1); + assert_eq!(accepted[0].to_bytes(), real.to_bytes(), "discard only the forged certifications"); + let keys: KeyRing = accepted.into_iter().collect(); + assert!(matches!(outcome(&message, ALICE, &keys), Outcome::Verified(_))); + + // A sixth existing forged packet costs the last slot needed by the back-signature: + // refuse the whole key, rather than returning one with an unusable signing subkey. + exact.details.users[1].signatures.push(copy.details.users[1].signatures[1].clone()); + let nine = exact.to_bytes().unwrap(); + assert!(PublicKey::parse(&nine).is_err(), "nine checks do not fit an eight-check key budget"); + nine + }); + // Leaving the scope restores the real default, through both its getter and its consumer. + assert_eq!(max_checks(), 512); + let restored = PublicKey::parse(&nine_checks).unwrap(); + assert_eq!(restored.len(), 1); + assert_eq!(restored[0].to_bytes(), real.to_bytes()); + let keys: KeyRing = restored.into_iter().collect(); + assert!(matches!(outcome(&message, ALICE, &keys), Outcome::Verified(_))); } #[test]