From: Anil Kaushik <anilkaushikwireless@gmail.com>
To: netdev@vger.kernel.org
Cc: davem@davemloft.net, edumazet@kernel.org, kuba@kernel.org,
pabeni@redhat.com, horms@kernel.org, idosch@nvidia.com,
nicolas.dichtel@6wind.com, jacob.e.keller@intel.com,
jiri@resnulli.us, fw@strlen.de, martineau@kernel.org,
v3rdant.xiang@gmail.com, aliceryhl@google.com,
linux-kernel@vger.kernel.org, nhorman@tuxdriver.com,
shuah@kernel.org, linux-kselftest@vger.kernel.org,
anilkaushikwireless@gmail.com
Subject: [PATCH net v9 2/2] selftests: net: test joining genetlink groups no family owns
Date: Wed, 25 Nov 2026 14:45:14 +0000 [thread overview]
Message-ID: <20261125144514.2717012-3-anilkaushikwireless@gmail.com> (raw)
In-Reply-To: <20261125144514.2717012-1-anilkaushikwireless@gmail.com>
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. drop_monitor is unloaded again at the
end. Skipped when NET_DM is already registered or drop_monitor is not
available.
The free IDs are found with a family dump, which only lists the families
of the current network namespace, so these checks are skipped outside
the initial one. It is found with its fixed nsfs inode number, which
also works in a container with its own PID namespace.
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 | 128 +++++++++++++++++++
2 files changed, 129 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 baea3e7f0f8f..875ce0443814 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 \
nsh_gso.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..8bbe3d22225c
--- /dev/null
+++ b/tools/testing/selftests/net/nl_genl_mcast.py
@@ -0,0 +1,128 @@
+#!/usr/bin/env python3
+# SPDX-License-Identifier: GPL-2.0
+
+"""
+Tests for joining Generic Netlink multicast groups that no family owns.
+"""
+
+import os
+import socket
+import struct
+
+from lib.py import ksft_run, ksft_exit, ksft_eq, ksft_raises
+from lib.py import KsftSkipEx, NlctrlFamily, NlError, cmd
+
+NETLINK_GENERIC = 16
+SOL_NETLINK = 270
+NETLINK_ADD_MEMBERSHIP = 1
+NETLINK_LIST_MEMBERSHIPS = 9
+# include/uapi/linux/nsfs.h
+NET_NS_INIT_INO = 0xEFFFFFF9
+
+
+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."""
+ # Outside init_net, the dump skips the families that are not netnsok,
+ # but their groups are still owned.
+ if os.stat('/proc/self/ns/net').st_ino != NET_NS_INIT_INO:
+ raise KsftSkipEx("not in the initial network namespace")
+
+ 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)
+ # An array of u32 in host byte order
+ words = struct.unpack(f'={len(buf) // 4}I', buf)
+ return sum(w << (32 * i) for i, w in enumerate(words))
+
+
+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."""
+ with _sock() as sk:
+ 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)
+
+ with _sock() as sk:
+ sk.bind((0, free))
+ ksft_eq(_members(sk), 0)
+
+ with _sock() as sk:
+ 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")
+
+ free = _free_ids(ctrl)
+ try:
+ with _sock() as sk:
+ sk.bind((0, sum(_bit(gid) for gid in free)))
+
+ # 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
+
+ if not any(gid in free for gid in ids.values()):
+ raise KsftSkipEx("NET_DM got no group ID that was free")
+
+ for name, gid in ids.items():
+ ksft_eq(_members(sk) & _bit(gid), 0,
+ comment=f"member of NET_DM {name}")
+ finally:
+ # So that the next run can load it again
+ cmd("modprobe -q -r drop_monitor", fail=False)
+
+
+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
prev parent reply other threads:[~2026-11-25 14:45 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-25 14:45 [PATCH net v9 0/2] genetlink: Reject joining multicast " Anil Kaushik
2026-11-25 14:45 ` [PATCH net v9 1/2] " Anil Kaushik
2026-11-25 14:45 ` Anil Kaushik [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261125144514.2717012-3-anilkaushikwireless@gmail.com \
--to=anilkaushikwireless@gmail.com \
--cc=aliceryhl@google.com \
--cc=davem@davemloft.net \
--cc=edumazet@kernel.org \
--cc=fw@strlen.de \
--cc=horms@kernel.org \
--cc=idosch@nvidia.com \
--cc=jacob.e.keller@intel.com \
--cc=jiri@resnulli.us \
--cc=kuba@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=martineau@kernel.org \
--cc=netdev@vger.kernel.org \
--cc=nhorman@tuxdriver.com \
--cc=nicolas.dichtel@6wind.com \
--cc=pabeni@redhat.com \
--cc=shuah@kernel.org \
--cc=v3rdant.xiang@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®