Skip to content

Commit 666edbc

Browse files
Jeff-A-Martincathay4t
authored andcommitted
Avoid panic in RouteNextHopBuffer length checks
If the next-hops length is u16::MAX, adding 8 (the PAYLOAD_OFFSET) to it would cause integer overflow and lead to a panic. Rather than switching to `saturating_add`, remove the addition entirely. It was unnecessary, as the provided length already accounts for the RTA struct.
1 parent fceb9c2 commit 666edbc

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

src/route/next_hops.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ impl<T: AsRef<[u8]>> RouteNextHopBuffer<T> {
6363
return Err(format!(
6464
"invalid RouteNextHopBuffer: length {} < {}",
6565
len,
66-
8 + self.length()
66+
self.length(),
6767
)
6868
.into());
6969
}

src/route/tests/route_flags.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use netlink_packet_utils::traits::{Emitable, Parseable};
88
use crate::route::flags::RouteFlags;
99
use crate::route::{
1010
RouteAttribute, RouteHeader, RouteMessage, RouteMessageBuffer,
11-
RouteProtocol, RouteScope, RouteType,
11+
RouteNextHopBuffer, RouteProtocol, RouteScope, RouteType,
1212
};
1313
use crate::AddressFamily;
1414

@@ -54,3 +54,15 @@ fn test_ipv6_add_route_onlink() {
5454

5555
assert_eq!(buf, raw);
5656
}
57+
58+
// Verify that [`RouteNextHopBuffer`] rejects the buffer when provided with
59+
// an invalid length.
60+
#[test]
61+
fn test_next_hop_max_buffer_len() {
62+
// Route next-hop buffer layout:
63+
// |byte0|byte1|byte2|byte3|byte4|byte5|byte6|byte7|bytes8+|
64+
// |-----|-----|-----|-----|-----|-----|-----|-----|-------|
65+
// | length |flags|hops | Interface Index |Payload|
66+
let buffer = [0xff, 0xff, 0, 0, 0, 0, 0, 0];
67+
assert!(RouteNextHopBuffer::new_checked(buffer).is_err());
68+
}

0 commit comments

Comments
 (0)