From 9b4f2b84f10c96efa37910f324bc66e27aec3828 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sa=C3=BAl=20Ibarra=20Corretg=C3=A9?= Date: Tue, 22 Apr 2014 22:45:58 +0200 Subject: [PATCH] unix, windows: validate flags on uv_udp|tcp_bind fixes #1247 --- src/unix/tcp.c | 4 ++++ src/win/tcp.c | 4 ++++ test/test-list.h | 2 ++ test/test-tcp-bind-error.c | 17 +++++++++++++++++ 4 files changed, 27 insertions(+) diff --git a/src/unix/tcp.c b/src/unix/tcp.c index cd842b08..8c19c1ab 100644 --- a/src/unix/tcp.c +++ b/src/unix/tcp.c @@ -63,6 +63,10 @@ int uv__tcp_bind(uv_tcp_t* tcp, int err; int on; + /* Cannot set IPv6-only mode on non-IPv6 socket. */ + if ((flags & UV_TCP_IPV6ONLY) && addr->sa_family != AF_INET6) + return -EINVAL; + err = maybe_new_socket(tcp, addr->sa_family, UV_STREAM_READABLE | UV_STREAM_WRITABLE); diff --git a/src/win/tcp.c b/src/win/tcp.c index 5dcdcd47..34df921b 100644 --- a/src/win/tcp.c +++ b/src/win/tcp.c @@ -243,6 +243,10 @@ static int uv_tcp_try_bind(uv_tcp_t* handle, int r; if (handle->socket == INVALID_SOCKET) { + /* Cannot set IPv6-only mode on non-IPv6 socket. */ + if ((flags & UV_TCP_IPV6ONLY) && addr->sa_family != AF_INET6) + return ERROR_INVALID_PARAMETER; + SOCKET sock = socket(addr->sa_family, SOCK_STREAM, 0); if (sock == INVALID_SOCKET) { return WSAGetLastError(); diff --git a/test/test-list.h b/test/test-list.h index afb9a7ac..5bb8f67a 100644 --- a/test/test-list.h +++ b/test/test-list.h @@ -65,6 +65,7 @@ TEST_DECLARE (tcp_bind_error_addrnotavail_2) TEST_DECLARE (tcp_bind_error_fault) TEST_DECLARE (tcp_bind_error_inval) TEST_DECLARE (tcp_bind_localhost_ok) +TEST_DECLARE (tcp_bind_invalid_flags) TEST_DECLARE (tcp_listen_without_bind) TEST_DECLARE (tcp_connect_error_fault) TEST_DECLARE (tcp_connect_timeout) @@ -335,6 +336,7 @@ TASK_LIST_START TEST_ENTRY (tcp_bind_error_fault) TEST_ENTRY (tcp_bind_error_inval) TEST_ENTRY (tcp_bind_localhost_ok) + TEST_ENTRY (tcp_bind_invalid_flags) TEST_ENTRY (tcp_listen_without_bind) TEST_ENTRY (tcp_connect_error_fault) TEST_ENTRY (tcp_connect_timeout) diff --git a/test/test-tcp-bind-error.c b/test/test-tcp-bind-error.c index 96bfe116..10ed68e1 100644 --- a/test/test-tcp-bind-error.c +++ b/test/test-tcp-bind-error.c @@ -185,6 +185,23 @@ TEST_IMPL(tcp_bind_localhost_ok) { } +TEST_IMPL(tcp_bind_invalid_flags) { + struct sockaddr_in addr; + uv_tcp_t server; + int r; + + ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr)); + + r = uv_tcp_init(uv_default_loop(), &server); + ASSERT(r == 0); + r = uv_tcp_bind(&server, (const struct sockaddr*) &addr, UV_TCP_IPV6ONLY); + ASSERT(r == UV_EINVAL); + + MAKE_VALGRIND_HAPPY(); + return 0; +} + + TEST_IMPL(tcp_listen_without_bind) { int r; uv_tcp_t server;