0020-disk-loopback-Reference-tracking-for-the-loopback.patch 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. From a81ef3044791e7ee02bd349b5ec0adcbf6947555 Mon Sep 17 00:00:00 2001
  2. From: B Horn <b@horn.uk>
  3. Date: Sun, 12 May 2024 03:26:19 +0100
  4. Subject: [PATCH] disk/loopback: Reference tracking for the loopback
  5. It was possible to delete a loopback while there were still references
  6. to it. This led to an exploitable use-after-free.
  7. Fixed by implementing a reference counting in the grub_loopback struct.
  8. Reported-by: B Horn <b@horn.uk>
  9. Signed-off-by: B Horn <b@horn.uk>
  10. Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
  11. Upstream: 67f70f70a36b6e87a65f928fe1e840a12eafb7ae
  12. Signed-off-by: Thomas Petazzoni <thomas.petazzoni@bootlin.com>
  13. ---
  14. grub-core/disk/loopback.c | 18 ++++++++++++++++++
  15. include/grub/err.h | 3 ++-
  16. 2 files changed, 20 insertions(+), 1 deletion(-)
  17. diff --git a/grub-core/disk/loopback.c b/grub-core/disk/loopback.c
  18. index 4635dcfde..2bea4e922 100644
  19. --- a/grub-core/disk/loopback.c
  20. +++ b/grub-core/disk/loopback.c
  21. @@ -24,6 +24,7 @@
  22. #include <grub/mm.h>
  23. #include <grub/extcmd.h>
  24. #include <grub/i18n.h>
  25. +#include <grub/safemath.h>
  26. GRUB_MOD_LICENSE ("GPLv3+");
  27. @@ -33,6 +34,7 @@ struct grub_loopback
  28. grub_file_t file;
  29. struct grub_loopback *next;
  30. unsigned long id;
  31. + grub_uint64_t refcnt;
  32. };
  33. static struct grub_loopback *loopback_list;
  34. @@ -64,6 +66,8 @@ delete_loopback (const char *name)
  35. if (! dev)
  36. return grub_error (GRUB_ERR_BAD_DEVICE, "device not found");
  37. + if (dev->refcnt > 0)
  38. + return grub_error (GRUB_ERR_STILL_REFERENCED, "device still referenced");
  39. /* Remove the device from the list. */
  40. *prev = dev->next;
  41. @@ -120,6 +124,7 @@ grub_cmd_loopback (grub_extcmd_context_t ctxt, int argc, char **args)
  42. newdev->file = file;
  43. newdev->id = last_id++;
  44. + newdev->refcnt = 0;
  45. /* Add the new entry to the list. */
  46. newdev->next = loopback_list;
  47. @@ -161,6 +166,9 @@ grub_loopback_open (const char *name, grub_disk_t disk)
  48. if (! dev)
  49. return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "can't open device");
  50. + if (grub_add (dev->refcnt, 1, &dev->refcnt))
  51. + grub_fatal ("Reference count overflow");
  52. +
  53. /* Use the filesize for the disk size, round up to a complete sector. */
  54. if (dev->file->size != GRUB_FILE_SIZE_UNKNOWN)
  55. disk->total_sectors = ((dev->file->size + GRUB_DISK_SECTOR_SIZE - 1)
  56. @@ -178,6 +186,15 @@ grub_loopback_open (const char *name, grub_disk_t disk)
  57. return 0;
  58. }
  59. +static void
  60. +grub_loopback_close (grub_disk_t disk)
  61. +{
  62. + struct grub_loopback *dev = disk->data;
  63. +
  64. + if (grub_sub (dev->refcnt, 1, &dev->refcnt))
  65. + grub_fatal ("Reference count underflow");
  66. +}
  67. +
  68. static grub_err_t
  69. grub_loopback_read (grub_disk_t disk, grub_disk_addr_t sector,
  70. grub_size_t size, char *buf)
  71. @@ -220,6 +237,7 @@ static struct grub_disk_dev grub_loopback_dev =
  72. .id = GRUB_DISK_DEVICE_LOOPBACK_ID,
  73. .disk_iterate = grub_loopback_iterate,
  74. .disk_open = grub_loopback_open,
  75. + .disk_close = grub_loopback_close,
  76. .disk_read = grub_loopback_read,
  77. .disk_write = grub_loopback_write,
  78. .next = 0
  79. diff --git a/include/grub/err.h b/include/grub/err.h
  80. index 1c07034cd..b0e54e0a0 100644
  81. --- a/include/grub/err.h
  82. +++ b/include/grub/err.h
  83. @@ -73,7 +73,8 @@ typedef enum
  84. GRUB_ERR_NET_NO_DOMAIN,
  85. GRUB_ERR_EOF,
  86. GRUB_ERR_BAD_SIGNATURE,
  87. - GRUB_ERR_BAD_FIRMWARE
  88. + GRUB_ERR_BAD_FIRMWARE,
  89. + GRUB_ERR_STILL_REFERENCED
  90. }
  91. grub_err_t;
  92. --
  93. 2.50.1