summaryrefslogtreecommitdiff
path: root/src/rust/protover/protover.rs
diff options
context:
space:
mode:
authorIsis Lovecruft <isis@torproject.org>2018-03-27 22:46:14 +0000
committerIsis Lovecruft <isis@torproject.org>2018-04-02 19:20:40 +0000
commitf2daf82794c59c37756abeaf3e41e5ebe1e7fcde (patch)
treed00a15fc6789601ca03e2d464f72927983b37e28 /src/rust/protover/protover.rs
parent6eea0dc5f186429d598edda046156afc2a93120c (diff)
downloadtor-f2daf82794c59c37756abeaf3e41e5ebe1e7fcde.tar.gz
tor-f2daf82794c59c37756abeaf3e41e5ebe1e7fcde.zip
rust: Fix ProtoSet and ProtoEntry to use the same DoS limits as C.
Previously, the limit for MAX_PROTOCOLS_TO_EXPAND was actually being applied in Rust to the maximum number of version (total, for all subprotocols). Whereas in C, it was being applied to the number of subprotocols that were allowed. This changes the Rust to match C's behaviour.
Diffstat (limited to 'src/rust/protover/protover.rs')
-rw-r--r--src/rust/protover/protover.rs18
1 files changed, 15 insertions, 3 deletions
diff --git a/src/rust/protover/protover.rs b/src/rust/protover/protover.rs
index b4fb2e842a..514aeffc58 100644
--- a/src/rust/protover/protover.rs
+++ b/src/rust/protover/protover.rs
@@ -26,7 +26,7 @@ const FIRST_TOR_VERSION_TO_ADVERTISE_PROTOCOLS: &'static str = "0.2.9.3-alpha";
/// before concluding that someone is trying to DoS us
///
/// C_RUST_COUPLED: src/or/protover.c `MAX_PROTOCOLS_TO_EXPAND`
-pub(crate) const MAX_PROTOCOLS_TO_EXPAND: usize = (1<<16);
+const MAX_PROTOCOLS_TO_EXPAND: usize = (1<<16);
/// Known subprotocols in Tor. Indicates which subprotocol a relay supports.
///
@@ -155,6 +155,10 @@ impl ProtoEntry {
supported.parse()
}
+ pub fn len(&self) -> usize {
+ self.0.len()
+ }
+
pub fn get(&self, protocol: &Protocol) -> Option<&ProtoSet> {
self.0.get(protocol)
}
@@ -209,8 +213,11 @@ impl FromStr for ProtoEntry {
let proto_name: Protocol = proto.parse()?;
proto_entry.insert(proto_name, versions);
- }
+ if proto_entry.len() > MAX_PROTOCOLS_TO_EXPAND {
+ return Err(ProtoverError::ExceedsMax);
+ }
+ }
Ok(proto_entry)
}
}
@@ -723,8 +730,13 @@ mod test {
}
#[test]
+ fn test_protoentry_from_str_allowed_number_of_versions() {
+ assert_protoentry_is_parseable!("Desc=1-4294967294");
+ }
+
+ #[test]
fn test_protoentry_from_str_too_many_versions() {
- assert_protoentry_is_unparseable!("Desc=1-65537");
+ assert_protoentry_is_unparseable!("Desc=1-4294967295");
}
#[test]