Skip to content

Fix Coverity Findings#68

Open
aidangarske wants to merge 4 commits intowolfSSL:masterfrom
aidangarske:fix-coverity-wolfip
Open

Fix Coverity Findings#68
aidangarske wants to merge 4 commits intowolfSSL:masterfrom
aidangarske:fix-coverity-wolfip

Conversation

@aidangarske
Copy link
Member

Description

Dead Code (10 fixes)

  • bsd_socket.c: Remove dead NULL check on pointer arithmetic result in wolfip_calc_msghdr_len
  • bsd_socket.c: Remove dead else branches in send(), write(), and sendto() nonblock EAGAIN paths
  • bsd_socket.c: Remove unreachable EAGAIN check in wolfIP_sock_recvmsg
  • wolfip.c: Add comment explaining window scale loop is kept for larger buffer configs
  • wolfip.c: Remove redundant if (!saw_end) before unconditional return -1 in dhcp_parse_ack
  • tcp_echo.c, tcp_netcat_poll.c, tcp_netcat_select.c: Remove unreachable code after while(1) loops

Null Pointer Dereference (3 fixes)

  • wolfip.c: Add NULL check after fifo_peek() in UDP recvfrom path
  • bsd_socket.c: Add NULL check for wolfIP_getdev() return value in init_wolfip_posix
  • test_wolfssl_forwarding.c: Add NULL check for wolfIP_getdev(server_stack)

Uninitialized Variable (1 fix)

  • bsd_socket.c: Use calloc() instead of malloc() for heap_buf in wolfIP_sock_recvmsg

Integer Issues (3 fixes)

  • wolfip.c: Explicit mask (~sum & 0xFFFF) in icmp_checksum
  • test_ttl_expired.c: Explicit mask (~acc & 0xFFFF) in ones_csum
  • wolfip.c: Guard modulo with if (f->size > 0) in fifo_align_tail

Buffer Overrun (2 fixes)

  • wolfip.c: Add bounds check (pos + 4 > dns_len) before reading IP bytes in dns_callback
  • wolfip.c: Add explicit frame buffer bounds check before ICMP memcpy in wolfIP_sock_sendto

Resource Leak (3 fixes)

  • tap_linux.c: Close sock_fd at end of successful tap_init path
  • test_dhcp_dns.c, test_eventloop.c, test_esp.c: Close listen and client fds on all pt_echoserver exit paths

Unchecked Return Values (4 fixes)

  • bsd_socket.c: Check fcntl return values in wolfip_fd_alloc; close pipes on failure
  • tcp_netcat_poll.c, tcp_netcat_select.c: Check send() return value
  • test_dhcp_dns.c, test_eventloop.c, test_esp.c, test_native_wolfssl.c: Cast setsockopt to (void)

Thread Safety (4 fixes)

  • bsd_socket.c: Re-fetch entry after unlock/relock in wolfip_accept_common
  • bsd_socket.c: Re-fetch internal_fd after unlock/relock in sendto()
  • bsd_socket.c: Add mutex lock around IPSTACK access in socket()
  • bsd_socket.c: Lock mutex around IPSTACK init and configuration in init_wolfip_posix

Weak Crypto (1 fix)

  • test_ttl_expired.c: Replace rand() with getrandom() (test-only code)

Negative Returns (2 fixes)

  • tcp_echo.c, esp_server.c: Change socket() == 0 to socket() < 0

Tainted Data (1 fix)

  • bsd_socket.c: Validate environment variable IP strings before passing to atoip4()

Buffer Not Null Terminated (1 fix)

  • tap_linux.c: Null-terminate ifr.ifr_name after strncpy

None of these are CVE worthy fixes imo. The most security-relevant items (DNS/ICMP bounds, divide-by-zero, weak RNG) are bounded by internal protocol constraints or only affect test code. All fixes are defense-in-depth hardening.

@aidangarske aidangarske self-assigned this Mar 5, 2026
Copilot AI review requested due to automatic review settings March 5, 2026 03:26
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR addresses a broad set of Coverity findings across the wolfIP core, POSIX port layer, and test utilities, focusing on removing dead/unreachable code and hardening error handling and bounds checks.

Changes:

  • Harden wolfIP core logic with additional bounds checks, integer masking, and defensive guards.
  • Improve POSIX wrapper correctness (thread-safety locking, fd allocation error handling, env var validation).
  • Fix test utilities for resource cleanup, unchecked return values, and weak RNG usage.

Reviewed changes

Copilot reviewed 13 out of 13 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
src/wolfip.c Adds safety guards (modulo-by-zero, bounds checks), checksum masking, and small logic cleanups.
src/port/posix/bsd_socket.c Adds fcntl error checking, mutex protection for socket/init paths, env IP validation, and dead-code cleanup.
src/port/posix/tap_linux.c Ensures ifname null-termination and closes sock_fd on successful init.
src/test/test_wolfssl_forwarding.c Adds NULL check around wolfIP_getdev() usage.
src/test/test_ttl_expired.c Switches RNG to getrandom() with fallback; checksum masking.
src/test/test_native_wolfssl.c Explicitly ignores setsockopt return value.
src/test/test_eventloop.c Ensures fd cleanup on error/exit paths; ignores setsockopt return value.
src/test/test_dhcp_dns.c Ensures fd cleanup on error/exit paths; ignores setsockopt return value.
src/test/tcp_netcat_select.c Checks send() return value; removes unreachable cleanup.
src/test/tcp_netcat_poll.c Checks send() return value; removes unreachable cleanup.
src/test/tcp_echo.c Fixes socket() failure check; removes unreachable cleanup.
src/test/esp/test_esp.c Ensures fd cleanup on error/exit paths; ignores setsockopt return value.
src/test/esp/esp_server.c Fixes socket() failure check.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

…oid potential deadlock

  - inet_aton: Validate host_stack_ip_str before calling inet_aton()
  - test_ttl_expired.c: Added #ifdef __linux__ guard around <sys/random.h> / getrandom() for macOS/FreeBSD portability
  - tcp_netcat_poll.c / tcp_netcat_select.c: Replaced simple send() with retry loop to handle short writes
…sing t->S->last_tick when t->S was NULL (due to a race where close_socket zeroes

  continue; in tcp_input.
  FreeBSD interop — That failure showed ssh segfaulting (exit code 139), which appears to be a CI VM infrastructure issue, not caused by our code.
  FreeBSD uses tap_freebsd.c, not tap_linux.c, and the SSH process itself crashed. This is likely not fixable on our side.
@aidangarske aidangarske requested review from Copilot and removed request for Copilot March 5, 2026 04:30
@aidangarske aidangarske assigned danielinux and unassigned aidangarske Mar 5, 2026
@aidangarske aidangarske requested a review from danielinux March 5, 2026 04:33
Copy link
Member

@danielinux danielinux left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: remove comment on default rwnd, that's config-specific, while the code covers all cases.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 13 out of 13 changed files in this pull request and generated 4 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@aidangarske aidangarske requested a review from danielinux March 5, 2026 18:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants