Swatantra-66 opened a new pull request, #21528:
URL: https://github.com/apache/nuttx/pull/21528

   ## Summary
   
   POSIX IEEE Std 1003.1 explicitly mandates for `recvmsg()`:
   > *"The `recvmsg()` function shall fail with `[EINVAL]` if the sum of the 
`iov_len` values in the `msg_iov` array overflows an `ssize_t`."*
   
   While `inet_sendmsg()` in `net/inet/inet_sockif.c` already implements this 
boundary check, `inet_recvmsg()` and `psock_tcp_recvfrom()` omitted it.
   
   In `psock_tcp_recvfrom()`, received bytes were accumulated into `ssize_t 
nrecv`. If the total capacity of the user's `iovec` array exceeded `SSIZE_MAX`, 
`nrecv` suffered signed integer overflow (undefined behavior in C) and wrapped 
negative. Callers interpreted this negative return value as a negated error 
code (e.g. `-EINVAL` or `-ECONNRESET`) despite data having been successfully 
received into user buffers.
   
   This change validates that the accumulated length of the `iovec` array fits 
in `ssize_t` before processing, failing upfront with `-EINVAL`.
   
   ### Behavior Comparison
   
   | Subsystem / Path | Before This PR | With This PR | POSIX Requirement |
   | :--- | :--- | :--- | :--- |
   | **`inet_sendmsg`** | Validates `iov_len > SSIZE_MAX - len` (`-EINVAL`) | 
Validates `iov_len > SSIZE_MAX - len` (`-EINVAL`) | IEEE Std 1003.1 `sendmsg` |
   | **`inet_recvmsg`** | No overflow check (missing) | Validates `iov_len > 
SSIZE_MAX - len` (`-EINVAL`) | IEEE Std 1003.1 `recvmsg` |
   | **`psock_tcp_recvfrom`** | Unchecked `nrecv += ret` wraps negative | 
Validates upfront before lock (`-EINVAL`) | Prevents signed overflow / UB |
   
   ## Impact
   
   - Conforms `recvmsg()` to POSIX IEEE Std 1003.1 for `AF_INET` and `AF_INET6` 
sockets.
   - Prevents signed integer overflow and return value corruption on large 
scatter/gather receive requests.
   - No ABI change. Flat, protected, and kernel builds unaffected.
   
   ## Testing
   
   Host: Linux x86_64 / Alpine Docker.
   
   - **Checkpatch**: `tools/checkpatch.sh -r -K -m -g HEAD~1..HEAD` passes:
     ```text
     ✔️ All checks pass.
     ```
   - **Coding Style (`nxstyle`)**:
     - `net/inet/inet_sockif.c`: 0 errors, 0 warnings
     - `net/tcp/tcp_recvfrom.c`: 0 errors, 0 warnings
   - **Spell check**: `codespell` reports 0 issues.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to