Commit a2f344a
kpartx: fix crash and truncated device creation with long -p delimiter
When the -p delimiter is long enough to make the formatted partition
name exceed PARTNAME_SIZE (128 bytes), three issues occur:
1. format_partname() fails but snprintf has already written a truncated
name into the buffer. dm_find_part() returns 0 and the caller
proceeds to dm_addmap() with the truncated name, creating a device
that was never intended.
2. dm_find_part() returns early without setting *part_uuid. The
uninitialized local variable part_uuid then gets passed to
check_uuid() -> strchr(), causing a SIGSEGV.
3. The callers cannot distinguish between 'partition not found, create
new' and 'name construction failed' since both return 0.
Fix by:
- Having dm_find_part() return DFP_ERR when format_partname() fails,
with an error message printed by dm_find_part() itself
- Defining an enum to represent the return values of dm_find_part():
enum {
DFP_DEVICE_CREATE = DM_DEVICE_CREATE,
DFP_DEVICE_RELOAD = DM_DEVICE_RELOAD,
DFP_ERR = -1
};
This enum is placed in kpartx/devmapper.h, which now also includes
<libdevmapper.h> directly to make the header self-contained.
dm_find_part() uses these enum values explicitly in all return
statements.
- In the ADD/UPDATE loops (both main and container), assigning the
return value directly to 'op' and checking for DFP_ERR to handle
errors
- In the DELETE loop, checking the return value against
DFP_DEVICE_RELOAD to proceed with removal, skipping on error or
not-found
- Initializing part_uuid to NULL in all three partition loop bodies
(ADD/UPDATE main loop, container partition loop, DELETE loop) so
that the 'if (part_uuid && uuid)' guard correctly skips the UUID
check when dm_find_part() returns early
Reproduce steps (now also in the test-kpartx script):
# Create test image
dd if=/dev/zero of=/tmp/vhlg-test.img bs=1M count=10
parted /tmp/vhlg-test.img mklabel msdos
parted /tmp/vhlg-test.img mkpart primary ext4 1MiB 5MiB
# Reproduce
kpartx -a -p $(python3 -c "print('A'*200)") /tmp/vhlg-test.img
# Cleanup
kpartx -d /tmp/vhlg-test.img
rm -f /tmp/vhlg-test.img
[mwilck: removed version history from commit message]
Signed-off-by: Kou Wenqi <kouwenqi@kylinos.cn>
Reviewed-by: Martin Wilck <mwilck@suse.com>1 parent e8832b5 commit a2f344a
3 files changed
Lines changed: 40 additions & 24 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
644 | 644 | | |
645 | 645 | | |
646 | 646 | | |
647 | | - | |
648 | | - | |
649 | | - | |
| 647 | + | |
| 648 | + | |
650 | 649 | | |
651 | 650 | | |
652 | 651 | | |
653 | | - | |
654 | | - | |
| 652 | + | |
| 653 | + | |
| 654 | + | |
| 655 | + | |
655 | 656 | | |
656 | 657 | | |
657 | 658 | | |
658 | | - | |
| 659 | + | |
659 | 660 | | |
660 | 661 | | |
661 | 662 | | |
| |||
688 | 689 | | |
689 | 690 | | |
690 | 691 | | |
691 | | - | |
| 692 | + | |
692 | 693 | | |
693 | 694 | | |
694 | 695 | | |
695 | 696 | | |
696 | 697 | | |
697 | 698 | | |
698 | | - | |
| 699 | + | |
699 | 700 | | |
700 | 701 | | |
701 | 702 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
| 4 | + | |
| 5 | + | |
4 | 6 | | |
5 | 7 | | |
6 | 8 | | |
| |||
18 | 20 | | |
19 | 21 | | |
20 | 22 | | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
21 | 30 | | |
22 | 31 | | |
23 | 32 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
437 | 437 | | |
438 | 438 | | |
439 | 439 | | |
440 | | - | |
| 440 | + | |
441 | 441 | | |
442 | 442 | | |
443 | 443 | | |
| |||
454 | 454 | | |
455 | 455 | | |
456 | 456 | | |
457 | | - | |
458 | | - | |
459 | | - | |
460 | | - | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
461 | 464 | | |
462 | 465 | | |
463 | 466 | | |
| |||
500 | 503 | | |
501 | 504 | | |
502 | 505 | | |
503 | | - | |
| 506 | + | |
504 | 507 | | |
505 | 508 | | |
506 | 509 | | |
| |||
526 | 529 | | |
527 | 530 | | |
528 | 531 | | |
529 | | - | |
530 | | - | |
531 | | - | |
532 | | - | |
533 | | - | |
| 532 | + | |
| 533 | + | |
| 534 | + | |
| 535 | + | |
| 536 | + | |
| 537 | + | |
| 538 | + | |
| 539 | + | |
534 | 540 | | |
535 | 541 | | |
536 | 542 | | |
| |||
570 | 576 | | |
571 | 577 | | |
572 | 578 | | |
573 | | - | |
574 | | - | |
575 | | - | |
576 | | - | |
577 | | - | |
| 579 | + | |
| 580 | + | |
| 581 | + | |
| 582 | + | |
| 583 | + | |
578 | 584 | | |
579 | 585 | | |
580 | 586 | | |
| |||
0 commit comments