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

From: Tristan Madani

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


From: Tristan Madani <tristan@xxxxxxxxxxxxxxxxxxx>

__build_packet_message() 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_log.c | 35 +++++++++++++++++------------------
1 file changed, 17 insertions(+), 18 deletions(-)

diff --git a/net/netfilter/nfnetlink_log.c b/net/netfilter/nfnetlink_log.c
index d923f2cb13980..d4fe9a12088f4 100644
--- a/net/netfilter/nfnetlink_log.c
+++ b/net/netfilter/nfnetlink_log.c
@@ -28,6 +28,7 @@
#include <linux/spinlock.h>
#include <linux/sysctl.h>
#include <linux/proc_fs.h>
+#include <linux/fs.h>
#include <linux/security.h>
#include <linux/list.h>
#include <linux/slab.h>
@@ -632,25 +633,23 @@ __build_packet_message(struct nfnl_log_net *log,
/* UID */
sk = skb->sk;
if (sk && sk_fullsock(sk)) {
- const struct socket *sock;
- const struct file *file;
-
- /* 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.
- */
+ struct socket *sock;
+ struct file *file;
+
sock = READ_ONCE(sk->sk_socket);
- file = sock ? READ_ONCE(sock->file) : NULL;
- if (file) {
- const struct cred *cred = file->f_cred;
- struct user_namespace *user_ns = inst->peer_user_ns;
- __be32 uid = htonl(from_kuid_munged(user_ns, cred->fsuid));
- __be32 gid = htonl(from_kgid_munged(user_ns, cred->fsgid));
- if (nla_put_be32(inst->skb, NFULA_UID, uid) ||
- nla_put_be32(inst->skb, NFULA_GID, gid))
- goto nla_put_failure;
+ if (sock) {
+ file = get_file_active(&sock->file);
+ if (file) {
+ const struct cred *cred = file->f_cred;
+ struct user_namespace *user_ns = inst->peer_user_ns;
+ __be32 uid = htonl(from_kuid_munged(user_ns, cred->fsuid));
+ __be32 gid = htonl(from_kgid_munged(user_ns, cred->fsgid));
+
+ fput(file);
+ if (nla_put_be32(inst->skb, NFULA_UID, uid) ||
+ nla_put_be32(inst->skb, NFULA_GID, gid))
+ goto nla_put_failure;
+ }
}
}

--
2.47.3