From b9c9cbf9267cc9b19649e8bdbf679946d40d9a23 Mon Sep 17 00:00:00 2001 From: Steven Hartland Date: Fri, 8 May 2020 14:17:55 +0000 Subject: [PATCH] net: only enable broadcast on sockets which support it Only enable broadcast on SOCK_DGRAM and SOCK_RAW sockets, SOCK_STREAM and others don't support it. Don't enable SO_BROADCAST on UNIX domain sockets as they don't support it. This caused failures on WSL which strictly checks setsockopt calls unlike other OSes which often silently ignore bad options. Also return error for setsockopt call for SO_BROADCAST on Windows matching all other platforms but for IPv4 only as it's not supported on IPv6 as per: https://docs.microsoft.com/en-us/windows/win32/winsock/socket-options Fixes #38954 Change-Id: I0503fd1ce96102b17121af548b66b3e9c2bb80d3 Reviewed-on: https://go-review.googlesource.com/c/go/+/232807 Run-TryBot: Ian Lance Taylor TryBot-Result: Gobot Gobot Reviewed-by: Ian Lance Taylor --- src/net/sockopt_aix.go | 7 +++++-- src/net/sockopt_bsd.go | 7 +++++-- src/net/sockopt_linux.go | 7 +++++-- src/net/sockopt_solaris.go | 7 +++++-- src/net/sockopt_windows.go | 6 ++++-- 5 files changed, 24 insertions(+), 10 deletions(-) diff --git a/src/net/sockopt_aix.go b/src/net/sockopt_aix.go index b49c4d5c7c..7729a4470b 100644 --- a/src/net/sockopt_aix.go +++ b/src/net/sockopt_aix.go @@ -16,8 +16,11 @@ func setDefaultSockopts(s, family, sotype int, ipv6only bool) error { // never admit this option. syscall.SetsockoptInt(s, syscall.IPPROTO_IPV6, syscall.IPV6_V6ONLY, boolint(ipv6only)) } - // Allow broadcast. - return os.NewSyscallError("setsockopt", syscall.SetsockoptInt(s, syscall.SOL_SOCKET, syscall.SO_BROADCAST, 1)) + if (sotype == syscall.SOCK_DGRAM || sotype == syscall.SOCK_RAW) && family != syscall.AF_UNIX { + // Allow broadcast. + return os.NewSyscallError("setsockopt", syscall.SetsockoptInt(s, syscall.SOL_SOCKET, syscall.SO_BROADCAST, 1)) + } + return nil } func setDefaultListenerSockopts(s int) error { diff --git a/src/net/sockopt_bsd.go b/src/net/sockopt_bsd.go index 4ecc8cb1cf..8fd1e882c6 100644 --- a/src/net/sockopt_bsd.go +++ b/src/net/sockopt_bsd.go @@ -31,8 +31,11 @@ func setDefaultSockopts(s, family, sotype int, ipv6only bool) error { // never admit this option. syscall.SetsockoptInt(s, syscall.IPPROTO_IPV6, syscall.IPV6_V6ONLY, boolint(ipv6only)) } - // Allow broadcast. - return os.NewSyscallError("setsockopt", syscall.SetsockoptInt(s, syscall.SOL_SOCKET, syscall.SO_BROADCAST, 1)) + if (sotype == syscall.SOCK_DGRAM || sotype == syscall.SOCK_RAW) && family != syscall.AF_UNIX { + // Allow broadcast. + return os.NewSyscallError("setsockopt", syscall.SetsockoptInt(s, syscall.SOL_SOCKET, syscall.SO_BROADCAST, 1)) + } + return nil } func setDefaultListenerSockopts(s int) error { diff --git a/src/net/sockopt_linux.go b/src/net/sockopt_linux.go index 0f70b12407..3d544299ac 100644 --- a/src/net/sockopt_linux.go +++ b/src/net/sockopt_linux.go @@ -16,8 +16,11 @@ func setDefaultSockopts(s, family, sotype int, ipv6only bool) error { // never admit this option. syscall.SetsockoptInt(s, syscall.IPPROTO_IPV6, syscall.IPV6_V6ONLY, boolint(ipv6only)) } - // Allow broadcast. - return os.NewSyscallError("setsockopt", syscall.SetsockoptInt(s, syscall.SOL_SOCKET, syscall.SO_BROADCAST, 1)) + if (sotype == syscall.SOCK_DGRAM || sotype == syscall.SOCK_RAW) && family != syscall.AF_UNIX { + // Allow broadcast. + return os.NewSyscallError("setsockopt", syscall.SetsockoptInt(s, syscall.SOL_SOCKET, syscall.SO_BROADCAST, 1)) + } + return nil } func setDefaultListenerSockopts(s int) error { diff --git a/src/net/sockopt_solaris.go b/src/net/sockopt_solaris.go index 0f70b12407..3d544299ac 100644 --- a/src/net/sockopt_solaris.go +++ b/src/net/sockopt_solaris.go @@ -16,8 +16,11 @@ func setDefaultSockopts(s, family, sotype int, ipv6only bool) error { // never admit this option. syscall.SetsockoptInt(s, syscall.IPPROTO_IPV6, syscall.IPV6_V6ONLY, boolint(ipv6only)) } - // Allow broadcast. - return os.NewSyscallError("setsockopt", syscall.SetsockoptInt(s, syscall.SOL_SOCKET, syscall.SO_BROADCAST, 1)) + if (sotype == syscall.SOCK_DGRAM || sotype == syscall.SOCK_RAW) && family != syscall.AF_UNIX { + // Allow broadcast. + return os.NewSyscallError("setsockopt", syscall.SetsockoptInt(s, syscall.SOL_SOCKET, syscall.SO_BROADCAST, 1)) + } + return nil } func setDefaultListenerSockopts(s int) error { diff --git a/src/net/sockopt_windows.go b/src/net/sockopt_windows.go index 8017426521..8afaf34514 100644 --- a/src/net/sockopt_windows.go +++ b/src/net/sockopt_windows.go @@ -16,8 +16,10 @@ func setDefaultSockopts(s syscall.Handle, family, sotype int, ipv6only bool) err // never admit this option. syscall.SetsockoptInt(s, syscall.IPPROTO_IPV6, syscall.IPV6_V6ONLY, boolint(ipv6only)) } - // Allow broadcast. - syscall.SetsockoptInt(s, syscall.SOL_SOCKET, syscall.SO_BROADCAST, 1) + if (sotype == syscall.SOCK_DGRAM || sotype == syscall.SOCK_RAW) && family != syscall.AF_UNIX && family != syscall.AF_INET6 { + // Allow broadcast. + return os.NewSyscallError("setsockopt", syscall.SetsockoptInt(s, syscall.SOL_SOCKET, syscall.SO_BROADCAST, 1)) + } return nil }