-
Notifications
You must be signed in to change notification settings - Fork 959
Use O_CLOEXEC to avoid race conditions #10162
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
5dd1eb7
fd052e1
b6087b6
b0c5f23
3fb83aa
8820776
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -24,6 +24,15 @@ | |||||||||||||
| #define WOLFSSL_STRERROR_BUFFER_SIZE 256 | ||||||||||||||
| #endif | ||||||||||||||
|
|
||||||||||||||
| /* Enable GNU extensions for accept4() on Linux/glibc. Must be defined | ||||||||||||||
| * before any system headers are included. Excluded for Zephyr and other | ||||||||||||||
| * embedded RTOSes whose libc layers conflict with glibc-style definitions | ||||||||||||||
| * (e.g., Zephyr's socket_select.h vs. glibc's fd_set). */ | ||||||||||||||
| #if (defined(__linux__) || defined(__ANDROID__)) && \ | ||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟠 [High] wolfio.c: _GNU_SOURCE placement may conflict if libwolfssl_sources.h pulls in system headers
If Note: in practice, Suggestion:
Suggested change
|
||||||||||||||
| !defined(WOLFSSL_ZEPHYR) && !defined(_GNU_SOURCE) | ||||||||||||||
| #define _GNU_SOURCE 1 | ||||||||||||||
| #endif | ||||||||||||||
|
|
||||||||||||||
| #include <wolfssl/wolfcrypt/libwolfssl_sources.h> | ||||||||||||||
|
|
||||||||||||||
| #ifndef WOLFCRYPT_ONLY | ||||||||||||||
|
|
@@ -42,6 +51,12 @@ | |||||||||||||
| #include <wolfssl/wolfio.h> | ||||||||||||||
| #include <wolfssl/wolfcrypt/logging.h> | ||||||||||||||
|
|
||||||||||||||
| /* SOCK_CLOEXEC sets close-on-exec atomically when the socket is created; | ||||||||||||||
| * fall back to a no-op flag value where it isn't supported. */ | ||||||||||||||
| #ifndef SOCK_CLOEXEC | ||||||||||||||
| #define SOCK_CLOEXEC 0 | ||||||||||||||
| #endif | ||||||||||||||
|
|
||||||||||||||
| #ifdef NUCLEUS_PLUS_2_3 | ||||||||||||||
| /* Holds last Nucleus networking error number */ | ||||||||||||||
| int Nucleus_Net_Errno; | ||||||||||||||
|
|
@@ -1494,7 +1509,17 @@ int wolfIO_TcpConnect(SOCKET_T* sockfd, const char* ip, word16 port, int to_sec) | |||||||||||||
| } | ||||||||||||||
| #endif | ||||||||||||||
|
|
||||||||||||||
| *sockfd = (SOCKET_T)socket(addr.ss_family, SOCK_STREAM, 0); | ||||||||||||||
| *sockfd = (SOCKET_T)socket(addr.ss_family, SOCK_STREAM | SOCK_CLOEXEC, 0); | ||||||||||||||
embhorn marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||
| #if !defined(USE_WINDOWS_API) && defined(FD_CLOEXEC) | ||||||||||||||
| if (*sockfd <= SOCKET_INVALID && errno == EINVAL) { | ||||||||||||||
| *sockfd = (SOCKET_T)socket(addr.ss_family, SOCK_STREAM, 0); | ||||||||||||||
| if (*sockfd > SOCKET_INVALID) { | ||||||||||||||
| int fdFlags = fcntl(*sockfd, F_GETFD); | ||||||||||||||
| if (fdFlags >= 0) | ||||||||||||||
| (void)fcntl(*sockfd, F_SETFD, fdFlags | FD_CLOEXEC); | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
| #endif | ||||||||||||||
| #ifdef USE_WINDOWS_API | ||||||||||||||
| if (*sockfd == SOCKET_INVALID) | ||||||||||||||
| #else | ||||||||||||||
|
|
@@ -1572,12 +1597,32 @@ int wolfIO_TcpBind(SOCKET_T* sockfd, word16 port) | |||||||||||||
| sin->sin6_family = AF_INET6; | ||||||||||||||
| sin->sin6_addr = in6addr_any; | ||||||||||||||
| sin->sin6_port = XHTONS(port); | ||||||||||||||
| *sockfd = (SOCKET_T)socket(AF_INET6, SOCK_STREAM, 0); | ||||||||||||||
| *sockfd = (SOCKET_T)socket(AF_INET6, SOCK_STREAM | SOCK_CLOEXEC, 0); | ||||||||||||||
| #if defined(FD_CLOEXEC) | ||||||||||||||
| if (*sockfd <= SOCKET_INVALID && errno == EINVAL) { | ||||||||||||||
| *sockfd = (SOCKET_T)socket(AF_INET6, SOCK_STREAM, 0); | ||||||||||||||
| if (*sockfd > SOCKET_INVALID) { | ||||||||||||||
| int fdFlags = fcntl(*sockfd, F_GETFD); | ||||||||||||||
| if (fdFlags >= 0) | ||||||||||||||
| (void)fcntl(*sockfd, F_SETFD, fdFlags | FD_CLOEXEC); | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
| #endif | ||||||||||||||
| #else | ||||||||||||||
| sin->sin_family = AF_INET; | ||||||||||||||
| sin->sin_addr.s_addr = INADDR_ANY; | ||||||||||||||
| sin->sin_port = XHTONS(port); | ||||||||||||||
| *sockfd = (SOCKET_T)socket(AF_INET, SOCK_STREAM, 0); | ||||||||||||||
| *sockfd = (SOCKET_T)socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0); | ||||||||||||||
| #if defined(FD_CLOEXEC) | ||||||||||||||
| if (*sockfd <= SOCKET_INVALID && errno == EINVAL) { | ||||||||||||||
| *sockfd = (SOCKET_T)socket(AF_INET, SOCK_STREAM, 0); | ||||||||||||||
| if (*sockfd > SOCKET_INVALID) { | ||||||||||||||
| int fdFlags = fcntl(*sockfd, F_GETFD); | ||||||||||||||
| if (fdFlags >= 0) | ||||||||||||||
| (void)fcntl(*sockfd, F_SETFD, fdFlags | FD_CLOEXEC); | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
| #endif | ||||||||||||||
| #endif | ||||||||||||||
|
|
||||||||||||||
| #ifdef USE_WINDOWS_API | ||||||||||||||
|
|
@@ -1623,7 +1668,31 @@ int wolfIO_TcpBind(SOCKET_T* sockfd, word16 port) | |||||||||||||
| #ifdef HAVE_SOCKADDR | ||||||||||||||
| int wolfIO_TcpAccept(SOCKET_T sockfd, SOCKADDR* peer_addr, XSOCKLENT* peer_len) | ||||||||||||||
| { | ||||||||||||||
| return (int)accept(sockfd, peer_addr, peer_len); | ||||||||||||||
| int fd; | ||||||||||||||
| #if !defined(USE_WINDOWS_API) && !defined(WOLFSSL_ZEPHYR) && \ | ||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔵 [Low] wolfio.c: defined(SOCK_CLOEXEC) guard in accept4 path is tautologically true The fallback Suggestion:
Suggested change
|
||||||||||||||
| (defined(__linux__) || defined(__ANDROID__)) | ||||||||||||||
| fd = (int)accept4(sockfd, peer_addr, peer_len, SOCK_CLOEXEC); | ||||||||||||||
| if (fd < 0 && (errno == ENOSYS || errno == EINVAL)) { | ||||||||||||||
| fd = (int)accept(sockfd, peer_addr, peer_len); | ||||||||||||||
| #ifdef FD_CLOEXEC | ||||||||||||||
| if (fd >= 0) { | ||||||||||||||
| int fdFlags = fcntl(fd, F_GETFD); | ||||||||||||||
| if (fdFlags >= 0) | ||||||||||||||
| (void)fcntl(fd, F_SETFD, fdFlags | FD_CLOEXEC); | ||||||||||||||
| } | ||||||||||||||
| #endif | ||||||||||||||
| } | ||||||||||||||
| #else | ||||||||||||||
embhorn marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||
| fd = (int)accept(sockfd, peer_addr, peer_len); | ||||||||||||||
| #if defined(FD_CLOEXEC) && !defined(USE_WINDOWS_API) | ||||||||||||||
| if (fd >= 0) { | ||||||||||||||
| int fdFlags = fcntl(fd, F_GETFD); | ||||||||||||||
| if (fdFlags >= 0) | ||||||||||||||
| (void)fcntl(fd, F_SETFD, fdFlags | FD_CLOEXEC); | ||||||||||||||
| } | ||||||||||||||
| #endif | ||||||||||||||
| #endif | ||||||||||||||
| return fd; | ||||||||||||||
| } | ||||||||||||||
| #endif /* HAVE_SOCKADDR */ | ||||||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.