aboutsummaryrefslogtreecommitdiff
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:59:16 +0000
commitc65088cb1943748412e1a390de655e20bdb28692 (patch)
treee2808db9bc2b138e89a11c23a52e8e618fe14e51 /src/rust/protover/protover.rs
parent4b4e36a413bb5d0e2ea499dd6bc34b3d24bd3375 (diff)
downloadtor-c65088cb1943748412e1a390de655e20bdb28692.tar.gz
tor-c65088cb1943748412e1a390de655e20bdb28692.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 fc89f70d4c..5e5a31cd33 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);
/// Currently supported protocols and their versions, as a byte-slice.
///
@@ -166,6 +166,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)
}
@@ -220,8 +224,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)
}
}
@@ -738,8 +745,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]