Skip to contentSkip to conversation

lib/lru_cache: Fixed array overflow caused by incorrect boundary handling.

PATCH In review John Sanpe 2 participants 2 messages Jul 23, 2022 – Aug 1, 2022

Subsystems (2)
lore
Share & tools
lore

John Sanpe

author
#0 lore raw

PATCH 1/1 lib/lru_cache: Fixed array overflow caused by incorrect boundary handling.

This problem occurs when malloc element failed on the first time. At this time, the counter i is 0. When it's released, we subtract 1 in advance without checking, which will cause i to become UINT_MAX, resulting in array overflow.

---
 lib/lru_cache.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/lru_cache.c +1 -1
index 52313acbfa62..04d95de92602 100644
@@ -147,7 +147,7 @@ struct lru_cache *lc_create(const char *name, struct kmem_cache *cache,
 		return lc;
 
 	/* else: could not allocate all elements, give up */
-	for (i--; i; i--) {
+	while (i--) {
 		void *p = element[i];
 		kmem_cache_free(cache, p - e_off);
 	}
Signature · 1 line
2.36.1
maintainer Reviewed-by on v1 1/1
#1 lore raw

Am 23.07.22 um 09:59 schrieb John Sanpe:

Quoted 15 lines

This problem occurs when malloc element failed on the first time. At this time, the counter i is 0. When it's released, we subtract 1 in advance without checking, which will cause i to become UINT_MAX, resulting in array overflow.

---
 lib/lru_cache.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/lru_cache.c +0 -0
index 52313acbfa62..04d95de92602 100644

lib/lru_cache.c struct lru_cache *lc_create(const char *name, struct kmem_cache *cache, · line 153

Quoted 8 lines
lib/lru_cache.c +1 -1
@@ -147,7 +147,7 @@ struct lru_cache *lc_create(const char *name, struct kmem_cache *cache,
 		return lc;
 
 	/* else: could not allocate all elements, give up */
-	for (i--; i; i--) {
+	while (i--) {
 		void *p = element[i];
 		kmem_cache_free(cache, p - e_off);
 	}

Thanks for the fix, looks good to me.

Signature · 3 lines
Christoph Böhmwalder
LINBIT | Keeping the Digital World Running
DRBD HA —  Disaster Recovery — Software defined Storage