Skip to content

net/bluetooth: validate msg_iovlen and defer iov dereference in sendmsg/recvmsg - #21649

Merged
xiaoxiang781216 merged 1 commit into
apache:masterfrom
Swatantra-66:fix/bluetooth-iovlen-bounds
Nov 11, 2026
Merged

xiaoxiang781216 merged 1 commit into
apache:masterfrom
Swatantra-66:fix/bluetooth-iovlen-bounds

Conversation

@Swatantra-66

Copy link
Copy Markdown
Contributor

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:

./tools/configure.sh -l sim:bluetooth
make -j$(nproc)

Build & Link Output:

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:

$ ./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.

In bluetooth_sendmsg() and bluetooth_recvmsg(), msg->msg_iov->iov_base
and msg->msg_iov->iov_len were dereferenced during local variable
initialization before validating msg->msg_iovlen or checking msg->msg_iov
for NULL. When msg->msg_iovlen == 0 or msg->msg_iov == NULL, this led
to immediate out-of-bounds pointer dereferences (CWE-476 / CWE-125).

Additionally:
1. POSIX IEEE Std 1003.1 specifies that sendmsg() and recvmsg() shall
   fail with [EMSGSIZE] if msg_iovlen is <= 0 or > IOV_MAX. Previously,
   passing an invalid msg_iovlen returned -ENOTSUP.
2. In bluetooth_recvfrom_rxqueue(), copylen was not bounded by
   pstate->ir_buflen, allowing frames larger than the receive buffer to
   overrun user memory. Bounded copylen and set MSG_TRUNC in msg_flags
   when truncation occurs per datagram semantics.
3. In bluetooth_recvmsg() and bluetooth_recvfrom_eventhandler(), completion
   checked (ret > 0), causing zero-length frames to hang waiting on a
   semaphore instead of completing.

This patch:
- Validates msg->msg_iovlen against 0 and IOV_MAX upfront, returning
  -EMSGSIZE per POSIX.
- Preserves -ENOTSUP when msg->msg_iovlen > 1.
- Validates msg->msg_iov != NULL and (len > 0 && buf == NULL)
  returning -EINVAL.
- Defers reading msg->msg_iov[0] until after bounds and nullity checks.
- Bounds copylen by ir_buflen and sets MSG_TRUNC on truncation.
- Updates completion checks to (ret >= 0) to support zero-length frames.
- Adds missing <limits.h> header in both files.

Assisted-by: gemini-3.8-flash
Signed-off-by: Swatantra Yadav <[email protected]>
@github-actions github-actions Bot added Area: Networking Effects networking subsystem Size: S The size of the change in this PR is small labels Nov 10, 2026
@github-actions

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

No memory changes detected for:


Catch memory regressions in CI with MemBrowse. Free for open source.

@xiaoxiang781216
xiaoxiang781216 merged commit 961a351 into apache:master Nov 11, 2026
52 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: Networking Effects networking subsystem Size: S The size of the change in this PR is small

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants