Skip to contentSkip to conversation

dm-region-hash: fix a missing-check bug in __rh_alloc()

PATCH In review Gen Zhang 2 participants 3 messages May 27, 2019 Device mapper

lore
Share & tools
lore

Gen Zhang

author
#0 lore raw

PATCH 1/1 dm-region-hash: fix a missing-check bug in __rh_alloc()

In function __rh_alloc(), the pointer nreg is allocated a memory space via kmalloc(). And it is used in the following codes. However, when there is a memory allocation error, kmalloc() fails. Thus null pointer dereference may happen. And it will cause the kernel to crash. Therefore, we should check the return value and handle the error. Further, in __rh_find(), we should also check the return value and handle the error.

---

drivers/md/dm-region-hash.c +6 -1
index 1f76045..2fa1641 100644
@@ -290,8 +290,11 @@ static struct dm_region *__rh_alloc(struct dm_region_hash *rh, region_t region)
 	struct dm_region *reg, *nreg;
 
 	nreg = mempool_alloc(&rh->region_pool, GFP_ATOMIC);
-	if (unlikely(!nreg))
+	if (unlikely(!nreg)) {
 		nreg = kmalloc(sizeof(*nreg), GFP_NOIO | __GFP_NOFAIL);
+		if (!nreg)
+			return NULL;
+	}
 
 	nreg->state = rh->log->type->in_sync(rh->log, region, 1) ?
 		      DM_RH_CLEAN : DM_RH_NOSYNC;
@@ -329,6 +332,8 @@ static struct dm_region *__rh_find(struct dm_region_hash *rh, region_t region)
 	if (!reg) {
 		read_unlock(&rh->hash_lock);
 		reg = __rh_alloc(rh, region);
+		if (!reg)
+			return NULL;
 		read_lock(&rh->hash_lock);
 	}
 

---

#1 lore raw
Quoted 17 lines · Gen Zhang

In function __rh_alloc(), the pointer nreg is allocated a memory space via kmalloc(). And it is used in the following codes. However, when there is a memory allocation error, kmalloc() fails. Thus null pointer dereference may happen. And it will cause the kernel to crash. Therefore, we should check the return value and handle the error. Further, in __rh_find(), we should also check the return value and handle the error.

---

drivers/md/dm-region-hash.c +0 -0
index 1f76045..2fa1641 100644
@@ -290,8 +290,11 @@ static struct dm_region *__rh_alloc(struct dm_region_hash *rh, region_t region)
 	struct dm_region *reg, *nreg;
 

drivers/md/dm-region-hash.c static struct dm_region *__rh_alloc(struct dm_region_hash *rh, region_t region) · line 300

Quoted 10 lines
drivers/md/dm-region-hash.c +4 -1
 	nreg = mempool_alloc(&rh->region_pool, GFP_ATOMIC);
-	if (unlikely(!nreg))
+	if (unlikely(!nreg)) {
 		nreg = kmalloc(sizeof(*nreg), GFP_NOIO | __GFP_NOFAIL);
+		if (!nreg)
+			return NULL;
+	}
 
 	nreg->state = rh->log->type->in_sync(rh->log, region, 1) ?
 		      DM_RH_CLEAN : DM_RH_NOSYNC;

This patch isn't needed. __GFP_NOFAIL means the allocation won't fail.

And there are many other users of __GFP_NOFAIL that don't check for failure.

Mike

author
#2 lore raw
Quoted 33 lines · Mike Snitzer
Quoted 17 lines · Gen Zhang

In function __rh_alloc(), the pointer nreg is allocated a memory space via kmalloc(). And it is used in the following codes. However, when there is a memory allocation error, kmalloc() fails. Thus null pointer dereference may happen. And it will cause the kernel to crash. Therefore, we should check the return value and handle the error. Further, in __rh_find(), we should also check the return value and handle the error.

---

drivers/md/dm-region-hash.c +0 -0
index 1f76045..2fa1641 100644
@@ -290,8 +290,11 @@ static struct dm_region *__rh_alloc(struct dm_region_hash *rh, region_t region)
 	struct dm_region *reg, *nreg;
 

drivers/md/dm-region-hash.c static struct dm_region *__rh_alloc(struct dm_region_hash *rh, region_t region) · line 300

Quoted 10 lines
drivers/md/dm-region-hash.c +4 -1
 	nreg = mempool_alloc(&rh->region_pool, GFP_ATOMIC);
-	if (unlikely(!nreg))
+	if (unlikely(!nreg)) {
 		nreg = kmalloc(sizeof(*nreg), GFP_NOIO | __GFP_NOFAIL);
+		if (!nreg)
+			return NULL;
+	}
 
 	nreg->state = rh->log->type->in_sync(rh->log, region, 1) ?
 		      DM_RH_CLEAN : DM_RH_NOSYNC;

This patch isn't needed. __GFP_NOFAIL means the allocation won't fail.

Quoted 4 lines

And there are many other users of __GFP_NOFAIL that don't check for failure.

Mike

Appreciate your reply, Mike.

Thanks
Gen