Skip to contentSkip to conversation

dm crypt: fix memory leak in dm_crypt_integrity_io_alloc() error path

PATCH In review [email protected] 2 participants 2 messages Feb 16, 2019 – Feb 22, 2019 Device mapper

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 dm crypt: fix memory leak in dm_crypt_integrity_io_alloc() error path

From: Sultan Alsawaf <[email protected]>

dm_crypt_integrity_io_alloc() allocates space for an integrity payload but doesn't free it in the error path, leaking memory. Add a bio_integrity_free() invocation upon error to fix the memory leak.

---
 drivers/md/dm-crypt.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/md/dm-crypt.c +3 -1
index dd538e6b2..f731e1fe0 100644
@@ -939,8 +939,10 @@ static int dm_crypt_integrity_io_alloc(struct dm_crypt_io *io, struct bio *bio)
 
 	ret = bio_integrity_add_page(bio, virt_to_page(io->integrity_metadata),
 				     tag_len, offset_in_page(io->integrity_metadata));
-	if (unlikely(ret != tag_len))
+	if (unlikely(ret != tag_len)) {
+		bio_integrity_free(bio);
 		return -ENOMEM;
+	}
 
 	return 0;
 }
Signature · 1 line
2.20.1
#1 lore raw
Quoted 17 lines · On Sat, Feb 16 2019 at 4:00pm -0500, [email protected]…

From: Sultan Alsawaf <[email protected]>

dm_crypt_integrity_io_alloc() allocates space for an integrity payload but doesn't free it in the error path, leaking memory. Add a bio_integrity_free() invocation upon error to fix the memory leak.

---
 drivers/md/dm-crypt.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/md/dm-crypt.c +0 -0
index dd538e6b2..f731e1fe0 100644
@@ -939,8 +939,10 @@ static int dm_crypt_integrity_io_alloc(struct dm_crypt_io *io, struct bio *bio)
 

drivers/md/dm-crypt.c static int dm_crypt_integrity_io_alloc(struct dm_crypt_io *io, struct bio *bio) · line 948

Quoted 10 lines
drivers/md/dm-crypt.c +3 -1
 	ret = bio_integrity_add_page(bio, virt_to_page(io->integrity_metadata),
 				     tag_len, offset_in_page(io->integrity_metadata));
-	if (unlikely(ret != tag_len))
+	if (unlikely(ret != tag_len)) {
+		bio_integrity_free(bio);
 		return -ENOMEM;
+	}
 
 	return 0;
 }

Since commit 7c20f11680a4 bio_integrity_free() is no longer and exported symbol.

But that aside, this dm-crypt clone bio's endio will clean up the bip once -ENOMEM return starts to make its way out via crypt_alloc_buffer()'s bio_put().

Mike