aya: netlink: tc: use ptr::read_unaligned instead of deferencing a potentially unaligned ptr

pull/14/head
Alessandro Decina 3 years ago
parent 7f2ceaf12e
commit 5f0ff1698a

@ -189,16 +189,28 @@ pub(crate) unsafe fn netlink_qdisc_attach(
return Err(io::Error::last_os_error())?; return Err(io::Error::last_os_error())?;
} }
let reply_msg = sock.recv()?; // find the RTM_NEWTFILTER reply and read the tcm_info field which we'll
let mut tcinfo = 0; // need to detach
for reply in &reply_msg { let tc_info = match sock
if reply.header.nlmsg_type == RTM_NEWTFILTER { .recv()?
let _tcmsg = reply._data.as_ptr() as *const tcmsg; .iter()
tcinfo = (*_tcmsg).tcm_info; .find(|reply| reply.header.nlmsg_type == RTM_NEWTFILTER)
break; {
Some(reply) => {
let msg = ptr::read_unaligned(reply.data.as_ptr() as *const tcmsg);
msg.tcm_info
} }
} None => {
let priority = ((tcinfo & TC_H_MAJ_MASK) >> 16) as u32; // if sock.recv() succeeds we should never get here unless there's a
// bug in the kernel
return Err(io::Error::new(
io::ErrorKind::Other,
"no RTM_NEWTFILTER reply received, this is a bug.",
));
}
};
let priority = ((tc_info & TC_H_MAJ_MASK) >> 16) as u32;
Ok(priority) Ok(priority)
} }
@ -337,7 +349,7 @@ impl NetlinkSocket {
struct NetlinkMessage { struct NetlinkMessage {
header: nlmsghdr, header: nlmsghdr,
_data: Vec<u8>, data: Vec<u8>,
error: Option<nlmsgerr>, error: Option<nlmsgerr>,
} }
@ -371,7 +383,7 @@ impl NetlinkMessage {
Ok(NetlinkMessage { Ok(NetlinkMessage {
header, header,
_data: data, data,
error, error,
}) })
} }

Loading…
Cancel
Save