Swatantra-66 opened a new pull request, #21649:
URL: https://github.com/apache/nuttx/pull/21649
## Summary
POSIX IEEE Std 1003.1 explicitly mandates for both `sendmsg()` and
`recvmsg()`:
> "[EMSGSIZE] The msg_iovlen member of the msghdr structure is less than or
equal to 0 or is greater than {IOV_MAX}."
In `net/bluetooth`:
1. In `bluetooth_sendmsg()` and `bluetooth_recvmsg()`,
`msg->msg_iov->iov_base` and `msg->msg_iov->iov_len` were dereferenced during
local variable declarations before validating `msg->msg_iovlen` or
`msg->msg_iov`. Passing `msg_iovlen == 0` or `msg_iov == NULL` resulted in an
immediate out-of-bounds pointer dereference (CWE-476 / CWE-125).
2. Passing `msg_iovlen <= 0` or `msg_iovlen > IOV_MAX` returned `-ENOTSUP`
instead of POSIX-mandated `-EMSGSIZE`.
3. In `bluetooth_recvfrom_rxqueue()`, `copylen` was not clamped to
`pstate->ir_buflen`, allowing incoming frames larger than the user receive
buffer to cause a buffer overflow (CWE-120).
4. In `bluetooth_recvmsg()` and `bluetooth_recvfrom_eventhandler()`,
completion checked `(ret > 0)`. When receiving a zero-length frame or zero-byte
read, `ret == 0` was not recognized as completion, causing the calling thread
to sleep indefinitely on `state.ir_sem`.
### Before vs After:
| Case | Before This PR | After This PR |
| :--- | :--- | :--- |
| **`msg->msg_iov == NULL` or `msg_iovlen == 0`** | Dereferenced
`msg->msg_iov->iov_base` in declaration $\rightarrow$ **CWE-476 / CWE-125
crash** | Deferred dereference; validates upfront and safely returns
`-EMSGSIZE` / `-EINVAL` |
| **`msg->msg_iovlen <= 0` or `> IOV_MAX`** | Returned `-ENOTSUP`
$\rightarrow$ **POSIX violation** | Returns `-EMSGSIZE` $\rightarrow$ **Strict
POSIX IEEE Std 1003.1 compliance** |
| **Frame size > User buffer (`ir_buflen`)** | `memcpy(buf, ..., frame_len)`
$\rightarrow$ **CWE-120 Buffer Overflow** | Clamps `copylen` to `ir_buflen` and
sets `MSG_TRUNC` in `msg->msg_flags` |
| **0-Byte Read / Zero-Length Frame** | Checked `(ret > 0)` $\rightarrow$
**Thread hung indefinitely on semaphore** | Checks `(ret >= 0)` $\rightarrow$
Completes cleanly with 0 bytes transferred |
---
### Technical Highlights:
- Adds upfront validation of `msg->msg_iovlen == 0 || msg->msg_iovlen >
IOV_MAX`, returning `-EMSGSIZE`.
- Preserves `-ENOTSUP` when `msg->msg_iovlen > 1` (since scatter/gather is
not yet supported for Bluetooth sockets).
- Validates `msg->msg_iov == NULL` and non-null buffer pointers for positive
lengths (`len > 0 && buf == NULL`), returning `-EINVAL`.
- Defers accessing `msg->msg_iov[0]` until all bounds and nullity checks
pass.
- Clamps `copylen` to `pstate->ir_buflen` in `bluetooth_recvfrom_rxqueue()`
and sets `MSG_TRUNC` in `msg->msg_flags` when truncation occurs per datagram
semantics.
- Changes completion checks to `(ret >= 0)` to allow zero-byte frames to
complete cleanly without blocking.
- Adds `#include <limits.h>` in both files.
## Impact
- **Is new feature added?** NO
- **Impact on user:** Fixes potential invalid pointer dereferences, buffer
overflows, and thread hangs in Bluetooth sockets; ensures strict POSIX
compliance.
- **Impact on build:** None.
- **Impact on hardware:** None.
- **Impact on documentation:** None.
- **Impact on security:** Prevents CWE-476 (NULL pointer dereference),
CWE-125 (Out-of-bounds Read), and CWE-120 (Buffer Overflow).
## Testing
- **Host OS:** Linux (Ubuntu 22.04 Docker container `nuttx-builder`)
- **Target:** `sim:bluetooth`
### Build Command:
```bash
./tools/configure.sh -l sim:bluetooth
make -j$(nproc)
```
### Build & Link Output:
```text
CC: bluetooth/bluetooth_sendmsg.c
CC: bluetooth/bluetooth_recvmsg.c
IN: net/libnet.a -> staging/libnet.a
LD: nuttx
Pac SIM with dynamic libs..
'/lib/x86_64-linux-gnu/libz.so.1' -> 'sim-pac/libs/libz.so.1'
'/lib/x86_64-linux-gnu/libc.so.6' -> 'sim-pac/libs/libc.so.6'
'/lib64/ld-linux-x86-64.so.2' -> 'sim-pac/ld-linux-x86-64.so.2'
SIM elf with dynamic libs archive in nuttx.tgz
```
### Code Style & Checkpatch:
```text
$ ./tools/nxstyle net/bluetooth/bluetooth_sendmsg.c
$ ./tools/nxstyle net/bluetooth/bluetooth_recvmsg.c
(0 errors, 0 warnings)
$ ./tools/checkpatch.sh -r -K -m -g HEAD~1..HEAD
✔️ All checks pass.
```
--
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]