Netdev List
 help / color / mirror / Atom feed
From: Antonio Quartulli <antonio@mandelbit.com>
To: willemdebruijn.kernel@gmail.com, jasowangio@gmail.com
Cc: Antonio Quartulli <antonio@mandelbit.com>,
	andrew+netdev@lunn.ch, davem@davemloft.net, edumazet@kernel.org,
	kuba@kernel.org, pabeni@redhat.com, ast@kernel.org,
	daniel@iogearbox.net, hawk@kernel.org, john.fastabend@gmail.com,
	sdf@fomichev.me, netdev@vger.kernel.org, bpf@vger.kernel.org
Subject: [PATCH net-next v3 2/3] tun: read tun->flags once in tun_get_user()
Date: Fri, 27 Nov 2026 15:14:04 +0100	[thread overview]
Message-ID: <20261127141406.651406-3-antonio@mandelbit.com> (raw)
In-Reply-To: <20261127141406.651406-1-antonio@mandelbit.com>

tun_get_user() reads tun->flags several times. tun_set_iff() can still
rewrite them after attaching a queue, and TUNSETVNETLE/BE can change the
vnet header byte order at any time, so a packet being written can see a
flag change halfway through: the PI header consumed under one IFF_NO_PI
and interpreted under the other, or the vnet header validated in one
byte order and applied to the skb in the other.

Read the flags once, pairing with the WRITE_ONCE() of their writers, and
use that copy throughout, including for the build_skb eligibility check.

Signed-off-by: Antonio Quartulli <antonio@mandelbit.com>
---
 drivers/net/tun.c | 27 +++++++++++++++------------
 1 file changed, 15 insertions(+), 12 deletions(-)

diff --git a/drivers/net/tun.c b/drivers/net/tun.c
index e2fe7174e438..09f31faaced2 100644
--- a/drivers/net/tun.c
+++ b/drivers/net/tun.c
@@ -1625,10 +1625,10 @@ static void tun_rx_batched(struct tun_struct *tun, struct tun_file *tfile,
 	}
 }
 
-static bool tun_can_build_skb(struct tun_struct *tun, struct tun_file *tfile,
+static bool tun_can_build_skb(unsigned int flags, struct tun_file *tfile,
 			      int len, int noblock, bool zerocopy)
 {
-	if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP)
+	if ((flags & TUN_TYPE_MASK) != IFF_TAP)
 		return false;
 
 	if (tfile->socket.sk->sk_sndbuf != INT_MAX)
@@ -1823,6 +1823,9 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
 	bool frags = tun_napi_frags_enabled(tfile);
 	enum skb_drop_reason drop_reason = SKB_DROP_REASON_NOT_SPECIFIED;
 	netdev_features_t features = 0;
+	unsigned int flags;
+
+	flags = READ_ONCE(tun->flags);
 
 	/*
 	 * Keep it easy and always zero the whole buffer, even if the
@@ -1832,7 +1835,7 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
 	memset(&hdr, 0, sizeof(hdr));
 	gso = (struct virtio_net_hdr *)&hdr;
 
-	if (!(tun->flags & IFF_NO_PI)) {
+	if (!(flags & IFF_NO_PI)) {
 		if (len < sizeof(pi))
 			return -EINVAL;
 		len -= sizeof(pi);
@@ -1841,11 +1844,11 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
 			return -EFAULT;
 	}
 
-	if (tun->flags & IFF_VNET_HDR) {
+	if (flags & IFF_VNET_HDR) {
 		int vnet_hdr_sz = READ_ONCE(tun->vnet_hdr_sz);
 
 		features = tun_vnet_hdr_guest_features(vnet_hdr_sz);
-		hdr_len = __tun_vnet_hdr_get(vnet_hdr_sz, tun->flags,
+		hdr_len = __tun_vnet_hdr_get(vnet_hdr_sz, flags,
 					     features, from, gso);
 		if (hdr_len < 0)
 			return hdr_len;
@@ -1853,7 +1856,7 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
 		len -= vnet_hdr_sz;
 	}
 
-	if ((tun->flags & TUN_TYPE_MASK) == IFF_TAP) {
+	if ((flags & TUN_TYPE_MASK) == IFF_TAP) {
 		align += NET_IP_ALIGN;
 		if (unlikely(len < ETH_HLEN || (hdr_len && hdr_len < ETH_HLEN)))
 			return -EINVAL;
@@ -1875,7 +1878,7 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
 			zerocopy = true;
 	}
 
-	if (!frags && tun_can_build_skb(tun, tfile, len, noblock, zerocopy)) {
+	if (!frags && tun_can_build_skb(flags, tfile, len, noblock, zerocopy)) {
 		/* For the packet that is not easy to be processed
 		 * (e.g gso or jumbo packet), we will do it at after
 		 * skb was created with generic XDP routine.
@@ -1929,9 +1932,9 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
 	}
 
 	skb->dev = tun->dev;
-	switch (tun->flags & TUN_TYPE_MASK) {
+	switch (flags & TUN_TYPE_MASK) {
 	case IFF_TUN:
-		if (tun->flags & IFF_NO_PI) {
+		if (flags & IFF_NO_PI) {
 			u8 ip_version;
 
 			drop_reason = pskb_may_pull_reason(skb, 1);
@@ -1967,14 +1970,14 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
 		break;
 	}
 
-	if (tun_vnet_hdr_tnl_to_skb(tun->flags, features, skb, &hdr,
-				    (tun->flags & TUN_TYPE_MASK) == IFF_TAP)) {
+	if (tun_vnet_hdr_tnl_to_skb(flags, features, skb, &hdr,
+				    (flags & TUN_TYPE_MASK) == IFF_TAP)) {
 		atomic_long_inc(&tun->rx_frame_errors);
 		err = -EINVAL;
 		goto free_skb;
 	}
 
-	if ((tun->flags & TUN_TYPE_MASK) == IFF_TAP)
+	if ((flags & TUN_TYPE_MASK) == IFF_TAP)
 		skb->protocol = eth_type_trans(skb, tun->dev);
 
 	/* copy skb_ubuf_info for callback when skb has no error */
-- 
2.56.1


  parent reply	other threads:[~2026-11-27 14:14 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-27 14:14 [PATCH net-next v3 0/3] tun: trim per-packet work on the write path Antonio Quartulli
2026-11-27 14:14 ` [PATCH net-next v3 1/3] tun: don't disable BHs for the XDP program check of every packet Antonio Quartulli
2026-11-27 14:14 ` Antonio Quartulli [this message]
2026-11-27 14:14 ` [PATCH net-next v3 3/3] tun: skip the virtio-net header parsing without IFF_VNET_HDR Antonio Quartulli

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=20261127141406.651406-3-antonio@mandelbit.com \
    --to=antonio@mandelbit.com \
    --cc=andrew+netdev@lunn.ch \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=davem@davemloft.net \
    --cc=edumazet@kernel.org \
    --cc=hawk@kernel.org \
    --cc=jasowangio@gmail.com \
    --cc=john.fastabend@gmail.com \
    --cc=kuba@kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=sdf@fomichev.me \
    --cc=willemdebruijn.kernel@gmail.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