* [PATCH net v7 0/2] genetlink: Reject joining multicast groups no family owns
@ 2026-11-16 12:40 Anil Kaushik
2026-11-16 12:40 ` [PATCH net v7 1/2] " Anil Kaushik
2026-11-16 12:40 ` [PATCH net v7 2/2] selftests: net: test joining genetlink " Anil Kaushik
0 siblings, 2 replies; 3+ messages in thread
From: Anil Kaushik @ 2026-11-16 12:40 UTC (permalink / raw)
To: netdev
Cc: davem, edumazet, kuba, pabeni, horms, idosch, nicolas.dichtel,
jacob.e.keller, jiri, fw, martineau, v3rdant.xiang, aliceryhl,
linux-kernel, nhorman, shuah, linux-kselftest,
anilkaushikwireless
genl_bind() lets any user join a multicast group ID that no family owns.
A family registered later can get that ID, and the socket stays a member
without a capability check. Patch 1 makes genl_bind() return -ENOENT for
such an ID. Patch 2 adds a selftest for it.
v7:
- Add a selftest as patch 2, as Nicolas asked.
- Say in the comments on the static IDs, and on NET_DM_GRP_ALERT, that
they can be joined only after the family is registered (Sashiko).
- Say in the commit message that the unregister window was already
there for the capability check.
v6: https://lore.kernel.org/netdev/20261112094205.1120090-1-anilkaushikwireless@gmail.com/
I used an LLM to help write and review the code, the tests and the
commit messages of this series. I checked and tested every change
myself.
Anil Kaushik (2):
genetlink: Reject joining multicast groups no family owns
selftests: net: test joining genetlink groups no family owns
include/linux/netlink.h | 3 +
include/uapi/linux/net_dropmon.h | 2 +
net/netlink/af_netlink.c | 6 +
net/netlink/genetlink.c | 5 +-
tools/testing/selftests/net/Makefile | 1 +
tools/testing/selftests/net/nl_genl_mcast.py | 109 +++++++++++++++++++
6 files changed, 125 insertions(+), 1 deletion(-)
create mode 100755 tools/testing/selftests/net/nl_genl_mcast.py
base-commit: 070411c1590c8172eb4d86f045d99c372f22fe23
--
2.25.1
^ permalink raw reply [flat|nested] 3+ messages in thread
* [PATCH net v7 1/2] genetlink: Reject joining multicast groups no family owns
2026-11-16 12:40 [PATCH net v7 0/2] genetlink: Reject joining multicast groups no family owns Anil Kaushik
@ 2026-11-16 12:40 ` Anil Kaushik
2026-11-16 12:40 ` [PATCH net v7 2/2] selftests: net: test joining genetlink " Anil Kaushik
1 sibling, 0 replies; 3+ messages in thread
From: Anil Kaushik @ 2026-11-16 12:40 UTC (permalink / raw)
To: netdev
Cc: davem, edumazet, kuba, pabeni, horms, idosch, nicolas.dichtel,
jacob.e.keller, jiri, fw, martineau, v3rdant.xiang, aliceryhl,
linux-kernel, nhorman, shuah, linux-kselftest,
anilkaushikwireless
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. Say so in the comments on the static
IDs.
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. Hitting it needs a
privileged module unload at the same time, and the capability check
already has the same window.
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.
Link: https://lore.kernel.org/netdev/179324692107.3134.14443689892607921337@kernel.org/
Fixes: 4d54cc32112d ("mptcp: avoid lock_fast usage in accept path")
Signed-off-by: Anil Kaushik <anilkaushikwireless@gmail.com>
---
include/linux/netlink.h | 3 +++
include/uapi/linux/net_dropmon.h | 2 ++
net/netlink/af_netlink.c | 6 ++++++
net/netlink/genetlink.c | 5 ++++-
4 files changed, 15 insertions(+), 1 deletion(-)
diff --git a/include/linux/netlink.h b/include/linux/netlink.h
index 882e9c1b6c1d..b4eb873850e7 100644
--- a/include/linux/netlink.h
+++ b/include/linux/netlink.h
@@ -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);
diff --git a/include/uapi/linux/net_dropmon.h b/include/uapi/linux/net_dropmon.h
index 87cbef48d4c7..4f2fee982c8a 100644
--- a/include/uapi/linux/net_dropmon.h
+++ b/include/uapi/linux/net_dropmon.h
@@ -58,6 +58,8 @@ enum {
/*
* Our group identifiers
+ *
+ * The group can be joined only after the NET_DM family is registered.
*/
#define NET_DM_GRP_ALERT 1
diff --git a/net/netlink/af_netlink.c b/net/netlink/af_netlink.c
index 69bb951b5fb8..ee573ba6396d 100644
--- a/net/netlink/af_netlink.c
+++ b/net/netlink/af_netlink.c
@@ -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;
}
diff --git a/net/netlink/genetlink.c b/net/netlink/genetlink.c
index 5cc1037d4917..e5d4f3c4e22a 100644
--- a/net/netlink/genetlink.c
+++ b/net/netlink/genetlink.c
@@ -85,6 +85,8 @@ static DEFINE_IDR(genl_fam_idr);
* cater to that by giving it a static family and group ID.
* Bit 18 is marked as already used since the PMCRAID driver
* did the same thing as the VFS quota code (maybe copied?)
+ * These IDs can be joined only after the family that owns them
+ * is registered.
*/
static unsigned long mc_group_start = 0x3 | BIT(GENL_ID_CTRL) |
BIT(GENL_ID_VFS_DQUOT) |
@@ -1825,7 +1827,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 +1843,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;
--
2.25.1
^ permalink raw reply related [flat|nested] 3+ messages in thread
* [PATCH net v7 2/2] selftests: net: test joining genetlink groups no family owns
2026-11-16 12:40 [PATCH net v7 0/2] genetlink: Reject joining multicast groups no family owns Anil Kaushik
2026-11-16 12:40 ` [PATCH net v7 1/2] " Anil Kaushik
@ 2026-11-16 12:40 ` Anil Kaushik
1 sibling, 0 replies; 3+ messages in thread
From: Anil Kaushik @ 2026-11-16 12:40 UTC (permalink / raw)
To: netdev
Cc: davem, edumazet, kuba, pabeni, horms, idosch, nicolas.dichtel,
jacob.e.keller, jiri, fw, martineau, v3rdant.xiang, aliceryhl,
linux-kernel, nhorman, shuah, linux-kselftest,
anilkaushikwireless
Check that a Generic Netlink group ID no family owns cannot be joined:
- setsockopt(NETLINK_ADD_MEMBERSHIP) on a free ID fails with ENOENT,
and still works on an owned group.
- bind() with a mask of free IDs succeeds and joins nothing. With an
owned group in the mask, it joins only that group.
- A socket bound to the free IDs is not a member of the NET_DM groups
after drop_monitor is loaded. Skipped when NET_DM is already
registered or drop_monitor is not available.
Suggested-by: Nicolas Dichtel <nicolas.dichtel@6wind.com>
Signed-off-by: Anil Kaushik <anilkaushikwireless@gmail.com>
---
tools/testing/selftests/net/Makefile | 1 +
tools/testing/selftests/net/nl_genl_mcast.py | 109 +++++++++++++++++++
2 files changed, 110 insertions(+)
create mode 100755 tools/testing/selftests/net/nl_genl_mcast.py
diff --git a/tools/testing/selftests/net/Makefile b/tools/testing/selftests/net/Makefile
index 85f51a30c6a1..a765bc77e14c 100644
--- a/tools/testing/selftests/net/Makefile
+++ b/tools/testing/selftests/net/Makefile
@@ -74,6 +74,7 @@ TEST_PROGS := \
netns-name.sh \
netns-sysctl.sh \
nk_qlease.py \
+ nl_genl_mcast.py \
nl_netdev.py \
nl_nlctrl.py \
pmtu.sh \
diff --git a/tools/testing/selftests/net/nl_genl_mcast.py b/tools/testing/selftests/net/nl_genl_mcast.py
new file mode 100755
index 000000000000..71fba0280440
--- /dev/null
+++ b/tools/testing/selftests/net/nl_genl_mcast.py
@@ -0,0 +1,109 @@
+#!/usr/bin/env python3
+# SPDX-License-Identifier: GPL-2.0
+
+"""
+Tests for joining Generic Netlink multicast groups that no family owns.
+"""
+
+import socket
+
+from lib.py import ksft_run, ksft_exit, ksft_eq, ksft_raises
+from lib.py import KsftSkipEx, NlctrlFamily, NlError
+
+NETLINK_GENERIC = 16
+SOL_NETLINK = 270
+NETLINK_ADD_MEMBERSHIP = 1
+NETLINK_LIST_MEMBERSHIPS = 9
+
+
+def _groups(fam):
+ """Return the multicast groups of a family as {name: id}."""
+ return {g['name']: g['id']
+ for grp in fam.get('mcast-groups', []) for g in grp.values()}
+
+
+def _free_ids(ctrl):
+ """Return the IDs in 1..32 that no family owns."""
+ owned = set()
+ for fam in ctrl.getfamily({}, dump=True):
+ owned.update(_groups(fam).values())
+ free = [gid for gid in range(1, 33) if gid not in owned]
+ if not free:
+ raise KsftSkipEx("no free group ID in 1..32")
+ return free
+
+
+def _sock():
+ return socket.socket(socket.AF_NETLINK, socket.SOCK_RAW, NETLINK_GENERIC)
+
+
+def _members(sk):
+ buf = sk.getsockopt(SOL_NETLINK, NETLINK_LIST_MEMBERSHIPS, 64)
+ return int.from_bytes(buf, 'little')
+
+
+def _bit(gid):
+ return 1 << (gid - 1)
+
+
+def _notify_id(ctrl):
+ return _groups(ctrl.getfamily({'family-name': 'nlctrl'}))['notify']
+
+
+def join_free(ctrl) -> None:
+ """setsockopt() on an ID no family owns fails with ENOENT."""
+ sk = _sock()
+ sk.bind((0, 0))
+ for gid in _free_ids(ctrl):
+ # ENOENT
+ with ksft_raises(FileNotFoundError):
+ sk.setsockopt(SOL_NETLINK, NETLINK_ADD_MEMBERSHIP, gid)
+
+ notify = _notify_id(ctrl)
+ sk.setsockopt(SOL_NETLINK, NETLINK_ADD_MEMBERSHIP, notify)
+ ksft_eq(_members(sk), _bit(notify))
+
+
+def bind_mask(ctrl) -> None:
+ """bind() skips the IDs no family owns and joins the others."""
+ free = sum(_bit(gid) for gid in _free_ids(ctrl))
+ notify = _notify_id(ctrl)
+
+ sk = _sock()
+ sk.bind((0, free))
+ ksft_eq(_members(sk), 0)
+
+ sk = _sock()
+ sk.bind((0, free | _bit(notify)))
+ ksft_eq(_members(sk), _bit(notify))
+
+
+def bind_before_family(ctrl) -> None:
+ """A free ID joined with bind() stays unjoined when a family takes it."""
+ names = [fam['family-name'] for fam in ctrl.getfamily({}, dump=True)]
+ if 'NET_DM' in names:
+ raise KsftSkipEx("NET_DM is already registered")
+
+ sk = _sock()
+ sk.bind((0, sum(_bit(gid) for gid in _free_ids(ctrl))))
+
+ # Looking up the family loads drop_monitor.
+ try:
+ ids = _groups(ctrl.getfamily({'family-name': 'NET_DM'}))
+ except NlError as e:
+ raise KsftSkipEx("drop_monitor is not available") from e
+
+ for name, gid in ids.items():
+ ksft_eq(_members(sk) & _bit(gid), 0,
+ comment=f"member of NET_DM {name}")
+
+
+def main() -> None:
+ """ Ksft boiler plate main """
+ ctrl = NlctrlFamily()
+ ksft_run([join_free, bind_mask, bind_before_family], args=(ctrl, ))
+ ksft_exit()
+
+
+if __name__ == "__main__":
+ main()
--
2.25.1
^ permalink raw reply related [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-11-16 12:40 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-16 12:40 [PATCH net v7 0/2] genetlink: Reject joining multicast groups no family owns Anil Kaushik
2026-11-16 12:40 ` [PATCH net v7 1/2] " Anil Kaushik
2026-11-16 12:40 ` [PATCH net v7 2/2] selftests: net: test joining genetlink " Anil Kaushik
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox