Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1paM9O-0008Sf-Eb for mharc-grub-devel@gnu.org; Thu, 09 Mar 2023 14:42:06 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1paM9N-0008SO-1V for grub-devel@gnu.org; Thu, 09 Mar 2023 14:42:05 -0500 Received: from mx0b-00069f02.pphosted.com ([205.220.177.32]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1paM9K-0004yo-Ob for grub-devel@gnu.org; Thu, 09 Mar 2023 14:42:04 -0500 Received: from pps.filterd (m0246632.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 329Hn2hk014801 for ; Thu, 9 Mar 2023 19:41:58 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id; s=corp-2022-7-12; bh=PS0xJWCi+0jDGTRKkp+fB3SENf0gvH51P1qCcgSZPuM=; b=QXOQFEHsqSWirAOx+r4DRvno9i3KFyecaBAy3g9ss1kyotHoHVjVlzXh+QU/Lmvtm/pU pacTKkw/a/8rYljcvhrSid5PRbov9XWx3XsvuV91Gff15fKTtyFMhw27Yq6st1owG6CJ SodBqwK1dPSxyzoJbavW87PZAcCk3DLqJ9GYYlWPIgW/MHc+P6Cj8nC9Kd3NguWHsWcA 9tPhfsuv5zcPFF+5nKepDAlj6d4Zz+qEa0HYB+zBplxX0Gj/kNHQBJCAcGOS8isT/Nlt A8itb4cpijyA3qHNENGjWSvqQgr3YfgbkYkTnO8/c5gnjbQAXai6pWkf4x3g9IrQfa3J Zg== Received: from phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta03.appoci.oracle.com [138.1.37.129]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3p417ckk6q-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK) for ; Thu, 09 Mar 2023 19:41:57 +0000 Received: from pps.filterd (phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (8.17.1.5/8.17.1.5) with ESMTP id 329ILVn7026460 for ; Thu, 9 Mar 2023 19:41:57 GMT Received: from pps.reinject (localhost [127.0.0.1]) by phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 3p6g9vnthf-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK) for ; Thu, 09 Mar 2023 19:41:56 +0000 Received: from phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by pps.reinject (8.17.1.5/8.17.1.5) with ESMTP id 329Jfu5D033008 for ; Thu, 9 Mar 2023 19:41:56 GMT Received: from localhost (dhcp-10-152-14-18.usdhcp.oraclecorp.com [10.152.14.18]) by phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTP id 3p6g9vnth1-1; Thu, 09 Mar 2023 19:41:56 +0000 From: Alec Brown To: grub-devel@gnu.org Cc: daniel.kiper@oracle.com, alec.r.brown@oracle.com Subject: [PATCH] util/grub-module-verifierXX.c: Add module_size parameter to functions for sanity checking Date: Thu, 9 Mar 2023 14:43:59 -0500 Message-Id: <1678391039-5974-1-git-send-email-alec.r.brown@oracle.com> X-Mailer: git-send-email 1.8.3.1 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.254,Aquarius:18.0.942,Hydra:6.0.573,FMLib:17.11.170.22 definitions=2023-03-09_10,2023-03-09_01,2023-02-09_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=0 mlxlogscore=771 adultscore=0 bulkscore=0 spamscore=0 malwarescore=0 mlxscore=0 phishscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2212070000 definitions=main-2303090157 X-Proofpoint-GUID: cTF81yQhRoi1Ar79JkYJF1nOTunoKZQo X-Proofpoint-ORIG-GUID: cTF81yQhRoi1Ar79JkYJF1nOTunoKZQo Received-SPF: pass client-ip=205.220.177.32; envelope-from=alec.r.brown@oracle.com; helo=mx0b-00069f02.pphosted.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_MED=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 09 Mar 2023 19:42:05 -0000 In grub-module-verifierXX.c, the function grub_module_verifyXX() performs an initial check that the ELF section headers are within the module's size, but doesn't check if the sections being accessed have contents that are within the module's size. In particular, we need to check that sh_offset and sh_size are less than the module's size. However, for some section header types we don't need to make these checks. For the type SHT_NULL, the section header is marked as inactive and the rest of the members within the section header have undefined values, so we don't need to check for sh_offset or sh_size. In the case of the type SHT_NOBITS, sh_offset has a conceptual offset which may be beyond the module size. Also, this type's sh_size may have a non-zero size, but a section of this type will take up no space in the module. This can all be checked in the function get_shdr(), but in order to do so, the parameter module_size must be added to functions so that the value of the module size can be used in get_shdr() from grub_module_verifyXX(). Also, had to rework some for loops to ensure the index passed to get_shdr() is within bounds. Signed-off-by: Alec Brown --- util/grub-module-verifierXX.c | 111 ++++++++++++++++++++-------------- 1 file changed, 64 insertions(+), 47 deletions(-) diff --git a/util/grub-module-verifierXX.c b/util/grub-module-verifierXX.c index d5907f268..b3f77ea0e 100644 --- a/util/grub-module-verifierXX.c +++ b/util/grub-module-verifierXX.c @@ -143,13 +143,24 @@ grub_target_to_host_real (const struct grub_module_verifier_arch *arch, grub_uin } static Elf_Shdr * -get_shdr (const struct grub_module_verifier_arch *arch, Elf_Ehdr *e, Elf_Word index) +get_shdr (const struct grub_module_verifier_arch *arch, Elf_Ehdr *e, Elf_Word index, size_t module_size) { + Elf_Shdr *s; + if (grub_target_to_host (e->e_shoff) == 0) grub_util_error ("Invalid section header offset"); - return (Elf_Shdr *) ((char *) e + grub_target_to_host (e->e_shoff) + - index * grub_target_to_host16 (e->e_shentsize)); + s = (Elf_Shdr *) ((char *) e + grub_target_to_host (e->e_shoff) + + index * grub_target_to_host16 (e->e_shentsize)); + /* Check that the header being accessed isn't outside the module size */ + if (grub_target_to_host32 (s->sh_type) != SHT_NULL && grub_target_to_host32 (s->sh_type) != SHT_NOBITS) + { + if (grub_target_to_host (s->sh_offset) > module_size) + grub_util_error ("Section %d starts after the end of the module", index); + if (grub_target_to_host (s->sh_offset) + grub_target_to_host (s->sh_size) > module_size) + grub_util_error ("Section %d ends after the end of the module", index); + } + return s; } static Elf_Shnum @@ -161,7 +172,7 @@ get_shnum (const struct grub_module_verifier_arch *arch, Elf_Ehdr *e) shnum = grub_target_to_host16 (e->e_shnum); if (shnum == SHN_UNDEF) { - s = get_shdr (arch, e, 0); + s = get_shdr (arch, e, 0, 0); shnum = grub_target_to_host (s->sh_size); if (shnum < SHN_LORESERVE) grub_util_error ("Invalid number of section header table entries in sh_size: %" PRIuGRUB_UINT64_T, (grub_uint64_t) shnum); @@ -176,7 +187,7 @@ get_shnum (const struct grub_module_verifier_arch *arch, Elf_Ehdr *e) } static Elf_Word -get_shstrndx (const struct grub_module_verifier_arch *arch, Elf_Ehdr *e) +get_shstrndx (const struct grub_module_verifier_arch *arch, Elf_Ehdr *e, size_t module_size) { Elf_Shdr *s; Elf_Word shstrndx; @@ -184,7 +195,7 @@ get_shstrndx (const struct grub_module_verifier_arch *arch, Elf_Ehdr *e) shstrndx = grub_target_to_host16 (e->e_shstrndx); if (shstrndx == SHN_XINDEX) { - s = get_shdr (arch, e, 0); + s = get_shdr (arch, e, 0, 0); shstrndx = grub_target_to_host (s->sh_link); if (shstrndx < SHN_LORESERVE) grub_util_error ("Invalid section header table index in sh_link: %d", shstrndx); @@ -199,28 +210,30 @@ get_shstrndx (const struct grub_module_verifier_arch *arch, Elf_Ehdr *e) } static Elf_Shdr * -find_section (const struct grub_module_verifier_arch *arch, Elf_Ehdr *e, const char *name) +find_section (const struct grub_module_verifier_arch *arch, Elf_Ehdr *e, const char *name, size_t module_size) { Elf_Shdr *s; const char *str; unsigned i; - s = get_shdr (arch, e, get_shstrndx (arch, e)); + s = get_shdr (arch, e, get_shstrndx (arch, e, module_size), module_size); str = (char *) e + grub_target_to_host (s->sh_offset); - for (i = 0, s = get_shdr (arch, e, 0); - i < get_shnum (arch, e); - i++, s = get_shdr (arch, e, i)) - if (strcmp (str + grub_target_to_host32 (s->sh_name), name) == 0) - return s; + for (i = 0; i < get_shnum (arch, e); i++) + { + s = get_shdr (arch, e, i, module_size); + + if (strcmp (str + grub_target_to_host32 (s->sh_name), name) == 0) + return s; + } return NULL; } static void check_license (const char * const filename, - const struct grub_module_verifier_arch *arch, Elf_Ehdr *e) + const struct grub_module_verifier_arch *arch, Elf_Ehdr *e, size_t module_size) { - Elf_Shdr *s = find_section (arch, e, ".module_license"); + Elf_Shdr *s = find_section (arch, e, ".module_license", module_size); if (s && (strcmp ((char *) e + grub_target_to_host(s->sh_offset), "LICENSE=GPLv3") == 0 || strcmp ((char *) e + grub_target_to_host(s->sh_offset), "LICENSE=GPLv3+") == 0 || strcmp ((char *) e + grub_target_to_host(s->sh_offset), "LICENSE=GPLv2+") == 0)) @@ -229,17 +242,19 @@ check_license (const char * const filename, } static Elf_Sym * -get_symtab (const struct grub_module_verifier_arch *arch, Elf_Ehdr *e, Elf_Word *size, Elf_Word *entsize) +get_symtab (const struct grub_module_verifier_arch *arch, Elf_Ehdr *e, Elf_Word *size, Elf_Word *entsize, size_t module_size) { unsigned i; Elf_Shdr *s; Elf_Sym *sym; - for (i = 0, s = get_shdr (arch, e, 0); - i < get_shnum (arch, e); - i++, s = get_shdr (arch, e, i)) - if (grub_target_to_host32 (s->sh_type) == SHT_SYMTAB) - break; + for (i = 0; i < get_shnum (arch, e); i++) + { + s = get_shdr (arch, e, i, module_size); + + if (grub_target_to_host32 (s->sh_type) == SHT_SYMTAB) + break; + } if (i == get_shnum (arch, e)) return NULL; @@ -267,7 +282,7 @@ is_whitelisted (const char *modname, const char **whitelist) static void check_symbols (const struct grub_module_verifier_arch *arch, Elf_Ehdr *e, const char *modname, - const char **whitelist_empty) + const char **whitelist_empty, size_t module_size) { Elf_Sym *sym; Elf_Word size, entsize; @@ -275,7 +290,7 @@ check_symbols (const struct grub_module_verifier_arch *arch, /* Module without symbol table and without .moddeps section is useless at boot time, so catch it early to prevent build errors */ - sym = get_symtab (arch, e, &size, &entsize); + sym = get_symtab (arch, e, &size, &entsize, module_size); if (!sym) { Elf_Shdr *s; @@ -287,7 +302,7 @@ check_symbols (const struct grub_module_verifier_arch *arch, if (is_whitelisted (modname, whitelist_empty)) return; - s = find_section (arch, e, ".moddeps"); + s = find_section (arch, e, ".moddeps", module_size); if (!s) grub_util_error ("%s: no symbol table and no .moddeps section", modname); @@ -342,13 +357,13 @@ is_symbol_local(Elf_Sym *sym) static void section_check_relocations (const char * const modname, const struct grub_module_verifier_arch *arch, void *ehdr, - Elf_Shdr *s, size_t target_seg_size) + Elf_Shdr *s, size_t target_seg_size, size_t module_size) { Elf_Rel *rel, *max; Elf_Sym *symtab; Elf_Word symtabsize, symtabentsize; - symtab = get_symtab (arch, ehdr, &symtabsize, &symtabentsize); + symtab = get_symtab (arch, ehdr, &symtabsize, &symtabentsize, module_size); if (!symtab) grub_util_error ("%s: relocation without symbol table", modname); @@ -424,30 +439,32 @@ section_check_relocations (const char * const modname, static void check_relocations (const char * const modname, - const struct grub_module_verifier_arch *arch, Elf_Ehdr *e) + const struct grub_module_verifier_arch *arch, Elf_Ehdr *e, size_t module_size) { Elf_Shdr *s; unsigned i; - for (i = 0, s = get_shdr (arch, e, 0); - i < get_shnum (arch, e); - i++, s = get_shdr (arch, e, i)) - if (grub_target_to_host32 (s->sh_type) == SHT_REL || grub_target_to_host32 (s->sh_type) == SHT_RELA) - { - Elf_Shdr *ts; + for (i = 0; i < get_shnum (arch, e); i++) + { + s = get_shdr (arch, e, i, module_size); - if (grub_target_to_host32 (s->sh_type) == SHT_REL && !(arch->flags & GRUB_MODULE_VERIFY_SUPPORTS_REL)) - grub_util_error ("%s: unsupported SHT_REL", modname); - if (grub_target_to_host32 (s->sh_type) == SHT_RELA && !(arch->flags & GRUB_MODULE_VERIFY_SUPPORTS_RELA)) - grub_util_error ("%s: unsupported SHT_RELA", modname); + if (grub_target_to_host32 (s->sh_type) == SHT_REL || grub_target_to_host32 (s->sh_type) == SHT_RELA) + { + Elf_Shdr *ts; - /* Find the target segment. */ - if (grub_target_to_host32 (s->sh_info) >= get_shnum (arch, e)) - grub_util_error ("%s: orphaned reloc section", modname); - ts = get_shdr (arch, e, grub_target_to_host32 (s->sh_info)); + if (grub_target_to_host32 (s->sh_type) == SHT_REL && !(arch->flags & GRUB_MODULE_VERIFY_SUPPORTS_REL)) + grub_util_error ("%s: unsupported SHT_REL", modname); + if (grub_target_to_host32 (s->sh_type) == SHT_RELA && !(arch->flags & GRUB_MODULE_VERIFY_SUPPORTS_RELA)) + grub_util_error ("%s: unsupported SHT_RELA", modname); - section_check_relocations (modname, arch, e, s, grub_target_to_host (ts->sh_size)); - } + /* Find the target segment. */ + if (grub_target_to_host32 (s->sh_info) >= get_shnum (arch, e)) + grub_util_error ("%s: orphaned reloc section", modname); + ts = get_shdr (arch, e, grub_target_to_host32 (s->sh_info), module_size); + + section_check_relocations (modname, arch, e, s, grub_target_to_host (ts->sh_size), module_size); + } + } } void @@ -488,17 +505,17 @@ SUFFIX(grub_module_verify) (const char * const filename, grub_util_error ("%s: ELF sections outside core", filename); } - check_license (filename, arch, e); + check_license (filename, arch, e, size); Elf_Shdr *s; const char *modname; - s = find_section (arch, e, ".modname"); + s = find_section (arch, e, ".modname", size); if (!s) grub_util_error ("%s: no module name found", filename); modname = (const char *) e + grub_target_to_host (s->sh_offset); - check_symbols(arch, e, modname, whitelist_empty); - check_relocations(modname, arch, e); + check_symbols(arch, e, modname, whitelist_empty, size); + check_relocations(modname, arch, e, size); } -- 2.27.0