-
Notifications
You must be signed in to change notification settings - Fork 969
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
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 |
|---|---|---|
|
|
@@ -1574,16 +1574,30 @@ static const char* bench_result_words3[][5] = { | |
| #include <linux/perf_event.h> | ||
| #include <sys/syscall.h> | ||
| #include <unistd.h> | ||
| #include <fcntl.h> | ||
| #include <errno.h> | ||
|
|
||
| #ifndef PERF_FLAG_FD_CLOEXEC | ||
| #define PERF_FLAG_FD_CLOEXEC (1UL << 3) | ||
| #endif | ||
|
|
||
| static THREAD_LS_T word64 begin_cycles; | ||
| static THREAD_LS_T word64 total_cycles; | ||
| static THREAD_LS_T int cycles = -1; | ||
| static THREAD_LS_T struct perf_event_attr atr; | ||
|
|
||
| /* Try with PERF_FLAG_FD_CLOEXEC first; on older kernels (< 3.14) this | ||
|
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. Do we need to support ancient kernels ? Why not to drop support for too old kernels/headers ? |
||
| * fails with EINVAL, so fall back to flags=0 and set FD_CLOEXEC via | ||
| * fcntl() as a best-effort. */ | ||
| #define INIT_CYCLE_COUNTER do { \ | ||
| atr.type = PERF_TYPE_HARDWARE; \ | ||
| atr.config = PERF_COUNT_HW_CPU_CYCLES; \ | ||
| cycles = (int)syscall(__NR_perf_event_open, &atr, 0, -1, -1, 0); \ | ||
| cycles = (int)syscall(__NR_perf_event_open, &atr, 0, -1, -1, \ | ||
| PERF_FLAG_FD_CLOEXEC); \ | ||
| if (cycles < 0 && errno == EINVAL) { \ | ||
| cycles = (int)syscall(__NR_perf_event_open, &atr, 0, -1, -1, 0); \ | ||
| wc_set_cloexec(cycles); \ | ||
| } \ | ||
| } while (0); | ||
|
embhorn marked this conversation as resolved.
|
||
|
|
||
| #define BEGIN_CYCLES read(cycles, &begin_cycles, sizeof(begin_cycles)); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,13 +19,19 @@ | |
| * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA | ||
| */ | ||
|
|
||
| #if defined(__linux__) && !defined(_GNU_SOURCE) | ||
| #define _GNU_SOURCE 1 | ||
|
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. Move that please to the global build config (in order to use this definition for all files in the project). |
||
| #endif | ||
|
|
||
| #include <wolfssl/wolfcrypt/libwolfssl_sources.h> | ||
|
|
||
| #if defined(WOLFSSL_AFALG_HASH) || (defined(WOLFSSL_AFALG_XILINX_SHA3) \ | ||
| && defined(WOLFSSL_SHA3)) | ||
|
|
||
| #include <wolfssl/wolfcrypt/port/af_alg/wc_afalg.h> | ||
| #include <wolfssl/wolfcrypt/port/af_alg/afalg_hash.h> | ||
| #include <errno.h> | ||
| #include <fcntl.h> | ||
|
|
||
| static const char WC_TYPE_HASH[] = "hash"; | ||
|
|
||
|
|
@@ -223,8 +229,8 @@ static int AfalgHashCopy(wolfssl_AFALG_Hash* src, wolfssl_AFALG_Hash* dst) | |
| } | ||
| #endif | ||
|
|
||
| dst->rdFd = accept(src->rdFd, NULL, 0); | ||
| dst->alFd = accept(src->alFd, NULL, 0); | ||
| dst->rdFd = wc_accept_cloexec(src->rdFd, NULL, NULL); | ||
| dst->alFd = wc_accept_cloexec(src->alFd, NULL, NULL); | ||
|
|
||
| if (dst->rdFd == WC_SOCK_NOTSET || dst->alFd == WC_SOCK_NOTSET) { | ||
| AfalgHashFree(dst); | ||
|
|
||
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.
No, solution is racy. Use kqueue1()