]> git.ipfire.org Git - thirdparty/kernel/stable-queue.git/blame - releases/4.19.31/bpf-lpm-fix-lookup-bug-in-map_delete_elem.patch
Linux 4.19.31
[thirdparty/kernel/stable-queue.git] / releases / 4.19.31 / bpf-lpm-fix-lookup-bug-in-map_delete_elem.patch
CommitLineData
36e3f504
SL
1From b767f332f8defabc623af4217793f028897be730 Mon Sep 17 00:00:00 2001
2From: Alban Crequy <alban@kinvolk.io>
3Date: Fri, 22 Feb 2019 14:19:08 +0100
4Subject: bpf, lpm: fix lookup bug in map_delete_elem
5
6[ Upstream commit 7c0cdf0b3940f63d9777c3fcf250a2f83859ca54 ]
7
8trie_delete_elem() was deleting an entry even though it was not matching
9if the prefixlen was correct. This patch adds a check on matchlen.
10
11Reproducer:
12
13$ sudo bpftool map create /sys/fs/bpf/mylpm type lpm_trie key 8 value 1 entries 128 name mylpm flags 1
14$ sudo bpftool map update pinned /sys/fs/bpf/mylpm key hex 10 00 00 00 aa bb cc dd value hex 01
15$ sudo bpftool map dump pinned /sys/fs/bpf/mylpm
16key: 10 00 00 00 aa bb cc dd value: 01
17Found 1 element
18$ sudo bpftool map delete pinned /sys/fs/bpf/mylpm key hex 10 00 00 00 ff ff ff ff
19$ echo $?
200
21$ sudo bpftool map dump pinned /sys/fs/bpf/mylpm
22Found 0 elements
23
24A similar reproducer is added in the selftests.
25
26Without the patch:
27
28$ sudo ./tools/testing/selftests/bpf/test_lpm_map
29test_lpm_map: test_lpm_map.c:485: test_lpm_delete: Assertion `bpf_map_delete_elem(map_fd, key) == -1 && errno == ENOENT' failed.
30Aborted
31
32With the patch: test_lpm_map runs without errors.
33
34Fixes: e454cf595853 ("bpf: Implement map_delete_elem for BPF_MAP_TYPE_LPM_TRIE")
35Cc: Craig Gallek <kraig@google.com>
36Signed-off-by: Alban Crequy <alban@kinvolk.io>
37Acked-by: Craig Gallek <kraig@google.com>
38Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
39Signed-off-by: Sasha Levin <sashal@kernel.org>
40---
41 kernel/bpf/lpm_trie.c | 1 +
42 tools/testing/selftests/bpf/test_lpm_map.c | 10 ++++++++++
43 2 files changed, 11 insertions(+)
44
45diff --git a/kernel/bpf/lpm_trie.c b/kernel/bpf/lpm_trie.c
46index 9058317ba9de..4f3138e6ecb2 100644
47--- a/kernel/bpf/lpm_trie.c
48+++ b/kernel/bpf/lpm_trie.c
49@@ -432,6 +432,7 @@ static int trie_delete_elem(struct bpf_map *map, void *_key)
50 }
51
52 if (!node || node->prefixlen != key->prefixlen ||
53+ node->prefixlen != matchlen ||
54 (node->flags & LPM_TREE_NODE_FLAG_IM)) {
55 ret = -ENOENT;
56 goto out;
57diff --git a/tools/testing/selftests/bpf/test_lpm_map.c b/tools/testing/selftests/bpf/test_lpm_map.c
58index 147e34cfceb7..02d7c871862a 100644
59--- a/tools/testing/selftests/bpf/test_lpm_map.c
60+++ b/tools/testing/selftests/bpf/test_lpm_map.c
61@@ -474,6 +474,16 @@ static void test_lpm_delete(void)
62 assert(bpf_map_lookup_elem(map_fd, key, &value) == -1 &&
63 errno == ENOENT);
64
65+ key->prefixlen = 30; // unused prefix so far
66+ inet_pton(AF_INET, "192.255.0.0", key->data);
67+ assert(bpf_map_delete_elem(map_fd, key) == -1 &&
68+ errno == ENOENT);
69+
70+ key->prefixlen = 16; // same prefix as the root node
71+ inet_pton(AF_INET, "192.255.0.0", key->data);
72+ assert(bpf_map_delete_elem(map_fd, key) == -1 &&
73+ errno == ENOENT);
74+
75 /* assert initial lookup */
76 key->prefixlen = 32;
77 inet_pton(AF_INET, "192.168.0.1", key->data);
78--
792.19.1
80