Swatantra-66 opened a new pull request, #21785:
URL: https://github.com/apache/nuttx/pull/21785
## 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/netlink` (`netlink_sockif.c`), several vulnerabilities and POSIX
non-compliances existed:
1. `msg->msg_iov->iov_base` was dereferenced at the beginning of
`netlink_sendmsg()` and `netlink_recvmsg()` before validating `msg_iovlen` or
checking `msg_iov != NULL`. Passing `msg_iovlen == 0` or `msg_iov == NULL`
resulted in immediate NULL pointer / out-of-bounds dereferences (CWE-476 /
CWE-125).
2. `msg_iovlen` was not validated against `<= 0 || > IOV_MAX` (returning
`-EMSGSIZE` per POSIX).
3. If `msg->msg_iov[0].iov_len < sizeof(struct nlmsghdr)`, or if
`nlmsg->nlmsg_len < sizeof(struct nlmsghdr) || nlmsg->nlmsg_len >
msg->msg_iov[0].iov_len`, `netlink_sendmsg()` would trigger assertion panics or
allow protocol handlers to read out-of-bounds memory.
4. `netlink_recvmsg()` did not clear `msg->msg_flags` or set `MSG_TRUNC`
when the received Netlink response exceeded the user-provided buffer capacity,
nor did it guard `memcpy()` against calling with a NULL buffer when `len == 0`.
This patch addresses these issues:
- Defers dereferencing `msg->msg_iov` until after validating `msg_iovlen`,
`msg_iov != NULL`, and buffer pointers.
- Validates `msg_iovlen == 0 || msg_iovlen > IOV_MAX` returning `-EMSGSIZE`.
- Preserves `-ENOTSUP` when `msg_iovlen != 1`.
- Validates `msg_iov == NULL` and `iov_len > 0 && iov_base == NULL`
returning `-EINVAL`.
- Validates that `msg_iov[0].iov_len >= sizeof(struct nlmsghdr)` and that
`nlmsg->nlmsg_len >= sizeof(struct nlmsghdr) && nlmsg->nlmsg_len <=
msg_iov[0].iov_len` before parsing in `netlink_sendmsg()`, returning `-EINVAL`
on malformed headers.
- Clears `msg->msg_flags = 0` and sets `MSG_TRUNC` when `len <
entry->msg.nlmsg_len` in `netlink_recvmsg()`.
- Guards `memcpy()` with `if (len > 0)` to prevent undefined behavior with
0-length NULL user buffers.
- Includes `<limits.h>` for `IOV_MAX`.
---
## Before vs After
| Scenario | Before | After |
|---|---|---|
| `msg_iovlen == 0` or `msg_iov == NULL` | Immediate NULL/OOB dereference in
local variable initialization (CWE-476 / CWE-125) | Safe validation; returns
`-EMSGSIZE` or `-EINVAL` before any dereference |
| `msg_iovlen > IOV_MAX` | Returned `-ENOTSUP` | Returns `-EMSGSIZE` per
POSIX IEEE Std 1003.1 |
| `iov_len > 0 && iov_base == NULL` | Unchecked, crashes later during buffer
access | Rejects upfront with `-EINVAL` |
| `sendmsg` with truncated Netlink header (`iov_len < sizeof(nlmsghdr)`) |
Dereferences `nlmsg->nlmsg_len` on uninitialized memory; assertion failure or
panic | Rejects upfront with `-EINVAL` |
| `sendmsg` with invalid `nlmsg_len` (`nlmsg_len > iov_len`) | Assertion
failure in debug mode, heap out-of-bounds read in release mode | Rejects
upfront with `-EINVAL` |
| `recvmsg` user buffer smaller than response | Truncated silently without
notifying caller; `msg_flags` untouched | Sets `MSG_TRUNC` in `msg->msg_flags`
per POSIX |
| `recvmsg` with zero-length buffer (`len == 0`) | Called `memcpy(NULL,
...)` leading to undefined behavior | Guarded with `if (len > 0)`; safely frees
entry and returns 0 |
---
## Impact
- Security: Eliminates potential CWE-476 NULL pointer dereferences and
CWE-125 out-of-bounds reads.
- POSIX Compliance: Conforms to POSIX IEEE Std 1003.1 `[EMSGSIZE]` and
`MSG_TRUNC` specifications.
- Compatibility: Preserves full backward compatibility for valid Netlink
sockets.
---
## Testing
### 1. Code Style Check (`nxstyle`)
```bash
$ ./tools/nxstyle net/netlink/netlink_sockif.c
```
Output:
```text
(0 errors, 0 warnings)
```
### 2. Full Build (`sim:dynconns` from `tools/ci/testlist/sim-02.dat`)
`sim:dynconns` enables `CONFIG_NET_NETLINK=y` and `CONFIG_NETLINK_ROUTE=y`.
```bash
$ ./tools/configure.sh sim:dynconns
$ make -j8
```
Output:
```text
Create version.h
...
CC: netlink/netlink_sockif.c
...
IN: libs/libc/libc.a -> staging/libc.a
CC: symtab_apps.c
IN: /repos/apps/libapps.a -> staging/libapps.a
CC: sim/sim_head.c
...
LD: nuttx
Pac SIM with dynamic libs..
'/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
```
Binary size:
```bash
$ size nuttx
text data bss dec hex filename
599545 5244 46752 651541 9f115 nuttx
```
### 3. Runtime Boot & Execution in Simulator
```bash
$ ./nuttx
NuttShell (NSH) NuttX-12.8.0
nsh> help
help usage: help [-v] [<cmd>]
. break exec mkdir rm true
[ cat exit mkfifo rmdir
truncate
? cd expr mkrd rmmod uname
addroute cp false mount set umount
alias cmp help mv kill unset
unalias dirname hexdump nslookup sleep uptime
arp date insmod poweroff usleep watch
base64dec delroute losetup quit source xd
base64enc dmesg ls printf test
basename echo du pwd time
Builtin Apps:
dd init nsh sh
nsh> exit
```
--
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]