Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1nFPyE-0001Iq-AQ for mharc-grub-devel@gnu.org; Wed, 02 Feb 2022 19:27:30 -0500 Received: from eggs.gnu.org ([209.51.188.92]:33444) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nFPyB-0001BW-PZ for grub-devel@gnu.org; Wed, 02 Feb 2022 19:27:27 -0500 Received: from mx0a-00069f02.pphosted.com ([205.220.165.32]:2272) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nFPy9-0006x7-Da for grub-devel@gnu.org; Wed, 02 Feb 2022 19:27:27 -0500 Received: from pps.filterd (m0246629.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.16.1.2/8.16.1.2) with SMTP id 212Kwj6q009891 for ; Thu, 3 Feb 2022 00:27:24 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : content-type : mime-version; s=corp-2021-07-09; bh=V0AFePETImjG9YL/SzMOaKs2iamOEglFDdFUuPbTKU8=; b=GNww6T22GHpSwCzByBUV0jQZTSssLl/F1NhIzfqAT3VaFKtPUIErkOE1TJUMl4aoaQpo ZpE1o7mO0V21/Z5+IoNG4VW1E6J3A75iPj/1CcV+QC872vHMBz8FghAP5erP+X5SvZxD jJbUjL4bS6px9OpkGhosdHjGr94dlIUbKGD/Xa7AUCLzSn5oHKo90bzhyjW7Qv9Og11k lkNH/KUoVdZz0/rn3gCBc5fVzAkJV2J7BwyGU/SMEIwBVSuq6RhmQojTVZ4YoCHUE4b+ PgTxZJOOjuSHGGo0H1z0iyMb1ew+S+7vrotyUbFuHvPVEFWHTNJYHWvAqkeJ2mPtrTPI Bw== Received: from userp3020.oracle.com (userp3020.oracle.com [156.151.31.79]) by mx0b-00069f02.pphosted.com with ESMTP id 3dxjac7nuy-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK) for ; Thu, 03 Feb 2022 00:27:23 +0000 Received: from pps.filterd (userp3020.oracle.com [127.0.0.1]) by userp3020.oracle.com (8.16.1.2/8.16.1.2) with SMTP id 2130GEH7176038 for ; Thu, 3 Feb 2022 00:27:22 GMT Received: from nam04-dm6-obe.outbound.protection.outlook.com (mail-dm6nam08lp2042.outbound.protection.outlook.com [104.47.73.42]) by userp3020.oracle.com with ESMTP id 3dvy1tm03q-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK) for ; Thu, 03 Feb 2022 00:27:22 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=dFAD1+43KaQ+LaZeQ0OX+/PuFSC3dqSt+RYMcd06JvNCNEE3AjxoyXgjhaudjBOn55Zf1ipgSHlpeaiCq5jSa0HqtzSSQlA2LDqaQV1a0HmDfgMgvosR9XAgT59pyoS8nGdiW7Uh2/lhr0MccGbDAO9uHYZw22Okz609F96wgtWl+9yhaaRo0I1k4ceNf2fhsiYRdCNs+7WgbcbPyPYwTArixWjk7GJcKPU0Bue/38zKjL3FveCDQhdEWiSaH7kmm2/VdL0EobqMKse2XX13Y4pir0sz62cY4DXmGFCVV/pz/5g9ZFUiZBNbHfz2X7K37ftNWI8tNEhJeYk40amOxQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=V0AFePETImjG9YL/SzMOaKs2iamOEglFDdFUuPbTKU8=; b=m/HL1pwH87cP0u5NgYu1C30n5XuxvqndmbGgAw4ejOs14dfegeTDJio0HYrf6k9C93hgDuQq4WpG4szDYr2bu2HT4wik5gEPvK4ZrohSlcxZOgtl5jBv9kxBtnT7i+Yu2hoGRykvR/ziIEEvgtC/p7fiaDzwuiu9dqDalx5GzAX+MUtynl0Uhc1kv0MWN9m1Opj8S9cqfRHWvxofC194uMbILrYXbg3W+rhob0eXrKdQFvCpNlkxBYVecXvfawy1FYYm+vIEMm/xVWYjmH9GkWOy0FpmnM8zcQdJZFWpWowAltG2D7hd9YVevPlgJ0a0wDztZVcBybWhw6xdH1ihpQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.onmicrosoft.com; s=selector2-oracle-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=V0AFePETImjG9YL/SzMOaKs2iamOEglFDdFUuPbTKU8=; b=DJME6c8+JYuFydal2nA9aa7/Yu64L/JKL3z1Etr6gq84kJ8G/3eIuhp5JIL2Yqze5UywSwjBOGO16MOWecYvmVSIPm9H0n6LRHC8aLi75YI5Ms7T5ruK+JMgA1MnhiUkA2sUR321Wuf0xyt0XoM5l7BuppwZCv+qbUCGJbSF+VI= Received: from DM6PR10MB2986.namprd10.prod.outlook.com (2603:10b6:5:6b::26) by MW4PR10MB5884.namprd10.prod.outlook.com (2603:10b6:303:18f::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4951.12; Thu, 3 Feb 2022 00:27:20 +0000 Received: from DM6PR10MB2986.namprd10.prod.outlook.com ([fe80::29f1:b031:153c:c9f9]) by DM6PR10MB2986.namprd10.prod.outlook.com ([fe80::29f1:b031:153c:c9f9%5]) with mapi id 15.20.4951.012; Thu, 3 Feb 2022 00:27:20 +0000 From: Alec Brown To: grub-devel@gnu.org Cc: daniel.kiper@oracle.com, darren.kenny@oracle.com Subject: [PATCH 2/4] util/grub-module-verifierXX.c: Validate number of elf section header table entries Date: Wed, 2 Feb 2022 19:26:58 -0500 Message-Id: <1643848020-8197-3-git-send-email-alec.r.brown@oracle.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1643848020-8197-1-git-send-email-alec.r.brown@oracle.com> References: <1643848020-8197-1-git-send-email-alec.r.brown@oracle.com> Content-Type: text/plain X-ClientProxiedBy: MN2PR01CA0059.prod.exchangelabs.com (2603:10b6:208:23f::28) To DM6PR10MB2986.namprd10.prod.outlook.com (2603:10b6:5:6b::26) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 390b7f0a-08f1-489d-3cc9-08d9e6abf098 X-MS-TrafficTypeDiagnostic: MW4PR10MB5884:EE_ X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:2887; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: j2/ugOhFDo4/1BC39Jixm60ghMZwSvoQBsaZGIHSM1Pboa4TH03vZv6Ka5rDE6UkbdVbFHNB+BhDOW/+I5+y9JvybezsB7to8CKGXy+8dmnxPs7ptWOHtu1UG52PWezLzEQCzrGHW7BOC09xChFuKlSkpdehXIA0pkcDg0rcwcZWOnmgoLG8dDUy41E8hdAmHqHBQFFj3ITMxms6R7qoqDvQG6mGTln9OYDEE+anD2l2RQbN5equJ2swo0CloM9Vy5UwSCYjIw68QFTFeTRyPD6BKrMKgukKUbwBOGIhoHABz0M5XTuwko/ixT3evfQIoIKoMHwUAvdLxe2BI0CE5zE1+yFJNHeCU72y3uBW7wL0RaL8gTiAVXPCmL8UpnWo9STIlz0cQp+vPOzuASEMKw++cVsCEi1XuIWJq19ciH1KEP4dSBaEuOy/ZbF4U/jYxIyfDdnq9nd/IDLJpmRc4DEz8AfyxuSJzSKr3DNFOFZ9az4N4NOLWgSAQYS5emIufTIxGrNGUrH73qTIGUeGCE4Q1fC5xgjTRgmcyCcfqmM+S83YiKG3YfwS+tBqBeb7EfWr08zP+EsPJN7DnMM2WAIOWeiZU8CjivWy95/fju3CIAUJixIUAqDP+NRkk6WlkHv/ciwhZIRPmsoTSEW8CQ== X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:DM6PR10MB2986.namprd10.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230001)(366004)(36756003)(2616005)(2906002)(508600001)(38100700002)(6486002)(316002)(6916009)(8936002)(8676002)(66476007)(6666004)(6512007)(66556008)(83380400001)(186003)(26005)(4326008)(6506007)(5660300002)(107886003)(66946007)(86362001); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?NKbUN1CQ7k/q6tX70WSblwG1Brf7Vjh4rW1DmufLH8slsn6iaYWYqYL4NSie?= =?us-ascii?Q?K+TSLIqnVErIMd6mf6Y0ECBMXevm3VthV3+ldwPRw8xXss+Dgh9rlT6Ey5mT?= =?us-ascii?Q?S5a82UC8yZRaUY3SLZR2YPGiA4/YxwD/yzYJG6QVJEyYRzQlL011a5GIv1yM?= =?us-ascii?Q?/OoKPlmoSiVhRdGIE2ZdzjDLqVw8qkruO7Lr1TuCODdSkelbAInNxxU48Nq8?= =?us-ascii?Q?xNVMcWBiX6w3i5sUB25HVx+Pj6WzeOis59iUe7WH1G2EbAx9XDySKaZbtPw9?= =?us-ascii?Q?yMEzUhc6kFkjODj8dix3j6E7u3ohDDMXRAbj/Vg7dlMBAbUUdoeEt+9dJJfh?= =?us-ascii?Q?Dyfw15ppcsJQYK9IC2agwCXyW520CrMOedzqZVh36d/WHvTHHWOBN2X//kJP?= =?us-ascii?Q?dKOxfW4LEhl6lSuz+UhZQ9yOlGt6ZyskEyVwFJ9SxJjJoLgYVOfVrd14rL8k?= =?us-ascii?Q?HaSF7R6TM8ib2akDAOoUZiuoAAGqYPKVd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Q?SqG2Q0tc9NXD4MF+AohWALCjqIOrMDxQyxVuCxXlRfgl0hCNH48j/iPMlauP?= =?us-ascii?Q?bvhNB79f8nWC98H0czv3Bp4I4BZec9DiDW2/L6ARy0cgXBL1Ms5xLDl53Vy3?= =?us-ascii?Q?wUZlz7JYm/stBhBSmheo8QREtN9gQRPdmy5qBdI//lYKVMYgQeUM8eYx5lf1?= =?us-ascii?Q?pU2wXimmxA6LJtx163CfEyyuYUt0dySpx/cCRGIiYM8V6PIuxcR+xUSjc5i7?= =?us-ascii?Q?OrYYO3XqJPu75lFnqVf7HA0=3D?= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 390b7f0a-08f1-489d-3cc9-08d9e6abf098 X-MS-Exchange-CrossTenant-AuthSource: DM6PR10MB2986.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 03 Feb 2022 00:27:20.1659 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 4e2c6054-71cb-48f1-bd6c-3a9705aca71b X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: +Rciqi/K3DRYEDmAbfKRJ2kb5Sb21cMi4VNwvIzSFxtvS8EGeBFxE5jMvOzTuPdtiBlpTvtQxYKYaQZ0L+8z4A== X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW4PR10MB5884 X-Proofpoint-Virus-Version: vendor=nai engine=6300 definitions=10246 signatures=673430 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=0 mlxscore=0 spamscore=0 bulkscore=0 adultscore=0 mlxlogscore=999 phishscore=0 malwarescore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2201110000 definitions=main-2202030000 X-Proofpoint-GUID: NAw78jTh4ds6AwHP4OtdbxV9-xq1XHwd X-Proofpoint-ORIG-GUID: NAw78jTh4ds6AwHP4OtdbxV9-xq1XHwd Received-SPF: pass client-ip=205.220.165.32; envelope-from=alec.r.brown@oracle.com; helo=mx0a-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, 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_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 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, 03 Feb 2022 00:27:28 -0000 In grub-module-verifierXX.c, grub_target_to_host16 (e->e_shnum) is used to obtain the number of section header table entries, but it wasn't being checked if the value was there. According to the elf(5) manual page, "If the number of entries in the section header table is larger than or equal to SHN_LORESERVE (0xff00), e_shnum holds the value zero and the real number of entries in the section header table is held in the sh_size member of the intial entry in section header table. Otherwise, the sh_size member of the initial entry in the section header table holds the value zero." Since this check wasn't being made, the function get_shnum() is being added to make this check and use whichever member doesn't have a value of zero. If both are zero, then we must return an error. We also need to make sure that e_shnum doesn't have a value greater than or equal to SHN_LORESERVE and sh_size isn't less than SHN_LORESERVE. Note that it may look as though the argument *arch isn't being used, it's actually required in order to use the macros grub_target_to_host*(x) which are unwinded to grub_target_to_host*_real(arch, (x)) based on defines earlier in the file. Fixes: CID 314021 Fixes: CID 314027 Fixes: CID 314033 Signed-off-by: Alec Brown --- util/grub-module-verifierXX.c | 35 +++++++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/util/grub-module-verifierXX.c b/util/grub-module-verifierXX.c index 92f9ae2a4..61b82141f 100644 --- a/util/grub-module-verifierXX.c +++ b/util/grub-module-verifierXX.c @@ -138,6 +138,29 @@ get_shdr (const struct grub_module_verifier_arch *arch, Elf_Ehdr *e, Elf_Word in index * grub_target_to_host16 (e->e_shentsize)); } +static Elf_Word +get_shnum (const struct grub_module_verifier_arch *arch, Elf_Ehdr *e) +{ + Elf_Shdr *s; + Elf_Word shnum; + + shnum = grub_target_to_host16 (e->e_shnum); + if (shnum == SHN_UNDEF) + { + s = get_shdr (arch, e, 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: %d", shnum); + } + else + { + if (shnum >= SHN_LORESERVE) + grub_util_error ("Invalid number of section header table entries in e_shnum: %d", shnum); + } + + return shnum; +} + static Elf_Shdr * find_section (const struct grub_module_verifier_arch *arch, Elf_Ehdr *e, const char *name) { @@ -149,7 +172,7 @@ find_section (const struct grub_module_verifier_arch *arch, Elf_Ehdr *e, const c str = (char *) e + grub_target_to_host (s->sh_offset); for (i = 0, s = get_shdr (arch, e, 0); - i < grub_target_to_host16 (e->e_shnum); + 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; @@ -176,12 +199,12 @@ get_symtab (const struct grub_module_verifier_arch *arch, Elf_Ehdr *e, Elf_Word Elf_Sym *sym; for (i = 0, s = get_shdr (arch, e, 0); - i < grub_target_to_host16 (e->e_shnum); + i < get_shnum (arch, e); i++, s = get_shdr (arch, e, i)) if (grub_target_to_host32 (s->sh_type) == SHT_SYMTAB) break; - if (i == grub_target_to_host16 (e->e_shnum)) + if (i == get_shnum (arch, e)) return NULL; sym = (Elf_Sym *) ((char *) e + grub_target_to_host (s->sh_offset)); @@ -370,7 +393,7 @@ check_relocations (const char * const modname, unsigned i; for (i = 0, s = get_shdr (arch, e, 0); - i < grub_target_to_host16 (e->e_shnum); + 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) { @@ -382,7 +405,7 @@ check_relocations (const char * const modname, grub_util_error ("%s: unsupported SHT_RELA", modname); /* Find the target segment. */ - if (grub_target_to_host32 (s->sh_info) >= grub_target_to_host16 (e->e_shnum)) + 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)); @@ -423,7 +446,7 @@ SUFFIX(grub_module_verify) (const char * const filename, /* Make sure that every section is within the core. */ if (size < grub_target_to_host (e->e_shoff) - + (grub_uint32_t) grub_target_to_host16 (e->e_shentsize) * grub_target_to_host16(e->e_shnum)) + + (grub_uint32_t) grub_target_to_host16 (e->e_shentsize) * get_shnum (arch, e)) { grub_util_error ("%s: ELF sections outside core", filename); } -- 2.27.0