Skip to content

Commit

Permalink
Add limits to provider ASN set in both repository and rtr.
Browse files Browse the repository at this point in the history
  • Loading branch information
partim committed Jan 10, 2025
1 parent 325687d commit 0b6d046
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
14 changes: 14 additions & 0 deletions src/repository/aspa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,13 +202,19 @@ impl AsProviderAttestation {
/// This type contains the provider AS set in encoded form. It guarantees that
/// the AS in this set are ordered, free of duplicates and there is at least
/// one AS.
///
/// It does not, at this point, enforce the maximum allowed number of 16380
/// ASNs. This will be added with the next breaking change.
#[derive(Clone, Debug)]
pub struct ProviderAsSet {
captured: Captured,
len: usize,
}

impl ProviderAsSet {
/// The maximum number of ASNs allowed in the set.
const MAX_LEN: usize = 16380;

#[allow(clippy::len_without_is_empty)] // never empty
pub fn len(&self) -> usize {
self.len
Expand All @@ -226,6 +232,9 @@ impl ProviderAsSet {
ProviderAsIter(self.captured.as_slice().into_source())
}

/// Takes the provider ASN sequence from an encoded source.
///
/// Enforces a maxium size of 16380 ASNs.
fn take_from<S: decode::Source>(
cons: &mut decode::Constructed<S>,
customer_as: Asn,
Expand All @@ -237,6 +246,11 @@ impl ProviderAsSet {
while let Some(asn) = Asn::take_opt_from(
cons
)? {
if len > Self::MAX_LEN {
return Err(cons.content_err(
"too many provider ASNs"
));
}
if asn == customer_as {
return Err(cons.content_err(
"customer AS in provider AS set"
Expand Down
8 changes: 6 additions & 2 deletions src/rtr/pdu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -799,7 +799,8 @@ impl Aspa {
/// # Panics
///
/// This function panics if the length of the resulting PDU doesn’t fit
/// in a `u32`.
/// in a `u32`. Because `ProviderAsns` is now limited in size, this can’t
/// happen.
pub fn new(
version: u8,
flags: u8,
Expand Down Expand Up @@ -948,6 +949,9 @@ impl AsMut<[u8]> for AspaFixed {
pub struct ProviderAsns(Bytes);

impl ProviderAsns {
/// The maximum number of provider ASNs.
const MAX_COUNT: usize = 16380;

/// Returns an empty value.
pub fn empty() -> Self {
Self(Bytes::new())
Expand All @@ -963,7 +967,7 @@ impl ProviderAsns {
let iter = iter.into_iter();
let mut providers = Vec::with_capacity(iter.size_hint().0);
iter.enumerate().try_for_each(|(idx, item)| {
if idx >= usize::from(u16::MAX) {
if idx > Self::MAX_COUNT {
return Err(ProviderAsnsError(()))
}
providers.extend_from_slice(&item.into_u32().to_be_bytes());
Expand Down

0 comments on commit 0b6d046

Please sign in to comment.