Skip to content

Commit

Permalink
Fix #1 (resolve stack-use-after-scope)
Browse files Browse the repository at this point in the history
* move the code of `send_ioctl` into the `get_ethtool_link_settings`
  • Loading branch information
ssfdust committed Feb 29, 2024
1 parent 35c00cc commit bc7baa8
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 23 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ members = [
"ethernet-info",
"xtask",
]
resolver = "2"
32 changes: 13 additions & 19 deletions ethernet-info/src/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,20 @@ impl CmdContext {
}

/// Convert the ifreq struct which is filled by kernel to the EthtoolCommnad.
pub fn get_ethtool_link_settings(&self) -> EthtoolCommnad {
pub fn get_ethtool_link_settings(
&mut self,
mut ecmd: EthtoolCommnad,
) -> Result<EthtoolCommnad, EthtoolError> {
unsafe {
let ecmd = self.ifr.ifr_ifru.ifru_data as *mut EthtoolCommnad;
*ecmd
self.ifr = ifreq {
ifr_name: str_to_arr(&self.devname),
ifr_ifru: libc::__c_anonymous_ifr_ifru {
ifru_data: { transmute(&mut ecmd as *mut _) },
},
};
let ret = ethtool_ioctl(self.fd, &mut self.ifr);
ret.map(|_| *(self.ifr.ifr_ifru.ifru_data as *mut EthtoolCommnad))
.map_err(|_| EthtoolError::new("Failed to get EthtoolCommnad"))
}
}

Expand All @@ -204,20 +214,4 @@ impl CmdContext {
pub fn ifname(&self) -> &str {
&self.devname
}

/// Send ioctl command to kernel, which will fill the ifreq struct.
pub fn send_ioctl(mut self, mut ecmd: EthtoolCommnad) -> Result<CmdContext, EthtoolError> {
self.ifr = ifreq {
ifr_name: str_to_arr(&self.devname),
ifr_ifru: libc::__c_anonymous_ifr_ifru {
ifru_data: unsafe { transmute(&mut ecmd as *mut _) },
},
};
let ret = unsafe { ethtool_ioctl(self.fd, &mut self.ifr) };
if ret.is_ok() {
Ok(self)
} else {
Err(EthtoolError::new("Failed to send EthtoolCommnad"))
}
}
}
6 changes: 2 additions & 4 deletions ethernet-info/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,7 @@ fn do_ioctl_get_ethernet_info(mut ctx: CmdContext) -> Result<EthernetInfo, Ethto
* opposite of what it is expecting. We request length 0 below
* (aka. invalid bitmap length) to get this info.
*/
ctx = ctx.send_ioctl(ecmd)?;
ecmd = ctx.get_ethtool_link_settings();
ecmd = ctx.get_ethtool_link_settings(ecmd)?;
if ecmd.req.link_mode_masks_nwords >= 0 || ecmd.req.cmd != ETHTOOL_GLINKSETTINGS {
return Err(EthtoolError::new(
"Failed to determine number of words for link mode bitmaps",
Expand All @@ -137,8 +136,7 @@ fn do_ioctl_get_ethernet_info(mut ctx: CmdContext) -> Result<EthernetInfo, Ethto
*/
ecmd.req.cmd = ETHTOOL_GLINKSETTINGS;
ecmd.req.link_mode_masks_nwords = -ecmd.req.link_mode_masks_nwords;
ctx = ctx.send_ioctl(ecmd)?;
ecmd = ctx.get_ethtool_link_settings();
ecmd = ctx.get_ethtool_link_settings(ecmd)?;

/* check the link_mode_masks_nwords again */
if ecmd.req.link_mode_masks_nwords <= 0 || ecmd.req.cmd != ETHTOOL_GLINKSETTINGS {
Expand Down

0 comments on commit bc7baa8

Please sign in to comment.