Skip to content

Commit 412bf01

Browse files
Hou Taogregkh
authored andcommitted
bpf: Fix exact match conditions in trie_get_next_key()
[ Upstream commit 27abc7b ] trie_get_next_key() uses node->prefixlen == key->prefixlen to identify an exact match, However, it is incorrect because when the target key doesn't fully match the found node (e.g., node->prefixlen != matchlen), these two nodes may also have the same prefixlen. It will return expected result when the passed key exist in the trie. However when a recently-deleted key or nonexistent key is passed to trie_get_next_key(), it may skip keys and return incorrect result. Fix it by using node->prefixlen == matchlen to identify exact matches. When the condition is true after the search, it also implies node->prefixlen equals key->prefixlen, otherwise, the search would return NULL instead. Fixes: b471f2f ("bpf: implement MAP_GET_NEXT_KEY command for LPM_TRIE map") Reviewed-by: Toke Høiland-Jørgensen <[email protected]> Signed-off-by: Hou Tao <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]> Signed-off-by: Sasha Levin <[email protected]>
1 parent 7218e44 commit 412bf01

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

kernel/bpf/lpm_trie.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -646,7 +646,7 @@ static int trie_get_next_key(struct bpf_map *map, void *_key, void *_next_key)
646646
struct lpm_trie_node **node_stack = NULL;
647647
int err = 0, stack_ptr = -1;
648648
unsigned int next_bit;
649-
size_t matchlen;
649+
size_t matchlen = 0;
650650

651651
/* The get_next_key follows postorder. For the 4 node example in
652652
* the top of this file, the trie_get_next_key() returns the following
@@ -685,7 +685,7 @@ static int trie_get_next_key(struct bpf_map *map, void *_key, void *_next_key)
685685
next_bit = extract_bit(key->data, node->prefixlen);
686686
node = rcu_dereference(node->child[next_bit]);
687687
}
688-
if (!node || node->prefixlen != key->prefixlen ||
688+
if (!node || node->prefixlen != matchlen ||
689689
(node->flags & LPM_TREE_NODE_FLAG_IM))
690690
goto find_leftmost;
691691

0 commit comments

Comments
 (0)