Skip to contentSkip to conversation

tun, tap: fix use-after-free of the socket's file

PATCH New Juan Agustin Niccolini 1 participant 3 messages Nov 28

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover tun, tap: fix use-after-free of the socket's file

tun and tap store the opened file in a socket embedded in their own object, and never clear the pointer. A packet written to the fd keeps the socket alive, so the socket can outlive the file, and code that follows sk->sk_socket->file for a packet still in the stack after close() reads a freed struct file, a use-after-free.

With tun, an unprivileged user can reach this from a user namespace and turn it into an arbitrary kernel memory read via nfnetlink_queue, which reports the uid and gid it reads through the freed file's f_cred.

With tap a process needs to be able to access the /dev/tapN node to reach the bug.

Patch 1 fixes tun and patch 2 fixes tap, both by clearing the pointer when the file is released.

Both bugs were discovered during an LLM-based code review. Reproducers for both: github.com/AgusNicco/tun-tap-uaf

Juan Agustin Niccolini (2):
tun: clear the socket file pointer on release to fix a struct file UAF
tap: clear the socket file pointer on release to fix a struct file UAF

 drivers/net/tap.c | 2 ++
 drivers/net/tun.c | 1 +
 2 files changed, 3 insertions(+)

base-commit: 28a371228d4eb4857cc31eeb1a0d7d01e9c3ea2f

Signature · 1 line
2.53.0
#1 lore raw

PATCH 1/2 tun: clear the socket file pointer on release to fix a struct file UAF

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 [email protected] # needs adjustments for <= 7.0

---
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(+)
drivers/net/tun.c +1 -0
index ae05b1e58abaa..95bda80bed255 100644
@@ -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;
Signature · 1 line
2.53.0
#2 lore raw

PATCH 2/2 tap: clear the socket file pointer on release to fix a struct file UAF

In reply to Juan Agustin Niccolini

tap_open() stores the file in the socket embedded in the tap_queue, and nothing has ever cleared it when the file is released. As in tun, a frame written to the fd keeps the socket alive after close(), and sk->sk_socket->file then points to a freed struct file.

A frame written to a macvtap device on top of a bridge reaches nfnetlink_queue with the tap socket attached, and NFQA_CFG_F_UID_GID makes it read file->f_cred through that pointer, a use-after-free:

  BUG: KASAN: slab-use-after-free in __nfqnl_enqueue_packet+0x33d0/0x38c0
  Read of size 8 at addr ff1100000ff59a30 by task tap_nfqueue_uaf/272

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. This takes CAP_NET_ADMIN in the network namespace of the device and access to its /dev/tapN node, which is not world-accessible the way /dev/net/tun usually is.

Fix this by clearing the pointer in tap_release(), 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 501c774cb13c ("net/macvtap: add vhost support")
Cc [email protected] # needs adjustments for <= 7.0

---
Reproducer: github.com/AgusNicco/tun-tap-uaf/blob/main/tap_nfqueue_uaf.c

Tested on mainline f4585067150b (same tap.c as net) with KASAN: the reproducer gets the report above on 100 of 100 runs as root on a macvtap device, 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 macvtap and ipvtap devices as the backend. Frames were sent in both directions, and the backend was then released in four
ways: closing the tap fd first, closing the vhost fd first, detaching the backend, and exiting the process. All 180 runs passed for each device type, with no KASAN report or warning in the kernel log.

 drivers/net/tap.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/net/tap.c +2 -0
index 2f19fb07bb2d4..e05bf8ecc65fd 100644
@@ -514,6 +514,8 @@ static int tap_open(struct inode *inode, struct file *file)
 static int tap_release(struct inode *inode, struct file *file)
 {
 	struct tap_queue *q = file->private_data;
+
+	WRITE_ONCE(q->sock.file, NULL);
 	tap_put_queue(q);
 	return 0;
 }
Signature · 1 line
2.53.0