realtek: l3: do not read a nexthop array that is not there

A route through a nexthop object keeps its nexthops in that object, and
the fib_info behind it is allocated with none of its own: for a route
carrying an nhid, fib_create_info() sets the nexthop count to zero and
sizes the structure by it, then points fi->nh at the object
(net/ipv4/fib_semantics.c). fib_info_nh() says the same by warning when
it is asked for a nexthop of such a route, before handing back the
address where the array would have started.

The IPv4 FIB handler reads that address in the variable initialisers of
three of its functions, ahead of every test any of them makes, so
"ip route add ... nhid N" on a switch whose L3 setup is in place takes
the warning and then dereferences whatever follows the allocation as a
net_device. The RTL839x carries that setup outside any config symbol, so
a stock build gets there.

Leave the route alone where the notifier already screens the IPv4 events
it cannot use, which is ahead of all three reads. Leaving it alone
rather than refusing it, as the check on the next line does for an IPv6
gateway, is deliberate: the kernel still installs the route, and an
error returned from this notifier would come back out of
register_fib_notifier(), which replays the routes that already exist -
so one such route present at probe would cost the switch its whole L3
offload rather than that one entry.

Assisted-by: Claude:claude-opus-5
Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Gennaro Cimmino <gcimmino@rayonra.net>
Link: https://github.com/openwrt/openwrt/pull/25310
Signed-off-by: Markus Stockhausen <markus.stockhausen@gmx.de>
This commit is contained in:
Gennaro Cimmino authored and Markus Stockhausen committed 2026-09-29 13:58:05 +02:00
1 parent 138fabb79f
commit 4ec7b59613
1 file changed
+9
@@ -1626,6 +1626,15 @@ static int otto_l3_fib_notifier(struct notifier_block *this, unsigned long event
if (info->family == AF_INET) {
struct fib_entry_notifier_info *fen_info = ptr;
/* A route through a nexthop object keeps no nexthop
* array of its own, and everything below reads one.
*/
if (fen_info->fi->nh) {
dev_dbg(ctrl->dev, "route through a nexthop object, not offloaded\n");
kfree(fib_work);
return NOTIFY_DONE;
}
if (fen_info->fi->fib_nh_is_v6) {
NL_SET_ERR_MSG_MOD(info->extack,
"IPv6 gateway with IPv4 route is not supported");