From 6e0af298e3085097adedeefb7eef14237e11296a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Frauenschl=C3=A4ger?= Date: Fri, 13 Nov 2026 09:28:30 +0100 Subject: [PATCH] Wait for an interrupted connect instead of reissuing it With no timeout, connect_timeout() retried connect() after EINTR. POSIX lets an interrupted connect() finish in the background, and BSD-derived kernels (macOS, FreeBSD) answer the second call with EALREADY while it is in progress or EISCONN once it is done, so a signal during the connect failed a connection that was about to succeed. On Linux the reissued call waits, which hid this. With a timeout, the poll() loop retried with the full timeout after each EINTR, so a steady stream of signals kept the connect waiting forever. With no timeout the connect stays blocking, so the platform's own limit still applies (Zephyr bounds it by CONFIG_NET_SOCKETS_CONNECT_TIMEOUT), and after EINTR it polls for writability instead of reissuing connect(). With a timeout it connects non-blocking and polls; after EINTR the poll resumes with the time left before the deadline. A signal that lands right at the deadline gets one last zero-wait poll, so a connect that finished meanwhile still counts. test_net runs a 300 ms connect to an unroutable address under a SIGALRM every 20 ms and requires it back after 250 ms and within 3 s; the old code never returned. On a host that refuses the address rather than dropping it, the case is skipped and says so. A blocking connect to a loopback listener must succeed and leave the socket blocking. The net test gets a 30 s ctest timeout, so a regression fails instead of hanging. Fixes F-17353. Fixes F-11054. --- src/net_posix.c | 42 +++++++------- tests/CMakeLists.txt | 1 + tests/unit/test_net.c | 130 +++++++++++++++++++++++++++++++++++++++++- 3 files changed, 151 insertions(+), 22 deletions(-) diff --git a/src/net_posix.c b/src/net_posix.c index 16fb6c7..cc608be 100644 --- a/src/net_posix.c +++ b/src/net_posix.c @@ -67,32 +67,33 @@ int64_t wolfcert_mono_ms(void) static int connect_timeout(int fd, const struct sockaddr* addr, socklen_t alen, int timeout_ms) { - if (timeout_ms <= 0) { - int rc; - do { - rc = connect(fd, addr, alen); - } - while (rc != 0 && errno == EINTR); - - return rc == 0 ? 0 : -1; - } - + int64_t deadline = (timeout_ms > 0) ? wolfcert_mono_ms() + timeout_ms : 0; int flags = fcntl(fd, F_GETFL, 0); - if (flags < 0) - return -1; + int rc; - if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) < 0) { + if (flags < 0 || + (timeout_ms > 0 && fcntl(fd, F_SETFL, flags | O_NONBLOCK) < 0)) return -1; - } - int rc = connect(fd, addr, alen); - if (rc != 0 && errno == EINPROGRESS) { + rc = connect(fd, addr, alen); + /* An interrupted connect() completes in the background (POSIX). */ + if (rc != 0 && (errno == EINPROGRESS || errno == EINTR)) { struct pollfd pfd = { .fd = fd, .events = POLLOUT }; int pr; - do { - pr = poll(&pfd, 1, timeout_ms); + + for (;;) { + int wait_ms = -1; + int last = 0; + + if (timeout_ms > 0) { + int64_t left = deadline - wolfcert_mono_ms(); + last = (left <= 0); + wait_ms = last ? 0 : (int)left; + } + pr = poll(&pfd, 1, wait_ms); + if (pr >= 0 || errno != EINTR || last) + break; } - while (pr < 0 && errno == EINTR); if (pr <= 0) { rc = -1; @@ -110,7 +111,8 @@ static int connect_timeout(int fd, const struct sockaddr* addr, socklen_t alen, rc = -1; } - (void)fcntl(fd, F_SETFL, flags); + if (timeout_ms > 0) + (void)fcntl(fd, F_SETFL, flags); return rc; } diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 3c8da4b..bbbf7ca 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -23,6 +23,7 @@ if(WOLFCERT_ENABLE_BUILTIN_TRANSPORT) target_include_directories(test_net PRIVATE ${CMAKE_SOURCE_DIR}/src) target_link_libraries(test_net PRIVATE wolfcert) add_test(NAME net COMMAND test_net) + set_tests_properties(net PROPERTIES TIMEOUT 30) endif() add_executable(test_transport unit/test_transport.c) diff --git a/tests/unit/test_net.c b/tests/unit/test_net.c index ebc9a58..d247250 100644 --- a/tests/unit/test_net.c +++ b/tests/unit/test_net.c @@ -17,7 +17,7 @@ * along with wolfCert. If not, see . */ -/* Built-in POSIX transport: connect timeout and SIGPIPE suppression. */ +/* Built-in POSIX transport: connect timeouts, signals, SIGPIPE suppression. */ #define _POSIX_C_SOURCE 200809L #define _DEFAULT_SOURCE @@ -28,12 +28,14 @@ #include "internal.h" #include +#include #include #include #include #include #include #include +#include #include #define REQUIRE(cond) \ @@ -121,6 +123,119 @@ static int test_sock_nosigpipe_suppresses(void) } #endif +static volatile sig_atomic_t g_alarmed; + +static void note_alarm(int sig) +{ + (void)sig; + g_alarmed = 1; +} + +typedef struct { + struct sigaction act; + sigset_t mask; +} AlarmSaved; + +/* SIGALRM every 20 ms without SA_RESTART, so blocking calls see EINTR. */ +static int alarms_start(AlarmSaved* old) +{ + struct sigaction sa; + struct itimerval it; + sigset_t alrm; + + memset(&sa, 0, sizeof(sa)); + sa.sa_handler = note_alarm; + sigemptyset(&sa.sa_mask); + sigemptyset(&alrm); + sigaddset(&alrm, SIGALRM); + REQUIRE(sigaction(SIGALRM, &sa, &old->act) == 0); + if (sigprocmask(SIG_UNBLOCK, &alrm, &old->mask) != 0) { + (void)sigaction(SIGALRM, &old->act, NULL); + REQUIRE(0); + } + + memset(&it, 0, sizeof(it)); + it.it_interval.tv_usec = 20000; + it.it_value.tv_usec = 20000; + g_alarmed = 0; + if (setitimer(ITIMER_REAL, &it, NULL) != 0) { + (void)sigprocmask(SIG_SETMASK, &old->mask, NULL); + (void)sigaction(SIGALRM, &old->act, NULL); + REQUIRE(0); + } + return 0; +} + +static int alarms_stop(const AlarmSaved* old) +{ + struct itimerval off; + + memset(&off, 0, sizeof(off)); + REQUIRE(setitimer(ITIMER_REAL, &off, NULL) == 0); + REQUIRE(sigprocmask(SIG_SETMASK, &old->mask, NULL) == 0); + REQUIRE(sigaction(SIGALRM, &old->act, NULL) == 0); + return 0; +} + +/* Signals during a timed connect must not stretch or cut short its deadline. + * Needs a host where 10.255.255.1 times out; baseline_ms is that timeout. */ +static int test_connect_eintr_keeps_deadline(int64_t baseline_ms, + int64_t* out_elapsed) +{ + AlarmSaved old; + int64_t t0; + int fd; + + *out_elapsed = -1; + if (baseline_ms < 240) { + printf("skip: 10.255.255.1 is refused here, not blackholed\n"); + return 0; + } + + if (alarms_start(&old)) + return 1; + t0 = wolfcert_mono_ms(); + fd = wolfcert_posix_connect("10.255.255.1", 9, 300, NULL); + *out_elapsed = wolfcert_mono_ms() - t0; + if (alarms_stop(&old)) + return 1; + if (fd >= 0) + close(fd); + + REQUIRE(fd < 0); + REQUIRE(g_alarmed); + REQUIRE(*out_elapsed >= 250); + REQUIRE(*out_elapsed < 3000); + return 0; +} + +/* A blocking connect succeeds and leaves the socket blocking. */ +static int test_blocking_connect_keeps_mode(void) +{ + struct sockaddr_in sa; + socklen_t slen = sizeof(sa); + int ls = socket(AF_INET, SOCK_STREAM, 0); + int fd; + int fl; + + REQUIRE(ls >= 0); + memset(&sa, 0, sizeof(sa)); + sa.sin_family = AF_INET; + sa.sin_addr.s_addr = htonl(INADDR_LOOPBACK); + REQUIRE(bind(ls, (struct sockaddr*)&sa, sizeof(sa)) == 0); + REQUIRE(listen(ls, 8) == 0); + REQUIRE(getsockname(ls, (struct sockaddr*)&sa, &slen) == 0); + + fd = wolfcert_posix_connect("127.0.0.1", ntohs(sa.sin_port), 0, NULL); + close(ls); + + REQUIRE(fd >= 0); + fl = fcntl(fd, F_GETFL, 0); + close(fd); + REQUIRE(fl >= 0 && (fl & O_NONBLOCK) == 0); + return 0; +} + int main(void) { /* Connect with a 1000 ms timeout to a loopback listener succeeds. */ @@ -149,6 +264,12 @@ int main(void) REQUIRE(fd2 < 0); REQUIRE(elapsed < 3000); + int64_t eintr_elapsed = 0; + if (test_connect_eintr_keeps_deadline(elapsed, &eintr_elapsed)) + return 1; + if (test_blocking_connect_keeps_mode()) + return 1; + if (test_no_sigpipe_on_dead_peer()) return 1; @@ -162,6 +283,11 @@ int main(void) return 1; #endif - printf("OK (unreachable connect returned in %ldms)\n", (long)elapsed); + if (eintr_elapsed < 0) + printf("OK (unreachable connect returned in %ldms; signal case " + "skipped)\n", (long)elapsed); + else + printf("OK (unreachable connect returned in %ldms, %ldms under " + "signals)\n", (long)elapsed, (long)eintr_elapsed); return 0; }