-
Notifications
You must be signed in to change notification settings - Fork 958
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
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 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -1659,13 +1659,19 @@ static int SwapLists(WOLFSSL_CRL* crl) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #include <sys/time.h> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #include <fcntl.h> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #include <unistd.h> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #include <errno.h> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #ifdef __MACH__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #define XEVENT_MODE O_EVTONLY | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #elif defined(__FreeBSD__) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #define XEVENT_MODE O_RDONLY | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #endif | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /* Fall back to no-op if O_CLOEXEC is unavailable on this platform. */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #ifndef O_CLOEXEC | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #define O_CLOEXEC 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #endif | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /* we need a unique kqueue user filter fd for crl in case user is doing custom | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * events too */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -1710,6 +1716,13 @@ static THREAD_RETURN WOLFSSL_THREAD DoMonitor(void* arg) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| SignalSetup(crl, MONITOR_SETUP_E); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return NULL; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #ifdef FD_CLOEXEC | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| int fdFlags = fcntl(crl->mfd, F_GETFD); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (fdFlags >= 0) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| (void)fcntl(crl->mfd, F_SETFD, fdFlags | FD_CLOEXEC); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #endif | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /* listen for custom shutdown event */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| EV_SET(&change, CRL_CUSTOM_FD, EVFILT_USER, EV_ADD, 0, 0, NULL); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -1724,7 +1737,17 @@ static THREAD_RETURN WOLFSSL_THREAD DoMonitor(void* arg) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fDER = -1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (crl->monitors[0].path) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fPEM = open(crl->monitors[0].path, XEVENT_MODE); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fPEM = open(crl->monitors[0].path, XEVENT_MODE | O_CLOEXEC); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #ifdef FD_CLOEXEC | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (fPEM == -1 && errno == EINVAL) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fPEM = open(crl->monitors[0].path, XEVENT_MODE); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (fPEM >= 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+1740
to
+1744
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| int fdFlags = fcntl(fPEM, F_GETFD); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (fdFlags >= 0) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| (void)fcntl(fPEM, F_SETFD, fdFlags | FD_CLOEXEC); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #endif | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (fPEM == -1) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| WOLFSSL_MSG("PEM event dir open failed"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| SignalSetup(crl, MONITOR_SETUP_E); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -1734,7 +1757,17 @@ static THREAD_RETURN WOLFSSL_THREAD DoMonitor(void* arg) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (crl->monitors[1].path) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fDER = open(crl->monitors[1].path, XEVENT_MODE); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fDER = open(crl->monitors[1].path, XEVENT_MODE | O_CLOEXEC); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #ifdef FD_CLOEXEC | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (fDER == -1 && errno == EINVAL) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fDER = open(crl->monitors[1].path, XEVENT_MODE); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (fDER >= 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| int fdFlags = fcntl(fDER, F_GETFD); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (fdFlags >= 0) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| (void)fcntl(fDER, F_SETFD, fdFlags | FD_CLOEXEC); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #endif | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (fDER == -1) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| WOLFSSL_MSG("DER event dir open failed"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (fPEM != -1) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -1801,6 +1834,13 @@ static THREAD_RETURN WOLFSSL_THREAD DoMonitor(void* arg) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #include <sys/inotify.h> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #include <sys/eventfd.h> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #include <unistd.h> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #include <fcntl.h> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #include <errno.h> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /* Fall back to no-op if EFD_CLOEXEC is unavailable. */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #ifndef EFD_CLOEXEC | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #define EFD_CLOEXEC 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+1840
to
+1842
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /* Fall back to no-op if EFD_CLOEXEC is unavailable. */ | |
| #ifndef EFD_CLOEXEC | |
| #define EFD_CLOEXEC 0 | |
| #ifndef EFD_CLOEXEC | |
| /* | |
| * Older platforms may not provide EFD_CLOEXEC. In that case, create the | |
| * eventfd without it and set FD_CLOEXEC with fcntl() as a best-effort | |
| * fallback. | |
| */ | |
| static int wolfssl_eventfd_cloexec_fallback(unsigned int initval, int flags) | |
| { | |
| int fd; | |
| int fdFlags; | |
| (void)flags; | |
| fd = eventfd(initval, 0); | |
| if (fd >= 0) { | |
| fdFlags = fcntl(fd, F_GETFD); | |
| if (fdFlags >= 0) { | |
| (void)fcntl(fd, F_SETFD, fdFlags | FD_CLOEXEC); | |
| } | |
| } | |
| return fd; | |
| } | |
| #define EFD_CLOEXEC 0 | |
| #define eventfd(initval, flags) \ | |
| wolfssl_eventfd_cloexec_fallback((initval), (flags)) |
Copilot
AI
Apr 13, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This runtime EINVAL fallback won’t fire when EFD_CLOEXEC is unavailable at compile time (because the first call becomes eventfd(0, 0) and succeeds). Consider a compile-time fallback path that sets FD_CLOEXEC via fcntl() after eventfd() whenever EFD_CLOEXEC isn’t defined/supported.
embhorn marked this conversation as resolved.
Show resolved
Hide resolved
embhorn marked this conversation as resolved.
Show resolved
Hide resolved
embhorn marked this conversation as resolved.
Show resolved
Hide resolved
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19568,6 +19568,11 @@ int wolfSSL_RAND_write_file(const char* fname) | |
| defined(HAVE_SYS_UN_H) | ||
| #define WOLFSSL_EGD_NBLOCK 0x01 | ||
| #include <sys/un.h> | ||
| #include <errno.h> | ||
| #include <fcntl.h> | ||
| #ifndef SOCK_CLOEXEC | ||
| #define SOCK_CLOEXEC 0 | ||
embhorn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| #endif | ||
|
Comment on lines
+19573
to
+19575
|
||
| #endif | ||
|
|
||
| /* This collects entropy from the path nm and seeds the global PRNG with it. | ||
|
|
@@ -19601,7 +19606,17 @@ int wolfSSL_RAND_egd(const char* nm) | |
| return WOLFSSL_FATAL_ERROR; | ||
| } | ||
|
|
||
| fd = socket(AF_UNIX, SOCK_STREAM, 0); | ||
| fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0); | ||
embhorn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| #ifdef FD_CLOEXEC | ||
| if (fd < 0 && errno == EINVAL) { | ||
| fd = socket(AF_UNIX, SOCK_STREAM, 0); | ||
| if (fd >= 0) { | ||
| int fdFlags = fcntl(fd, F_GETFD); | ||
| if (fdFlags >= 0) | ||
| (void)fcntl(fd, F_SETFD, fdFlags | FD_CLOEXEC); | ||
| } | ||
|
Comment on lines
+19613
to
+19617
|
||
| } | ||
| #endif | ||
| if (fd < 0) { | ||
| WOLFSSL_MSG("Error creating socket"); | ||
| WC_FREE_VAR_EX(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER); | ||
embhorn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| 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__)) && \ | ||||||||||||||||||||
embhorn marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||
| !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 | ||||||||||||||||||||
|
Comment on lines
+54
to
+58
|
||||||||||||||||||||
| /* 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 | |
| /* SOCK_CLOEXEC sets close-on-exec atomically when the socket is created. | |
| * Do not define it to 0 when unavailable; socket creation code must branch | |
| * at compile time and use a non-SOCK_CLOEXEC path on platforms that do not | |
| * provide the flag. */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Defining O_CLOEXEC as 0 means open(...|O_CLOEXEC) will silently omit close-on-exec when the macro is missing, and the EINVAL retry path won’t run. Prefer compile-time branching: if O_CLOEXEC is unavailable, open() without it and set FD_CLOEXEC via fcntl() (best-effort).