Skip to contentSkip to conversation

dm btree: Fix potential read of array with negative index i

PATCH In review Colin King 2 participants 2 messages May 21, 2021 – May 26, 2021

Subsystems (2)
lore
Share & tools
lore

Colin King

author
#0 lore raw

PATCH 1/1 dm btree: Fix potential read of array with negative index i

From: Colin Ian King <[email protected]>

The call to lower_bound can return -1 if the key is not found with the bsearch, leading to a negative index access into array node->keys[]. Ensure this cannot occur by checking for a negative index before reading from the array.

Addresses-Coverity: ("Negative array index read")

Fixes d69e2e7e28bd ("dm btree: improve btree residency")
---
 drivers/md/persistent-data/dm-btree.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/md/persistent-data/dm-btree.c +1 -1
index b8d21b6e2953..266deaea5eea 100644
@@ -1048,7 +1048,7 @@ static bool contains_key(struct btree_node *node, uint64_t key)
 {
 	int i = lower_bound(node, key);
 
-	if (le64_to_cpu(node->keys[i]) == key)
+	if (i >= 0 && le64_to_cpu(node->keys[i]) == key)
 		return true;
 
 	return false;
Signature · 1 line
2.31.1
#1 lore raw

ack

Quoted 29 lines · Colin King

From: Colin Ian King <[email protected]>

The call to lower_bound can return -1 if the key is not found with the bsearch, leading to a negative index access into array node->keys[]. Ensure this cannot occur by checking for a negative index before reading from the array.

Addresses-Coverity: ("Negative array index read")

Fixes d69e2e7e28bd ("dm btree: improve btree residency")
---
 drivers/md/persistent-data/dm-btree.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/md/persistent-data/dm-btree.c +1 -1
index b8d21b6e2953..266deaea5eea 100644
@@ -1048,7 +1048,7 @@ static bool contains_key(struct btree_node *node,
 uint64_t key)
 {
        int i = lower_bound(node, key);
 
-       if (le64_to_cpu(node->keys[i]) == key)
+       if (i >= 0 && le64_to_cpu(node->keys[i]) == key)
                return true;
 
        return false;
Quoted 4 lines

--
2.31.1

--
dm-devel mailing list
[email protected]
https://listman.redhat.com/mailman/listinfo/dm-devel