From ee37daadeb0dc6f8e6125f7a51c2dd0a47fc73bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juozapas=20Bo=C4=8Dkus?= Date: Wed, 11 Dec 2024 14:51:01 +0200 Subject: [PATCH] Fix double tunsetif and invalid cstring handling on linux --- neptun/src/device/tun_linux.rs | 26 ++++---------------------- 1 file changed, 4 insertions(+), 22 deletions(-) diff --git a/neptun/src/device/tun_linux.rs b/neptun/src/device/tun_linux.rs index d31e6ea..ad62ca8 100644 --- a/neptun/src/device/tun_linux.rs +++ b/neptun/src/device/tun_linux.rs @@ -120,7 +120,7 @@ impl TunSocket { pub fn new_from_fd(fd: RawFd) -> Result { #[cfg(target_os = "linux")] - let mut ifr = ifreq { + let ifr = ifreq { ifr_name: [0; IFNAMSIZ], ifr_ifru: IfrIfru { ifru_intval: 0 }, }; @@ -140,29 +140,11 @@ impl TunSocket { if flags & IFF_TUN as c_short == 0 { return Err(Error::InvalidTunnelName); } - let name = std::str::from_utf8(&ifr.ifr_name[..]) + let name = std::ffi::CStr::from_bytes_until_nul(&ifr.ifr_name) + .map_err(|_| Error::InvalidTunnelName)? + .to_str() .map_err(|_| Error::InvalidTunnelName)? .to_owned(); - - #[cfg(target_os = "linux")] - { - ifr.ifr_ifru = IfrIfru { - ifru_flags: (IFF_TUN | IFF_MULTI_QUEUE) as _, - }; - if unsafe { ioctl(fd, TUNSETIFF as _, &ifr) } < 0 { - let error = Error::IOCtl(io::Error::last_os_error()); - let flags = unsafe { format!("{:x}", ifr.ifr_ifru.ifru_flags) }; - error!( - ?error, - op = "TUNSETIFF", - name, - flags, - "Failed to configure tunnel flags" - ); - return Err(error); - } - } - Ok(TunSocket { fd, name }) }