From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D4D4C221D9F; Thu, 12 Dec 2024 16:28:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734020929; cv=none; b=BmkngmIo6lek55ysZRymFAgWa5Fz7TMa1qm0L34K5xUNCTXGAF2mi4X0mcGcHaRRUqUnfpt2pFE7lopYLqxfJ1ZiQV4e/kJ/7Cz1VTZu6EaxdTnFV9EywRbZgG+tIjyWNi/qhMHbnAftSdRj9yBsLhVhEvpedfM3ndY8eXld2vI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734020929; c=relaxed/simple; bh=DD1G4otlBHHbB7BNJD7v8RgVxPwqCZeAxHfLcL1BrEU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=b/wl+28qO2Zm6wbJpjoQTCLOjnqGHkJGNCAaraeO1Q7KM9CzFCJIX44MGkUDWGMRDsIdZd3NifyRieSPOnG4AhovGtRxkSzklW5InIkGGwM3R7LUpp2hFDRkjgCx5cZuiMZkw6R824I6U9qRKrovihi1p1Ms2Fzt3P3vlPBm/zM= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b=SmiVS618; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b="SmiVS618" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 293A3C4CED0; Thu, 12 Dec 2024 16:28:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1734020929; bh=DD1G4otlBHHbB7BNJD7v8RgVxPwqCZeAxHfLcL1BrEU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=SmiVS618RmcCxjTt3Daz+xrCo6hRVg/DqqyjqisTL9yBQXeBayvxmye3OphuRsyFk /bYo5Js1nC5XXxGGo5pUKM4BVOzlFmtbL6j9G2GslwYmO505t47D/xc2NP6J0e2I5W LvcF/KoXIwl0udBuha+j/BhEhhftzSCaZFErximE= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, =?UTF-8?q?Toke=20H=C3=B8iland-J=C3=B8rgensen?= , Hou Tao , Alexei Starovoitov , Sasha Levin Subject: [PATCH 6.1 601/772] bpf: Fix exact match conditions in trie_get_next_key() Date: Thu, 12 Dec 2024 15:59:06 +0100 Message-ID: <20241212144414.759470057@linuxfoundation.org> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20241212144349.797589255@linuxfoundation.org> References: <20241212144349.797589255@linuxfoundation.org> User-Agent: quilt/0.67 X-stable: review X-Patchwork-Hint: ignore Precedence: bulk X-Mailing-List: stable@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 6.1-stable review patch. If anyone has any objections, please let me know. ------------------ From: Hou Tao [ Upstream commit 27abc7b3fa2e09bbe41e2924d328121546865eda ] 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: b471f2f1de8b ("bpf: implement MAP_GET_NEXT_KEY command for LPM_TRIE map") Reviewed-by: Toke Høiland-Jørgensen Signed-off-by: Hou Tao Link: https://lore.kernel.org/r/20241206110622.1161752-6-houtao@huaweicloud.com Signed-off-by: Alexei Starovoitov Signed-off-by: Sasha Levin --- kernel/bpf/lpm_trie.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/kernel/bpf/lpm_trie.c b/kernel/bpf/lpm_trie.c index c5fa2a74de771..fd6e31e722908 100644 --- a/kernel/bpf/lpm_trie.c +++ b/kernel/bpf/lpm_trie.c @@ -641,7 +641,7 @@ static int trie_get_next_key(struct bpf_map *map, void *_key, void *_next_key) struct lpm_trie_node **node_stack = NULL; int err = 0, stack_ptr = -1; unsigned int next_bit; - size_t matchlen; + size_t matchlen = 0; /* The get_next_key follows postorder. For the 4 node example in * the top of this file, the trie_get_next_key() returns the following @@ -680,7 +680,7 @@ static int trie_get_next_key(struct bpf_map *map, void *_key, void *_next_key) next_bit = extract_bit(key->data, node->prefixlen); node = rcu_dereference(node->child[next_bit]); } - if (!node || node->prefixlen != key->prefixlen || + if (!node || node->prefixlen != matchlen || (node->flags & LPM_TREE_NODE_FLAG_IM)) goto find_leftmost; -- 2.43.0