Skip to content

Commit 556573e

Browse files
aidangarskedanielinux
authored andcommitted
Fix wolfip coverity 1683231, 1683213, 1686341, 1683243, 1683239, 1683212, 1683230, 1683217, 1683242 (×3 locations), 1683226, 1683236, 1683238
1 parent c2db895 commit 556573e

6 files changed

Lines changed: 51 additions & 29 deletions

File tree

src/port/posix/bsd_socket.c

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -899,7 +899,7 @@ int wolfIP_sock_recvmsg(struct wolfIP *ipstack, int sockfd, struct msghdr *msg,
899899
socklen_t addrlen = 0;
900900
size_t total_len = 0;
901901
int ret;
902-
uint8_t stack_buf[WOLFIP_IOV_STACK_BUF];
902+
uint8_t stack_buf[WOLFIP_IOV_STACK_BUF] = {0};
903903
uint8_t *heap_buf = NULL;
904904
uint8_t *buf = NULL;
905905
struct pollfd pfd;
@@ -1274,6 +1274,7 @@ static int wolfip_accept_common(int sockfd, struct sockaddr *addr, socklen_t *ad
12741274
return -1;
12751275
}
12761276
wolfip_drain_pipe_locked(entry);
1277+
want_nonblock = wolfip_fd_is_nonblock(sockfd);
12771278
}
12781279
} while (internal_ret == -EAGAIN);
12791280
if (internal_ret < 0) {
@@ -1450,12 +1451,9 @@ ssize_t sendto(int sockfd, const void *buf, size_t len, int flags, const struct
14501451
}
14511452
if (ret == -EAGAIN) {
14521453
if (nonblock) {
1453-
if (sent == 0) {
1454-
errno = EAGAIN;
1455-
pthread_mutex_unlock(&wolfIP_mutex);
1456-
return -1;
1457-
}
1458-
break;
1454+
errno = EAGAIN;
1455+
pthread_mutex_unlock(&wolfIP_mutex);
1456+
return -1;
14591457
}
14601458
if (entry) {
14611459
wait_ret = wolfip_wait_for_event_locked(entry, POLLOUT, entry->snd_timeout_ms);
@@ -1472,7 +1470,7 @@ ssize_t sendto(int sockfd, const void *buf, size_t len, int flags, const struct
14721470
if (internal_fd < 0) {
14731471
pthread_mutex_unlock(&wolfIP_mutex);
14741472
errno = EBADF;
1475-
return (sent == 0) ? -1 : (ssize_t)sent;
1473+
return -1;
14761474
}
14771475
}
14781476
continue;
@@ -1521,12 +1519,9 @@ ssize_t send(int sockfd, const void *buf, size_t len, int flags) {
15211519
}
15221520
if (ret == -EAGAIN) {
15231521
if (nonblock) {
1524-
if (sent == 0) {
1525-
errno = EAGAIN;
1526-
pthread_mutex_unlock(&wolfIP_mutex);
1527-
return -1;
1528-
}
1529-
break;
1522+
errno = EAGAIN;
1523+
pthread_mutex_unlock(&wolfIP_mutex);
1524+
return -1;
15301525
}
15311526
if (entry) {
15321527
wait_ret = wolfip_wait_for_event_locked(entry, POLLOUT, entry->snd_timeout_ms);
@@ -1580,12 +1575,9 @@ ssize_t write(int sockfd, const void *buf, size_t len) {
15801575
}
15811576
if (ret == -EAGAIN) {
15821577
if (nonblock) {
1583-
if (sent == 0) {
1584-
errno = EAGAIN;
1585-
pthread_mutex_unlock(&wolfIP_mutex);
1586-
return -1;
1587-
}
1588-
break;
1578+
errno = EAGAIN;
1579+
pthread_mutex_unlock(&wolfIP_mutex);
1580+
return -1;
15891581
}
15901582
if (entry) {
15911583
wait_ret = wolfip_wait_for_event_locked(entry, POLLOUT, entry->snd_timeout_ms);
@@ -1687,8 +1679,18 @@ void __attribute__((constructor)) init_wolfip_posix() {
16871679
#if WOLFIP_POSIX_TCPDUMP
16881680
static int tcpdump_atexit_registered;
16891681
#endif
1690-
if (IPSTACK)
1682+
{
1683+
static int mutex_initialized = 0;
1684+
if (!mutex_initialized) {
1685+
pthread_mutex_init(&wolfIP_mutex, NULL);
1686+
mutex_initialized = 1;
1687+
}
1688+
}
1689+
pthread_mutex_lock(&wolfIP_mutex);
1690+
if (IPSTACK) {
1691+
pthread_mutex_unlock(&wolfIP_mutex);
16911692
return;
1693+
}
16921694
host_stack_ip_str = getenv("WOLFIP_HOST_IP");
16931695
if (!host_stack_ip_str || host_stack_ip_str[0] == '\0') {
16941696
host_stack_ip_str = HOST_STACK_IP;
@@ -1734,8 +1736,6 @@ void __attribute__((constructor)) init_wolfip_posix() {
17341736
swap_socketcall(select, "select");
17351737
swap_socketcall(fcntl, "fcntl");
17361738

1737-
pthread_mutex_init(&wolfIP_mutex, NULL);
1738-
pthread_mutex_lock(&wolfIP_mutex);
17391739
wolfIP_init_static(&IPSTACK);
17401740
tapdev = wolfIP_getdev(IPSTACK);
17411741
if (!tapdev) {

src/test/ipfilter_logger.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ static void *pt_echoclient(void *arg)
288288
}
289289
wolfSSL_set_fd(client_ssl, fd);
290290
sleep(1);
291-
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &(int){1}, sizeof(int));
291+
(void)setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &(int){1}, sizeof(int));
292292
printf("Connecting to echo server\n");
293293
ret = connect(fd, (struct sockaddr *)&remote_sock, sizeof(remote_sock));
294294
if (ret < 0) {

src/test/test_eventloop_tun.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ void *pt_echoclient(void *arg)
230230
return (void *)-1;
231231
}
232232
sleep(1);
233-
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &(int){1}, sizeof(int));
233+
(void)setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &(int){1}, sizeof(int));
234234
printf("Connecting to echo server\n");
235235
old_flags = fcntl(fd, F_GETFL, 0);
236236
if (old_flags < 0) {
@@ -343,7 +343,7 @@ void *pt_echoclient(void *arg)
343343
*/
344344
static void *pt_echoserver(void *arg)
345345
{
346-
int fd, ret;
346+
int fd, listen_fd, ret;
347347
unsigned total_r = 0;
348348
uint8_t local_buf[BUFFER_SIZE];
349349
struct sockaddr_in local_sock = {
@@ -358,21 +358,25 @@ static void *pt_echoserver(void *arg)
358358
return (void *)-1;
359359
}
360360
local_sock.sin_addr.s_addr = inet_addr(HOST_STACK_IP);
361-
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &(int){1}, sizeof(int));
361+
(void)setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &(int){1}, sizeof(int));
362362
ret = bind(fd, (struct sockaddr *)&local_sock, sizeof(local_sock));
363363
if (ret < 0) {
364364
printf("test server bind: %d (%s)\n", ret, strerror(errno));
365+
close(fd);
365366
return (void *)-1;
366367
}
367368
ret = listen(fd, 1);
368369
if (ret < 0) {
369370
printf("test server listen: %d\n", ret);
371+
close(fd);
370372
return (void *)-1;
371373
}
374+
listen_fd = fd;
372375
printf("Waiting for client\n");
373376
ret = accept(fd, NULL, NULL);
374377
if (ret < 0) {
375378
printf("test server accept: %d\n", ret);
379+
close(listen_fd);
376380
return (void *)-1;
377381
}
378382
printf("test server: client %d connected\n", ret);
@@ -381,10 +385,14 @@ static void *pt_echoserver(void *arg)
381385
ret = read(fd, local_buf + total_r, sizeof(local_buf) - total_r);
382386
if (ret < 0) {
383387
printf("failed test server read: %d (%s) \n", ret, strerror(errno));
388+
close(fd);
389+
close(listen_fd);
384390
return (void *)-1;
385391
}
386392
if (ret == 0) {
387393
printf("test server read: client has closed the connection.\n");
394+
close(fd);
395+
close(listen_fd);
388396
if (wolfIP_closing)
389397
return (void *)0;
390398
else

src/test/test_ttl_expired.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,16 @@ uint32_t wolfIP_getrandom(void)
125125
return ret;
126126
}
127127
#endif
128-
ret = (uint32_t)rand();
128+
{
129+
FILE *f = fopen("/dev/urandom", "rb");
130+
if (f) {
131+
size_t r = fread(&ret, sizeof(ret), 1, f);
132+
fclose(f);
133+
if (r == 1)
134+
return ret;
135+
}
136+
}
137+
ret = 0;
129138
return ret;
130139
}
131140

src/test/unit/unit_tests_api.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3305,3 +3305,4 @@ START_TEST(test_dns_wrapper_apis)
33053305
}
33063306
END_TEST
33073307

3308+

src/wolfip.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2013,11 +2013,15 @@ static struct tsocket *tcp_new_socket(struct wolfIP *s)
20132013
t->sock.tcp.peer_sack_count = 0;
20142014
memset(t->sock.tcp.ooo, 0, sizeof(t->sock.tcp.ooo));
20152015
{
2016+
#if RXBUF_SIZE > 0xFFFF
20162017
uint32_t space = RXBUF_SIZE;
20172018
uint8_t shift = 0;
20182019
while (shift < 14 && (space >> shift) > 0xFFFF)
20192020
shift++;
20202021
t->sock.tcp.rcv_wscale = shift;
2022+
#else
2023+
t->sock.tcp.rcv_wscale = 0;
2024+
#endif
20212025
}
20222026
/* We always include WS in the initial SYN (shift may be 0), so
20232027
* mark that we offered it to accept the peer's WS in SYN-ACK. */
@@ -4518,7 +4522,7 @@ int wolfIP_sock_sendto(struct wolfIP *s, int sockfd, const void *buf, size_t len
45184522
}
45194523
if (sizeof(struct wolfIP_ip_packet) + payload_len > sizeof(frame))
45204524
return -WOLFIP_EINVAL;
4521-
memcpy(&icmp->type, buf, payload_len);
4525+
memcpy(frame + sizeof(struct wolfIP_ip_packet), buf, payload_len);
45224526
if (icmp->type == ICMP_ECHO_REQUEST)
45234527
icmp_set_echo_id(icmp, ts->src_port);
45244528
icmp->csum = 0;

0 commit comments

Comments
 (0)