linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
To: linux-kernel@vger.kernel.org
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	stable@vger.kernel.org, Jonathan Lemon <jonathan.lemon@gmail.com>,
	Martin KaFai Lau <kafai@fb.com>,
	Daniel Borkmann <daniel@iogearbox.net>
Subject: [PATCH 4.19 63/72] bpf: lpm_trie: check left child of last leftmost node for NULL
Date: Tue,  2 Jul 2019 10:02:04 +0200	[thread overview]
Message-ID: <20190702080127.900689440@linuxfoundation.org> (raw)
In-Reply-To: <20190702080124.564652899@linuxfoundation.org>

From: Jonathan Lemon <jonathan.lemon@gmail.com>

commit da2577fdd0932ea4eefe73903f1130ee366767d2 upstream.

If the leftmost parent node of the tree has does not have a child
on the left side, then trie_get_next_key (and bpftool map dump) will
not look at the child on the right.  This leads to the traversal
missing elements.

Lookup is not affected.

Update selftest to handle this case.

Reproducer:

 bpftool map create /sys/fs/bpf/lpm type lpm_trie key 6 \
     value 1 entries 256 name test_lpm flags 1
 bpftool map update pinned /sys/fs/bpf/lpm key  8 0 0 0  0   0 value 1
 bpftool map update pinned /sys/fs/bpf/lpm key 16 0 0 0  0 128 value 2
 bpftool map dump   pinned /sys/fs/bpf/lpm

Returns only 1 element. (2 expected)

Fixes: b471f2f1de8b ("bpf: implement MAP_GET_NEXT_KEY command for LPM_TRIE")
Signed-off-by: Jonathan Lemon <jonathan.lemon@gmail.com>
Acked-by: Martin KaFai Lau <kafai@fb.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

---
 kernel/bpf/lpm_trie.c                      |    9 ++++--
 tools/testing/selftests/bpf/test_lpm_map.c |   41 ++++++++++++++++++++++++++---
 2 files changed, 45 insertions(+), 5 deletions(-)

--- a/kernel/bpf/lpm_trie.c
+++ b/kernel/bpf/lpm_trie.c
@@ -676,9 +676,14 @@ find_leftmost:
 	 * have exact two children, so this function will never return NULL.
 	 */
 	for (node = search_root; node;) {
-		if (!(node->flags & LPM_TREE_NODE_FLAG_IM))
+		if (node->flags & LPM_TREE_NODE_FLAG_IM) {
+			node = rcu_dereference(node->child[0]);
+		} else {
 			next_node = node;
-		node = rcu_dereference(node->child[0]);
+			node = rcu_dereference(node->child[0]);
+			if (!node)
+				node = rcu_dereference(next_node->child[1]);
+		}
 	}
 do_copy:
 	next_key->prefixlen = next_node->prefixlen;
--- a/tools/testing/selftests/bpf/test_lpm_map.c
+++ b/tools/testing/selftests/bpf/test_lpm_map.c
@@ -573,13 +573,13 @@ static void test_lpm_get_next_key(void)
 
 	/* add one more element (total two) */
 	key_p->prefixlen = 24;
-	inet_pton(AF_INET, "192.168.0.0", key_p->data);
+	inet_pton(AF_INET, "192.168.128.0", key_p->data);
 	assert(bpf_map_update_elem(map_fd, key_p, &value, 0) == 0);
 
 	memset(key_p, 0, key_size);
 	assert(bpf_map_get_next_key(map_fd, NULL, key_p) == 0);
 	assert(key_p->prefixlen == 24 && key_p->data[0] == 192 &&
-	       key_p->data[1] == 168 && key_p->data[2] == 0);
+	       key_p->data[1] == 168 && key_p->data[2] == 128);
 
 	memset(next_key_p, 0, key_size);
 	assert(bpf_map_get_next_key(map_fd, key_p, next_key_p) == 0);
@@ -592,7 +592,7 @@ static void test_lpm_get_next_key(void)
 
 	/* Add one more element (total three) */
 	key_p->prefixlen = 24;
-	inet_pton(AF_INET, "192.168.128.0", key_p->data);
+	inet_pton(AF_INET, "192.168.0.0", key_p->data);
 	assert(bpf_map_update_elem(map_fd, key_p, &value, 0) == 0);
 
 	memset(key_p, 0, key_size);
@@ -628,6 +628,41 @@ static void test_lpm_get_next_key(void)
 	assert(bpf_map_get_next_key(map_fd, key_p, next_key_p) == 0);
 	assert(next_key_p->prefixlen == 24 && next_key_p->data[0] == 192 &&
 	       next_key_p->data[1] == 168 && next_key_p->data[2] == 1);
+
+	memcpy(key_p, next_key_p, key_size);
+	assert(bpf_map_get_next_key(map_fd, key_p, next_key_p) == 0);
+	assert(next_key_p->prefixlen == 24 && next_key_p->data[0] == 192 &&
+	       next_key_p->data[1] == 168 && next_key_p->data[2] == 128);
+
+	memcpy(key_p, next_key_p, key_size);
+	assert(bpf_map_get_next_key(map_fd, key_p, next_key_p) == 0);
+	assert(next_key_p->prefixlen == 16 && next_key_p->data[0] == 192 &&
+	       next_key_p->data[1] == 168);
+
+	memcpy(key_p, next_key_p, key_size);
+	assert(bpf_map_get_next_key(map_fd, key_p, next_key_p) == -1 &&
+	       errno == ENOENT);
+
+	/* Add one more element (total five) */
+	key_p->prefixlen = 28;
+	inet_pton(AF_INET, "192.168.1.128", key_p->data);
+	assert(bpf_map_update_elem(map_fd, key_p, &value, 0) == 0);
+
+	memset(key_p, 0, key_size);
+	assert(bpf_map_get_next_key(map_fd, NULL, key_p) == 0);
+	assert(key_p->prefixlen == 24 && key_p->data[0] == 192 &&
+	       key_p->data[1] == 168 && key_p->data[2] == 0);
+
+	memset(next_key_p, 0, key_size);
+	assert(bpf_map_get_next_key(map_fd, key_p, next_key_p) == 0);
+	assert(next_key_p->prefixlen == 28 && next_key_p->data[0] == 192 &&
+	       next_key_p->data[1] == 168 && next_key_p->data[2] == 1 &&
+	       next_key_p->data[3] == 128);
+
+	memcpy(key_p, next_key_p, key_size);
+	assert(bpf_map_get_next_key(map_fd, key_p, next_key_p) == 0);
+	assert(next_key_p->prefixlen == 24 && next_key_p->data[0] == 192 &&
+	       next_key_p->data[1] == 168 && next_key_p->data[2] == 1);
 
 	memcpy(key_p, next_key_p, key_size);
 	assert(bpf_map_get_next_key(map_fd, key_p, next_key_p) == 0);



  parent reply	other threads:[~2019-07-02  8:08 UTC|newest]

Thread overview: 84+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-07-02  8:01 [PATCH 4.19 00/72] 4.19.57-stable review Greg Kroah-Hartman
2019-07-02  8:01 ` [PATCH 4.19 01/72] perf ui helpline: Use strlcpy() as a shorter form of strncpy() + explicit set nul Greg Kroah-Hartman
2019-07-02  8:01 ` [PATCH 4.19 02/72] perf help: Remove needless use of strncpy() Greg Kroah-Hartman
2019-07-02  8:01 ` [PATCH 4.19 03/72] perf header: Fix unchecked usage " Greg Kroah-Hartman
2019-07-02  8:01 ` [PATCH 4.19 04/72] arm64: Dont unconditionally add -Wno-psabi to KBUILD_CFLAGS Greg Kroah-Hartman
2019-07-02  8:01 ` [PATCH 4.19 05/72] Revert "x86/uaccess, ftrace: Fix ftrace_likely_update() vs. SMAP" Greg Kroah-Hartman
2019-07-02  8:01 ` [PATCH 4.19 06/72] IB/hfi1: Close PSM sdma_progress sleep window Greg Kroah-Hartman
2019-07-02  8:01 ` [PATCH 4.19 07/72] 9p/xen: fix check for xenbus_read error in front_probe Greg Kroah-Hartman
2019-07-02  8:01 ` [PATCH 4.19 08/72] 9p: Use a slab for allocating requests Greg Kroah-Hartman
2019-07-02  8:01 ` [PATCH 4.19 09/72] 9p: embed fcall in req to round down buffer allocs Greg Kroah-Hartman
2019-07-02  8:01 ` [PATCH 4.19 10/72] 9p: add a per-client fcall kmem_cache Greg Kroah-Hartman
2019-07-02  8:01 ` [PATCH 4.19 11/72] 9p: rename p9_free_req() function Greg Kroah-Hartman
2019-07-02  8:01 ` [PATCH 4.19 12/72] 9p: Add refcount to p9_req_t Greg Kroah-Hartman
2019-07-02  8:01 ` [PATCH 4.19 13/72] 9p/rdma: do not disconnect on down_interruptible EAGAIN Greg Kroah-Hartman
2019-07-02  8:01 ` [PATCH 4.19 14/72] 9p: Rename req to rreq in trans_fd Greg Kroah-Hartman
2019-07-02  8:01 ` [PATCH 4.19 15/72] 9p: acl: fix uninitialized iattr access Greg Kroah-Hartman
2019-07-02  8:01 ` [PATCH 4.19 16/72] 9p/rdma: remove useless check in cm_event_handler Greg Kroah-Hartman
2019-07-02  8:01 ` [PATCH 4.19 17/72] 9p: p9dirent_read: check network-provided name length Greg Kroah-Hartman
2019-07-02  8:01 ` [PATCH 4.19 18/72] 9p: potential NULL dereference Greg Kroah-Hartman
2019-07-02  8:01 ` [PATCH 4.19 19/72] 9p/trans_fd: abort p9_read_work if req status changed Greg Kroah-Hartman
2019-07-02  8:01 ` [PATCH 4.19 20/72] 9p/trans_fd: put worker reqs on destroy Greg Kroah-Hartman
2019-07-02  8:01 ` [PATCH 4.19 21/72] net/9p: include trans_common.h to fix missing prototype warning Greg Kroah-Hartman
2019-07-02  8:01 ` [PATCH 4.19 22/72] qmi_wwan: Fix out-of-bounds read Greg Kroah-Hartman
2019-07-02  8:01 ` [PATCH 4.19 23/72] Revert "usb: dwc3: gadget: Clear req->needs_extra_trb flag on cleanup" Greg Kroah-Hartman
2019-07-02  8:01 ` [PATCH 4.19 24/72] usb: dwc3: gadget: combine unaligned and zero flags Greg Kroah-Hartman
2019-07-02  8:01 ` [PATCH 4.19 25/72] usb: dwc3: gadget: track number of TRBs per request Greg Kroah-Hartman
2019-07-02  8:01 ` [PATCH 4.19 26/72] usb: dwc3: gadget: use num_trbs when skipping TRBs on ->dequeue() Greg Kroah-Hartman
2019-07-03  2:03   ` Sasha Levin
2019-07-03  7:20     ` Greg Kroah-Hartman
2019-07-03 19:59       ` Sasha Levin
2019-07-02  8:01 ` [PATCH 4.19 27/72] usb: dwc3: gadget: extract dwc3_gadget_ep_skip_trbs() Greg Kroah-Hartman
2019-07-02  8:01 ` [PATCH 4.19 28/72] usb: dwc3: gadget: introduce cancelled_list Greg Kroah-Hartman
2019-07-02  8:01 ` [PATCH 4.19 29/72] usb: dwc3: gadget: move requests to cancelled_list Greg Kroah-Hartman
2019-07-02  8:01 ` [PATCH 4.19 30/72] usb: dwc3: gadget: remove wait_end_transfer Greg Kroah-Hartman
2019-07-02  8:01 ` [PATCH 4.19 31/72] usb: dwc3: gadget: Clear req->needs_extra_trb flag on cleanup Greg Kroah-Hartman
2019-07-02  8:01 ` [PATCH 4.19 32/72] fs/proc/array.c: allow reporting eip/esp for all coredumping threads Greg Kroah-Hartman
2019-07-02  8:01 ` [PATCH 4.19 33/72] mm/mempolicy.c: fix an incorrect rebind node in mpol_rebind_nodemask Greg Kroah-Hartman
2019-07-02  8:01 ` [PATCH 4.19 34/72] fs/binfmt_flat.c: make load_flat_shared_library() work Greg Kroah-Hartman
2019-07-02  8:01 ` [PATCH 4.19 35/72] clk: socfpga: stratix10: fix divider entry for the emac clocks Greg Kroah-Hartman
2019-07-02  8:01 ` [PATCH 4.19 36/72] mm: soft-offline: return -EBUSY if set_hwpoison_free_buddy_page() fails Greg Kroah-Hartman
2019-07-02  8:01 ` [PATCH 4.19 37/72] mm: hugetlb: soft-offline: dissolve_free_huge_page() return zero on !PageHuge Greg Kroah-Hartman
2019-07-02  8:01 ` [PATCH 4.19 38/72] mm/page_idle.c: fix oops because end_pfn is larger than max_pfn Greg Kroah-Hartman
2019-07-02  8:01 ` [PATCH 4.19 39/72] dm log writes: make sure super sector log updates are written in order Greg Kroah-Hartman
2019-07-02  8:01 ` [PATCH 4.19 40/72] scsi: vmw_pscsi: Fix use-after-free in pvscsi_queue_lck() Greg Kroah-Hartman
2019-07-02  8:01 ` [PATCH 4.19 41/72] x86/speculation: Allow guests to use SSBD even if host does not Greg Kroah-Hartman
2019-07-02  8:01 ` [PATCH 4.19 42/72] x86/microcode: Fix the microcode load on CPU hotplug for real Greg Kroah-Hartman
2019-07-02  8:01 ` [PATCH 4.19 43/72] x86/resctrl: Prevent possible overrun during bitmap operations Greg Kroah-Hartman
2019-07-02  8:01 ` [PATCH 4.19 44/72] KVM: x86/mmu: Allocate PAE root array when using SVMs 32-bit NPT Greg Kroah-Hartman
2019-07-02  8:01 ` [PATCH 4.19 45/72] NFS/flexfiles: Use the correct TCP timeout for flexfiles I/O Greg Kroah-Hartman
2019-07-02  8:01 ` [PATCH 4.19 46/72] cpu/speculation: Warn on unsupported mitigations= parameter Greg Kroah-Hartman
2019-07-02  8:01 ` [PATCH 4.19 47/72] SUNRPC: Clean up initialisation of the struct rpc_rqst Greg Kroah-Hartman
2019-07-02  8:01 ` [PATCH 4.19 48/72] irqchip/mips-gic: Use the correct local interrupt map registers Greg Kroah-Hartman
2019-07-02  8:01 ` [PATCH 4.19 49/72] eeprom: at24: fix unexpected timeout under high load Greg Kroah-Hartman
2019-07-02  8:01 ` [PATCH 4.19 50/72] af_packet: Block execution of tasks waiting for transmit to complete in AF_PACKET Greg Kroah-Hartman
2019-07-02  8:01 ` [PATCH 4.19 51/72] bonding: Always enable vlan tx offload Greg Kroah-Hartman
2019-07-02  8:01 ` [PATCH 4.19 52/72] ipv4: Use return value of inet_iif() for __raw_v4_lookup in the while loop Greg Kroah-Hartman
2019-07-02  8:01 ` [PATCH 4.19 53/72] net/packet: fix memory leak in packet_set_ring() Greg Kroah-Hartman
2019-07-02  8:01 ` [PATCH 4.19 54/72] net: remove duplicate fetch in sock_getsockopt Greg Kroah-Hartman
2019-07-02  8:01 ` [PATCH 4.19 55/72] net: stmmac: fixed new system time seconds value calculation Greg Kroah-Hartman
2019-07-02  8:01 ` [PATCH 4.19 56/72] net: stmmac: set IC bit when transmitting frames with HW timestamp Greg Kroah-Hartman
2019-07-02  8:01 ` [PATCH 4.19 57/72] sctp: change to hold sk after auth shkey is created successfully Greg Kroah-Hartman
2019-07-02  8:01 ` [PATCH 4.19 58/72] team: Always enable vlan tx offload Greg Kroah-Hartman
2019-07-02  8:02 ` [PATCH 4.19 59/72] tipc: change to use register_pernet_device Greg Kroah-Hartman
2019-07-02  8:02 ` [PATCH 4.19 60/72] tipc: check msg->req data len in tipc_nl_compat_bearer_disable Greg Kroah-Hartman
2019-07-02  8:02 ` [PATCH 4.19 61/72] tun: wake up waitqueues after IFF_UP is set Greg Kroah-Hartman
2019-07-02  8:02 ` [PATCH 4.19 62/72] bpf: simplify definition of BPF_FIB_LOOKUP related flags Greg Kroah-Hartman
2019-07-02  8:02 ` Greg Kroah-Hartman [this message]
2019-07-02  8:02 ` [PATCH 4.19 64/72] bpf: fix nested bpf tracepoints with per-cpu data Greg Kroah-Hartman
2019-07-02  8:02 ` [PATCH 4.19 65/72] bpf: fix unconnected udp hooks Greg Kroah-Hartman
2019-07-02  8:02 ` [PATCH 4.19 66/72] bpf: udp: Avoid calling reuseports bpf_prog from udp_gro Greg Kroah-Hartman
2019-07-02  8:02 ` [PATCH 4.19 67/72] bpf: udp: ipv6: Avoid running reuseports bpf_prog from __udp6_lib_err Greg Kroah-Hartman
2019-07-02  8:02 ` [PATCH 4.19 68/72] arm64: futex: Avoid copying out uninitialised stack in failed cmpxchg() Greg Kroah-Hartman
2019-07-02  8:02 ` [PATCH 4.19 69/72] bpf, arm64: use more scalable stadd over ldxr / stxr loop in xadd Greg Kroah-Hartman
2019-07-02  8:02 ` [PATCH 4.19 70/72] futex: Update comments and docs about return values of arch futex code Greg Kroah-Hartman
2019-07-02  8:02 ` [PATCH 4.19 71/72] RDMA: Directly cast the sockaddr union to sockaddr Greg Kroah-Hartman
2019-07-02  8:02 ` [PATCH 4.19 72/72] tipc: pass tunnel dev as NULL to udp_tunnel(6)_xmit_skb Greg Kroah-Hartman
2019-07-02 12:32 ` [PATCH 4.19 00/72] 4.19.57-stable review kernelci.org bot
2019-07-02 16:54 ` Naresh Kamboju
2019-07-02 20:23 ` Guenter Roeck
2019-07-03 14:46   ` Greg Kroah-Hartman
2019-07-02 21:08 ` Kelsey Skunberg
2019-07-02 22:52 ` shuah
2019-07-03 10:21 ` Jon Hunter
2019-07-04  5:29 ` Bharath Vedartham

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190702080127.900689440@linuxfoundation.org \
    --to=gregkh@linuxfoundation.org \
    --cc=daniel@iogearbox.net \
    --cc=jonathan.lemon@gmail.com \
    --cc=kafai@fb.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=stable@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).