From: Tristan Madani Since commit 0ede61d8589cc ("file: convert to SLAB_TYPESAFE_BY_RCU"), struct file objects can have their slab slot reused while RCU readers still hold pointers to them. The NFQUEUE UID/GID metadata construction reads file->f_cred without pinning the file, which could lead to reporting credentials from an unrelated file if the slab slot is reused. Use get_file_rcu() to take a reference on the file before reading credentials, and fput() to release it after extracting the cred pointer. Fixes: 0ede61d8589cc ("file: convert to SLAB_TYPESAFE_BY_RCU") Cc: stable@vger.kernel.org Signed-off-by: Tristan Madani --- net/netfilter/nfnetlink_queue.c | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/net/netfilter/nfnetlink_queue.c b/net/netfilter/nfnetlink_queue.c index a3bc002800510..ebf9c9a8a9f98 100644 --- a/net/netfilter/nfnetlink_queue.c +++ b/net/netfilter/nfnetlink_queue.c @@ -566,8 +566,8 @@ 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; + struct socket *sock; + struct file *file; const struct cred *cred; if (!sk_fullsock(sk)) @@ -575,14 +575,19 @@ static int nfqnl_put_sk_uidgid(struct sk_buff *skb, struct sock *sk) /* 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. + * if the socket is closed. Since struct file uses + * SLAB_TYPESAFE_BY_RCU, pin the file with + * get_file_rcu() to ensure f_cred is not from a + * reused slab slot. */ sock = READ_ONCE(sk->sk_socket); - file = sock ? READ_ONCE(sock->file) : NULL; + if (sock) + file = get_file_rcu(&sock->file); + else + file = NULL; if (file) { cred = file->f_cred; + fput(file); if (nla_put_be32(skb, NFQA_UID, htonl(from_kuid_munged(&init_user_ns, cred->fsuid)))) goto nla_put_failure; -- 2.47.3