netlink-request: add length check for serliazed netlink buffer

pull/186/head
Jake McGinty 2022-01-08 13:33:39 -06:00
parent 09e68c2c01
commit 4784a695ad
1 changed files with 12 additions and 1 deletions

View File

@ -1,5 +1,8 @@
#[cfg(target_os = "linux")]
mod linux {
const NETLINK_BUFFER_LENGTH: usize = 4096;
use netlink_packet_core::{
NetlinkDeserializable, NetlinkMessage, NetlinkPayload, NetlinkSerializable, NLM_F_ACK,
NLM_F_CREATE, NLM_F_EXCL, NLM_F_REQUEST,
@ -78,9 +81,17 @@ mod linux {
I: Clone + Debug + Eq + NetlinkSerializable + NetlinkDeserializable,
{
let mut req = NetlinkMessage::from(message);
if req.buffer_len() > NETLINK_BUFFER_LENGTH {
return Err(io::Error::new(
io::ErrorKind::InvalidInput,
format!("Serialized netlink packet larger than maximum size {}", NETLINK_BUFFER_LENGTH),
));
}
req.header.flags = flags.unwrap_or(NLM_F_REQUEST | NLM_F_ACK | NLM_F_EXCL | NLM_F_CREATE);
req.finalize();
let mut buf = [0; 4096];
let mut buf = [0; NETLINK_BUFFER_LENGTH];
req.serialize(&mut buf);
let len = req.buffer_len();