Skip to contentSkip to conversation

kpartx: check slice size before looking for partition to remove

PATCH In review Benjamin Marzinski 3 participants 3 messages Jul 21 Device mapper

lore
Share & tools
lore

Benjamin Marzinski

author maintainer
#0 lore raw

PATCH 1/1 kpartx: check slice size before looking for partition to remove

After commit 03071031 ("kpartx: fix crash and truncated device creation with long -p delimiter"), when attempting to remove deleted partitions on kpartx -u, kpartx started calling dm_find_part() to check if the partition existed before checking if the slice size was 0 (and thus should get removed). The problem is that if dm_find_part() returns DFP_DEVICE_RELOAD because the partition device exists, part_uuid gets allocated, and if the slices[j].size is non-zero and the partition device shouldn't be removed, part_uuid wasn't getting freed. Fix this by reverting to checking the slice size first.

Fixes 03071031 ("kpartx: fix crash and truncated device creation

with long -p delimiter")

---
 kpartx/kpartx.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
kpartx/kpartx.c +5 -4
index fc8f9e5e..370d09a1 100644
@@ -577,10 +577,11 @@ main(int argc, char **argv){
 
 			for (j = MAXSLICES-1; j >= 0; j--) {
 				char *part_uuid = NULL, *reason;
-				int res = dm_find_part(mapname, delim, j + 1, uuid,
-						       partname, sizeof(partname),
-						       &part_uuid, verbose);
-				if (slices[j].size || res != DFP_DEVICE_RELOAD)
+
+				if (slices[j].size ||
+				    dm_find_part(mapname, delim, j + 1, uuid, partname,
+						 sizeof(partname), &part_uuid,
+						 verbose) != DFP_DEVICE_RELOAD)
 					continue;
 
 				if (part_uuid && uuid) {
Signature · 1 line
2.53.0
Reviewed-by Kou Wenqi on v1 1/1
#1 lore raw
Quoted 5 lines · Benjamin Marzinski

After commit 03071031 ("kpartx: fix crash and truncated device creation with long -p delimiter"), when attempting to remove deleted partitions on kpartx -u, kpartx started calling dm_find_part() to check if the partition existed before checking if the slice size was 0 (and thus should get removed).

You're right. My patch changed the order of checks in the DELETE loop, causing dm_find_part() to be called even when slices[j].size is non-zero (meaning the partition is still valid and should not be removed).

Quoted 5 lines

The problem is that if dm_find_part() returns DFP_DEVICE_RELOAD because the partition device exists, part_uuid gets allocated, and if the slices[j].size is non-zero and the partition device shouldn't be removed, part_uuid wasn't getting freed. Fix this by reverting to checking the slice size first.

I see the memory leak now. When slices[j].size != 0 and dm_find_part() returns DFP_DEVICE_RELOAD, we hit the "continue" without freeing part_uuid.

Reverting to checking slice size first is the correct fix - it restores the original optimization of only calling dm_find_part() for partitions that actually need to be removed.

Thank you for catching this!

Reviewed-by on v1 1/1
#2 lore raw
In reply to Benjamin Marzinski
Quoted 13 lines · Benjamin Marzinski

After commit 03071031 ("kpartx: fix crash and truncated device creation
with long -p delimiter"), when attempting to remove deleted partitions
on kpartx -u, kpartx started calling dm_find_part() to check if the partition existed before checking if the slice size was 0 (and thus should get removed). The problem is that if dm_find_part() returns DFP_DEVICE_RELOAD because the partition device exists, part_uuid gets allocated, and if the slices[j].size is non-zero and the partition device shouldn't be removed, part_uuid wasn't getting freed. Fix this by
reverting to checking the slice size first.

Quoted 4 lines
Fixes 03071031 ("kpartx: fix crash and truncated device creation

with long -p delimiter")

Good catch, thanks!