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 v7 2/2] selftests: net: test joining genetlink groups no family owns
Date: Mon, 16 Nov 2026 12:40:30 +0000 [thread overview]
Message-ID: <20261116124030.1595567-3-anilkaushikwireless@gmail.com> (raw)
In-Reply-To: <20261116124030.1595567-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. 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
prev parent reply other threads:[~2026-11-16 12:40 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-16 12:40 [PATCH net v7 0/2] genetlink: Reject joining multicast " Anil Kaushik
2026-11-16 12:40 ` [PATCH net v7 1/2] " Anil Kaushik
2026-11-16 12:40 ` 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=20261116124030.1595567-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®