1 From 68a09a74f6d726d79709847f3671c0a08e4fb5a0 Mon Sep 17 00:00:00 2001
2 From: Colin Watson <cjwatson@debian.org>
3 Date: Sat, 25 Jul 2020 12:15:37 +0100
4 Subject: [PATCH 9/9] linux: Fix integer overflows in initrd size handling
6 These could be triggered by a crafted filesystem with very large files.
10 Signed-off-by: Colin Watson <cjwatson@debian.org>
11 Reviewed-by: Jan Setje-Eilers <jan.setjeeilers@oracle.com>
12 Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
14 Upstream-Status: Backport
17 Reference to upstream patch:
18 https://git.savannah.gnu.org/cgit/grub.git/commit/?id=e7b8856f8be3292afdb38d2e8c70ad8d62a61e10
20 Signed-off-by: Yongxin Liu <yongxin.liu@windriver.com>
22 grub-core/loader/linux.c | 74 +++++++++++++++++++++++++++++++++++-------------
23 1 file changed, 54 insertions(+), 20 deletions(-)
25 diff --git a/grub-core/loader/linux.c b/grub-core/loader/linux.c
26 index 471b214..8c8565a 100644
27 --- a/grub-core/loader/linux.c
28 +++ b/grub-core/loader/linux.c
30 #include <grub/misc.h>
31 #include <grub/file.h>
33 +#include <grub/safemath.h>
37 @@ -98,13 +99,13 @@ free_dir (struct dir *root)
43 insert_dir (const char *name, struct dir **root,
45 + grub_uint8_t *ptr, grub_size_t *size)
47 struct dir *cur, **head = root;
48 const char *cb, *ce = name;
49 - grub_size_t size = 0;
53 for (cb = ce; *cb == '/'; cb++);
54 @@ -130,14 +131,22 @@ insert_dir (const char *name, struct dir **root,
55 ptr = make_header (ptr, name, ce - name,
58 - size += ALIGN_UP ((ce - (char *) name)
59 - + sizeof (struct newc_head), 4);
60 + if (grub_add (*size,
61 + ALIGN_UP ((ce - (char *) name)
62 + + sizeof (struct newc_head), 4),
65 + grub_error (GRUB_ERR_OUT_OF_RANGE, N_("overflow is detected"));
66 + grub_free (n->name);
76 + return GRUB_ERR_NONE;
80 @@ -173,26 +182,33 @@ grub_initrd_init (int argc, char *argv[],
81 eptr = grub_strchr (ptr, ':');
84 + grub_size_t dir_size, name_len;
86 initrd_ctx->components[i].newc_name = grub_strndup (ptr, eptr - ptr);
87 - if (!initrd_ctx->components[i].newc_name)
88 + if (!initrd_ctx->components[i].newc_name ||
89 + insert_dir (initrd_ctx->components[i].newc_name, &root, 0,
92 grub_initrd_close (initrd_ctx);
96 - += ALIGN_UP (sizeof (struct newc_head)
97 - + grub_strlen (initrd_ctx->components[i].newc_name),
99 - initrd_ctx->size += insert_dir (initrd_ctx->components[i].newc_name,
101 + name_len = grub_strlen (initrd_ctx->components[i].newc_name);
102 + if (grub_add (initrd_ctx->size,
103 + ALIGN_UP (sizeof (struct newc_head) + name_len, 4),
104 + &initrd_ctx->size) ||
105 + grub_add (initrd_ctx->size, dir_size, &initrd_ctx->size))
113 - initrd_ctx->size += ALIGN_UP (sizeof (struct newc_head)
114 - + sizeof ("TRAILER!!!") - 1, 4);
115 + if (grub_add (initrd_ctx->size,
116 + ALIGN_UP (sizeof (struct newc_head)
117 + + sizeof ("TRAILER!!!") - 1, 4),
118 + &initrd_ctx->size))
123 @@ -208,19 +224,29 @@ grub_initrd_init (int argc, char *argv[],
124 initrd_ctx->nfiles++;
125 initrd_ctx->components[i].size
126 = grub_file_size (initrd_ctx->components[i].file);
127 - initrd_ctx->size += initrd_ctx->components[i].size;
128 + if (grub_add (initrd_ctx->size, initrd_ctx->components[i].size,
129 + &initrd_ctx->size))
135 initrd_ctx->size = ALIGN_UP (initrd_ctx->size, 4);
136 - initrd_ctx->size += ALIGN_UP (sizeof (struct newc_head)
137 - + sizeof ("TRAILER!!!") - 1, 4);
138 + if (grub_add (initrd_ctx->size,
139 + ALIGN_UP (sizeof (struct newc_head)
140 + + sizeof ("TRAILER!!!") - 1, 4),
141 + &initrd_ctx->size))
147 return GRUB_ERR_NONE;
151 + grub_initrd_close (initrd_ctx);
152 + return grub_error (GRUB_ERR_OUT_OF_RANGE, N_("overflow is detected"));
156 @@ -261,8 +287,16 @@ grub_initrd_load (struct grub_linux_initrd_context *initrd_ctx,
158 if (initrd_ctx->components[i].newc_name)
160 - ptr += insert_dir (initrd_ctx->components[i].newc_name,
162 + grub_size_t dir_size;
164 + if (insert_dir (initrd_ctx->components[i].newc_name, &root, ptr,
168 + grub_initrd_close (initrd_ctx);
172 ptr = make_header (ptr, initrd_ctx->components[i].newc_name,
173 grub_strlen (initrd_ctx->components[i].newc_name),