🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: linux.kernel.netdev
4 messages
4 total messages Started by Daniël Trujillo via B4 Relay Thu, 08 Oct 2026 22:10
[PATCH net] ipv4: fib_trie: prevent speculative out-of-bounds child access
#970
Author: Daniël Trujillo via B4 Relay
Date: Thu, 08 Oct 2026 22:10
54 lines
8107 bytes
From: Daniël Trujillo <datrujillo@nvidia.com>

fib_table_lookup() checks the child index against the bounds before
passing it to get_child_rcu().

The index is derived from the lookup key, which in turn is derived from
flp->daddr. A userspace process can therefore influence the index by
triggering lookups for chosen destination IPv4 addresses.

If this bounds check is mispredicted as in-bounds, an out-of-bounds index
can be transiently passed to get_child_rcu(). The value loaded beyond the
child array is then treated as a node pointer. The next loop iteration
dereferences that transient pointer through get_cindex(), causing a
page-table walk whose cache footprint can leak (parts of) the pointer.

Sanitize the index with array_index_nospec() before accessing the child
array.

Fixes: 9f9e636d4f89 ("fib_trie: Optimize fib_table_lookup to avoid wasting time on loops/variables")
Cc: stable@vger.kernel.org
Signed-off-by: Daniël Trujillo <datrujillo@nvidia.com>
---
 net/ipv4/fib_trie.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/net/ipv4/fib_trie.c b/net/ipv4/fib_trie.c
index 248514dce0cd..bd8a368c5502 100644
--- a/net/ipv4/fib_trie.c
+++ b/net/ipv4/fib_trie.c
@@ -61,6 +61,7 @@
 #include <linux/export.h>
 #include <linux/vmalloc.h>
 #include <linux/notifier.h>
+#include <linux/nospec.h>
 #include <net/net_namespace.h>
 #include <net/inet_dscp.h>
 #include <net/ip.h>
@@ -1474,6 +1475,7 @@ int fib_table_lookup(struct fib_table *tb, const struct flowi4 *flp,
 			cindex = index;
 		}
 
+		index = array_index_nospec(index, 1ul << n->bits);
 		n = get_child_rcu(n, index);
 		if (unlikely(!n))
 			goto backtrace;

---
base-commit: 37f12441f557468a56c1e27790413aa78c82afa2
change-id: 20261008-fib-trie-nospec-5c76028c8e7c

Best regards,
--  
Daniël Trujillo <datrujillo@nvidia.com>
Re: [PATCH net] ipv4: fib_trie: prevent speculative out-of-bounds child access
#976
Author: netdev-bot+sinfo
Date: Thu, 08 Oct 2026 22:17
22 lines
6460 bytes
Hi!

This is an automated message. This series looks like a fix, but its
commit messages seem to be missing some information:

 - How the issue was discovered, e.g. hit in production, hit during
   development, syzbot report, manual code inspection, LLM or static
   analysis tool scan.

 - Whether the issue was actually triggered, or is only theoretical
   (e.g. found by code inspection). If it was triggered please include
   the symptoms, like the stack trace or error messages.

Please do not repost the series just to address the above. Instead,
reply to this email with the missing information, so that reviewers
can take it into account. If the series needs another revision for
other reasons, please include the information in the commit messages
then.

The evaluation is done by an LLM so it may be wrong, if you think
that is the case please reply and explain.
Re: [PATCH net] ipv4: fib_trie: prevent speculative out-of-bounds child access
#977
Author: Eric Dumazet
Date: Thu, 08 Oct 2026 22:25
117 lines
10621 bytes
Le ven. 9 oct. 2026 à 00:10, Daniël Trujillo via B4 Relay
<devnull+datrujillo.nvidia.com@kernel.org> a écrit :
>
> From: Daniël Trujillo <datrujillo@nvidia.com>
>
> fib_table_lookup() checks the child index against the bounds before
> passing it to get_child_rcu().
>
> The index is derived from the lookup key, which in turn is derived from
> flp->daddr. A userspace process can therefore influence the index by
> triggering lookups for chosen destination IPv4 addresses.
>
> If this bounds check is mispredicted as in-bounds, an out-of-bounds index
> can be transiently passed to get_child_rcu(). The value loaded beyond the
> child array is then treated as a node pointer. The next loop iteration
> dereferences that transient pointer through get_cindex(), causing a
> page-table walk whose cache footprint can leak (parts of) the pointer.

Has this been demonstrated, or is it the output of a static tool ?

key, pos and bits all live in the first 8 bytes of struct key_vector,
so the bound can not be made slow relative to the index by evicting
a cache line. The conditional branch should resolve a couple of cycles
after the out-of-bounds load is issued, before a dependent dereference
can start.

For a patch targeting net and stable, in the IPv4 lookup fast path,
we would like the changelog to say what was actually observed.

>
> Sanitize the index with array_index_nospec() before accessing the child
> array.
>
> Fixes: 9f9e636d4f89 ("fib_trie: Optimize fib_table_lookup to avoid wasting time on loops/variables")
> Cc: stable@vger.kernel.org
> Signed-off-by: Daniël Trujillo <datrujillo@nvidia.com>
> ---
>  net/ipv4/fib_trie.c | 2 ++
>  1 file changed, 2 insertions(+)
>
> diff --git a/net/ipv4/fib_trie.c b/net/ipv4/fib_trie.c
> index 248514dce0cd..bd8a368c5502 100644
> --- a/net/ipv4/fib_trie.c
> +++ b/net/ipv4/fib_trie.c
> @@ -61,6 +61,7 @@
>  #include <linux/export.h>
>  #include <linux/vmalloc.h>
>  #include <linux/notifier.h>
> +#include <linux/nospec.h>
>  #include <net/net_namespace.h>
>  #include <net/inet_dscp.h>
>  #include <net/ip.h>
> @@ -1474,6 +1475,7 @@ int fib_table_lookup(struct fib_table *tb, const struct flowi4 *flp,
>                         cindex = index;
>                 }
>
> +               index = array_index_nospec(index, 1ul << n->bits);
>                 n = get_child_rcu(n, index);
>                 if (unlikely(!n))
>                         goto backtrace;


This is not complete.

cindex has been set from the unclamped index just above. If the
(!n) branch is predicted taken under the same mispredicted bounds
check, the backtrace path does :

cindex &= cindex - 1;
cptr = &pn->tnode[cindex];
...
n = rcu_dereference(*cptr);

and the result is dereferenced at the top of step 2
(prefix_mismatch(key, n), n->slen, n->pos).

This is the same pattern you describe : one out-of-bounds load, then
a dereference of the loaded value.

The clamp needs to be done before cindex is recorded, and can stay
after the IS_LEAF() test so that leaf hits do not pay for it :

@@ -1465,7 +1466,9 @@ int fib_table_lookup(struct fib_table *tb, const
struct flowi4 *flp,
  /* we have found a leaf. Prefixes have already been compared */
  if (IS_LEAF(n))
  goto found;

+ index = array_index_nospec(index, 1ul << n->bits);
+
  /* only record pn and cindex if we are going to be chopping
  * bits later.  Otherwise we are just wasting cycles.
  */

Also, fib_find_node() has the exact same construct (same bounds check,
then get_child_rcu(n, index) on the next iteration), and is called
with a user provided key from fib_table_insert() and fib_table_delete().
RTM_NEWROUTE / RTM_DELROUTE only need CAP_NET_ADMIN in the netns owner
user namespace. This runs under RTNL, there is no performance concern
there.

Please also take a look at leaf_walk_rcu() (cindex >> pn->bits), and
say in the changelog why it is or is not affected.

>
> ---
> base-commit: 37f12441f557468a56c1e27790413aa78c82afa2
> change-id: 20261008-fib-trie-nospec-5c76028c8e7c
>
> Best regards,
> --
> Daniël Trujillo <datrujillo@nvidia.com>
>
>
Re: [PATCH net] ipv4: fib_trie: prevent speculative out-of-bounds child access
#2546
Author: netdev-bot+sashiko
Date: Mon, 12 Oct 2026 23:40
126 lines
9792 bytes
Thank you for your contribution! Sashiko AI review found 1 potential
issue(s) to consider.

Critical: 0 · High: 1 (1) · Medium: 0 · Low: 0

If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).

- [High] The Spectre v1 mitigation in fib_table_lookup()
  (net/ipv4/fib_trie.c) is incomplete: the raw index is copied into…

Pre-existing issues:
- [High] fib_find_node() in net/ipv4/fib_trie.c has the same Spectre v1
  gadget the patch fixes in fib_table_lookup(), and the patch leaves it…

--

ipv4: fib_trie: prevent speculative out-of-bounds child access

This patch adds array_index_nospec() in fib_table_lookup(). It clamps the
child index before get_child_rcu() reads n->tnode[]. The goal is to stop a
mispredicted bounds check from loading past the child array with an
index derived from flp->daddr.

> diff --git a/net/ipv4/fib_trie.c b/net/ipv4/fib_trie.c
> index 248514dce0cdd..bd8a368c55026 100644
> --- a/net/ipv4/fib_trie.c
> +++ b/net/ipv4/fib_trie.c

[ ... ]

> @@ -1474,6 +1475,7 @@ int fib_table_lookup(struct fib_table *tb, const struct flowi4 *flp,
>  			cindex = index;
>  		}
>  
> +		index = array_index_nospec(index, 1ul << n->bits);
>  		n = get_child_rcu(n, index);
>  		if (unlikely(!n))
>  			goto backtrace;

[Severity: High]
Does the clamp run too late to cover cindex? In the Step 1 loop, the raw
index is saved into cindex before the new array_index_nospec() line runs:

		if (n->slen > n->pos) {
			pn = n;
			cindex = index;
		}

		index = array_index_nospec(index, 1ul << n->bits);

Suppose the "index >= (1ul << n->bits)" check is mispredicted for an
out-of-bounds index and n->slen > n->pos. Then cindex and pn keep the
unmasked value. The clamped index then loads tnode[0].

Execution can reach backtrace with those values still in place in two
ways: tnode[0] is NULL, or Step 2 later fails prefix_mismatch(). cindex
is non-zero, so the while (!cindex) loop is skipped, and this runs:

			cindex &= cindex - 1;

			/* grab pointer for next child node */
			cptr = &pn->tnode[cindex];

That only clears the lowest set bit, so cindex stays out of bounds and
its high bits are still the ones the attacker chose. For example, 0x10003
becomes 0x10002 with bits = 4.

The slot is then loaded and used as a node pointer at the top of Step 2:

		while ((n = rcu_dereference(*cptr)) == NULL) {
		...
		if (unlikely(prefix_mismatch(key, n)) || (n->slen == n->pos))

Isn't this the same "load past the child array, then dereference the
value as a node pointer" sequence the commit message describes?

Would moving the array_index_nospec() line above the
"if (n->slen > n->pos)" block clamp both cindex and the index passed to
get_child_rcu()?

[Severity: High]
This is a pre-existing issue and was not introduced by this patch.
fib_find_node() in the same file seems to have the same gadget, and this
patch leaves it as is:

net/ipv4/fib_trie.c:fib_find_node() {
	do {
		pn = n;
		n = get_child_rcu(n, index);

		if (!n)
			break;

		index = get_cindex(key, n);
		...
		if (index >= (1ul << n->bits)) {
			n = NULL;
			break;
		}

		/* keep searching until we find a perfect match leaf or NULL */
	} while (IS_TNODE(n));
	...
}

If that bounds check is mispredicted, IS_TNODE(n) is still true. The next
pass then calls get_child_rcu(n, index) with the unmasked index and
dereferences the loaded value through get_cindex().

The key is ntohl(cfg->fc_dst), set in fib_table_insert() and
fib_table_delete(). Both call fib_find_node(t, &tp, key), and they are
reached from RTM_NEWROUTE and RTM_DELROUTE. That needs CAP_NET_ADMIN over
the netns, which an unprivileged user can often get by creating a user
namespace.

The subject line says "prevent speculative out-of-bounds child access".
Should fib_find_node() get the same array_index_nospec() clamp before
its child load?

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261008-fib-trie-nospec-v1-1-fa18726d485e%40nvidia.com
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads