0012-fsck.fat-Fix-read-beyond-end-of-array-on-FAT12.patch 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. From a41fc323f2ef38f884954a4ba3773a296fd809f8 Mon Sep 17 00:00:00 2001
  2. From: Andreas Bombe <aeb@debian.org>
  3. Date: Wed, 11 Mar 2015 21:45:04 +0100
  4. Subject: [PATCH 12/14] fsck.fat: Fix read beyond end of array on FAT12
  5. When a FAT12 filesystem contains an odd number of clusters, setting the
  6. last cluster with set_fat() will trigger a read of the next entry,
  7. which does not exist in the fat array allocated for this.
  8. Round up the allocation to an even number of FAT entries for FAT12 so
  9. that this is fixed without introducing special casing in get_fat().
  10. Signed-off-by: Andreas Bombe <aeb@debian.org>
  11. ---
  12. src/fat.c | 14 +++++++++++---
  13. 1 file changed, 11 insertions(+), 3 deletions(-)
  14. diff --git a/src/fat.c b/src/fat.c
  15. index 027c586..5a92f56 100644
  16. --- a/src/fat.c
  17. +++ b/src/fat.c
  18. @@ -80,7 +80,7 @@ void get_fat(FAT_ENTRY * entry, void *fat, uint32_t cluster, DOS_FS * fs)
  19. */
  20. void read_fat(DOS_FS * fs)
  21. {
  22. - int eff_size;
  23. + int eff_size, alloc_size;
  24. uint32_t i;
  25. void *first, *second = NULL;
  26. int first_ok, second_ok;
  27. @@ -96,10 +96,18 @@ void read_fat(DOS_FS * fs)
  28. total_num_clusters = fs->clusters + 2UL;
  29. eff_size = (total_num_clusters * fs->fat_bits + 7) / 8ULL;
  30. - first = alloc(eff_size);
  31. +
  32. + if (fs->fat_bits != 12)
  33. + alloc_size = eff_size;
  34. + else
  35. + /* round up to an even number of FAT entries to avoid special
  36. + * casing the last entry in get_fat() */
  37. + alloc_size = (total_num_clusters * 12 + 23) / 24 * 3;
  38. +
  39. + first = alloc(alloc_size);
  40. fs_read(fs->fat_start, eff_size, first);
  41. if (fs->nfats > 1) {
  42. - second = alloc(eff_size);
  43. + second = alloc(alloc_size);
  44. fs_read(fs->fat_start + fs->fat_size, eff_size, second);
  45. }
  46. if (second && memcmp(first, second, eff_size) != 0) {
  47. --
  48. 1.9.1