|
|
c294fc |
From 9715e08cf30ebd8a24ca27b7c4dda8e949e100df Mon Sep 17 00:00:00 2001
|
|
|
c294fc |
From: Colin Watson <cjwatson@debian.org>
|
|
|
c294fc |
Date: Sat, 25 Jul 2020 12:15:37 +0100
|
|
|
c294fc |
Subject: [PATCH 314/314] linux: Fix integer overflows in initrd size handling
|
|
|
c294fc |
|
|
|
c294fc |
These could be triggered by a crafted filesystem with very large files.
|
|
|
c294fc |
|
|
|
c294fc |
Fixes: CVE-2020-15707
|
|
|
c294fc |
|
|
|
c294fc |
Signed-off-by: Colin Watson <cjwatson@debian.org>
|
|
|
c294fc |
Reviewed-by: Jan Setje-Eilers <jan.setjeeilers@oracle.com>
|
|
|
c294fc |
---
|
|
|
c294fc |
grub-core/loader/linux.c | 74 +++++++++++++++++++++++++++++-----------
|
|
|
c294fc |
1 file changed, 54 insertions(+), 20 deletions(-)
|
|
|
c294fc |
|
|
|
c294fc |
diff --git a/grub-core/loader/linux.c b/grub-core/loader/linux.c
|
|
|
c294fc |
index 61a2e144db0..0953f6d3266 100644
|
|
|
c294fc |
--- a/grub-core/loader/linux.c
|
|
|
c294fc |
+++ b/grub-core/loader/linux.c
|
|
|
c294fc |
@@ -5,6 +5,7 @@
|
|
|
c294fc |
#include <grub/file.h>
|
|
|
c294fc |
#include <grub/mm.h>
|
|
|
c294fc |
#include <grub/tpm.h>
|
|
|
c294fc |
+#include <grub/safemath.h>
|
|
|
c294fc |
|
|
|
c294fc |
struct newc_head
|
|
|
c294fc |
{
|
|
|
c294fc |
@@ -99,13 +100,13 @@ free_dir (struct dir *root)
|
|
|
c294fc |
grub_free (root);
|
|
|
c294fc |
}
|
|
|
c294fc |
|
|
|
c294fc |
-static grub_size_t
|
|
|
c294fc |
+static grub_err_t
|
|
|
c294fc |
insert_dir (const char *name, struct dir **root,
|
|
|
c294fc |
- grub_uint8_t *ptr)
|
|
|
c294fc |
+ grub_uint8_t *ptr, grub_size_t *size)
|
|
|
c294fc |
{
|
|
|
c294fc |
struct dir *cur, **head = root;
|
|
|
c294fc |
const char *cb, *ce = name;
|
|
|
c294fc |
- grub_size_t size = 0;
|
|
|
c294fc |
+ *size = 0;
|
|
|
c294fc |
while (1)
|
|
|
c294fc |
{
|
|
|
c294fc |
for (cb = ce; *cb == '/'; cb++);
|
|
|
c294fc |
@@ -131,14 +132,22 @@ insert_dir (const char *name, struct dir **root,
|
|
|
c294fc |
ptr = make_header (ptr, name, ce - name,
|
|
|
c294fc |
040777, 0);
|
|
|
c294fc |
}
|
|
|
c294fc |
- size += ALIGN_UP ((ce - (char *) name)
|
|
|
c294fc |
- + sizeof (struct newc_head), 4);
|
|
|
c294fc |
+ if (grub_add (*size,
|
|
|
c294fc |
+ ALIGN_UP ((ce - (char *) name)
|
|
|
c294fc |
+ + sizeof (struct newc_head), 4),
|
|
|
c294fc |
+ size))
|
|
|
c294fc |
+ {
|
|
|
c294fc |
+ grub_error (GRUB_ERR_OUT_OF_RANGE, N_("overflow is detected"));
|
|
|
c294fc |
+ grub_free (n->name);
|
|
|
c294fc |
+ grub_free (n);
|
|
|
c294fc |
+ return grub_errno;
|
|
|
c294fc |
+ }
|
|
|
c294fc |
*head = n;
|
|
|
c294fc |
cur = n;
|
|
|
c294fc |
}
|
|
|
c294fc |
root = &cur->next;
|
|
|
c294fc |
}
|
|
|
c294fc |
- return size;
|
|
|
c294fc |
+ return GRUB_ERR_NONE;
|
|
|
c294fc |
}
|
|
|
c294fc |
|
|
|
c294fc |
grub_err_t
|
|
|
c294fc |
@@ -175,26 +184,33 @@ grub_initrd_init (int argc, char *argv[],
|
|
|
c294fc |
if (eptr)
|
|
|
c294fc |
{
|
|
|
c294fc |
grub_file_filter_disable_compression ();
|
|
|
c294fc |
+ grub_size_t dir_size, name_len;
|
|
|
c294fc |
+
|
|
|
c294fc |
initrd_ctx->components[i].newc_name = grub_strndup (ptr, eptr - ptr);
|
|
|
c294fc |
- if (!initrd_ctx->components[i].newc_name)
|
|
|
c294fc |
+ if (!initrd_ctx->components[i].newc_name ||
|
|
|
c294fc |
+ insert_dir (initrd_ctx->components[i].newc_name, &root, 0,
|
|
|
c294fc |
+ &dir_size))
|
|
|
c294fc |
{
|
|
|
c294fc |
grub_initrd_close (initrd_ctx);
|
|
|
c294fc |
return grub_errno;
|
|
|
c294fc |
}
|
|
|
c294fc |
- initrd_ctx->size
|
|
|
c294fc |
- += ALIGN_UP (sizeof (struct newc_head)
|
|
|
c294fc |
- + grub_strlen (initrd_ctx->components[i].newc_name),
|
|
|
c294fc |
- 4);
|
|
|
c294fc |
- initrd_ctx->size += insert_dir (initrd_ctx->components[i].newc_name,
|
|
|
c294fc |
- &root, 0);
|
|
|
c294fc |
+ name_len = grub_strlen (initrd_ctx->components[i].newc_name);
|
|
|
c294fc |
+ if (grub_add (initrd_ctx->size,
|
|
|
c294fc |
+ ALIGN_UP (sizeof (struct newc_head) + name_len, 4),
|
|
|
c294fc |
+ &initrd_ctx->size) ||
|
|
|
c294fc |
+ grub_add (initrd_ctx->size, dir_size, &initrd_ctx->size))
|
|
|
c294fc |
+ goto overflow;
|
|
|
c294fc |
newc = 1;
|
|
|
c294fc |
fname = eptr + 1;
|
|
|
c294fc |
}
|
|
|
c294fc |
}
|
|
|
c294fc |
else if (newc)
|
|
|
c294fc |
{
|
|
|
c294fc |
- initrd_ctx->size += ALIGN_UP (sizeof (struct newc_head)
|
|
|
c294fc |
- + sizeof ("TRAILER!!!") - 1, 4);
|
|
|
c294fc |
+ if (grub_add (initrd_ctx->size,
|
|
|
c294fc |
+ ALIGN_UP (sizeof (struct newc_head)
|
|
|
c294fc |
+ + sizeof ("TRAILER!!!") - 1, 4),
|
|
|
c294fc |
+ &initrd_ctx->size))
|
|
|
c294fc |
+ goto overflow;
|
|
|
c294fc |
free_dir (root);
|
|
|
c294fc |
root = 0;
|
|
|
c294fc |
newc = 0;
|
|
|
c294fc |
@@ -209,19 +225,29 @@ grub_initrd_init (int argc, char *argv[],
|
|
|
c294fc |
initrd_ctx->nfiles++;
|
|
|
c294fc |
initrd_ctx->components[i].size
|
|
|
c294fc |
= grub_file_size (initrd_ctx->components[i].file);
|
|
|
c294fc |
- initrd_ctx->size += initrd_ctx->components[i].size;
|
|
|
c294fc |
+ if (grub_add (initrd_ctx->size, initrd_ctx->components[i].size,
|
|
|
c294fc |
+ &initrd_ctx->size))
|
|
|
c294fc |
+ goto overflow;
|
|
|
c294fc |
}
|
|
|
c294fc |
|
|
|
c294fc |
if (newc)
|
|
|
c294fc |
{
|
|
|
c294fc |
initrd_ctx->size = ALIGN_UP (initrd_ctx->size, 4);
|
|
|
c294fc |
- initrd_ctx->size += ALIGN_UP (sizeof (struct newc_head)
|
|
|
c294fc |
- + sizeof ("TRAILER!!!") - 1, 4);
|
|
|
c294fc |
+ if (grub_add (initrd_ctx->size,
|
|
|
c294fc |
+ ALIGN_UP (sizeof (struct newc_head)
|
|
|
c294fc |
+ + sizeof ("TRAILER!!!") - 1, 4),
|
|
|
c294fc |
+ &initrd_ctx->size))
|
|
|
c294fc |
+ goto overflow;
|
|
|
c294fc |
free_dir (root);
|
|
|
c294fc |
root = 0;
|
|
|
c294fc |
}
|
|
|
c294fc |
|
|
|
c294fc |
return GRUB_ERR_NONE;
|
|
|
c294fc |
+
|
|
|
c294fc |
+overflow:
|
|
|
c294fc |
+ free_dir (root);
|
|
|
c294fc |
+ grub_initrd_close (initrd_ctx);
|
|
|
c294fc |
+ return grub_error (GRUB_ERR_OUT_OF_RANGE, N_("overflow is detected"));
|
|
|
c294fc |
}
|
|
|
c294fc |
|
|
|
c294fc |
grub_size_t
|
|
|
c294fc |
@@ -262,8 +288,16 @@ grub_initrd_load (struct grub_linux_initrd_context *initrd_ctx,
|
|
|
c294fc |
|
|
|
c294fc |
if (initrd_ctx->components[i].newc_name)
|
|
|
c294fc |
{
|
|
|
c294fc |
- ptr += insert_dir (initrd_ctx->components[i].newc_name,
|
|
|
c294fc |
- &root, ptr);
|
|
|
c294fc |
+ grub_size_t dir_size;
|
|
|
c294fc |
+
|
|
|
c294fc |
+ if (insert_dir (initrd_ctx->components[i].newc_name, &root, ptr,
|
|
|
c294fc |
+ &dir_size))
|
|
|
c294fc |
+ {
|
|
|
c294fc |
+ free_dir (root);
|
|
|
c294fc |
+ grub_initrd_close (initrd_ctx);
|
|
|
c294fc |
+ return grub_errno;
|
|
|
c294fc |
+ }
|
|
|
c294fc |
+ ptr += dir_size;
|
|
|
c294fc |
ptr = make_header (ptr, initrd_ctx->components[i].newc_name,
|
|
|
c294fc |
grub_strlen (initrd_ctx->components[i].newc_name),
|
|
|
c294fc |
0100777,
|
|
|
c294fc |
--
|
|
|
c294fc |
2.26.2
|
|
|
c294fc |
|