Skip to content

net/inet: Check the option length of IPv6 multicast options. - #22376

Merged
acassis merged 1 commit into
apache:masterfrom
casaroli:sockopt-ipv6-len-up
Nov 26, 2026
Merged

acassis merged 1 commit into
apache:masterfrom
casaroli:sockopt-ipv6-len-up

Conversation

@casaroli

@casaroli casaroli commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Summary

ipv6_setsockopt() checked only that the option length is not 0. For IPV6_JOIN_GROUP and IPV6_LEAVE_GROUP it then gave the value to mld_joingroup() / mld_leavegroup(), which read a full struct ipv6_mreq (20 bytes). For IPV6_MULTICAST_IF it read an int. With a shorter option the kernel read past the end of the caller's buffer, and the call did not fail.

Now these options return EINVAL when the length is less than sizeof(struct ipv6_mreq) or sizeof(int). MCAST_JOIN_GROUP in the same function and the IPv4 options in ipv4_setsockopt() already check the length. Linux also returns EINVAL for these lengths.

Impact

setsockopt() with a short IPV6_JOIN_GROUP, IPV6_LEAVE_GROUP or IPV6_MULTICAST_IF option now fails with EINVAL. A call with the correct length does not change. No API, ABI or Kconfig change. The same in flat, protected and kernel builds and on all architectures.

Testing

New checks in test_mcast_optlen_ipv6 of cmocka_net_mcast (apache/nuttx-apps#4150): short IPV6_JOIN_GROUP, IPV6_LEAVE_GROUP and IPV6_MULTICAST_IF must fail with EINVAL.

cmocka_net_mcast on sim:nsh with IPv6, UDP, loopback, ICMPv6 and MLD (CI image, apps with the new checks):

master 28db478 this PR
test_mcast_optlen_ipv6 FAIL: 0 != -1 at test_mcast_ipv6.c:211 (the short IPV6_JOIN_GROUP succeeds) pass
other 5 cases pass pass

Builds with -Werror in the CI image (gcc 12.3): qemu-armv8a:netnsh and rv-virt:knetnsh64 (kernel) with IPv6, ICMPv6 and MLD; qemu-armv8a:netnsh, rv-virt:netnsh, rv-virt:knetnsh64; qemu-armv8a:knsh (kernel) with IPv4, IPv6, MLD.

ipv6_setsockopt() checked only that value_len is not 0.  Then
IPV6_JOIN_GROUP and IPV6_LEAVE_GROUP gave the value to mld_joingroup()
and mld_leavegroup(), which read a full struct ipv6_mreq (20 bytes), and
IPV6_MULTICAST_IF read an int.  With a shorter option the kernel read
past the end of the caller's buffer, and the call did not fail.

Return EINVAL if value_len is less than sizeof(struct ipv6_mreq) for
IPV6_JOIN_GROUP and IPV6_LEAVE_GROUP, and less than sizeof(int) for
IPV6_MULTICAST_IF.  MCAST_JOIN_GROUP in the same function and the IPv4
options in ipv4_setsockopt() already check the length.  Linux also
returns EINVAL for these lengths.

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
@casaroli
casaroli requested a review from jerpelea as a code owner November 26, 2026 07:05
@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 26, 2026
@github-actions

Copy link
Copy Markdown

@acassis
acassis merged commit 24f2db7 into apache:master Nov 26, 2026
58 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.

4 participants