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

From: Tristan Madani

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


From: Tristan Madani <tristan@xxxxxxxxxxxxxxxxxxx>

nft_meta_get_eval_skugid() 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/nft_meta.c | 16 ++++++++--------
1 file changed, 8 insertions(+), 8 deletions(-)

diff --git a/net/netfilter/nft_meta.c b/net/netfilter/nft_meta.c
index 01cfbaa365250..abf0bbce9bfda 100644
--- a/net/netfilter/nft_meta.c
+++ b/net/netfilter/nft_meta.c
@@ -17,6 +17,7 @@
#include <linux/random.h>
#include <linux/smp.h>
#include <linux/static_key.h>
+#include <linux/fs.h>
#include <net/dst.h>
#include <net/ip.h>
#include <net/sock.h>
@@ -135,19 +136,17 @@ nft_meta_get_eval_skugid(enum nft_meta_keys key,
const struct nft_pktinfo *pkt)
{
const struct sock *sk = skb_to_full_sk(pkt->skb);
- const struct socket *sock;
- const struct file *file;
+ struct socket *sock;
+ struct file *file;

if (!sk || !sk_fullsock(sk) || !net_eq(nft_net(pkt), sock_net(sk)))
return false;

- /* 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 (!sock)
+ return false;
+
+ file = get_file_active(&sock->file);
if (!file)
return false;

@@ -164,6 +163,7 @@ nft_meta_get_eval_skugid(enum nft_meta_keys key,
break;
}

+ fput(file);
return true;
}

--
2.47.3