mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH net] nfc: only send the SE_IO response to privileged listeners
@ 2026-11-21 11:21 Suyuan Wang via B4 Relay
  0 siblings, 0 replies; only message in thread
From: Suyuan Wang via B4 Relay @ 2026-11-21 11:21 UTC (permalink / raw)
  To: david, kuba, edumazet, pabeni, davem
  Cc: oe-linux-nfc, horms, linux-kernel, netdev

From: Suyuan Wang <stevewong@hust.edu.cn>

NFC_CMD_SE_IO sends an APDU to a secure element and needs CAP_NET_ADMIN
(GENL_ADMIN_PERM). The response from the secure element is not sent
back to the requester, though: se_io_cb() multicasts it with
NFC_ATTR_SE_APDU on the "events" group, and generic netlink lets any
user join that group. So an unprivileged user can read every response
APDU that a privileged process exchanges with the secure element.

The response has to stay on the multicast group, because that is where
userspace reads it: neard's seeld sends NFC_CMD_SE_IO on one socket and
handles the response on the socket it has subscribed to "events".
Requiring CAP_NET_ADMIN for the whole group would also hide the device
and target events from unprivileged listeners.

So keep the multicast, but filter the receivers: deliver the SE_IO
response only to sockets that were opened with CAP_NET_ADMIN in the
initial user namespace, the capability that NFC_CMD_SE_IO itself needs.
The check uses the credentials of the socket's opener, as
do_one_broadcast() does, because se_io_cb() runs in the driver's
context; a socket opened by a privileged process still gets the
response after the process drops the capability or passes the socket
on. Kernel sockets have no file and are skipped. Other events on the
group are not affected.

Found with a static analysis tool and checked by hand.

Fixes: 5ce3f32b5264 ("NFC: netlink: SE API implementation")
Cc: stable@vger.kernel.org
Co-developed-by: Kaixuan Li <kaixuan.li@ntu.edu.sg>
Signed-off-by: Kaixuan Li <kaixuan.li@ntu.edu.sg>
Signed-off-by: Suyuan Wang <stevewong@hust.edu.cn>
---
Tested in QEMU (x86_64, KASAN) with a test-only NFC driver whose se_io
answers from a work item. Four listeners subscribe to "events": root;
a socket opened by root that then drops CAP_NET_ADMIN; and two sockets
opened without the capability (root without CAP_NET_ADMIN, nobody).
Root then does DEV_UP (which adds the secure element), ENABLE_SE and
SE_IO.

Before: all four get SE_ADDED and the SE_IO response with the APDU.
After:  root and the socket opened by root get both; the two sockets
        opened without the capability get SE_ADDED only.

Not tested on hardware (st21nfca and st-nci implement se_io).
---
 net/nfc/netlink.c | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)

diff --git a/net/nfc/netlink.c b/net/nfc/netlink.c
index 224bdfa2dd0d..ebacbdd7193b 100644
--- a/net/nfc/netlink.c
+++ b/net/nfc/netlink.c
@@ -1472,6 +1472,19 @@ struct se_io_ctx {
 	u32 se_idx;
 };
 
+/*
+ * Only listeners whose socket was opened with the capability that
+ * NFC_CMD_SE_IO requires get the response APDU. Check the opener of the
+ * socket, as do_one_broadcast() does, since this runs in the driver's
+ * context. Kernel sockets have no file and get nothing.
+ */
+static int se_io_filter(struct sock *dsk, struct sk_buff *skb, void *data)
+{
+	struct file *file = dsk->sk_socket->file;
+
+	return !file || !file_ns_capable(file, &init_user_ns, CAP_NET_ADMIN);
+}
+
 static void se_io_cb(void *context, u8 *apdu, size_t apdu_len, int err)
 {
 	struct se_io_ctx *ctx = context;
@@ -1496,7 +1509,8 @@ static void se_io_cb(void *context, u8 *apdu, size_t apdu_len, int err)
 
 	genlmsg_end(msg, hdr);
 
-	genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
+	genlmsg_multicast_netns_filtered(&nfc_genl_family, &init_net, msg, 0, 0,
+					 GFP_KERNEL, se_io_filter, NULL);
 
 	kfree(ctx);
 





^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2026-11-21 11:21 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-21 11:21 [PATCH net] nfc: only send the SE_IO response to privileged listeners Suyuan Wang via B4 Relay

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®