From 5f0ff1698a12141ffe50e160de252f664773c140 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alessandro=C2=A0Decina?= Date: Sun, 20 Jun 2021 08:25:58 +0000 Subject: [PATCH] aya: netlink: tc: use ptr::read_unaligned instead of deferencing a potentially unaligned ptr --- aya/src/sys/netlink.rs | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/aya/src/sys/netlink.rs b/aya/src/sys/netlink.rs index 693d6b93..cde36884 100644 --- a/aya/src/sys/netlink.rs +++ b/aya/src/sys/netlink.rs @@ -189,16 +189,28 @@ pub(crate) unsafe fn netlink_qdisc_attach( return Err(io::Error::last_os_error())?; } - let reply_msg = sock.recv()?; - let mut tcinfo = 0; - for reply in &reply_msg { - if reply.header.nlmsg_type == RTM_NEWTFILTER { - let _tcmsg = reply._data.as_ptr() as *const tcmsg; - tcinfo = (*_tcmsg).tcm_info; - break; + // find the RTM_NEWTFILTER reply and read the tcm_info field which we'll + // need to detach + let tc_info = match sock + .recv()? + .iter() + .find(|reply| reply.header.nlmsg_type == RTM_NEWTFILTER) + { + Some(reply) => { + let msg = ptr::read_unaligned(reply.data.as_ptr() as *const tcmsg); + msg.tcm_info } - } - let priority = ((tcinfo & TC_H_MAJ_MASK) >> 16) as u32; + None => { + // 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) } @@ -337,7 +349,7 @@ impl NetlinkSocket { struct NetlinkMessage { header: nlmsghdr, - _data: Vec, + data: Vec, error: Option, } @@ -371,7 +383,7 @@ impl NetlinkMessage { Ok(NetlinkMessage { header, - _data: data, + data, error, }) }