Przeglądaj źródła

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

Alessandro Decina 3 lat temu
rodzic
commit
5f0ff1698a
1 zmienionych plików z 23 dodań i 11 usunięć
  1. 23 11
      aya/src/sys/netlink.rs

+ 23 - 11
aya/src/sys/netlink.rs

@@ -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()?;
-    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)
     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,
         })
         })
     }
     }