net: nexthop: fix panic when IPv4 route references IPv6 nexthop
Subsystems (3)
PATCH 1/1 net: nexthop: fix panic when IPv4 route references IPv6 nexthop
From: Jiayuan Chen <[email protected]>
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.
--- net/ipv4/nexthop.c | 11 +++++++++++ 1 file changed, 11 insertions(+)
net/ipv4/nexthop.c +11 -0
@@ -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); }
Signature · 1 line
2.43.0
Jakub Kicinski
maintainerQuoted 15 lines · On Sat, 28 Feb 2026 11:13:59 +0800 Jiayuan Chen wrote:
From: Jiayuan Chen <[email protected]>
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()accessesnhc->nhc_pcpu_rth_outputwhich is never allocated for IPv6 nexthops (fib6_nh_initdoes not callfib_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_INETnexthop with nhc_gw_family=AF_INET6 and properly allocatesnhc_pcpu_rth_outputviafib_nh_common_init(). The bug here is anAF_INET6nexthop object being directly referenced by an IPv4 route, which is an invalid combination.
Quoted 3 lines
Add the missing family check in
fib_check_nexthop(), mirroring whatfib6_check_nexthop()already does for the reverse direction (rejecting IPv6 routes that reference IPv4 nexthop objects).
AFAICT this breaks a bunch of tests, quickest to repro with is gre_multipath_nh.sh but you should probably run fib_nexthops.sh on your fix as well.
Quoted 7 lines
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
Signature · 1 line
pw-bot: cr
David Ahern
maintainerQuoted 16 lines · Jakub Kicinski
Quoted 15 lines · On Sat, 28 Feb 2026 11:13:59 +0800 Jiayuan Chen wrote:
From: Jiayuan Chen <[email protected]>
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()accessesnhc->nhc_pcpu_rth_outputwhich is never allocated for IPv6 nexthops (fib6_nh_initdoes not callfib_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_INETnexthop with nhc_gw_family=AF_INET6 and properly allocatesnhc_pcpu_rth_outputviafib_nh_common_init(). The bug here is anAF_INET6nexthop object being directly referenced by an IPv4 route, which is an invalid combination.
Quoted 8 lines
Quoted 3 lines
Add the missing family check in
fib_check_nexthop(), mirroring whatfib6_check_nexthop()already does for the reverse direction (rejecting IPv6 routes that reference IPv4 nexthop objects).AFAICT this breaks a bunch of tests, quickest to repro with is
gre_multipath_nh.shbut you should probably runfib_nexthops.shon your fix as well.
nothing to fix. The patch is wrong. IPv4 supports IPv6 gateways; that is a known feature.
please post the stack trace for the panic
Quoted 27 lines · David Ahern
Quoted 16 lines · Jakub Kicinski
Quoted 15 lines · On Sat, 28 Feb 2026 11:13:59 +0800 Jiayuan Chen wrote:
From: Jiayuan Chen <[email protected]>
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()accessesnhc->nhc_pcpu_rth_outputwhich is never allocated for IPv6 nexthops (fib6_nh_initdoes not callfib_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_INETnexthop with nhc_gw_family=AF_INET6 and properly allocatesnhc_pcpu_rth_outputviafib_nh_common_init(). The bug here is anAF_INET6nexthop object being directly referenced by an IPv4 route, which is an invalid combination.Quoted 8 lines
Quoted 3 lines
Add the missing family check in
fib_check_nexthop(), mirroring whatfib6_check_nexthop()already does for the reverse direction (rejecting IPv6 routes that reference IPv4 nexthop objects).AFAICT this breaks a bunch of tests, quickest to repro with is
gre_multipath_nh.shbut you should probably runfib_nexthops.shon your fix as well.
Quoted 4 lines
nothing to fix. The patch is wrong. IPv4 supports IPv6 gateways; that is a known feature.
please post the stack trace for the panic
Quoted 29 lines · Eric Dumazet
Quoted 26 lines · David Ahern
On 2/28/26 8:39 AM, Jakub Kicinski wrote:
On Sat, 28 Feb 2026 11:13:59 +0800 Jiayuan Chen wrote:
From: Jiayuan Chen <[email protected]>
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()accessesnhc->nhc_pcpu_rth_outputwhich is never allocated for IPv6 nexthops (fib6_nh_initdoes not callfib_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_INETnexthop with nhc_gw_family=AF_INET6 and properly allocatesnhc_pcpu_rth_outputviafib_nh_common_init(). The bug here is anAF_INET6nexthop object being directly referenced by an IPv4 route, which is an invalid combination.Add the missing family check in
fib_check_nexthop(), mirroring whatfib6_check_nexthop()already does for the reverse direction (rejecting IPv6 routes that reference IPv4 nexthop objects).AFAICT this breaks a bunch of tests, quickest to repro with is
gre_multipath_nh.shbut you should probably runfib_nexthops.shon your fix as well.
Quoted 7 lines
Quoted 5 lines
nothing to fix. The patch is wrong. IPv4 supports IPv6 gateways; that is a known feature.
please post the stack trace for the panic
My bad, the previous fix was wrong - IPv4 routes referencing IPv6 nexthop objects is totally via this path.
The crash actually only happens with loopback nexthops, e.g.:
ip nexthop add id 100 via fe80::1 dev lo
In fib6_nh_init(), nexthop objects always have fc_dst=:: (no destination prefix), so fib6_is_reject() returns true for any nexthop using loopback device. This causes it to skip fib_nh_common_init(), leaving nhc_pcpu_rth_output, nhc_exceptions and nhc_rth_input all NULL. When an IPv4 route later references this nexthop, __mkroute_output() hits raw_cpu_ptr(NULL) and crashes.
The simplest fix is just allocating nhc_pcpu_rth_output in the reject path of fib6_nh_init(). The release path already handles it correctly.
net/ipv6/route.c +6 -0
@@ -3643,6 +3643,12 @@ int fib6_nh_init(struct net *net, struct fib6_nh *fib6_nh, goto out; } } + fib6_nh->nh_common.nhc_pcpu_rth_output = + alloc_percpu_gfp(struct rtable __rcu *, gfp_flags); +if (!fib6_nh->nh_common.nhc_pcpu_rth_output) { + err = -ENOMEM ; + goto out; + } goto pcpu_alloc; }
./fib_nexthops.sh
Tests passed: 244 Tests failed: 0 Tests skipped: 2
root@bms-ytl-d1-ap
David Ahern
maintainerQuoted 8 lines · Jiayuan Chen
net/ipv6/route.c +0
-0@@ -3643,6 +3643,12 @@ int fib6_nh_init(struct net *net, struct fib6_nh *fib6_nh, goto out; }
net/ipv6/route.c int fib6_nh_init(struct net *net, struct fib6_nh *fib6_nh, · line 3654
Quoted 10 lines
net/ipv6/route.c +6
-0} + fib6_nh->nh_common.nhc_pcpu_rth_output = + alloc_percpu_gfp(struct rtable __rcu *, gfp_flags); +if (!fib6_nh->nh_common.nhc_pcpu_rth_output) { + err = -ENOMEM ; + goto out; + } goto pcpu_alloc; }
Already done by fib6_nh_init -> fib_nh_common_init():
nhc->nhc_pcpu_rth_output = alloc_percpu_gfp(struct rtable __rcu *,
gfp_flags);
if (!nhc->nhc_pcpu_rth_output)
return -ENOMEM;David Ahern
maintainerQuoted 11 lines · Jiayuan Chen
The crash actually only happens with loopback nexthops, e.g.:
ip nexthop add id 100 via fe80::1 dev lo
In
fib6_nh_init(), nexthop objects always have fc_dst=:: (no destination prefix), sofib6_is_reject()returns true for any nexthop using loopback device. This causes it to skipfib_nh_common_init(), leavingnhc_pcpu_rth_output,nhc_exceptionsandnhc_rth_inputall NULL. When an IPv4 route later references this nexthop,__mkroute_output()hitsraw_cpu_ptr(NULL)and crashes.
Quoted 3 lines
The simplest fix is just allocating
nhc_pcpu_rth_outputin the reject path offib6_nh_init(). The release path already handles it correctly.
should have read this before that last response. fib_nh_common_init exists to avoid putting ipv4 logic in ipv6 code, so your proposed patch is wrong.
Perhaps the better change is to not use fib6_is_reject in fib6_nh_init or relax the loopback check or move the jump label pcpu_alloc to still call fib_nh_common_init.
Also, a test should be added to the nexthops test for this case.