Skip to content

Commit e7bf90e

Browse files
wenwenwang1axboe
authored andcommitted
block/bio-integrity: fix a memory leak bug
In bio_integrity_prep(), a kernel buffer is allocated through kmalloc() to hold integrity metadata. Later on, the buffer will be attached to the bio structure through bio_integrity_add_page(), which returns the number of bytes of integrity metadata attached. Due to unexpected situations, bio_integrity_add_page() may return 0. As a result, bio_integrity_prep() needs to be terminated with 'false' returned to indicate this error. However, the allocated kernel buffer is not freed on this execution path, leading to a memory leak. To fix this issue, free the allocated buffer before returning from bio_integrity_prep(). Reviewed-by: Ming Lei <[email protected]> Acked-by: Martin K. Petersen <[email protected]> Signed-off-by: Wenwen Wang <[email protected]> Signed-off-by: Jens Axboe <[email protected]>
1 parent 7d30c81 commit e7bf90e

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

block/bio-integrity.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,8 +276,12 @@ bool bio_integrity_prep(struct bio *bio)
276276
ret = bio_integrity_add_page(bio, virt_to_page(buf),
277277
bytes, offset);
278278

279-
if (ret == 0)
280-
return false;
279+
if (ret == 0) {
280+
printk(KERN_ERR "could not attach integrity payload\n");
281+
kfree(buf);
282+
status = BLK_STS_RESOURCE;
283+
goto err_end_io;
284+
}
281285

282286
if (ret < bytes)
283287
break;

0 commit comments

Comments
 (0)