Skip to content

Commit 7c0cdf0

Browse files
albanborkmann
authored andcommitted
bpf, lpm: fix lookup bug in map_delete_elem
trie_delete_elem() was deleting an entry even though it was not matching if the prefixlen was correct. This patch adds a check on matchlen. Reproducer: $ sudo bpftool map create /sys/fs/bpf/mylpm type lpm_trie key 8 value 1 entries 128 name mylpm flags 1 $ sudo bpftool map update pinned /sys/fs/bpf/mylpm key hex 10 00 00 00 aa bb cc dd value hex 01 $ sudo bpftool map dump pinned /sys/fs/bpf/mylpm key: 10 00 00 00 aa bb cc dd value: 01 Found 1 element $ sudo bpftool map delete pinned /sys/fs/bpf/mylpm key hex 10 00 00 00 ff ff ff ff $ echo $? 0 $ sudo bpftool map dump pinned /sys/fs/bpf/mylpm Found 0 elements A similar reproducer is added in the selftests. Without the patch: $ sudo ./tools/testing/selftests/bpf/test_lpm_map test_lpm_map: test_lpm_map.c:485: test_lpm_delete: Assertion `bpf_map_delete_elem(map_fd, key) == -1 && errno == ENOENT' failed. Aborted With the patch: test_lpm_map runs without errors. Fixes: e454cf5 ("bpf: Implement map_delete_elem for BPF_MAP_TYPE_LPM_TRIE") Cc: Craig Gallek <kraig@google.com> Signed-off-by: Alban Crequy <alban@kinvolk.io> Acked-by: Craig Gallek <kraig@google.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
1 parent 11fe926 commit 7c0cdf0

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

kernel/bpf/lpm_trie.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,7 @@ static int trie_delete_elem(struct bpf_map *map, void *_key)
471471
}
472472

473473
if (!node || node->prefixlen != key->prefixlen ||
474+
node->prefixlen != matchlen ||
474475
(node->flags & LPM_TREE_NODE_FLAG_IM)) {
475476
ret = -ENOENT;
476477
goto out;

tools/testing/selftests/bpf/test_lpm_map.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,16 @@ static void test_lpm_delete(void)
474474
assert(bpf_map_lookup_elem(map_fd, key, &value) == -1 &&
475475
errno == ENOENT);
476476

477+
key->prefixlen = 30; // unused prefix so far
478+
inet_pton(AF_INET, "192.255.0.0", key->data);
479+
assert(bpf_map_delete_elem(map_fd, key) == -1 &&
480+
errno == ENOENT);
481+
482+
key->prefixlen = 16; // same prefix as the root node
483+
inet_pton(AF_INET, "192.255.0.0", key->data);
484+
assert(bpf_map_delete_elem(map_fd, key) == -1 &&
485+
errno == ENOENT);
486+
477487
/* assert initial lookup */
478488
key->prefixlen = 32;
479489
inet_pton(AF_INET, "192.168.0.1", key->data);

0 commit comments

Comments
 (0)