[PATCH v2 nf 4/5] netfilter: nfnetlink_queue: pin file with get_file_active before reading f_cred

From: Tristan Madani

Date: Thu Nov 26 2026 - 19:17:08 EST


From: Tristan Madani <tristan@xxxxxxxxxxxxxxxxxxx>

nfqnl_put_sk_uidgid() reads sk->sk_socket->file->f_cred without
holding a reference to the file. Since the file slab uses
SLAB_TYPESAFE_BY_RCU, a concurrent close() may free and recycle the
file object, causing stale credential data to be read through f_cred.

Pin the file with get_file_active() before accessing its credentials
and release with fput() afterwards.

Found by manual code inspection; not triggered at runtime.

Fixes: 0ede61d8589c ("file: convert to SLAB_TYPESAFE_BY_RCU")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Tristan Madani <tristan@xxxxxxxxxxxxxxxxxxx>
---
net/netfilter/nfnetlink_queue.c | 35 ++++++++++++++++-----------------
1 file changed, 17 insertions(+), 18 deletions(-)

diff --git a/net/netfilter/nfnetlink_queue.c b/net/netfilter/nfnetlink_queue.c
index a8aa7ad832c3f..7a31267ae38f6 100644
--- a/net/netfilter/nfnetlink_queue.c
+++ b/net/netfilter/nfnetlink_queue.c
@@ -22,6 +22,7 @@
#include <linux/netdevice.h>
#include <linux/netfilter.h>
#include <linux/proc_fs.h>
+#include <linux/fs.h>
#include <linux/netfilter_ipv4.h>
#include <linux/netfilter_ipv6.h>
#include <linux/netfilter_bridge.h>
@@ -578,29 +579,27 @@ nfqnl_put_packet_info(struct sk_buff *nlskb, struct sk_buff *packet,

static int nfqnl_put_sk_uidgid(struct sk_buff *skb, struct sock *sk)
{
- const struct socket *sock;
- const struct file *file;
- const struct cred *cred;
+ struct socket *sock;
+ struct file *file;

if (!sk_fullsock(sk))
return 0;

- /* The sk pointer remains valid as long as the skb is.
- * The sk_socket and file pointer may become NULL
- * if the socket is closed.
- * Both structures (including file->cred) are RCU freed
- * which means they can be accessed within a RCU read section.
- */
sock = READ_ONCE(sk->sk_socket);
- file = sock ? READ_ONCE(sock->file) : NULL;
- if (file) {
- cred = file->f_cred;
- if (nla_put_be32(skb, NFQA_UID,
- htonl(from_kuid_munged(&init_user_ns, cred->fsuid))))
- goto nla_put_failure;
- if (nla_put_be32(skb, NFQA_GID,
- htonl(from_kgid_munged(&init_user_ns, cred->fsgid))))
- goto nla_put_failure;
+ if (sock) {
+ file = get_file_active(&sock->file);
+ if (file) {
+ const struct cred *cred = file->f_cred;
+ __be32 uid = htonl(from_kuid_munged(&init_user_ns,
+ cred->fsuid));
+ __be32 gid = htonl(from_kgid_munged(&init_user_ns,
+ cred->fsgid));
+
+ fput(file);
+ if (nla_put_be32(skb, NFQA_UID, uid) ||
+ nla_put_be32(skb, NFQA_GID, gid))
+ goto nla_put_failure;
+ }
}
return 0;

--
2.47.3