Netdev List
 help / color / mirror / Atom feed
From: Juan Agustin Niccolini <juanagusnicco@gmail.com>
To: Willem de Bruijn <willemdebruijn.kernel@gmail.com>,
	Jason Wang <jasowangio@gmail.com>,
	Andrew Lunn <andrew+netdev@lunn.ch>,
	"David S . Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@kernel.org>,
	Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>
Cc: netdev@vger.kernel.org,
	Simon Schippers <simon.schippers@tu-dortmund.de>,
	"Michael S . Tsirkin" <mst@redhat.com>,
	Dongli Zhang <dongli.zhang@oracle.com>,
	Yuchung Cheng <ycheng@google.com>,
	Al Viro <viro@ZenIV.linux.org.uk>,
	Stanislav Kinsbursky <skinsbursky@parallels.com>,
	Sridhar Samudrala <sri@us.ibm.com>, Arnd Bergmann <arnd@arndb.de>
Subject: [PATCH net 1/2] tun: clear the socket file pointer on release to fix a struct file UAF
Date: Sat, 28 Nov 2026 12:48:43 -0500	[thread overview]
Message-ID: <20261128174844.53113-2-juanagusnicco@gmail.com> (raw)
In-Reply-To: <20261128174844.53113-1-juanagusnicco@gmail.com>

tun_chr_open() stores the file in the socket embedded in the tun_file,
and nothing clears it when the file is released. A packet written to
the fd is charged to that socket and keeps it alive, so the socket can
outlive the file. For a packet that is still in the stack after
close(), sk->sk_socket->file points to a freed struct file.

xt_owner, nft_meta, nfnetlink_log, nfnetlink_queue, nf_log_syslog and
cls_flow follow that pointer to read file->f_cred, a use-after-free:

  BUG: KASAN: slab-use-after-free in __nfqnl_enqueue_packet+0x33d0/0x38c0
  Read of size 8 at addr ff11000010d09930 by task tun_nfqueue_uaf/270

An unprivileged user can trigger this from a user and network
namespace, by queueing the frames of a bridged tun device (IFF_TAP) to
nfnetlink_queue with NFQA_CFG_F_UID_GID, closing the fd and requeueing
a frame with NF_REPEAT. nfnetlink_queue reports the fsuid and fsgid it
finds through f_cred, so once the freed memory has been reused for data
the user controls, f_cred is an address of the user's choosing and the
eight bytes stored there are returned: an arbitrary kernel memory read.

__tun_detach() used to clear the pointer. Commit 66d1b9263a37 ("tun:
don't zeroize sock->file on detach") removed that in favour of
sock_release(), which already skipped the tun socket by then and which
tun no longer calls.

Fix this by clearing the pointer in tun_chr_close(), as
__sock_release() does for other sockets. vhost-net also uses the
pointer, but only while it holds a reference to the file.

Trees before v7.1 need the clear to be done under sk_callback_lock,
which the netfilter readers still take there.

Fixes: 66d1b9263a37 ("tun: don't zeroize sock->file on detach")
Cc: stable@vger.kernel.org # needs adjustments for <= 7.0
Signed-off-by: Juan Agustin Niccolini <juanagusnicco@gmail.com>
---
Reproducer: github.com/AgusNicco/tun-tap-uaf/blob/main/tun_nfqueue_uaf.c

An exploit that uses this bug to read arbitrary kernel memory as an
unprivileged user is available to the maintainers on request.

The patch was tested on mainline f4585067150b (same tun.c as net) with
KASAN: the reproducer gets the report above on 100 of 100 runs as an
unprivileged user, and on none of 20 with this patch. The github repo
above also has the full KASAN report.

vhost-net also uses this pointer, so it was tested with this patch
applied, using a tun device as the backend. Frames were sent in both
directions, and the backend was then released in four ways: closing
the tun fd first, closing the vhost fd first, detaching the backend,
and exiting the process. All 180 runs passed, with no KASAN report or
warning in the kernel log.

One side effect, in cls_flow: it checks sk->sk_socket->file and then
loads it a second time to use it. With this patch a close() racing
with the classifier can make it dereference NULL for a tun socket, as
it already can for any other socket. The pending patch "net/sched:
cls_flow: pin file with get_file_active before reading f_cred" removes
the second load, so I left cls_flow alone.

 drivers/net/tun.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/drivers/net/tun.c b/drivers/net/tun.c
index ae05b1e58abaa..95bda80bed255 100644
--- a/drivers/net/tun.c
+++ b/drivers/net/tun.c
@@ -3671,6 +3671,7 @@ static int tun_chr_close(struct inode *inode, struct file *file)
 {
 	struct tun_file *tfile = file->private_data;
 
+	WRITE_ONCE(tfile->socket.file, NULL);
 	tun_detach(tfile, true);
 
 	return 0;
-- 
2.53.0


  reply	other threads:[~2026-11-28 17:48 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-28 17:48 [PATCH net 0/2] tun, tap: fix use-after-free of the socket's file Juan Agustin Niccolini
2026-11-28 17:48 ` Juan Agustin Niccolini [this message]
2026-11-28 17:48 ` [PATCH net 2/2] tap: clear the socket file pointer on release to fix a struct file UAF Juan Agustin Niccolini

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261128174844.53113-2-juanagusnicco@gmail.com \
    --to=juanagusnicco@gmail.com \
    --cc=andrew+netdev@lunn.ch \
    --cc=arnd@arndb.de \
    --cc=davem@davemloft.net \
    --cc=dongli.zhang@oracle.com \
    --cc=edumazet@kernel.org \
    --cc=jasowangio@gmail.com \
    --cc=kuba@kernel.org \
    --cc=mst@redhat.com \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=simon.schippers@tu-dortmund.de \
    --cc=skinsbursky@parallels.com \
    --cc=sri@us.ibm.com \
    --cc=viro@ZenIV.linux.org.uk \
    --cc=willemdebruijn.kernel@gmail.com \
    --cc=ycheng@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox