From 975ac1583adfcefc21f4717304521277349730a9 Mon Sep 17 00:00:00 2001 From: Zhiqiang Liu Date: Tue, 9 Feb 2021 10:31:56 +0800 Subject: [PATCH] dosfstools: backport patches to fix two memory leak problems - backport patches to fix two memory leak problems. - rename patches - set release num to 9 for ci. Signed-off-by: Zhiqiang Liu Conflicts: dosfstools.spec --- ...ix-memory-leaks-in-read_fat-function.patch | 89 +++++++++++++++++++ ...ruct-DOS_FS-into-char-array-from-poi.patch | 65 ++++++++++++++ dosfstools.spec | 10 ++- 3 files changed, 162 insertions(+), 2 deletions(-) create mode 100644 0008-Fix-memory-leaks-in-read_fat-function.patch create mode 100644 0009-Turn-label-in-struct-DOS_FS-into-char-array-from-poi.patch diff --git a/0008-Fix-memory-leaks-in-read_fat-function.patch b/0008-Fix-memory-leaks-in-read_fat-function.patch new file mode 100644 index 0000000..9d4bdb4 --- /dev/null +++ b/0008-Fix-memory-leaks-in-read_fat-function.patch @@ -0,0 +1,89 @@ +From 1a1ba0053830b98e99f9b2713f64dbcb36e2a6cd Mon Sep 17 00:00:00 2001 +From: =?UTF-8?q?Pali=20Roh=C3=A1r?= +Date: Sun, 18 Nov 2018 20:47:29 +0100 +Subject: [PATCH] Fix memory leaks in read_fat() function + +Function read_fat() allocates memory to the user supplied buffer. Therefore +that function needs complement function for releasing allocated memory and +user needs to call if after finish its work. + +This patch fixes memory leaks in fsck.fat and fatlabel tools. + +Conflicts: + src/fsck.fat.c + src/fatlabel.c +[Zhiqiang Liu modifies context] + +Fixes #13 +--- + src/fat.c | 17 +++++++++++------ + src/fat.h | 5 +++++ + src/fsck.fat.c | 1 + + 3 files changed, 17 insertions(+), 6 deletions(-) + +diff --git a/src/fat.c b/src/fat.c +index d994e8e..849c758 100644 +--- a/src/fat.c ++++ b/src/fat.c +@@ -75,6 +75,16 @@ void get_fat(FAT_ENTRY * entry, void *fat, uint32_t cluster, DOS_FS * fs) + } + } + ++void release_fat(DOS_FS * fs) ++{ ++ if (fs->fat) ++ free(fs->fat); ++ if (fs->cluster_owner) ++ free(fs->cluster_owner); ++ fs->fat = NULL; ++ fs->cluster_owner = NULL; ++} ++ + /** + * Build a bookkeeping structure from the partition's FAT table. + * If the partition has multiple FATs and they don't agree, try to pick a winner, +@@ -92,12 +102,7 @@ void read_fat(DOS_FS * fs) + uint32_t total_num_clusters; + + /* Clean up from previous pass */ +- if (fs->fat) +- free(fs->fat); +- if (fs->cluster_owner) +- free(fs->cluster_owner); +- fs->fat = NULL; +- fs->cluster_owner = NULL; ++ release_fat(fs); + + total_num_clusters = fs->data_clusters + 2; + eff_size = (total_num_clusters * fs->fat_bits + 7) / 8ULL; +diff --git a/src/fat.h b/src/fat.h +index 5c77634..f9b7643 100644 +--- a/src/fat.h ++++ b/src/fat.h +@@ -28,6 +28,11 @@ void read_fat(DOS_FS * fs); + /* Loads the FAT of the filesystem described by FS. Initializes the FAT, + replaces broken FATs and rejects invalid cluster entries. */ + ++void release_fat(DOS_FS * fs); ++ ++/* Release the FAT of the filesystem described by FS and free allocated memory. ++ Call it after finish work with FAT. */ ++ + void get_fat(FAT_ENTRY * entry, void *fat, uint32_t cluster, DOS_FS * fs); + + /* Retrieve the FAT entry (next chained cluster) for CLUSTER. */ +diff --git a/src/fsck.fat.c b/src/fsck.fat.c +index c244aba..b1aafe9 100644 +--- a/src/fsck.fat.c ++++ b/src/fsck.fat.c +@@ -203,6 +203,7 @@ int main(int argc, char **argv) + reclaim_free(&fs); + qfree(&mem_queue); + } ++ release_fat(&fs); + + exit: + if (fs_changed()) { +-- +1.8.3.1 + diff --git a/0009-Turn-label-in-struct-DOS_FS-into-char-array-from-poi.patch b/0009-Turn-label-in-struct-DOS_FS-into-char-array-from-poi.patch new file mode 100644 index 0000000..14227b5 --- /dev/null +++ b/0009-Turn-label-in-struct-DOS_FS-into-char-array-from-poi.patch @@ -0,0 +1,65 @@ +From 08cf67bb19f8b9cce0c2dd03432951ade476dadd Mon Sep 17 00:00:00 2001 +From: Andreas Bombe +Date: Thu, 26 Jan 2017 21:31:03 +0100 +Subject: [PATCH] Turn label in struct DOS_FS into char array from pointer + +Signed-off-by: Andreas Bombe +--- + src/boot.c | 6 +----- + src/fatlabel.c | 2 +- + src/fsck.fat.h | 2 +- + 3 files changed, 3 insertions(+), 7 deletions(-) + +diff --git a/src/boot.c b/src/boot.c +index 58b4286..b4a3300 100644 +--- a/src/boot.c ++++ b/src/boot.c +@@ -435,18 +435,14 @@ void read_boot(DOS_FS * fs) + fs->eff_fat_bits = (fs->fat_bits == 32) ? 28 : fs->fat_bits; + fs->fat_size = fat_length * logical_sector_size; + +- fs->label = calloc(12, sizeof(uint8_t)); ++ fs->label[0] = 0; + if (fs->fat_bits == 12 || fs->fat_bits == 16) { + struct boot_sector_16 *b16 = (struct boot_sector_16 *)&b; + if (b16->extended_sig == 0x29) + memmove(fs->label, b16->label, 11); +- else +- fs->label = NULL; + } else if (fs->fat_bits == 32) { + if (b.extended_sig == 0x29) + memmove(fs->label, &b.label, 11); +- else +- fs->label = NULL; + } + + total_fat_entries = (uint64_t)fs->fat_size * 8 / fs->fat_bits; +diff --git a/src/fatlabel.c b/src/fatlabel.c +index 9268ddb..cd3d2ee 100644 +--- a/src/fatlabel.c ++++ b/src/fatlabel.c +@@ -133,7 +133,7 @@ int main(int argc, char *argv[]) + if (!rw) { + offset = find_volume_de(&fs, &de); + if (offset == 0) +- fprintf(stdout, "%s\n", fs.label); ++ fprintf(stdout, "%11s\n", fs.label); + else + fprintf(stdout, "%.8s%.3s\n", de.name, de.name + 8); + exit(0); +diff --git a/src/fsck.fat.h b/src/fsck.fat.h +index 5e93178..e91437d 100644 +--- a/src/fsck.fat.h ++++ b/src/fsck.fat.h +@@ -164,7 +164,7 @@ typedef struct { + off_t backupboot_start; /* 0 if not present */ + unsigned char *fat; + DOS_FILE **cluster_owner; +- char *label; ++ char label[11]; + } DOS_FS; + + extern int interactive, rw, list, verbose, test, write_immed; +-- +1.8.3.1 + diff --git a/dosfstools.spec b/dosfstools.spec index ea3a4e7..6486449 100644 --- a/dosfstools.spec +++ b/dosfstools.spec @@ -1,6 +1,6 @@ Name: dosfstools Version: 4.1 -Release: 10 +Release: 11 Summary: FAT file system userspace tools License: GPLv3+ URL: http://www.github.com/dosfstools/dosfstools @@ -16,6 +16,8 @@ Patch4: 0004-Fix-gcc-sprintf-length-warnings.patch Patch5: 0005-fsck.fat-Fix-Year-2038-Bug.patch Patch6: 0006-mkfs.fat-Fix-parsing-of-block-number.patch Patch7: 0007-device_info-Fix-parsing-partition-number.patch +Patch8: 0008-Fix-memory-leaks-in-read_fat-function.patch +Patch9: 0009-Turn-label-in-struct-DOS_FS-into-char-array-from-poi.patch %description The dosfstools package contains programs mkfs.fat, fsck.fat and fatlabel to @@ -52,9 +54,13 @@ make check %{_mandir}/man8/* %changelog -* Fri Jul 30 2021 chenyanpanHW - 4.1-10 +* Fri Jul 30 2021 chenyanpanHW - 4.1-11 - DESC: delete -S git from %autosetup, and delete BuildRequires git +* Tue Feb 9 2021 Zhiqiang Liu - 4.1-10 +- backport patches to fix two memory leak problems, rename patch names, + and set release num to 9 for CI. + * Wed Nov 4 2020 lixiaokeng - 4.1-9 - add make check