From d7f22fd07c058f52ddacf087dcb749cb9c8de1e9 Mon Sep 17 00:00:00 2001 From: Uli Schlachter Date: Sat, 6 May 2023 16:13:51 +0200 Subject: [PATCH] Fix recvmsg() on abstract unix sockets This case was just not implemented at all. Abstract unix sockets are not null-terminated and are indicated by sun_path beginning with a null byte. Fixes: https://github.com/bytecodealliance/rustix/issues/660 Signed-off-by: Uli Schlachter --- src/backend/linux_raw/net/read_sockaddr.rs | 36 ++++++++++++++-------- 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/src/backend/linux_raw/net/read_sockaddr.rs b/src/backend/linux_raw/net/read_sockaddr.rs index b9bc09b96..fdab8d108 100644 --- a/src/backend/linux_raw/net/read_sockaddr.rs +++ b/src/backend/linux_raw/net/read_sockaddr.rs @@ -155,19 +155,31 @@ pub(crate) unsafe fn read_sockaddr_os(storage: *const c::sockaddr, len: usize) - SocketAddrAny::Unix(SocketAddrUnix::new(&[][..]).unwrap()) } else { let decode = *storage.cast::(); - assert_eq!( - decode.sun_path[len - 1 - offsetof_sun_path], - b'\0' as c::c_char - ); - SocketAddrAny::Unix( - SocketAddrUnix::new( - decode.sun_path[..len - 1 - offsetof_sun_path] - .iter() - .map(|c| *c as u8) - .collect::>(), + if decode.sun_path[0] == 0 { + SocketAddrAny::Unix( + SocketAddrUnix::new_abstract_name( + &decode.sun_path[1..len - offsetof_sun_path] + .iter() + .map(|c| *c as u8) + .collect::>(), + ) + .unwrap(), + ) + } else { + assert_eq!( + decode.sun_path[len - 1 - offsetof_sun_path], + b'\0' as c::c_char + ); + SocketAddrAny::Unix( + SocketAddrUnix::new( + decode.sun_path[..len - 1 - offsetof_sun_path] + .iter() + .map(|c| *c as u8) + .collect::>(), + ) + .unwrap(), ) - .unwrap(), - ) + } } } other => unimplemented!("{:?}", other),