From: "Xiang Mei (Microsoft)" <xmei5@asu.edu>
To: kuba@kernel.org, pablo@netfilter.org,
Andrea Mayer <andrea.mayer@uniroma2.it>
Cc: "David S . Miller" <davem@davemloft.net>,
Paolo Abeni <pabeni@redhat.com>, Simon Horman <horms@kernel.org>,
netdev@vger.kernel.org, linux-kernel@vger.kernel.org,
Ryoga Saito <contact@proelbtn.com>,
AutonomousCodeSecurity@microsoft.com,
tgopinath@linux.microsoft.com, kys@microsoft.com,
"Xiang Mei (Microsoft)" <xmei5@asu.edu>
Subject: [PATCH net v2 2/2] seg6: check lwtunnel state after nf hooks in iptunnel
Date: Tue, 28 Jul 2026 21:54:48 +0000 [thread overview]
Message-ID: <20260728215448.1543553-2-xmei5@asu.edu> (raw)
In-Reply-To: <20260728215448.1543553-1-xmei5@asu.edu>
seg6_input_core() and seg6_output_core() are the okfns of the
POST_ROUTING hook that seg6_input_nf() and seg6_output_nf() dispatch
through when nf_hooks_lwtunnel is enabled, and they read
skb_dst(skb)->lwtstate on the assumption fixed for seg6local in patch 1.
A hook may drop the dst, replace it with a metadata dst, or leave a route
whose lwtstate is NULL; the last is reachable with SNAT plus an XFRM
policy.
Validate the dst and the encap type before use. seg6_do_srh() reads
skb_dst(skb)->lwtstate too, but these two are its only callers and
neither touches the dst in between.
In seg6_output_core() the validated lwtstate also takes over the dst-loop
comparison, so "orig_dst" is gone, matching seg6_input_core().
Fixes: 7a3f5b0de364 ("netfilter: add netfilter hooks to SRv6 data plane")
Reported-by: Andrea Mayer <andrea.mayer@uniroma2.it>
Signed-off-by: Xiang Mei (Microsoft) <xmei5@asu.edu>
---
v2: new patch, split out from the seg6local fix (Andrea Mayer).
net/ipv6/seg6_iptunnel.c | 33 +++++++++++++++++++++++++++------
1 file changed, 27 insertions(+), 6 deletions(-)
diff --git a/net/ipv6/seg6_iptunnel.c b/net/ipv6/seg6_iptunnel.c
index 4c45c0a77d75..60883dae5ae9 100644
--- a/net/ipv6/seg6_iptunnel.c
+++ b/net/ipv6/seg6_iptunnel.c
@@ -23,6 +23,7 @@
#include <net/addrconf.h>
#include <net/ip6_route.h>
#include <net/dst_cache.h>
+#include <net/dst_metadata.h>
#ifdef CONFIG_IPV6_SEG6_HMAC
#include <net/seg6_hmac.h>
#endif
@@ -65,6 +66,17 @@ seg6_encap_lwtunnel(struct lwtunnel_state *lwt)
return seg6_lwt_lwtunnel(lwt)->tuninfo;
}
+static struct lwtunnel_state *seg6_lwtst_from_skb(struct sk_buff *skb)
+{
+ struct dst_entry *dst = skb_dst(skb);
+
+ if (!skb_valid_dst(skb) || !dst->lwtstate ||
+ dst->lwtstate->type != LWTUNNEL_ENCAP_SEG6)
+ return NULL;
+
+ return dst->lwtstate;
+}
+
static const struct nla_policy seg6_iptunnel_policy[SEG6_IPTUNNEL_MAX + 1] = {
[SEG6_IPTUNNEL_SRH] = { .type = NLA_BINARY },
[SEG6_IPTUNNEL_SRC] = NLA_POLICY_EXACT_LEN(sizeof(struct in6_addr)),
@@ -488,18 +500,21 @@ static int seg6_input_finish(struct net *net, struct sock *sk,
static int seg6_input_core(struct net *net, struct sock *sk,
struct sk_buff *skb)
{
- struct dst_entry *orig_dst = skb_dst(skb);
struct dst_entry *dst = NULL;
struct lwtunnel_state *lwtst;
struct seg6_lwt *slwt;
int err;
- /* We cannot dereference "orig_dst" once ip6_route_input() or
+ /* We cannot dereference the incoming dst once ip6_route_input() or
* skb_dst_drop() is called. However, in order to detect a dst loop, we
* need the address of its lwtstate. So, save the address of lwtstate
* now and use it later as a comparison.
*/
- lwtst = orig_dst->lwtstate;
+ lwtst = seg6_lwtst_from_skb(skb);
+ if (unlikely(!lwtst)) {
+ err = -EINVAL;
+ goto drop;
+ }
slwt = seg6_lwt_lwtunnel(lwtst);
@@ -581,12 +596,18 @@ static int seg6_input(struct sk_buff *skb)
static int seg6_output_core(struct net *net, struct sock *sk,
struct sk_buff *skb)
{
- struct dst_entry *orig_dst = skb_dst(skb);
struct dst_entry *dst = NULL;
+ struct lwtunnel_state *lwtst;
struct seg6_lwt *slwt;
int err;
- slwt = seg6_lwt_lwtunnel(orig_dst->lwtstate);
+ lwtst = seg6_lwtst_from_skb(skb);
+ if (unlikely(!lwtst)) {
+ err = -EINVAL;
+ goto drop;
+ }
+
+ slwt = seg6_lwt_lwtunnel(lwtst);
local_bh_disable();
dst = dst_cache_get(&slwt->cache_output);
@@ -614,7 +635,7 @@ static int seg6_output_core(struct net *net, struct sock *sk,
}
/* cache only if we don't create a dst reference loop */
- if (orig_dst->lwtstate != dst->lwtstate) {
+ if (lwtst != dst->lwtstate) {
local_bh_disable();
dst_cache_set_ip6(&slwt->cache_output, dst, &fl6.saddr);
local_bh_enable();
--
2.43.0
prev parent reply other threads:[~2026-07-28 21:55 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-07-28 21:54 [PATCH net v2 1/2] seg6: check lwtunnel state after nf hooks in seg6local Xiang Mei (Microsoft)
2026-07-28 21:54 ` Xiang Mei (Microsoft) [this message]
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=20260728215448.1543553-2-xmei5@asu.edu \
--to=xmei5@asu.edu \
--cc=AutonomousCodeSecurity@microsoft.com \
--cc=andrea.mayer@uniroma2.it \
--cc=contact@proelbtn.com \
--cc=davem@davemloft.net \
--cc=horms@kernel.org \
--cc=kuba@kernel.org \
--cc=kys@microsoft.com \
--cc=linux-kernel@vger.kernel.org \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=pablo@netfilter.org \
--cc=tgopinath@linux.microsoft.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