Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1oy3K9-0004Zu-Jh for mharc-grub-devel@gnu.org; Wed, 23 Nov 2022 22:54:53 -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 1oy3K7-0004Zg-2n for grub-devel@gnu.org; Wed, 23 Nov 2022 22:54:51 -0500 Received: from mail-vi1eur05on2061.outbound.protection.outlook.com ([40.107.21.61] helo=EUR05-VI1-obe.outbound.protection.outlook.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oy3K4-0006sm-OY for grub-devel@gnu.org; Wed, 23 Nov 2022 22:54:50 -0500 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ahUz9MQPTOYXGHgv11kDjvOrgA69l3Crcf5Dm3cpW5CjMP949I8yuFOlJhuLIP0H87emvOZsHsWDAGe/lvGXiM/tdPa2pxzVCFFqi0wuDq/fWd90ddXWRSj66tyO+ViJRpPbN+hhJ+PMbR3p1at9bncN3nipMS3YIAbXJksxvYEyCg5xNFpADScOQkaCa7MixFTF+tHqC977sLlZ4cye0VRdXY3TWscsqbI7pHhBLoGpoEXOnbaE+wNjCEBlZQWTqB/ZhacydYBLreAAVut8OYAwTdEmNDLJn6BdKRXFE1hJofbVBK68+xwi3TMtkYZGyIFBjzk1GMErlWV4VNcFSw== 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=wC87mIOppHFW2xcvUQ+VI64M2W/WmVnNrP825spWD98=; b=Ap3+9R3gBSrMBspcLfDKVmXRduTtRAZ4cS11hDQgbEuialDLOh4dOfldMtpn12obONk7w9WPQiv6MxPKqSFrpOXMVRVzaJTk70ECrNElCq1rvTh4l6O18k/zmnKNeK6x0SV6OmELD8hX/N2Rvt7OWQZw0EQW3gg3pj/rETT1Zt+nk18f82bAkq6MwmjS5T7rXPgNkGmnnCxXRbJelxuCacIxq2AxrMAzxdvmImf7K9uHtvKmokyQaWByXChNvE71QOTN6eWhuqJh0FR9kMjeUO2e+9dWOGmMrbe5ICv7PDR6Ub0GH2CrCTATSAb5FokyIGwme8gRXFO07hrfpAiNjA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=suse.com; dmarc=pass action=none header.from=suse.com; dkim=pass header.d=suse.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=wC87mIOppHFW2xcvUQ+VI64M2W/WmVnNrP825spWD98=; b=jcziHrMXKXAYZyBhfhO93K76tU9Id6/lNqlxPQTghbRlbvrVQAeanMs1EHiizRQ/RkeD+FwAZ9kzERvpsSNx0+B8IRN9HJg9c7oMz5stStkJ/jFRYAuKf9MRd1Fv+G2cJb8kWYUUCEvKXILyQHo+e2Tqw4npR5QIBfMDSuPEYqNDC/25ymEP1EuWcr8GTQIdf3cUxGJRMISFiJ4ActMVshS2Yq+n7QYCR4vghI79CawvGo50JH+CELaFPb54CNZqwgXx80hZHq/gLYM1j3nqHuJBZ6C9NClwAM7I5Cjpi33+1ER5tO9I6fl9x8kuxakElW0+ejEguUhxsjQcC9TKAA== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=suse.com; Received: from DBBPR04MB6140.eurprd04.prod.outlook.com (2603:10a6:10:c7::18) by PA4PR04MB9485.eurprd04.prod.outlook.com (2603:10a6:102:27d::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5857.19; Thu, 24 Nov 2022 03:54:44 +0000 Received: from DBBPR04MB6140.eurprd04.prod.outlook.com ([fe80::c3d7:a3aa:ceaa:6acf]) by DBBPR04MB6140.eurprd04.prod.outlook.com ([fe80::c3d7:a3aa:ceaa:6acf%4]) with mapi id 15.20.5834.015; Thu, 24 Nov 2022 03:54:44 +0000 Date: Thu, 24 Nov 2022 11:54:28 +0800 From: Gary Lin To: The development of GNU GRUB , Daniel Kiper Subject: Re: [PATCH] loader: Ensure the newc pathname is NULL-terminated Message-ID: <20221124035428.vhntablvjatducmt@GaryLaptop> References: <20221123064021.7318-1-glin@suse.com> <20221123144454.ycs4imug346evrh7@tomti.i.net-space.pl> Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20221123144454.ycs4imug346evrh7@tomti.i.net-space.pl> X-ClientProxiedBy: TYAPR01CA0006.jpnprd01.prod.outlook.com (2603:1096:404::18) To DBBPR04MB6140.eurprd04.prod.outlook.com (2603:10a6:10:c7::18) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DBBPR04MB6140:EE_|PA4PR04MB9485:EE_ X-MS-Office365-Filtering-Correlation-Id: f877eee9-9a94-48fb-226c-08dacdcf9ef2 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: h4L6gWrXHHiD6rA8K2psu2/suX/2nyR/adep8DlJtGiwvCQQiR4DffrjQw4IXMuHxL66ANwQ8sMcB96BJJILl8lRMHaGipHsA77ZLDhaaYtAOV+P/PdjgCR7+7ix7/XfN7xbRFJm8nBMa1Lnf0G72nWaqo2HGwoNGa7y23EtfZSh+0OG0567OO4WibPFc5clYbKptz4YiubeVkmeyxEdHqUUAT//ml2NR6muieQRHS3uXU1xm7ti7z1CW2uL+y5FD4qdlARoIfT7N4NbZxpSn0RGQ3fq1R7s22FCFP8gNqekxGPJ8+G0YCqmMkYKNvx18cetb+Ep7uHjdnKWQPNlYfb2AeMqNwMftKbTXfwF5wAc7KKh+Je6FRH4PRLK6AMF7upzFGMC0AVk5pewZbKg78fAxkSXyKZggt/WbremgaSVH1LWCk6xtCF/QNvOUGA0wr0D1kVrOxvEupVfSCy0bZYUM+oXv/R+eUQ4sMi8EQ75eBr17CpiihIp52VAYo8Tw2yHtWPE0BbGF4r7yydc7q/8upMHVf38zyydBZfIbvy7wOv5IaMX6lrlZMK32ezrXv+IksA46Pr9m87ENu3cI8CgRw6+57tkZRZr0ERn3V5nsD8K3w5ohb94ACvUooJKJA8OunnhAcodHP9tWPGHPYKX0K8f43d7k9Xmq1JCmdU= X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:DBBPR04MB6140.eurprd04.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230022)(7916004)(39860400002)(346002)(366004)(396003)(136003)(376002)(451199015)(86362001)(38100700002)(8676002)(41300700001)(66476007)(66946007)(66556008)(110136005)(966005)(316002)(5660300002)(3716004)(33716001)(8936002)(6486002)(2906002)(83380400001)(6666004)(6506007)(478600001)(186003)(1076003)(26005)(9686003)(6512007); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?T9NfsElDIecCsBO+BFdmdZU26qtytHM8GIQi5E/Vt7Ud/NWgsIRRuJGgZd8e?= =?us-ascii?Q?WF5Q+a43CxPKaaYZob8XaaXyTs+rbBChaZ53t8Z9XK5A7YXKMxEXlGjUN2Cx?= =?us-ascii?Q?bq2hI5yjanTIoa0UbUN3m1GkGbHAjZJDgvaJdOkT2gmFQgShzSwT4D0oSHGj?= =?us-ascii?Q?x9BrVmtkDNaM6KBIB2wUqPIdoMp1g1SelbvPj3/cWtV/nWWjX2QNbsv1bXvo?= =?us-ascii?Q?kC70BzpZWb610HqEsG6GU8a9ik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?5YLDE5gTjXJgaQE9P+Jw9nhbTS8bLsxZPKxrDYtT1viIPGph8PjCN5MKnYzM?= =?us-ascii?Q?mAlGag/+aRBi0R6NnSjzwvYrkntiu4LyPMCwvBn8WEmAH1SIN9LK0jOjXK1a?= =?us-ascii?Q?pKYSkpXpPGDyw1CDR95xm+nNVgZRnhinFyscvt4QZxTCIfnyg/ZABAY8Llhw?= =?us-ascii?Q?JDDrxOyldWcRg3m5oSrXHPdsa7Vc9STqhLVY+iGUu/ruW4MYPUyMsjKbyWLs?= =?us-ascii?Q?p6VmylwJggWuGovzuWku6GXa9HNiXYqI2BqYCL+uFSk3b1SEiSFnF8T/sOMV?= =?us-ascii?Q?Cdm0hHuESYRKANSBTORt2Oo5sqik5TeGoV10dJPNRmiOOKWKKBp1v5GxgwlZ?= =?us-ascii?Q?Gc2v4sXpZ+B91Al5zV1+sdZr2KkjBlbAptQI2BB9AB2bJidTgC8g3TyQt7GE?= =?us-ascii?Q?wc8/uAc2eDiZ7iqrN5c=3D?= X-OriginatorOrg: suse.com X-MS-Exchange-CrossTenant-Network-Message-Id: f877eee9-9a94-48fb-226c-08dacdcf9ef2 X-MS-Exchange-CrossTenant-AuthSource: DBBPR04MB6140.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Nov 2022 03:54:43.7751 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: f7a17af6-1c5c-4a36-aa8b-f5be247aa4ba X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: WlcEU97WAkmutL4zDmhAlKxwGXuKj0ieZqgZsC3s1k4pvwcaVvCBMG4Zx5opMvHI X-MS-Exchange-Transport-CrossTenantHeadersStamped: PA4PR04MB9485 Received-SPF: pass client-ip=40.107.21.61; envelope-from=GLin@suse.com; helo=EUR05-VI1-obe.outbound.protection.outlook.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 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_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_PASS=-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, 24 Nov 2022 03:54:51 -0000 On Wed, Nov 23, 2022 at 03:44:54PM +0100, Daniel Kiper wrote: > On Wed, Nov 23, 2022 at 02:40:21PM +0800, Gary Lin via Grub-devel wrote: > > Per "man 5 cpio", the namesize in the cpio header includes the trailing > > NULL byte of the pathname and the pathname is followed by NULL bytes, but > > s/NULL/NUL/ and below please... > Will fix in v2. > > the current implementation excludes the trailing NULL byte when making > > the newc header plus the pathname. Although make_header() would pad the > > pathname string, the padding won't happen when strlen(name) + > > sizeof(struct newc_head) is a multiple of 4, and the non-NULL-terminated > > pathname may lead to unexpected results. > > > > For example, when loading linux with the following command: > > > > linux /boot/vmlinuz > > initrd newc:test12:/boot/test12 /boot/initrd > > > > Since strlen("test12") + sizeof(struct newc_head) is 116 = 29 * 4, there > > is no padding for the pathname, and the following error may show during > > linux boot: > > > > Initramfs unpacking failed: ZSTD-compressed data is trunc > > > > To avoid the potential problems, this commit includes the NULL byte when > > calling make_header() and adjusts the initrd size accordingly. > > > > Signed-off-by: Gary Lin > > --- > > grub-core/loader/linux.c | 16 +++++++++------- > > 1 file changed, 9 insertions(+), 7 deletions(-) > > > > diff --git a/grub-core/loader/linux.c b/grub-core/loader/linux.c > > index 830360172..2b26d293f 100644 > > --- a/grub-core/loader/linux.c > > +++ b/grub-core/loader/linux.c > > @@ -127,12 +127,14 @@ insert_dir (const char *name, struct dir **root, > > n->name = grub_strndup (cb, ce - cb); > > if (ptr) > > { > > + char *tmp_name = grub_strndup (name, ce - name); > > Why is this needed? Because I need the extra '\0' for make_header(). For example, if "name" is "dir123/file", insert_dir() only sends 'd''i''r''1''2''3' to make_header() to create the header like this: 00000070 30 37 30 37 30 31 32 31 38 33 32 41 37 43 30 30 |07070121832A7C00| 00000080 30 30 34 31 45 44 30 30 30 30 30 33 45 38 30 30 |0041ED000003E800| 00000090 30 30 30 30 36 34 30 30 30 30 30 30 30 33 36 33 |0000640000000363| 000000a0 37 37 32 44 43 38 30 30 30 30 30 30 30 30 30 30 |772DC80000000000| 000000b0 30 30 30 30 30 38 30 30 30 30 30 30 31 33 30 30 |0000080000001300| 000000c0 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 |0000000000000000| 000000d0 30 30 30 30 30 36 30 30 30 30 30 30 30 30 64 69 |00000600000000di| 000000e0 72 31 32 33 30 37 30 37 30 31 31 31 34 44 37 46 |r123070701114D7F| ^ end of "dir123" As you can see, the pathname is ended without '\0', and it violates the cpio format. If we give make_header() the extra '\0': 00000070 30 37 30 37 30 31 32 31 38 33 32 41 37 43 30 30 |07070121832A7C00| 00000080 30 30 34 31 45 44 30 30 30 30 30 33 45 38 30 30 |0041ED000003E800| 00000090 30 30 30 30 36 34 30 30 30 30 30 30 30 33 36 33 |0000640000000363| 000000a0 37 37 32 44 43 38 30 30 30 30 30 30 30 30 30 30 |772DC80000000000| 000000b0 30 30 30 30 30 38 30 30 30 30 30 30 31 33 30 30 |0000080000001300| 000000c0 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 |0000000000000000| 000000d0 30 30 30 30 30 37 30 30 30 30 30 30 30 30 64 69 |00000700000000di| 000000e0 72 31 32 33 00 00 00 00 30 37 30 37 30 31 31 31 |r123....07070111| There are 3 more NUL bytes appended, and the user can safely read the pathname without a further check. > And if it is needed you have to check for NULL here. Okay. Will add the check in v2. > I do not mention that formatting is broken too. You use spaces instead > of tabs here and below. > I thought the indentation is 2-space without tab. Will convert 8 spaces into one tab. > > grub_dprintf ("linux", "Creating directory %s, %s\n", name, ce); > > - ptr = make_header (ptr, name, ce - name, > > + ptr = make_header (ptr, tmp_name, ce - name + 1, > > 040777, 0); > > + grub_free (tmp_name); > > } > > if (grub_add (*size, > > - ALIGN_UP ((ce - (char *) name) > > + ALIGN_UP ((ce - (char *) name + 1) > > + sizeof (struct newc_head), 4), > > size)) > > { > > @@ -191,7 +193,7 @@ grub_initrd_init (int argc, char *argv[], > > grub_initrd_close (initrd_ctx); > > return grub_errno; > > } > > - name_len = grub_strlen (initrd_ctx->components[i].newc_name); > > + name_len = grub_strlen (initrd_ctx->components[i].newc_name) + 1; > > if (grub_add (initrd_ctx->size, > > ALIGN_UP (sizeof (struct newc_head) + name_len, 4), > > &initrd_ctx->size) || > > @@ -205,7 +207,7 @@ grub_initrd_init (int argc, char *argv[], > > { > > if (grub_add (initrd_ctx->size, > > ALIGN_UP (sizeof (struct newc_head) > > - + sizeof ("TRAILER!!!") - 1, 4), > > + + sizeof ("TRAILER!!!"), 4), > > This change looks strange and begs for explanation in the patch. > The adjustment is to match the change of "TRAILER!!!" below. > > &initrd_ctx->size)) > > goto overflow; > > free_dir (root); > > @@ -233,7 +235,7 @@ grub_initrd_init (int argc, char *argv[], > > initrd_ctx->size = ALIGN_UP (initrd_ctx->size, 4); > > if (grub_add (initrd_ctx->size, > > ALIGN_UP (sizeof (struct newc_head) > > - + sizeof ("TRAILER!!!") - 1, 4), > > + + sizeof ("TRAILER!!!"), 4), > > Ditto and bellow... > > > &initrd_ctx->size)) > > goto overflow; > > free_dir (root); > > @@ -304,7 +306,7 @@ grub_initrd_load (struct grub_linux_initrd_context *initrd_ctx, > > } > > else if (newc) > > { > > - ptr = make_header (ptr, "TRAILER!!!", sizeof ("TRAILER!!!") - 1, > > + ptr = make_header (ptr, "TRAILER!!!", sizeof ("TRAILER!!!"), > > 0, 0); The "TRAILER!!!" header should count the NUL byte, not ignore it. Gary Lin > > free_dir (root); > > root = 0; > > @@ -327,7 +329,7 @@ grub_initrd_load (struct grub_linux_initrd_context *initrd_ctx, > > { > > grub_memset (ptr, 0, ALIGN_UP_OVERHEAD (cursize, 4)); > > ptr += ALIGN_UP_OVERHEAD (cursize, 4); > > - ptr = make_header (ptr, "TRAILER!!!", sizeof ("TRAILER!!!") - 1, 0, 0); > > + ptr = make_header (ptr, "TRAILER!!!", sizeof ("TRAILER!!!"), 0, 0); > > Daniel > > _______________________________________________ > Grub-devel mailing list > Grub-devel@gnu.org > https://lists.gnu.org/mailman/listinfo/grub-devel