Repository navigation
net/bluetooth: validate msg_iovlen and defer iov dereference in sendmsg/recvmsg - #21649
Merged
xiaoxiang781216 merged 1 commit intoNov 11, 2026
Merged
Conversation
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]>
|
acassis
approved these changes
Nov 11, 2026
xiaoxiang781216
approved these changes
Nov 11, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
POSIX IEEE Std 1003.1 explicitly mandates for both
sendmsg()andrecvmsg():In
net/bluetooth:bluetooth_sendmsg()andbluetooth_recvmsg(),msg->msg_iov->iov_baseandmsg->msg_iov->iov_lenwere dereferenced during local variable declarations before validatingmsg->msg_iovlenormsg->msg_iov. Passingmsg_iovlen == 0ormsg_iov == NULLresulted in an immediate out-of-bounds pointer dereference (CWE-476 / CWE-125).msg_iovlen <= 0ormsg_iovlen > IOV_MAXreturned-ENOTSUPinstead of POSIX-mandated-EMSGSIZE.bluetooth_recvfrom_rxqueue(),copylenwas not clamped topstate->ir_buflen, allowing incoming frames larger than the user receive buffer to cause a buffer overflow (CWE-120).bluetooth_recvmsg()andbluetooth_recvfrom_eventhandler(), completion checked(ret > 0). When receiving a zero-length frame or zero-byte read,ret == 0was not recognized as completion, causing the calling thread to sleep indefinitely onstate.ir_sem.Before vs After:
msg->msg_iov == NULLormsg_iovlen == 0msg->msg_iov->iov_basein declaration-EMSGSIZE/-EINVALmsg->msg_iovlen <= 0or> IOV_MAX-ENOTSUP-EMSGSIZEir_buflen)memcpy(buf, ..., frame_len)copylentoir_buflenand setsMSG_TRUNCinmsg->msg_flags(ret > 0)(ret >= 0)Technical Highlights:
msg->msg_iovlen == 0 || msg->msg_iovlen > IOV_MAX, returning-EMSGSIZE.-ENOTSUPwhenmsg->msg_iovlen > 1(since scatter/gather is not yet supported for Bluetooth sockets).msg->msg_iov == NULLand non-null buffer pointers for positive lengths (len > 0 && buf == NULL), returning-EINVAL.msg->msg_iov[0]until all bounds and nullity checks pass.copylentopstate->ir_bufleninbluetooth_recvfrom_rxqueue()and setsMSG_TRUNCinmsg->msg_flagswhen truncation occurs per datagram semantics.(ret >= 0)to allow zero-byte frames to complete cleanly without blocking.#include <limits.h>in both files.Impact
Testing
nuttx-builder)sim:bluetoothBuild Command:
./tools/configure.sh -l sim:bluetooth make -j$(nproc)Build & Link Output:
Code Style & Checkpatch: