summaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
author Michael Karcher <github@mkarcher.dialup.fu-berlin.de>2025-09-09 01:35:20 +0200
committer GitHub <noreply@github.com>2025-09-08 19:35:20 -0400
commit7d42c6c3cc0c6926f6b69c55176705521561e0ee (patch)
tree4390894f66a4e810f8c5490738d531ac2af14c5e
parentb6adee8879ad3c8527c64ac854a104fd3670390c (diff)
Basic fix for pc_chd support in imgtool (#14148)
* imgtool: Take ownership of stream in pc_chd_image_open `imgtool::image::internal_open` passes an rvalue reference to the stream to the `open` function of the image format module. It expects the `open` function to take ownership if it keeps a reference to the stream. If `open` does not do so, the `stream` is going to be destroyed at the end of `internal_open`. `pc_chd_image_open` fails to take ownership, yet it persists a reference to the stream as part of `info->hard_disk`. This causes an use-after-free condition * imgtool: Correct determination of total sectors of a FAT volume The number of total sectors of a FAT volume is stored either in the 16-bit word at offset 19 or, if that word is zero, in the 32-bit word at offset 32 instead. The 32-bit word is not a high word to build a 48-bit value in conjunction with the 16-bit word at offset 19, but it supersedes it.
-rw-r--r--src/tools/imgtool/modules/fat.cpp2
-rw-r--r--src/tools/imgtool/modules/pc_hard.cpp1
2 files changed, 2 insertions, 1 deletions
diff --git a/src/tools/imgtool/modules/fat.cpp b/src/tools/imgtool/modules/fat.cpp
index d6611932c0d..a7ee5832a6e 100644
--- a/src/tools/imgtool/modules/fat.cpp
+++ b/src/tools/imgtool/modules/fat.cpp
@@ -436,7 +436,7 @@ static imgtoolerr_t fat_partition_open(imgtool::partition &partition, uint64_t f
if (info->sectors_per_cluster == 0)
return IMGTOOLERR_CORRUPTIMAGE;
- info->total_sectors = total_sectors_l + (uint64_t(total_sectors_h) << 16);
+ info->total_sectors = total_sectors_l != 0 ? total_sector_l : total_sectors_h;
available_sectors = info->total_sectors - info->reserved_sectors
- (info->sectors_per_fat * info->fat_count)
- (info->root_entries * FAT_DIRENT_SIZE + FAT_SECLEN - 1) / FAT_SECLEN;
diff --git a/src/tools/imgtool/modules/pc_hard.cpp b/src/tools/imgtool/modules/pc_hard.cpp
index 353a6b5dac5..8198e9b0b29 100644
--- a/src/tools/imgtool/modules/pc_hard.cpp
+++ b/src/tools/imgtool/modules/pc_hard.cpp
@@ -292,6 +292,7 @@ static imgtoolerr_t pc_chd_image_open(imgtool::image &image, imgtool::stream::pt
if (err)
return err;
+ stream.release();
return IMGTOOLERR_SUCCESS;
}