Skip to content

Commit 72be219

Browse files
committed
Changes from review
1 parent 040eff6 commit 72be219

5 files changed

Lines changed: 40 additions & 11 deletions

File tree

.wolfssl_known_macro_extras

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,7 @@ ID_TRNG
315315
IGNORE_KEY_EXTENSIONS
316316
IGNORE_NETSCAPE_CERT_TYPE
317317
INCLUDE_uxTaskGetStackHighWaterMark
318+
IN_CLOEXEC
318319
INTEGRITY
319320
INTIMEVER
320321
IOTSAFE_NO_GETDATA
@@ -468,6 +469,7 @@ NO_WOLFSSL_XILINX_TAG_MALLOC
468469
NRF52
469470
NRF52_SERIES
470471
NRF_ERROR_MODULE_ALREADY_INITIALIZED
472+
O_CLOEXEC
471473
OLD_HELLO_ALLOWED
472474
OPENSSL_EXTRA_BSD
473475
OPENSSL_EXTRA_NO_ASN1
@@ -519,6 +521,7 @@ SL_SE_KEY_TYPE_ECC_X25519
519521
SL_SE_KEY_TYPE_ECC_X448
520522
SL_SE_PRF_HMAC_SHA1
521523
SNIFFER_SINGLE_SESSION_CACHE
524+
SOCK_CLOEXEC
522525
SOFTDEVICE_PRESENT
523526
SO_NOSIGPIPE
524527
SO_REUSEPORT

src/wolfio.c

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@
2424
#define WOLFSSL_STRERROR_BUFFER_SIZE 256
2525
#endif
2626

27+
/* Enable GNU extensions for accept4() on Linux/glibc. Must be defined
28+
* before any system headers are included. */
29+
#if !defined(_WIN32) && !defined(_GNU_SOURCE)
30+
#define _GNU_SOURCE 1
31+
#endif
32+
2733
#include <wolfssl/wolfcrypt/libwolfssl_sources.h>
2834

2935
#ifndef WOLFCRYPT_ONLY
@@ -1623,10 +1629,19 @@ int wolfIO_TcpBind(SOCKET_T* sockfd, word16 port)
16231629
#ifdef HAVE_SOCKADDR
16241630
int wolfIO_TcpAccept(SOCKET_T sockfd, SOCKADDR* peer_addr, XSOCKLENT* peer_len)
16251631
{
1626-
int fd = (int)accept(sockfd, peer_addr, peer_len);
1632+
int fd;
1633+
#if !defined(USE_WINDOWS_API) && defined(SOCK_CLOEXEC) && \
1634+
(defined(__linux__) || defined(__ANDROID__))
1635+
fd = (int)accept4(sockfd, peer_addr, peer_len, SOCK_CLOEXEC);
1636+
#else
1637+
fd = (int)accept(sockfd, peer_addr, peer_len);
16271638
#if defined(FD_CLOEXEC) && !defined(USE_WINDOWS_API)
1628-
if (fd >= 0)
1629-
(void)fcntl(fd, F_SETFD, FD_CLOEXEC);
1639+
if (fd >= 0) {
1640+
int fdFlags = fcntl(fd, F_GETFD);
1641+
if (fdFlags >= 0)
1642+
(void)fcntl(fd, F_SETFD, fdFlags | FD_CLOEXEC);
1643+
}
1644+
#endif
16301645
#endif
16311646
return fd;
16321647
}

wolfcrypt/benchmark/benchmark.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1584,11 +1584,22 @@ static const char* bench_result_words3[][5] = {
15841584
static THREAD_LS_T int cycles = -1;
15851585
static THREAD_LS_T struct perf_event_attr atr;
15861586

1587+
/* Try with PERF_FLAG_FD_CLOEXEC first; on older kernels (< 3.14) this
1588+
* fails with EINVAL, so fall back to flags=0 and set FD_CLOEXEC via
1589+
* fcntl() as a best-effort. */
15871590
#define INIT_CYCLE_COUNTER do { \
15881591
atr.type = PERF_TYPE_HARDWARE; \
15891592
atr.config = PERF_COUNT_HW_CPU_CYCLES; \
1590-
cycles = (int)syscall(__NR_perf_event_open, &atr, 0, -1, -1, \
1591-
PERF_FLAG_FD_CLOEXEC); \
1593+
cycles = (int)syscall(__NR_perf_event_open, &atr, 0, -1, -1, \
1594+
PERF_FLAG_FD_CLOEXEC); \
1595+
if (cycles < 0 && errno == EINVAL) { \
1596+
cycles = (int)syscall(__NR_perf_event_open, &atr, 0, -1, -1, 0); \
1597+
if (cycles >= 0) { \
1598+
int _fdFlags = fcntl(cycles, F_GETFD); \
1599+
if (_fdFlags >= 0) \
1600+
(void)fcntl(cycles, F_SETFD, _fdFlags | FD_CLOEXEC); \
1601+
} \
1602+
} \
15921603
} while (0);
15931604

15941605
#define BEGIN_CYCLES read(cycles, &begin_cycles, sizeof(begin_cycles));

wolfcrypt/src/port/af_alg/afalg_hash.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -224,11 +224,11 @@ static int AfalgHashCopy(wolfssl_AFALG_Hash* src, wolfssl_AFALG_Hash* dst)
224224
#endif
225225

226226
#if defined(__linux__) && defined(SOCK_CLOEXEC)
227-
dst->rdFd = accept4(src->rdFd, NULL, 0, SOCK_CLOEXEC);
228-
dst->alFd = accept4(src->alFd, NULL, 0, SOCK_CLOEXEC);
227+
dst->rdFd = accept4(src->rdFd, NULL, NULL, SOCK_CLOEXEC);
228+
dst->alFd = accept4(src->alFd, NULL, NULL, SOCK_CLOEXEC);
229229
#else
230-
dst->rdFd = accept(src->rdFd, NULL, 0);
231-
dst->alFd = accept(src->alFd, NULL, 0);
230+
dst->rdFd = accept(src->rdFd, NULL, NULL);
231+
dst->alFd = accept(src->alFd, NULL, NULL);
232232
#endif
233233

234234
if (dst->rdFd == WC_SOCK_NOTSET || dst->alFd == WC_SOCK_NOTSET) {

wolfcrypt/src/port/af_alg/wc_afalg.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ int wc_Afalg_Accept(struct sockaddr_alg* in, int inSz, int sock)
5757
}
5858

5959
#if defined(__linux__) && defined(SOCK_CLOEXEC)
60-
return accept4(sock, NULL, 0, SOCK_CLOEXEC);
60+
return accept4(sock, NULL, NULL, SOCK_CLOEXEC);
6161
#else
62-
return accept(sock, NULL, 0);
62+
return accept(sock, NULL, NULL);
6363
#endif
6464
}
6565

0 commit comments

Comments
 (0)