genetlink: Reject joining multicast groups no family owns
Subsystems (2)
PATCH v6 1/1 genetlink: Reject joining multicast groups no family owns
genl_bind() checks the GENL_MCAST_CAP_* flags of a group only when a family owns it. For a group ID that no family owns, it returns 0, so any user can join it.
A family registered later can get that ID, and the socket stays a member:
nothing checks it again. An unprivileged user can join the free IDs, load a family module with CTRL_CMD_GETFAMILY, which calls request_module(), and then receive the notifications of a group with GENL_MCAST_CAP_NET_ADMIN or GENL_MCAST_CAP_SYS_ADMIN. For example the drop_monitor "events" group always gets ID 1, and is free until the drop_monitor module is loaded.
Fix by returning -ENOENT from genl_bind() when no family owns the group. There are no group flags to check before the family registers, so this applies to privileged callers too: a program that joins a fixed ID such as NET_DM_GRP_ALERT before the family is loaded no longer gets its messages. I don't know of a program that does this. One that resolves the family first still joins.
bind() takes a mask of groups, and most IDs in it can have no owner. Such a mask used to work, so keep it working: netlink_bind() skips an ID when the bind callback returns -ENOENT, and joins the others. Only genl_bind() returns -ENOENT. A skipped ID is not joined, so it is no longer in the groups that getsockname() and NETLINK_LIST_MEMBERSHIPS report. setsockopt() returns the error, as it asks for one group.
This does not cover a family that is unregistered between the check and the point where the membership bit is set.
Found by the Sashiko review of the gtp multicast group fix. Tested as an unprivileged user who joins all free IDs, then loads gtp and drop_monitor:
Before:
joins: ok=62 EPERM=2
gtp/gtp (ID 15): member
NET_DM/events (ID 1): member
After:
joins: ok=19 ENOENT=43 EPERM=2
gtp/gtp (ID 15): not member
NET_DM/events (ID 1): not member
bind(), an owned group and free IDs: ok, member of the owned group only
genl_bind() was added, with its first capability check, in the commit in the Fixes tag.
---
v6:
- Drop patch 2 of v5, as Jakub found it too ugly. A family unregistered between the check and setting the membership bit is not covered.
- Say that early joins to fixed IDs stop working, also for privileged callers, and that NETLINK_LIST_MEMBERSHIPS changes.
- Document the -ENOENT meaning on the bind member.
- Rebase on net/main.
v5: https://lore.kernel.org/netdev/[email protected]/
I used an LLM to help write and review the code, the tests and the commit message of this patch. I checked and tested every change myself.
include/linux/netlink.h | 3 +++ net/netlink/af_netlink.c | 6 ++++++ net/netlink/genetlink.c | 3 ++- 3 files changed, 11 insertions(+), 1 deletion(-)
include/linux/netlink.h +3 -0
@@ -48,6 +48,9 @@ struct netlink_kernel_cfg {unsigned int groups;unsigned int flags;void (*input)(struct sk_buff *skb); +/* -ENOENT means no one owns the group: bind() skips it and joins the +* others, setsockopt() returns the error. +*/ int (*bind)(struct net *net,int group);void (*unbind)(struct net *net,int group);void (*release) (struct sock *sk,unsigned long *groups);
net/netlink/af_netlink.c +6 -0
@@ -1033,6 +1033,12 @@ static int netlink_bind(struct socket *sock, struct sockaddr_unsized *addr, err = nlk->netlink_bind (net, group +1 );if (!err)continue ; +/* no one owns this group, still join the others */ +if (err == -ENOENT ) { +__clear_bit (group, &groups); + err =0 ; +continue ; + }netlink_undo_bind (group, groups, sk);return err; }
net/netlink/genetlink.c +2 -1
@@ -1825,7 +1825,7 @@ static int genl_bind(struct net *net, int group) {const struct genl_family *family;unsigned int id; -int ret =0 ; +int ret = -ENOENT ;down_read (&cb_lock);
@@ -1841,6 +1841,7 @@ static int genl_bind(struct net *net, int group)continue ; grp = &family->mcgrps[i]; + ret =0 ;if ((grp->flags &GENL_MCAST_CAP_NET_ADMIN ) && !ns_capable (net->user_ns,CAP_NET_ADMIN )) ret = -EPERM ;
Signature · 1 line
2.25.1
Le 12/11/2026 à 10:42, Anil Kaushik a écrit :
Quoted 59 lines
genl_bind()checks the GENL_MCAST_CAP_* flags of a group only when a family owns it. For a group ID that no family owns, it returns 0, so any user can join it.A family registered later can get that ID, and the socket stays a member:
nothing checks it again. An unprivileged user can join the free IDs, load a family module withCTRL_CMD_GETFAMILY, which callsrequest_module(), and then receive the notifications of a group withGENL_MCAST_CAP_NET_ADMINorGENL_MCAST_CAP_SYS_ADMIN. For example thedrop_monitor"events" group always gets ID 1, and is free until thedrop_monitormodule is loaded.Fix by returning
-ENOENTfromgenl_bind()when no family owns the group. There are no group flags to check before the family registers, so this applies to privileged callers too: a program that joins a fixed ID such asNET_DM_GRP_ALERTbefore the family is loaded no longer gets its messages. I don't know of a program that does this. One that resolves the family first still joins.
bind()takes a mask of groups, and most IDs in it can have no owner. Such a mask used to work, so keep it working:netlink_bind()skips an ID when the bind callback returns-ENOENT, and joins the others. Onlygenl_bind()returns-ENOENT. A skipped ID is not joined, so it is no longer in the groups thatgetsockname()andNETLINK_LIST_MEMBERSHIPSreport.setsockopt()returns the error, as it asks for one group.This does not cover a family that is unregistered between the check and the point where the membership bit is set.
Found by the Sashiko review of the gtp multicast group fix. Tested as an unprivileged user who joins all free IDs, then loads gtp and
drop_monitor:Before:
joins: ok=62 EPERM=2
gtp/gtp (ID 15): member
NET_DM/events (ID 1): memberAfter:
joins: ok=19 ENOENT=43 EPERM=2
gtp/gtp (ID 15): not member
NET_DM/events (ID 1): not member
bind(), an owned group and free IDs: ok, member of the owned group only
genl_bind()was added, with its first capability check, in the commit in the Fixes tag.Fixes 4d54cc32112d ("mptcp: avoid lock_fast usage in accept path")Signed-off-by Anil Kaushik <[email protected]>---
v6:
- Drop patch 2 of v5, as Jakub found it too ugly. A family unregistered between the check and setting the membership bit is not covered.
- Say that early joins to fixed IDs stop working, also for privileged callers, and thatNETLINK_LIST_MEMBERSHIPSchanges.
- Document the-ENOENTmeaning on the bind member.
- Rebase on net/main.
Quoted 4 lines
v5: https://lore.kernel.org/netdev/[email protected]/
I used an LLM to help write and review the code, the tests and the commit message of this patch. I checked and tested every change myself.
Could these tests be added in selftests?
Quoted 1 line · Nicolas Dichtel
Could these tests be added in selftests?
Yes, I will add a selftest for it. I will wait a day or two for more review and then send v7 with the test as a second patch.
Thanks,
Anil
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider.
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider.
Critical: 0 · High: 0 · Medium: 1 · Low: 1
If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).
- [Medium] The fix still lets a socket become a member of a multicast group ID that no family owns.
- [Low] The patch changes how the reserved, uAPI-exported static multicast group IDs behave, and nothing a uAPI consumer would read…
--
genetlink: Reject joining multicast groups no family owns
With this patch, genl_bind() returns -ENOENT for a multicast group ID that no registered family owns. This stops unprivileged sockets from joining IDs ahead of time and later getting the messages of a capability-gated family. netlink_bind() skips IDs whose bind callback returns -ENOENT and joins the others, and setsockopt(NETLINK_ADD_MEMBERSHIP) returns the error.
Quoted 5 lines
net/netlink/af_netlink.c +0
-0
net/netlink/af_netlink.c static int netlink_bind(struct socket *sock, struct sockaddr_unsized *addr, · line 1041
Quoted 9 lines
net/netlink/af_netlink.c +6
-0@@ -1033,6 +1033,12 @@ static int netlink_bind(struct socket *sock, struct sockaddr_unsized *addr, err = nlk->netlink_bind (net, group +1 );if (!err)continue ; +/* no one owns this group, still join the others */ +if (err == -ENOENT ) { +__clear_bit (group, &groups); + err =0 ; +continue ; + }
[Severity: Low]
Should the uAPI side document this change for the reserved static IDs?
include/uapi/linux/net_dropmon.h still exports:
/*
* Our group identifiers
*/
#define NET_DM_GRP_ALERT 1
genetlink.c also still reserves that ID for static use in mc_group_start ("the drop-monitor code abuses the API and thinks it can statically use group 1"). It reserves GENL_ID_VFS_DQUOT and GENL_ID_PMCRAID the same way, and genl_validate_assign_mc_groups() always uses first_id = 1 for NET_DM.
After this patch, genl_bind() returns -ENOENT for these IDs until the owning family registers. If setsockopt(NETLINK_ADD_MEMBERSHIP, NET_DM_GRP_ALERT) runs before drop_monitor (or pmcraid) is loaded, it now fails with ENOENT.
A bind() with nl_groups bit 0 set returns 0. Because of the __clear_bit() above, the socket does not get that membership, so the program sees no events and no error.
The commit message accepts this trade-off ("a program that joins a fixed ID such as NET_DM_GRP_ALERT before the family is loaded no longer gets its messages"). Could the uAPI header and the comment above mc_group_start be updated to say that the family has to be registered before the join?
Quoted 17 lines
net/netlink/af_netlink.c +0
-0netlink_undo_bind (group, groups, sk);return err; }net/netlink/genetlink.c +1
-1@@ -1825,7 +1825,7 @@ static int genl_bind(struct net *net, int group) {const struct genl_family *family;unsigned int id; -int ret =0 ; +int ret = -ENOENT ;down_read (&cb_lock);
net/netlink/genetlink.c static int genl_bind(struct net *net, int group) · line 1847
Quoted 7 lines
net/netlink/genetlink.c +1
-0@@ -1841,6 +1841,7 @@ static int genl_bind(struct net *net, int group)continue ; grp = &family->mcgrps[i]; + ret =0 ;if ((grp->flags &GENL_MCAST_CAP_NET_ADMIN ) && !ns_capable (net->user_ns,CAP_NET_ADMIN )) ret = -EPERM ;
[Severity: Medium]
Can a socket still become a member of an ID that no family owns?
genl_bind() checks ownership under down_read(&cb_lock) and releases the lock before it returns. The membership is set later, and ownership is not checked again:
netlink_bind()
err = nlk->netlink_bind(net, group + 1); /* genl_bind(), cb_lock dropped */
...
netlink_table_grab();
...
nlk->groups[0] = (nlk->groups[0] & ~0xffffffffUL) | groups;netlink_setsockopt() has the same gap for NETLINK_ADD_MEMBERSHIP:
err = nlk->netlink_bind(sock_net(sk), val);
...
netlink_table_grab();
netlink_update_socket_mc(nlk, val,
optname == NETLINK_ADD_MEMBERSHIP);If genl_unregister_family() runs in that window, it takes cb_lock for write and calls genl_unregister_mc_groups():
__netlink_clear_multicast_users(
net->genl_sock, family->mcgrp_offset + i);
...
if (grp_id != 1)
clear_bit(grp_id, mc_groups);The binding socket is not a member yet, so this does not clear it. The ID is then freed and the family is removed from genl_fam_idr. After that, netlink_bind() or netlink_update_socket_mc() sets the bit on the free ID.
A family registered later through genl_allocate_reserve_groups() could get that ID and inherit the member. One example is a family loaded on demand through CTRL_CMD_GETFAMILY -> request_module(). NET_DM could also come back on ID 1. Either way, the GENL_MCAST_CAP_NET_ADMIN or GENL_MCAST_CAP_SYS_ADMIN check never runs for that socket.
When the socket closes, netlink_release() -> genl_unbind() then calls the new family's unbind(i) with no matching bind(i). Could that unbalance the listener counting in families that implement bind/unbind, such as thermal_genl_unbind()?
The commit message says this window is not covered ("This does not cover a family that is unregistered between the check and the point where the membership bit is set"). Hitting it needs a family to be unregistered at the same time, usually a privileged module unload or a module init failure. But the result is the state this patch is meant to prevent.
Is a recheck at the point where the membership is set planned as a follow-up?
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261112094205.1120090-1-anilkaushikwireless%40gmail.com
Quoted 2 lines · On Mon, Nov 16, 2026 at 09:43:58AM +0000, [email protected] wrote:
- [Medium] The fix still lets a socket become a member of a multicast group ID that no family owns.
This race is not new. It was there before this patch, also for groups with GENL_MCAST_CAP_*. This patch does not fix it, and the commit message says so. It needs a privileged module unload at the same time as the join.
I can send a separate fix for net-next if needed.
Quoted 2 lines
- [Low] The patch changes how the reserved, uAPI-exported static multicast group IDs behave, and nothing a uAPI consumer would read...
Agreed. v7 updates the comments on the static IDs and on NET_DM_GRP_ALERT to say they can be joined only after the family is registered:
https://lore.kernel.org/netdev/[email protected]/
Thanks,
Anil