disk/loopback: Reference tracking for the loopback

It was possible to delete a loopback while there were still references
to it. This led to an exploitable use-after-free.

Fixed by implementing a reference counting in the grub_loopback struct.

Reported-by: B Horn <b@horn.uk>
Signed-off-by: B Horn <b@horn.uk>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
This commit is contained in:
B Horn authored and Daniel Kiper committed 2025-01-23 16:22:47 +01:00
1 parent 13febd78db
commit 67f70f70a3
2 files changed
+20 -1

No files matched your search

+18
View File
@@ -24,6 +24,7 @@
#include <grub/mm.h> #include <grub/mm.h>
#include <grub/extcmd.h> #include <grub/extcmd.h>
#include <grub/i18n.h> #include <grub/i18n.h>
#include <grub/safemath.h>
GRUB_MOD_LICENSE ("GPLv3+"); GRUB_MOD_LICENSE ("GPLv3+");
@@ -33,6 +34,7 @@ struct grub_loopback
grub_file_t file; grub_file_t file;
struct grub_loopback *next; struct grub_loopback *next;
unsigned long id; unsigned long id;
grub_uint64_t refcnt;
}; };
static struct grub_loopback *loopback_list; static struct grub_loopback *loopback_list;
@@ -64,6 +66,8 @@ delete_loopback (const char *name)
if (! dev) if (! dev)
return grub_error (GRUB_ERR_BAD_DEVICE, "device not found"); return grub_error (GRUB_ERR_BAD_DEVICE, "device not found");
if (dev->refcnt > 0)
return grub_error (GRUB_ERR_STILL_REFERENCED, "device still referenced");
/* Remove the device from the list. */ /* Remove the device from the list. */
*prev = dev->next; *prev = dev->next;
@@ -120,6 +124,7 @@ grub_cmd_loopback (grub_extcmd_context_t ctxt, int argc, char **args)
newdev->file = file; newdev->file = file;
newdev->id = last_id++; newdev->id = last_id++;
newdev->refcnt = 0;
/* Add the new entry to the list. */ /* Add the new entry to the list. */
newdev->next = loopback_list; newdev->next = loopback_list;
@@ -161,6 +166,9 @@ grub_loopback_open (const char *name, grub_disk_t disk)
if (! dev) if (! dev)
return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "can't open device"); return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "can't open device");
if (grub_add (dev->refcnt, 1, &dev->refcnt))
grub_fatal ("Reference count overflow");
/* Use the filesize for the disk size, round up to a complete sector. */ /* Use the filesize for the disk size, round up to a complete sector. */
if (dev->file->size != GRUB_FILE_SIZE_UNKNOWN) if (dev->file->size != GRUB_FILE_SIZE_UNKNOWN)
disk->total_sectors = ((dev->file->size + GRUB_DISK_SECTOR_SIZE - 1) disk->total_sectors = ((dev->file->size + GRUB_DISK_SECTOR_SIZE - 1)
@@ -178,6 +186,15 @@ grub_loopback_open (const char *name, grub_disk_t disk)
return 0; return 0;
} }
static void
grub_loopback_close (grub_disk_t disk)
{
struct grub_loopback *dev = disk->data;
if (grub_sub (dev->refcnt, 1, &dev->refcnt))
grub_fatal ("Reference count underflow");
}
static grub_err_t static grub_err_t
grub_loopback_read (grub_disk_t disk, grub_disk_addr_t sector, grub_loopback_read (grub_disk_t disk, grub_disk_addr_t sector,
grub_size_t size, char *buf) grub_size_t size, char *buf)
@@ -220,6 +237,7 @@ static struct grub_disk_dev grub_loopback_dev =
.id = GRUB_DISK_DEVICE_LOOPBACK_ID, .id = GRUB_DISK_DEVICE_LOOPBACK_ID,
.disk_iterate = grub_loopback_iterate, .disk_iterate = grub_loopback_iterate,
.disk_open = grub_loopback_open, .disk_open = grub_loopback_open,
.disk_close = grub_loopback_close,
.disk_read = grub_loopback_read, .disk_read = grub_loopback_read,
.disk_write = grub_loopback_write, .disk_write = grub_loopback_write,
.next = 0 .next = 0
+2 -1
View File
@@ -73,7 +73,8 @@ typedef enum
GRUB_ERR_NET_NO_DOMAIN, GRUB_ERR_NET_NO_DOMAIN,
GRUB_ERR_EOF, GRUB_ERR_EOF,
GRUB_ERR_BAD_SIGNATURE, GRUB_ERR_BAD_SIGNATURE,
GRUB_ERR_BAD_FIRMWARE GRUB_ERR_BAD_FIRMWARE,
GRUB_ERR_STILL_REFERENCED
} }
grub_err_t; grub_err_t;