public inbox for netdev@vger.kernel.org
 help / color / mirror / Atom feed
From: Jiayuan Chen <jiayuan.chen@linux.dev>
To: netdev@vger.kernel.org
Cc: jiayuan.chen@linux.dev, Jiayuan Chen <jiayuan.chen@shopee.com>,
	syzbot+334190e097a98a1b81bb@syzkaller.appspotmail.com,
	David Ahern <dsahern@kernel.org>,
	"David S. Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@google.com>,
	Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
	Simon Horman <horms@kernel.org>,
	linux-kernel@vger.kernel.org
Subject: [PATCH net v1] net: nexthop: fix panic when IPv4 route references IPv6 nexthop
Date: Sat, 28 Feb 2026 11:13:59 +0800	[thread overview]
Message-ID: <20260228031400.163009-1-jiayuan.chen@linux.dev> (raw)

From: Jiayuan Chen <jiayuan.chen@shopee.com>

fib_check_nexthop() does not validate that the nexthop family matches
the route family. This allows an IPv4 route to reference an IPv6
nexthop object. When the IPv4 route is looked up, __mkroute_output()
accesses nhc->nhc_pcpu_rth_output which is never allocated for IPv6
nexthops (fib6_nh_init does not call fib_nh_common_init), causing a
NULL pointer dereference.

Note that this is not about IPv4 routes with IPv6 gateways (RFC 5549),
which uses an AF_INET nexthop with nhc_gw_family=AF_INET6 and properly
allocates nhc_pcpu_rth_output via fib_nh_common_init(). The bug here
is an AF_INET6 nexthop object being directly referenced by an IPv4
route, which is an invalid combination.

Add the missing family check in fib_check_nexthop(), mirroring what
fib6_check_nexthop() already does for the reverse direction (rejecting
IPv6 routes that reference IPv4 nexthop objects).

Reproducer:

  unshare -rn
  ip link set lo up
  ip nexthop add id 100 via fe80::1 dev lo
  ip route add 172.20.20.0/24 nhid 100
  ping -c1 172.20.20.1

After fix:
...
$ ip route add 172.20.20.0/24 nhid 100
Error: IPv4 routes can not use an IPv6 nexthop.

Reported-by: syzbot+334190e097a98a1b81bb@syzkaller.appspotmail.com
Closes: https://lore.kernel.org/all/698f8482.a70a0220.2c38d7.00ca.GAE@google.com/T/
Fixes: 4c7e8084fd46 ("ipv4: Plumb support for nexthop object in a fib_info")
Signed-off-by: Jiayuan Chen <jiayuan.chen@shopee.com>
---
 net/ipv4/nexthop.c | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/net/ipv4/nexthop.c b/net/ipv4/nexthop.c
index 7b9d70f9b31c..0f236110cd58 100644
--- a/net/ipv4/nexthop.c
+++ b/net/ipv4/nexthop.c
@@ -1634,6 +1634,12 @@ int fib_check_nexthop(struct nexthop *nh, u8 scope,
 			goto out;
 		}
 
+		if (!nhg->has_v4) {
+			NL_SET_ERR_MSG(extack, "IPv4 routes can not use an IPv6 nexthop");
+			err = -EINVAL;
+			goto out;
+		}
+
 		if (scope == RT_SCOPE_HOST) {
 			NL_SET_ERR_MSG(extack, "Route with host scope can not have multiple nexthops");
 			err = -EINVAL;
@@ -1650,6 +1656,11 @@ int fib_check_nexthop(struct nexthop *nh, u8 scope,
 			err = -EINVAL;
 			goto out;
 		}
+		if (nhi->family != AF_INET) {
+			NL_SET_ERR_MSG(extack, "IPv4 routes can not use an IPv6 nexthop");
+			err = -EINVAL;
+			goto out;
+		}
 		err = nexthop_check_scope(nhi, scope, extack);
 	}
 
-- 
2.43.0


             reply	other threads:[~2026-02-28  3:14 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-02-28  3:13 Jiayuan Chen [this message]
2026-02-28 15:39 ` [PATCH net v1] net: nexthop: fix panic when IPv4 route references IPv6 nexthop Jakub Kicinski
2026-02-28 16:33   ` David Ahern
2026-02-28 17:04     ` Eric Dumazet
2026-03-01  1:57       ` Jiayuan Chen
2026-03-01 18:05         ` David Ahern
2026-03-01 18:11         ` David Ahern

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=20260228031400.163009-1-jiayuan.chen@linux.dev \
    --to=jiayuan.chen@linux.dev \
    --cc=davem@davemloft.net \
    --cc=dsahern@kernel.org \
    --cc=edumazet@google.com \
    --cc=horms@kernel.org \
    --cc=jiayuan.chen@shopee.com \
    --cc=kuba@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=syzbot+334190e097a98a1b81bb@syzkaller.appspotmail.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