Skip to content

Commit 07df379

Browse files
committed
Updates from review
1 parent 0d8b1bc commit 07df379

11 files changed

Lines changed: 75 additions & 38 deletions

File tree

.wolfssl_known_macro_extras

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ DTLS_RECEIVEFROM_NO_TIMEOUT_ON_INVALID_PEER
219219
ECCSI_ORDER_MORE_BITS_THAN_PRIME
220220
ECC_DUMP_OID
221221
ECDHE_SIZE
222+
EFD_CLOEXEC
222223
ENABLED_BSDKM_REGISTER
223224
ENABLE_SECURE_SOCKETS_LOGS
224225
ESP32
@@ -234,6 +235,7 @@ ETHERNET_AVAILABLE
234235
ETHERNET_H
235236
EV_TRIGGER
236237
EXTERNAL_LOADER_APP
238+
FD_CLOEXEC
237239
FIPS_OPTEST_FULL_RUN_AT_MODULE_INIT
238240
FORCE_FAILURE_GETRANDOM
239241
FP_ECC_CONTROL
@@ -479,6 +481,7 @@ OS_WINDOWS
479481
OTHERBOARD
480482
OTHER_BOARD
481483
PEER_INFO
484+
PERF_FLAG_FD_CLOEXEC
482485
PKA_ECC_SCALAR_MUL_IN_B_COEFF
483486
PLATFORMIO
484487
PLUTON_CRYPTO_ECC

src/crl.c

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1666,6 +1666,11 @@ static int SwapLists(WOLFSSL_CRL* crl)
16661666
#define XEVENT_MODE O_RDONLY
16671667
#endif
16681668

1669+
/* Fall back to no-op if O_CLOEXEC is unavailable on this platform. */
1670+
#ifndef O_CLOEXEC
1671+
#define O_CLOEXEC 0
1672+
#endif
1673+
16691674

16701675
/* we need a unique kqueue user filter fd for crl in case user is doing custom
16711676
* events too */
@@ -1727,7 +1732,7 @@ static THREAD_RETURN WOLFSSL_THREAD DoMonitor(void* arg)
17271732
fDER = -1;
17281733

17291734
if (crl->monitors[0].path) {
1730-
fPEM = open(crl->monitors[0].path, XEVENT_MODE | WC_CLOEXEC);
1735+
fPEM = open(crl->monitors[0].path, XEVENT_MODE | O_CLOEXEC);
17311736
if (fPEM == -1) {
17321737
WOLFSSL_MSG("PEM event dir open failed");
17331738
SignalSetup(crl, MONITOR_SETUP_E);
@@ -1737,7 +1742,7 @@ static THREAD_RETURN WOLFSSL_THREAD DoMonitor(void* arg)
17371742
}
17381743

17391744
if (crl->monitors[1].path) {
1740-
fDER = open(crl->monitors[1].path, XEVENT_MODE | WC_CLOEXEC);
1745+
fDER = open(crl->monitors[1].path, XEVENT_MODE | O_CLOEXEC);
17411746
if (fDER == -1) {
17421747
WOLFSSL_MSG("DER event dir open failed");
17431748
if (fPEM != -1)
@@ -1804,6 +1809,12 @@ static THREAD_RETURN WOLFSSL_THREAD DoMonitor(void* arg)
18041809
#include <sys/inotify.h>
18051810
#include <sys/eventfd.h>
18061811
#include <unistd.h>
1812+
#include <fcntl.h>
1813+
1814+
/* Fall back to no-op if EFD_CLOEXEC is unavailable. */
1815+
#ifndef EFD_CLOEXEC
1816+
#define EFD_CLOEXEC 0
1817+
#endif
18071818

18081819

18091820
#ifndef max
@@ -1839,7 +1850,7 @@ static THREAD_RETURN WOLFSSL_THREAD DoMonitor(void* arg)
18391850

18401851
WOLFSSL_ENTER("DoMonitor");
18411852

1842-
crl->mfd = eventfd(0, WC_EFD_CLOEXEC); /* our custom shutdown event */
1853+
crl->mfd = eventfd(0, EFD_CLOEXEC); /* our custom shutdown event */
18431854
if (crl->mfd < 0) {
18441855
WOLFSSL_MSG("eventfd failed");
18451856
SignalSetup(crl, MONITOR_SETUP_E);
@@ -1850,6 +1861,13 @@ static THREAD_RETURN WOLFSSL_THREAD DoMonitor(void* arg)
18501861
notifyFd = inotify_init1(IN_CLOEXEC);
18511862
#else
18521863
notifyFd = inotify_init();
1864+
#ifdef FD_CLOEXEC
1865+
if (notifyFd >= 0) {
1866+
int fdFlags = fcntl(notifyFd, F_GETFD);
1867+
if (fdFlags >= 0)
1868+
(void)fcntl(notifyFd, F_SETFD, fdFlags | FD_CLOEXEC);
1869+
}
1870+
#endif
18531871
#endif
18541872
if (notifyFd < 0) {
18551873
WOLFSSL_MSG("inotify failed");

src/ssl.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19568,6 +19568,9 @@ int wolfSSL_RAND_write_file(const char* fname)
1956819568
defined(HAVE_SYS_UN_H)
1956919569
#define WOLFSSL_EGD_NBLOCK 0x01
1957019570
#include <sys/un.h>
19571+
#ifndef SOCK_CLOEXEC
19572+
#define SOCK_CLOEXEC 0
19573+
#endif
1957119574
#endif
1957219575

1957319576
/* This collects entropy from the path nm and seeds the global PRNG with it.
@@ -19601,7 +19604,7 @@ int wolfSSL_RAND_egd(const char* nm)
1960119604
return WOLFSSL_FATAL_ERROR;
1960219605
}
1960319606

19604-
fd = socket(AF_UNIX, SOCK_STREAM | WC_SOCK_CLOEXEC, 0);
19607+
fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
1960519608
if (fd < 0) {
1960619609
WOLFSSL_MSG("Error creating socket");
1960719610
WC_FREE_VAR_EX(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);

src/wolfio.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
* embedded RTOSes whose libc layers conflict with glibc-style definitions
3030
* (e.g., Zephyr's socket_select.h vs. glibc's fd_set). */
3131
#if (defined(__linux__) || defined(__ANDROID__)) && \
32-
!defined(__ZEPHYR__) && !defined(_GNU_SOURCE)
32+
!defined(WOLFSSL_ZEPHYR) && !defined(_GNU_SOURCE)
3333
#define _GNU_SOURCE 1
3434
#endif
3535

@@ -51,6 +51,12 @@
5151
#include <wolfssl/wolfio.h>
5252
#include <wolfssl/wolfcrypt/logging.h>
5353

54+
/* SOCK_CLOEXEC sets close-on-exec atomically when the socket is created;
55+
* fall back to a no-op flag value where it isn't supported. */
56+
#ifndef SOCK_CLOEXEC
57+
#define SOCK_CLOEXEC 0
58+
#endif
59+
5460
#ifdef NUCLEUS_PLUS_2_3
5561
/* Holds last Nucleus networking error number */
5662
int Nucleus_Net_Errno;
@@ -1503,7 +1509,7 @@ int wolfIO_TcpConnect(SOCKET_T* sockfd, const char* ip, word16 port, int to_sec)
15031509
}
15041510
#endif
15051511

1506-
*sockfd = (SOCKET_T)socket(addr.ss_family, SOCK_STREAM | WC_SOCK_CLOEXEC, 0);
1512+
*sockfd = (SOCKET_T)socket(addr.ss_family, SOCK_STREAM | SOCK_CLOEXEC, 0);
15071513
#ifdef USE_WINDOWS_API
15081514
if (*sockfd == SOCKET_INVALID)
15091515
#else
@@ -1581,12 +1587,12 @@ int wolfIO_TcpBind(SOCKET_T* sockfd, word16 port)
15811587
sin->sin6_family = AF_INET6;
15821588
sin->sin6_addr = in6addr_any;
15831589
sin->sin6_port = XHTONS(port);
1584-
*sockfd = (SOCKET_T)socket(AF_INET6, SOCK_STREAM | WC_SOCK_CLOEXEC, 0);
1590+
*sockfd = (SOCKET_T)socket(AF_INET6, SOCK_STREAM | SOCK_CLOEXEC, 0);
15851591
#else
15861592
sin->sin_family = AF_INET;
15871593
sin->sin_addr.s_addr = INADDR_ANY;
15881594
sin->sin_port = XHTONS(port);
1589-
*sockfd = (SOCKET_T)socket(AF_INET, SOCK_STREAM | WC_SOCK_CLOEXEC, 0);
1595+
*sockfd = (SOCKET_T)socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0);
15901596
#endif
15911597

15921598
#ifdef USE_WINDOWS_API
@@ -1633,7 +1639,7 @@ int wolfIO_TcpBind(SOCKET_T* sockfd, word16 port)
16331639
int wolfIO_TcpAccept(SOCKET_T sockfd, SOCKADDR* peer_addr, XSOCKLENT* peer_len)
16341640
{
16351641
int fd;
1636-
#if !defined(USE_WINDOWS_API) && !defined(__ZEPHYR__) && \
1642+
#if !defined(USE_WINDOWS_API) && !defined(WOLFSSL_ZEPHYR) && \
16371643
defined(SOCK_CLOEXEC) && (defined(__linux__) || defined(__ANDROID__))
16381644
fd = (int)accept4(sockfd, peer_addr, peer_len, SOCK_CLOEXEC);
16391645
#else

wolfcrypt/benchmark/benchmark.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1574,6 +1574,7 @@ static const char* bench_result_words3[][5] = {
15741574
#include <linux/perf_event.h>
15751575
#include <sys/syscall.h>
15761576
#include <unistd.h>
1577+
#include <fcntl.h>
15771578

15781579
#ifndef PERF_FLAG_FD_CLOEXEC
15791580
#define PERF_FLAG_FD_CLOEXEC (1UL << 3)

wolfcrypt/src/port/af_alg/wc_afalg.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@
2525

2626
#include <wolfssl/wolfcrypt/port/af_alg/wc_afalg.h>
2727
#include <linux/if_alg.h>
28+
#include <sys/socket.h>
29+
30+
#ifndef SOCK_CLOEXEC
31+
#define SOCK_CLOEXEC 0
32+
#endif
2833

2934

3035
/* Sets the type of socket address to use */
@@ -70,7 +75,7 @@ int wc_Afalg_Socket(void)
7075
{
7176
int sock;
7277

73-
if ((sock = socket(AF_ALG, SOCK_SEQPACKET | WC_SOCK_CLOEXEC, 0)) < 0) {
78+
if ((sock = socket(AF_ALG, SOCK_SEQPACKET | SOCK_CLOEXEC, 0)) < 0) {
7479
WOLFSSL_MSG("Failed to get AF_ALG socket");
7580
return WC_AFALG_SOCK_E;
7681
}

wolfcrypt/src/port/caam/wolfcaam_qnx.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@
3636

3737
#include <errno.h>
3838

39+
#ifndef O_CLOEXEC
40+
#define O_CLOEXEC 0
41+
#endif
42+
3943
/* for devctl use */
4044
int caamFd = -1;
4145
static wolfSSL_Mutex caamMutex;
@@ -48,7 +52,7 @@ int wc_CAAMInitInterface()
4852
return -1;
4953
}
5054

51-
caamFd = open("/dev/wolfCrypt", O_RDWR | WC_CLOEXEC);
55+
caamFd = open("/dev/wolfCrypt", O_RDWR | O_CLOEXEC);
5256
if (caamFd < 0) {
5357
WOLFSSL_MSG("Could not open /dev/wolfCrypt");
5458
return -1;

wolfcrypt/src/port/devcrypto/wc_devcrypto.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,14 @@ static volatile int fd;
2727

2828
#include <wolfssl/wolfcrypt/port/devcrypto/wc_devcrypto.h>
2929

30+
#ifndef O_CLOEXEC
31+
#define O_CLOEXEC 0
32+
#endif
33+
3034
int wc_DevCryptoInit(void)
3135
{
3236
/* create descriptor */
33-
if ((fd = open("/dev/crypto", O_RDWR | WC_CLOEXEC, 0)) < 0) {
37+
if ((fd = open("/dev/crypto", O_RDWR | O_CLOEXEC, 0)) < 0) {
3438
WOLFSSL_MSG("Error opening /dev/crypto is cryptodev module loaded?");
3539
return WC_DEVCRYPTO_E;
3640
}

wolfcrypt/src/port/intel/quickassist_mem.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@
5858
#include <sys/ioctl.h>
5959
#include <sys/mman.h>
6060

61+
#ifndef O_CLOEXEC
62+
#define O_CLOEXEC 0
63+
#endif
64+
6165
#ifdef SAL_IOMMU_CODE
6266
#include <icp_sal_iommu.h>
6367
#endif
@@ -714,7 +718,7 @@ CpaStatus qaeMemInit(void)
714718
{
715719
if (g_qaeMemFd < 0) {
716720
#ifndef QAT_V2
717-
g_qaeMemFd = open(QAE_MEM, O_RDWR | WC_CLOEXEC);
721+
g_qaeMemFd = open(QAE_MEM, O_RDWR | O_CLOEXEC);
718722
if (g_qaeMemFd < 0) {
719723
printf("unable to open %s %d\n", QAE_MEM, g_qaeMemFd);
720724
return CPA_STATUS_FAIL;

wolfcrypt/src/random.c

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,12 @@ This library contains implementation for the random number generator.
213213
#ifndef EBSNET
214214
#include <unistd.h>
215215
#endif
216+
/* O_CLOEXEC is preferred (atomic close-on-exec at open() time) but
217+
* is unavailable on older kernels and some platforms; fall back to a
218+
* no-op so the | flag has no effect. */
219+
#ifndef O_CLOEXEC
220+
#define O_CLOEXEC 0
221+
#endif
216222
#endif
217223

218224
#if defined(WOLFSSL_SILABS_SE_ACCEL)
@@ -3829,15 +3835,15 @@ int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
38293835
if (!os->seedFdOpen)
38303836
{
38313837
#ifndef NO_DEV_URANDOM /* way to disable use of /dev/urandom */
3832-
os->fd = open("/dev/urandom", O_RDONLY | WC_CLOEXEC);
3838+
os->fd = open("/dev/urandom", O_RDONLY | O_CLOEXEC);
38333839
#if defined(DEBUG_WOLFSSL)
38343840
WOLFSSL_MSG("opened /dev/urandom.");
38353841
#endif /* DEBUG_WOLFSSL */
38363842
if (os->fd == XBADFD)
38373843
#endif /* NO_DEV_URANDOM */
38383844
{
38393845
/* may still have /dev/random */
3840-
os->fd = open("/dev/random", O_RDONLY | WC_CLOEXEC);
3846+
os->fd = open("/dev/random", O_RDONLY | O_CLOEXEC);
38413847
#if defined(DEBUG_WOLFSSL)
38423848
WOLFSSL_MSG("opened /dev/random.");
38433849
#endif /* DEBUG_WOLFSSL */
@@ -3855,15 +3861,15 @@ int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
38553861
}
38563862
#else /* WOLFSSL_KEEP_RNG_SEED_FD_OPEN */
38573863
#ifndef NO_DEV_URANDOM /* way to disable use of /dev/urandom */
3858-
os->fd = open("/dev/urandom", O_RDONLY | WC_CLOEXEC);
3864+
os->fd = open("/dev/urandom", O_RDONLY | O_CLOEXEC);
38593865
#if defined(DEBUG_WOLFSSL)
38603866
WOLFSSL_MSG("opened /dev/urandom.");
38613867
#endif /* DEBUG_WOLFSSL */
38623868
if (os->fd == XBADFD)
38633869
#endif /* !NO_DEV_URANDOM */
38643870
{
38653871
/* may still have /dev/random */
3866-
os->fd = open("/dev/random", O_RDONLY | WC_CLOEXEC);
3872+
os->fd = open("/dev/random", O_RDONLY | O_CLOEXEC);
38673873
#if defined(DEBUG_WOLFSSL)
38683874
WOLFSSL_MSG("opened /dev/random.");
38693875
#endif /* DEBUG_WOLFSSL */
@@ -3940,11 +3946,14 @@ int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
39403946

39413947
#if defined(CUSTOM_RAND_GENERATE_BLOCK) && defined(WOLFSSL_KCAPI)
39423948
#include <fcntl.h>
3949+
#ifndef O_CLOEXEC
3950+
#define O_CLOEXEC 0
3951+
#endif
39433952
int wc_hwrng_generate_block(byte *output, word32 sz)
39443953
{
39453954
int fd;
39463955
int ret = 0;
3947-
fd = open("/dev/hwrng", O_RDONLY | WC_CLOEXEC);
3956+
fd = open("/dev/hwrng", O_RDONLY | O_CLOEXEC);
39483957
if (fd == -1)
39493958
return OPEN_RAN_E;
39503959
while(sz)

0 commit comments

Comments
 (0)