Skip to content

Commit 77b9232

Browse files
MBaeskenpull[bot]
authored andcommitted
8320168: handle setsocktopt return values
Reviewed-by: lucy, alanb, vtewari
1 parent b13965d commit 77b9232

File tree

8 files changed

+53
-15
lines changed

8 files changed

+53
-15
lines changed

src/java.base/unix/native/libnet/Inet4AddressImpl.c

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,11 @@ tcp_ping4(JNIEnv *env, SOCKETADDRESS *sa, SOCKETADDRESS *netif, jint timeout,
263263

264264
// set TTL
265265
if (ttl > 0) {
266-
setsockopt(fd, IPPROTO_IP, IP_TTL, &ttl, sizeof(ttl));
266+
if (setsockopt(fd, IPPROTO_IP, IP_TTL, &ttl, sizeof(ttl)) < 0) {
267+
NET_ThrowNew(env, errno, "setsockopt IP_TTL failed");
268+
close(fd);
269+
return JNI_FALSE;
270+
}
267271
}
268272

269273
// A network interface was specified, so let's bind to it.
@@ -349,11 +353,19 @@ ping4(JNIEnv *env, jint fd, SOCKETADDRESS *sa, SOCKETADDRESS *netif,
349353
struct timeval tv = { 0, 0 };
350354
const size_t plen = ICMP_MINLEN + sizeof(tv);
351355

352-
setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &size, sizeof(size));
356+
if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &size, sizeof(size)) < 0) {
357+
NET_ThrowNew(env, errno, "setsockopt SO_RCVBUF failed");
358+
close(fd);
359+
return JNI_FALSE;
360+
}
353361

354362
// sets the ttl (max number of hops)
355363
if (ttl > 0) {
356-
setsockopt(fd, IPPROTO_IP, IP_TTL, &ttl, sizeof(ttl));
364+
if (setsockopt(fd, IPPROTO_IP, IP_TTL, &ttl, sizeof(ttl)) < 0) {
365+
NET_ThrowNew(env, errno, "setsockopt IP_TTL failed");
366+
close(fd);
367+
return JNI_FALSE;
368+
}
357369
}
358370

359371
// a specific interface was specified, so let's bind the socket

src/java.base/unix/native/libnet/Inet6AddressImpl.c

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -463,12 +463,16 @@ tcp_ping6(JNIEnv *env, SOCKETADDRESS *sa, SOCKETADDRESS *netif, jint timeout,
463463

464464
// set TTL
465465
if (ttl > 0) {
466-
setsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &ttl, sizeof(ttl));
466+
if (setsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &ttl, sizeof(ttl)) < 0) {
467+
NET_ThrowNew(env, errno, "setsockopt IPV6_UNICAST_HOPS failed");
468+
close(fd);
469+
return JNI_FALSE;
470+
}
467471
}
468472

469473
// A network interface was specified, so let's bind to it.
470474
if (netif != NULL) {
471-
if (bind(fd, &netif->sa, sizeof(struct sockaddr_in6)) <0) {
475+
if (bind(fd, &netif->sa, sizeof(struct sockaddr_in6)) < 0) {
472476
NET_ThrowNew(env, errno, "Can't bind socket");
473477
close(fd);
474478
return JNI_FALSE;
@@ -557,11 +561,19 @@ ping6(JNIEnv *env, jint fd, SOCKETADDRESS *sa, SOCKETADDRESS *netif,
557561
setsockopt(fd, SOL_RAW, IPV6_CHECKSUM, &csum_offset, sizeof(int));
558562
#endif
559563

560-
setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &size, sizeof(size));
564+
if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &size, sizeof(size)) < 0) {
565+
NET_ThrowNew(env, errno, "setsockopt SO_RCVBUF failed");
566+
close(fd);
567+
return JNI_FALSE;
568+
}
561569

562570
// sets the ttl (max number of hops)
563571
if (ttl > 0) {
564-
setsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &ttl, sizeof(ttl));
572+
if (setsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &ttl, sizeof(ttl)) < 0) {
573+
NET_ThrowNew(env, errno, "setsockopt IPV6_UNICAST_HOPS failed");
574+
close(fd);
575+
return JNI_FALSE;
576+
}
565577
}
566578

567579
// a specific interface was specified, so let's bind the socket

src/java.base/unix/native/libnet/net_util_md.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,9 @@ NET_SetSockOpt(int fd, int level, int opt, const void *arg,
536536
}
537537

538538
if (sotype == SOCK_DGRAM) {
539-
setsockopt(fd, level, SO_REUSEPORT, arg, len);
539+
if (setsockopt(fd, level, SO_REUSEPORT, arg, len) < 0) {
540+
return -1;
541+
}
540542
}
541543
}
542544
#endif

src/java.base/windows/native/libnet/Inet4AddressImpl.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,11 @@ tcp_ping4(JNIEnv *env, SOCKETADDRESS *sa, SOCKETADDRESS *netif, jint timeout,
232232

233233
// set TTL
234234
if (ttl > 0) {
235-
setsockopt(fd, IPPROTO_IP, IP_TTL, (const char *)&ttl, sizeof(ttl));
235+
if (setsockopt(fd, IPPROTO_IP, IP_TTL, (const char *)&ttl, sizeof(ttl)) == SOCKET_ERROR) {
236+
NET_ThrowNew(env, WSAGetLastError(), "setsockopt IP_TTL failed");
237+
closesocket(fd);
238+
return JNI_FALSE;
239+
}
236240
}
237241

238242
// A network interface was specified, so let's bind to it.

src/java.base/windows/native/libnet/Inet6AddressImpl.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,11 @@ tcp_ping6(JNIEnv *env, SOCKETADDRESS *sa, SOCKETADDRESS *netif, jint timeout,
310310

311311
// set TTL
312312
if (ttl > 0) {
313-
setsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, (const char *)&ttl, sizeof(ttl));
313+
if (setsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, (const char *)&ttl, sizeof(ttl)) == SOCKET_ERROR) {
314+
NET_ThrowNew(env, WSAGetLastError(), "setsockopt IPV6_UNICAST_HOPS failed");
315+
closesocket(fd);
316+
return JNI_FALSE;
317+
}
314318
}
315319

316320
// A network interface was specified, so let's bind to it.

src/java.base/windows/native/libnio/ch/Net.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ Java_sun_nio_ch_Net_socket0(JNIEnv *env, jclass cl, jboolean preferIPv6,
156156
if (s != INVALID_SOCKET) {
157157
SetHandleInformation((HANDLE)s, HANDLE_FLAG_INHERIT, 0);
158158

159-
/* IPV6_V6ONLY is true by default */
159+
/* Attempt to disable IPV6_V6ONLY to ensure dual-socket support; ignore errors */
160160
if (domain == AF_INET6) {
161161
int opt = 0;
162162
setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY,

src/java.base/windows/native/libnio/ch/WindowsAsynchronousServerSocketChannelImpl.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2008, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2008, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -131,7 +131,9 @@ Java_sun_nio_ch_WindowsAsynchronousServerSocketChannelImpl_updateAcceptContext(J
131131
SOCKET s1 = (SOCKET)jlong_to_ptr(listenSocket);
132132
SOCKET s2 = (SOCKET)jlong_to_ptr(acceptSocket);
133133

134-
setsockopt(s2, SOL_SOCKET, SO_UPDATE_ACCEPT_CONTEXT, (char *)&s1, sizeof(s1));
134+
if (setsockopt(s2, SOL_SOCKET, SO_UPDATE_ACCEPT_CONTEXT, (char *)&s1, sizeof(s1)) == SOCKET_ERROR) {
135+
JNU_ThrowIOExceptionWithLastError(env, "setsockopt failed");
136+
}
135137
}
136138

137139

src/java.base/windows/native/libnio/ch/WindowsAsynchronousSocketChannelImpl.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2008, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2008, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -123,7 +123,9 @@ Java_sun_nio_ch_WindowsAsynchronousSocketChannelImpl_updateConnectContext(JNIEnv
123123
jlong socket)
124124
{
125125
SOCKET s = (SOCKET)jlong_to_ptr(socket);
126-
setsockopt(s, SOL_SOCKET, SO_UPDATE_CONNECT_CONTEXT, NULL, 0);
126+
if (setsockopt(s, SOL_SOCKET, SO_UPDATE_CONNECT_CONTEXT, NULL, 0) == SOCKET_ERROR) {
127+
JNU_ThrowIOExceptionWithLastError(env, "setsockopt failed");
128+
}
127129
}
128130

129131

0 commit comments

Comments
 (0)