From 285a72d7760bb5e975e5d8c0bc5842b4b1c71345 Mon Sep 17 00:00:00 2001 From: zonyitoo Date: Sun, 16 Jun 2024 21:20:38 +0800 Subject: [PATCH] feat(shadowsocks): check local_addr is an IPv4-mapped-IPv6 address --- crates/shadowsocks/src/net/sys/mod.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/crates/shadowsocks/src/net/sys/mod.rs b/crates/shadowsocks/src/net/sys/mod.rs index 0531f9a3ed2a..0944f8c785f1 100644 --- a/crates/shadowsocks/src/net/sys/mod.rs +++ b/crates/shadowsocks/src/net/sys/mod.rs @@ -185,6 +185,25 @@ fn check_ipv4_mapped_ipv6_capability() -> io::Result<()> { socket1.listen(128)?; let socket1_address = socket1.local_addr()?; + match socket1_address.as_socket() { + None => return Err(io::Error::new(ErrorKind::Other, "invalid local_addr")), + Some(socket_addr) => match socket_addr { + SocketAddr::V4(..) => { + return Err(io::Error::new( + ErrorKind::Other, + "local_addr shouldn't be an IPv4 address", + )) + } + SocketAddr::V6(ref v6) => { + if let None = v6.ip().to_ipv4_mapped() { + return Err(io::Error::new( + ErrorKind::Other, + "local_addr is not an IPv4-mapped-IPv6 address", + )); + } + } + }, + } let socket2 = Socket::new(Domain::IPV6, Type::STREAM, Some(Protocol::TCP))?; socket2.set_only_v6(false)?;