From patchwork Fri Jan 7 13:38:09 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Disseldorp X-Patchwork-Id: 12706694 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id F2241C433F5 for ; Fri, 7 Jan 2022 13:38:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1347614AbiAGNiZ (ORCPT ); Fri, 7 Jan 2022 08:38:25 -0500 Received: from smtp-out1.suse.de ([195.135.220.28]:50226 "EHLO smtp-out1.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238475AbiAGNiY (ORCPT ); Fri, 7 Jan 2022 08:38:24 -0500 Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out1.suse.de (Postfix) with ESMTP id 34A5321115; Fri, 7 Jan 2022 13:38:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1641562703; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=1ly1qWDMyzaC9LSjWtQrvzZF4gUZP1qfdd/b/C0c1M4=; b=cCl5fFhI+LwE6v389Eki8Fgw4d9hux1WeGjY85cikjygjb4gjKKob+lwYHzsNOFeGdvSnk Ez1gA6g2aBOi4dxGm7hFn6NG30z5Z1wH1EQ7eqN6pHhBmMSt1lqUXULhVUTWs8IEDXwBDk zXRp19RedPMHW1uRjQiQqjmUDQNyJEI= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1641562703; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=1ly1qWDMyzaC9LSjWtQrvzZF4gUZP1qfdd/b/C0c1M4=; b=HQSNZqVUH1pbY+34kDStkqoIdVL9AU10gn3GHzMSgXGKAK1RrpWuxSNEnMICM0yQOd2mjd /TXBRDgJnwRjkODg== Received: from echidna.suse.de (ddiss.udp.ovpn2.nue.suse.de [10.163.47.146]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by relay2.suse.de (Postfix) with ESMTPS id F2171A3B87; Fri, 7 Jan 2022 13:38:22 +0000 (UTC) From: David Disseldorp To: linux-fsdevel@vger.kernel.org Cc: Martin Wilck , viro@zeniv.linux.org.uk, willy@infradead.org, David Disseldorp Subject: [PATCH v6 1/6] initramfs: refactor do_header() cpio magic checks Date: Fri, 7 Jan 2022 14:38:09 +0100 Message-Id: <20220107133814.32655-2-ddiss@suse.de> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20220107133814.32655-1-ddiss@suse.de> References: <20220107133814.32655-1-ddiss@suse.de> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org do_header() is called for each cpio entry and fails if the first six bytes don't match "newc" magic. The magic check includes a special case error message if POSIX.1 ASCII (cpio -H odc) magic is detected. This special case POSIX.1 check can be nested under the "newc" mismatch code path to avoid calling memcmp() twice in a non-error case. Signed-off-by: David Disseldorp Reviewed-by: Martin Wilck Acked-by: Christian Brauner --- init/initramfs.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/init/initramfs.c b/init/initramfs.c index 2f3d96dc3db6..2f79b3ec0b40 100644 --- a/init/initramfs.c +++ b/init/initramfs.c @@ -257,12 +257,11 @@ static int __init do_collect(void) static int __init do_header(void) { - if (memcmp(collected, "070707", 6)==0) { - error("incorrect cpio method used: use -H newc option"); - return 1; - } if (memcmp(collected, "070701", 6)) { - error("no cpio magic"); + if (memcmp(collected, "070707", 6) == 0) + error("incorrect cpio method used: use -H newc option"); + else + error("no cpio magic"); return 1; } parse_header(collected); From patchwork Fri Jan 7 13:38:10 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Disseldorp X-Patchwork-Id: 12706695 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id C0454C433EF for ; Fri, 7 Jan 2022 13:38:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238904AbiAGNiZ (ORCPT ); Fri, 7 Jan 2022 08:38:25 -0500 Received: from smtp-out1.suse.de ([195.135.220.28]:50240 "EHLO smtp-out1.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238922AbiAGNiY (ORCPT ); Fri, 7 Jan 2022 08:38:24 -0500 Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out1.suse.de (Postfix) with ESMTP id 67D2821126; Fri, 7 Jan 2022 13:38:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1641562703; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=AS/16BCZa/oBRlH7qsbsbsMGtE7/cqCGGMo4B/o4Rjs=; b=iidlWejSJpecSonRfzBkps0u0zdcuoqQxbbfjtLVyHWTtbIwg15E6yRi6c1xWWthjHmK8b tuap/m7majxp504aD92T7AoU7bB5QVWMMwU/J/EzkNfYE84CINfVtxCjmWRi/3DsTRpxeU oFBc8EWM4ZyBcGPtzZiOPrHEssxuS1E= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1641562703; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=AS/16BCZa/oBRlH7qsbsbsMGtE7/cqCGGMo4B/o4Rjs=; b=saUCHHPOKPXFvSGn+i6JIX6N1IoOJ87k/ZBceYsZKzRv6kbut24OK2UYtVZn6FBjajT8lc ZPytU7CXJaGbhyBQ== Received: from echidna.suse.de (ddiss.udp.ovpn2.nue.suse.de [10.163.47.146]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by relay2.suse.de (Postfix) with ESMTPS id 3CF6EA3B8B; Fri, 7 Jan 2022 13:38:23 +0000 (UTC) From: David Disseldorp To: linux-fsdevel@vger.kernel.org Cc: Martin Wilck , viro@zeniv.linux.org.uk, willy@infradead.org, David Disseldorp Subject: [PATCH v6 2/6] initramfs: make dir_entry.name a flexible array member Date: Fri, 7 Jan 2022 14:38:10 +0100 Message-Id: <20220107133814.32655-3-ddiss@suse.de> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20220107133814.32655-1-ddiss@suse.de> References: <20220107133814.32655-1-ddiss@suse.de> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org dir_entry.name is currently allocated via a separate kstrdup(). Change it to a flexible array member and allocate it along with struct dir_entry. Signed-off-by: David Disseldorp Acked-by: Christian Brauner --- init/initramfs.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/init/initramfs.c b/init/initramfs.c index 2f79b3ec0b40..656d2d71349f 100644 --- a/init/initramfs.c +++ b/init/initramfs.c @@ -130,17 +130,20 @@ static long __init do_utime(char *filename, time64_t mtime) static __initdata LIST_HEAD(dir_list); struct dir_entry { struct list_head list; - char *name; time64_t mtime; + char name[]; }; static void __init dir_add(const char *name, time64_t mtime) { - struct dir_entry *de = kmalloc(sizeof(struct dir_entry), GFP_KERNEL); + size_t nlen = strlen(name) + 1; + struct dir_entry *de; + + de = kmalloc(sizeof(struct dir_entry) + nlen, GFP_KERNEL); if (!de) panic_show_mem("can't allocate dir_entry buffer"); INIT_LIST_HEAD(&de->list); - de->name = kstrdup(name, GFP_KERNEL); + strscpy(de->name, name, nlen); de->mtime = mtime; list_add(&de->list, &dir_list); } @@ -151,7 +154,6 @@ static void __init dir_utime(void) list_for_each_entry_safe(de, tmp, &dir_list, list) { list_del(&de->list); do_utime(de->name, de->mtime); - kfree(de->name); kfree(de); } } From patchwork Fri Jan 7 13:38:11 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Disseldorp X-Patchwork-Id: 12706696 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 904AFC4332F for ; Fri, 7 Jan 2022 13:38:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238903AbiAGNi0 (ORCPT ); Fri, 7 Jan 2022 08:38:26 -0500 Received: from smtp-out1.suse.de ([195.135.220.28]:50252 "EHLO smtp-out1.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238929AbiAGNiY (ORCPT ); Fri, 7 Jan 2022 08:38:24 -0500 Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out1.suse.de (Postfix) with ESMTP id 9BE2E2112A; Fri, 7 Jan 2022 13:38:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1641562703; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=84U1vFcv8GwovCdJ5zJl7DCbYhAEqk1UpP0RwFHjsj4=; b=LkwVIoS8l5f3q81JjkJDPcaTvWjArl5USHmL4NKhXT3vFWTt7B0x2jennxNVoWb6Uv+mJU Iv2Ts96XiLgb3YdAAEO1zAJRyH+kBLSZzuNj9Z1G7MFhqF+GK1d8buwIeg28UViHV4RSPV danKADOik/dd8aRVuHApyZWF0Xfig2I= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1641562703; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=84U1vFcv8GwovCdJ5zJl7DCbYhAEqk1UpP0RwFHjsj4=; b=07cbuf5A8ieBgur20NZsjL344C51yd+tWLw1dq5gWAKz+Psa75H4cxQnG5b2D7oii/ZLbH rWzx0nHfwWoemECQ== Received: from echidna.suse.de (ddiss.udp.ovpn2.nue.suse.de [10.163.47.146]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by relay2.suse.de (Postfix) with ESMTPS id 6FC9CA3B89; Fri, 7 Jan 2022 13:38:23 +0000 (UTC) From: David Disseldorp To: linux-fsdevel@vger.kernel.org Cc: Martin Wilck , viro@zeniv.linux.org.uk, willy@infradead.org, David Disseldorp Subject: [PATCH v6 3/6] initramfs: add INITRAMFS_PRESERVE_MTIME Kconfig option Date: Fri, 7 Jan 2022 14:38:11 +0100 Message-Id: <20220107133814.32655-4-ddiss@suse.de> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20220107133814.32655-1-ddiss@suse.de> References: <20220107133814.32655-1-ddiss@suse.de> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org initramfs cpio mtime preservation, as implemented in commit 889d51a10712 ("initramfs: add option to preserve mtime from initramfs cpio images"), uses a linked list to defer directory mtime processing until after all other items in the cpio archive have been processed. This is done to ensure that parent directory mtimes aren't overwritten via subsequent child creation. Contrary to the 889d51a10712 commit message, the mtime preservation behaviour is unconditional. This change adds a new INITRAMFS_PRESERVE_MTIME Kconfig option, which can be used to disable on-by-default mtime retention and in turn speed up initramfs extraction, particularly for cpio archives with large directory counts. Benchmarks with a one million directory cpio archive extracted 20 times demonstrated: mean extraction time (s) std dev INITRAMFS_PRESERVE_MTIME=y 3.808 0.006 INITRAMFS_PRESERVE_MTIME unset 3.056 0.004 The above extraction times were measured using ftrace (initcall_finish - initcall_start) values for populate_rootfs() with initramfs_async disabled. Signed-off-by: David Disseldorp Reviewed-by: Martin Wilck [ddiss: rebase atop dir_entry.name flexible array member] --- init/Kconfig | 10 ++++++++ init/initramfs.c | 50 +++------------------------------------- init/initramfs_mtime.h | 52 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 47 deletions(-) create mode 100644 init/initramfs_mtime.h diff --git a/init/Kconfig b/init/Kconfig index 4b7bac10c72d..a98f63d3c366 100644 --- a/init/Kconfig +++ b/init/Kconfig @@ -1357,6 +1357,16 @@ config BOOT_CONFIG If unsure, say Y. +config INITRAMFS_PRESERVE_MTIME + bool "Preserve cpio archive mtimes in initramfs" + default y + help + Each entry in an initramfs cpio archive carries an mtime value. When + enabled, extracted cpio items take this mtime, with directory mtime + setting deferred until after creation of any child entries. + + If unsure, say Y. + choice prompt "Compiler optimization level" default CC_OPTIMIZE_FOR_PERFORMANCE diff --git a/init/initramfs.c b/init/initramfs.c index 656d2d71349f..5b4ca8ecadb5 100644 --- a/init/initramfs.c +++ b/init/initramfs.c @@ -17,6 +17,8 @@ #include #include +#include "initramfs_mtime.h" + static ssize_t __init xwrite(struct file *file, const char *p, size_t count, loff_t *pos) { @@ -116,48 +118,6 @@ static void __init free_hash(void) } } -static long __init do_utime(char *filename, time64_t mtime) -{ - struct timespec64 t[2]; - - t[0].tv_sec = mtime; - t[0].tv_nsec = 0; - t[1].tv_sec = mtime; - t[1].tv_nsec = 0; - return init_utimes(filename, t); -} - -static __initdata LIST_HEAD(dir_list); -struct dir_entry { - struct list_head list; - time64_t mtime; - char name[]; -}; - -static void __init dir_add(const char *name, time64_t mtime) -{ - size_t nlen = strlen(name) + 1; - struct dir_entry *de; - - de = kmalloc(sizeof(struct dir_entry) + nlen, GFP_KERNEL); - if (!de) - panic_show_mem("can't allocate dir_entry buffer"); - INIT_LIST_HEAD(&de->list); - strscpy(de->name, name, nlen); - de->mtime = mtime; - list_add(&de->list, &dir_list); -} - -static void __init dir_utime(void) -{ - struct dir_entry *de, *tmp; - list_for_each_entry_safe(de, tmp, &dir_list, list) { - list_del(&de->list); - do_utime(de->name, de->mtime); - kfree(de); - } -} - static __initdata time64_t mtime; /* cpio header parsing */ @@ -381,14 +341,10 @@ static int __init do_name(void) static int __init do_copy(void) { if (byte_count >= body_len) { - struct timespec64 t[2] = { }; if (xwrite(wfile, victim, body_len, &wfile_pos) != body_len) error("write error"); - t[0].tv_sec = mtime; - t[1].tv_sec = mtime; - vfs_utimes(&wfile->f_path, t); - + do_utime_path(&wfile->f_path, mtime); fput(wfile); eat(body_len); state = SkipIt; diff --git a/init/initramfs_mtime.h b/init/initramfs_mtime.h new file mode 100644 index 000000000000..688ed4b6f327 --- /dev/null +++ b/init/initramfs_mtime.h @@ -0,0 +1,52 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#ifdef CONFIG_INITRAMFS_PRESERVE_MTIME +static void __init do_utime(char *filename, time64_t mtime) +{ + struct timespec64 t[2] = { { .tv_sec = mtime }, { .tv_sec = mtime } }; + init_utimes(filename, t); +} + +static void __init do_utime_path(const struct path *path, time64_t mtime) +{ + struct timespec64 t[2] = { { .tv_sec = mtime }, { .tv_sec = mtime } }; + vfs_utimes(path, t); +} + +static __initdata LIST_HEAD(dir_list); +struct dir_entry { + struct list_head list; + time64_t mtime; + char name[]; +}; + +static void __init dir_add(const char *name, time64_t mtime) +{ + size_t nlen = strlen(name) + 1; + struct dir_entry *de; + + de = kmalloc(sizeof(struct dir_entry) + nlen, GFP_KERNEL); + if (!de) + panic("can't allocate dir_entry buffer"); + INIT_LIST_HEAD(&de->list); + strscpy(de->name, name, nlen); + de->mtime = mtime; + list_add(&de->list, &dir_list); +} + +static void __init dir_utime(void) +{ + struct dir_entry *de, *tmp; + + list_for_each_entry_safe(de, tmp, &dir_list, list) { + list_del(&de->list); + do_utime(de->name, de->mtime); + kfree(de); + } +} +#else +static void __init do_utime(char *filename, time64_t mtime) {} +static void __init do_utime_path(const struct path *path, time64_t mtime) {} +static void __init dir_add(const char *name, time64_t mtime) {} +static void __init dir_utime(void) {} +#endif From patchwork Fri Jan 7 13:38:12 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Disseldorp X-Patchwork-Id: 12706697 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id BC6BFC433EF for ; Fri, 7 Jan 2022 13:38:28 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1347602AbiAGNi1 (ORCPT ); Fri, 7 Jan 2022 08:38:27 -0500 Received: from smtp-out1.suse.de ([195.135.220.28]:50264 "EHLO smtp-out1.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238886AbiAGNiZ (ORCPT ); Fri, 7 Jan 2022 08:38:25 -0500 Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out1.suse.de (Postfix) with ESMTP id CF1B0212B8; Fri, 7 Jan 2022 13:38:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1641562703; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=dT3bNEkeoqlQtpJ1IGL9Z6s5aNZ4925QHeV43AShAkw=; b=Y1sEYz+B6YGl0+BDwuXBxCclvNONiO1U/wqzwp7+917dgaoV723r4/3yOC2NRAe4ZJBtSN M5U5yBJ1uPoXLbloWhbnZvVj4fMXBjs30yozbK5OO7tBXT9L1yPSXn9NPpPrHCb0Sdi3Zz BbUQF1knyI1WTFFN/MHVMl4uw8ktFDc= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1641562703; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=dT3bNEkeoqlQtpJ1IGL9Z6s5aNZ4925QHeV43AShAkw=; b=vBwEVJGfFblLcwVbfBi6Z4sDWrEYOfHCZULWrXDvgA62cEP9KW02M/iBBThi/QSUmj4VgG zZPYZf4Iu+DXsCCw== Received: from echidna.suse.de (ddiss.udp.ovpn2.nue.suse.de [10.163.47.146]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by relay2.suse.de (Postfix) with ESMTPS id A4348A3B87; Fri, 7 Jan 2022 13:38:23 +0000 (UTC) From: David Disseldorp To: linux-fsdevel@vger.kernel.org Cc: Martin Wilck , viro@zeniv.linux.org.uk, willy@infradead.org, David Disseldorp Subject: [PATCH v6 4/6] gen_init_cpio: fix short read file handling Date: Fri, 7 Jan 2022 14:38:12 +0100 Message-Id: <20220107133814.32655-5-ddiss@suse.de> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20220107133814.32655-1-ddiss@suse.de> References: <20220107133814.32655-1-ddiss@suse.de> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org When processing a "file" entry, gen_init_cpio attempts to allocate a buffer large enough to stage the entire contents of the source file. It then attempts to fill the buffer via a single read() call and subsequently writes out the entire buffer length, without checking that read() returned the full length, potentially writing uninitialized buffer memory. Fix this by breaking up file I/O into 64k chunks and only writing the length returned by the prior read() call. Signed-off-by: David Disseldorp Reviewed-by: Martin Wilck --- usr/gen_init_cpio.c | 44 +++++++++++++++++++++++++------------------- 1 file changed, 25 insertions(+), 19 deletions(-) diff --git a/usr/gen_init_cpio.c b/usr/gen_init_cpio.c index 0e2c8a5838b1..9a0f8c37273a 100644 --- a/usr/gen_init_cpio.c +++ b/usr/gen_init_cpio.c @@ -20,6 +20,7 @@ #define xstr(s) #s #define str(s) xstr(s) +#define MIN(a, b) ((a) < (b) ? (a) : (b)) static unsigned int offset; static unsigned int ino = 721; @@ -297,9 +298,8 @@ static int cpio_mkfile(const char *name, const char *location, unsigned int nlinks) { char s[256]; - char *filebuf = NULL; struct stat buf; - long size; + unsigned long size; int file = -1; int retval; int rc = -1; @@ -326,22 +326,17 @@ static int cpio_mkfile(const char *name, const char *location, buf.st_mtime = 0xffffffff; } - filebuf = malloc(buf.st_size); - if (!filebuf) { - fprintf (stderr, "out of memory\n"); - goto error; - } - - retval = read (file, filebuf, buf.st_size); - if (retval < 0) { - fprintf (stderr, "Can not read %s file\n", location); + if (buf.st_size > 0xffffffff) { + fprintf(stderr, "%s: Size exceeds maximum cpio file size\n", + location); goto error; } size = 0; for (i = 1; i <= nlinks; i++) { /* data goes on last link */ - if (i == nlinks) size = buf.st_size; + if (i == nlinks) + size = buf.st_size; if (name[0] == '/') name++; @@ -366,23 +361,34 @@ static int cpio_mkfile(const char *name, const char *location, push_string(name); push_pad(); - if (size) { - if (fwrite(filebuf, size, 1, stdout) != 1) { + while (size) { + unsigned char filebuf[65536]; + ssize_t this_read; + size_t this_size = MIN(size, sizeof(filebuf)); + + this_read = read(file, filebuf, this_size); + if (this_read <= 0 || this_read > this_size) { + fprintf(stderr, "Can not read %s file\n", location); + goto error; + } + + if (fwrite(filebuf, this_read, 1, stdout) != 1) { fprintf(stderr, "writing filebuf failed\n"); goto error; } - offset += size; - push_pad(); + offset += this_read; + size -= this_read; } + push_pad(); name += namesize; } ino++; rc = 0; - + error: - if (filebuf) free(filebuf); - if (file >= 0) close(file); + if (file >= 0) + close(file); return rc; } From patchwork Fri Jan 7 13:38:13 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Disseldorp X-Patchwork-Id: 12706699 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id E24C6C433F5 for ; Fri, 7 Jan 2022 13:38:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1347617AbiAGNia (ORCPT ); Fri, 7 Jan 2022 08:38:30 -0500 Received: from smtp-out2.suse.de ([195.135.220.29]:44112 "EHLO smtp-out2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1347596AbiAGNiZ (ORCPT ); Fri, 7 Jan 2022 08:38:25 -0500 Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out2.suse.de (Postfix) with ESMTP id 0D7F21F39C; Fri, 7 Jan 2022 13:38:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1641562704; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=7oWt2FRIMhQvJnF1eCCxZr7+iUoAy5qAr/dXG0Rhu5E=; b=uBOdRI26N/BTTEa0IAoqhxOB7A2MD4iFEexzzwyMDKK9pakTkUNWZO06vTx13E8Oe2kX63 X0YzyMkOzte0t3DU5H6Gbza0p47VcWCPojB0ToJGvn1B7RpUcx3TLNKdPvWfOdobHke7c+ ajAdnhOF/DOnW6xo10eRyRIajJ8IPdY= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1641562704; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=7oWt2FRIMhQvJnF1eCCxZr7+iUoAy5qAr/dXG0Rhu5E=; b=eR3O5fLy31TfLroqLK/qgF6hZUEMuT0xxTsYGwdagu8sNFLF3+3uPn5YXSEbT004Ct0ECm NVKjcHmswkU+IQBw== Received: from echidna.suse.de (ddiss.udp.ovpn2.nue.suse.de [10.163.47.146]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by relay2.suse.de (Postfix) with ESMTPS id D75BBA3B8A; Fri, 7 Jan 2022 13:38:23 +0000 (UTC) From: David Disseldorp To: linux-fsdevel@vger.kernel.org Cc: Martin Wilck , viro@zeniv.linux.org.uk, willy@infradead.org, David Disseldorp Subject: [PATCH v6 5/6] gen_init_cpio: support file checksum archiving Date: Fri, 7 Jan 2022 14:38:13 +0100 Message-Id: <20220107133814.32655-6-ddiss@suse.de> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20220107133814.32655-1-ddiss@suse.de> References: <20220107133814.32655-1-ddiss@suse.de> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org Documentation/driver-api/early-userspace/buffer-format.rst includes the specification for checksum-enabled cpio archives. Implement support for this format in gen_init_cpio via a new '-c' parameter. Suggested-by: Matthew Wilcox (Oracle) Signed-off-by: David Disseldorp --- usr/gen_init_cpio.c | 54 +++++++++++++++++++++++++++++++++++++-------- 1 file changed, 45 insertions(+), 9 deletions(-) diff --git a/usr/gen_init_cpio.c b/usr/gen_init_cpio.c index 9a0f8c37273a..dc838e26a5b9 100644 --- a/usr/gen_init_cpio.c +++ b/usr/gen_init_cpio.c @@ -1,6 +1,8 @@ // SPDX-License-Identifier: GPL-2.0 #include #include +#include +#include #include #include #include @@ -25,6 +27,7 @@ static unsigned int offset; static unsigned int ino = 721; static time_t default_mtime; +static bool do_csum = false; struct file_handler { const char *type; @@ -78,7 +81,7 @@ static void cpio_trailer(void) sprintf(s, "%s%08X%08X%08lX%08lX%08X%08lX" "%08X%08X%08X%08X%08X%08X%08X", - "070701", /* magic */ + do_csum ? "070702" : "070701", /* magic */ 0, /* ino */ 0, /* mode */ (long) 0, /* uid */ @@ -110,7 +113,7 @@ static int cpio_mkslink(const char *name, const char *target, name++; sprintf(s,"%s%08X%08X%08lX%08lX%08X%08lX" "%08X%08X%08X%08X%08X%08X%08X", - "070701", /* magic */ + do_csum ? "070702" : "070701", /* magic */ ino++, /* ino */ S_IFLNK | mode, /* mode */ (long) uid, /* uid */ @@ -159,7 +162,7 @@ static int cpio_mkgeneric(const char *name, unsigned int mode, name++; sprintf(s,"%s%08X%08X%08lX%08lX%08X%08lX" "%08X%08X%08X%08X%08X%08X%08X", - "070701", /* magic */ + do_csum ? "070702" : "070701", /* magic */ ino++, /* ino */ mode, /* mode */ (long) uid, /* uid */ @@ -253,7 +256,7 @@ static int cpio_mknod(const char *name, unsigned int mode, name++; sprintf(s,"%s%08X%08X%08lX%08lX%08X%08lX" "%08X%08X%08X%08X%08X%08X%08X", - "070701", /* magic */ + do_csum ? "070702" : "070701", /* magic */ ino++, /* ino */ mode, /* mode */ (long) uid, /* uid */ @@ -293,6 +296,29 @@ static int cpio_mknod_line(const char *line) return rc; } +static int cpio_mkfile_csum(int fd, unsigned long size, uint32_t *csum) +{ + while (size) { + unsigned char filebuf[65536]; + ssize_t this_read; + size_t i, this_size = MIN(size, sizeof(filebuf)); + + this_read = read(fd, filebuf, this_size); + if (this_read <= 0 || this_read > this_size) + return -1; + + for (i = 0; i < this_read; i++) + *csum += filebuf[i]; + + size -= this_read; + } + /* seek back to the start for data segment I/O */ + if (lseek(fd, 0, SEEK_SET) < 0) + return -1; + + return 0; +} + static int cpio_mkfile(const char *name, const char *location, unsigned int mode, uid_t uid, gid_t gid, unsigned int nlinks) @@ -305,6 +331,7 @@ static int cpio_mkfile(const char *name, const char *location, int rc = -1; int namesize; unsigned int i; + uint32_t csum = 0; mode |= S_IFREG; @@ -332,6 +359,11 @@ static int cpio_mkfile(const char *name, const char *location, goto error; } + if (do_csum && cpio_mkfile_csum(file, buf.st_size, &csum) < 0) { + fprintf(stderr, "Failed to checksum file %s\n", location); + goto error; + } + size = 0; for (i = 1; i <= nlinks; i++) { /* data goes on last link */ @@ -343,7 +375,7 @@ static int cpio_mkfile(const char *name, const char *location, namesize = strlen(name) + 1; sprintf(s,"%s%08X%08X%08lX%08lX%08X%08lX" "%08lX%08X%08X%08X%08X%08X%08X", - "070701", /* magic */ + do_csum ? "070702" : "070701", /* magic */ ino, /* ino */ mode, /* mode */ (long) uid, /* uid */ @@ -356,7 +388,7 @@ static int cpio_mkfile(const char *name, const char *location, 0, /* rmajor */ 0, /* rminor */ namesize, /* namesize */ - 0); /* chksum */ + size ? csum : 0); /* chksum */ push_hdr(s); push_string(name); push_pad(); @@ -464,7 +496,7 @@ static int cpio_mkfile_line(const char *line) static void usage(const char *prog) { fprintf(stderr, "Usage:\n" - "\t%s [-t ] \n" + "\t%s [-t ] [-c] \n" "\n" " is a file containing newline separated entries that\n" "describe the files to be included in the initramfs archive:\n" @@ -499,7 +531,8 @@ static void usage(const char *prog) "\n" " is time in seconds since Epoch that will be used\n" "as mtime for symlinks, special files and directories. The default\n" - "is to use the current time for these entries.\n", + "is to use the current time for these entries.\n" + "-c: calculate and store 32-bit checksums for file data.\n", prog); } @@ -541,7 +574,7 @@ int main (int argc, char *argv[]) default_mtime = time(NULL); while (1) { - int opt = getopt(argc, argv, "t:h"); + int opt = getopt(argc, argv, "t:ch"); char *invalid; if (opt == -1) @@ -556,6 +589,9 @@ int main (int argc, char *argv[]) exit(1); } break; + case 'c': + do_csum = true; + break; case 'h': case '?': usage(argv[0]); From patchwork Fri Jan 7 13:38:14 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Disseldorp X-Patchwork-Id: 12706698 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3A059C433F5 for ; Fri, 7 Jan 2022 13:38:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1347616AbiAGNi2 (ORCPT ); Fri, 7 Jan 2022 08:38:28 -0500 Received: from smtp-out2.suse.de ([195.135.220.29]:44126 "EHLO smtp-out2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1347609AbiAGNiZ (ORCPT ); Fri, 7 Jan 2022 08:38:25 -0500 Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out2.suse.de (Postfix) with ESMTP id 3FD051F3A2; Fri, 7 Jan 2022 13:38:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1641562704; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=a1M3BktK5WSpmRIcwuj0jxFaJnOUMmNc659XQjpfM3Q=; b=t1myR8V2M3A+scrb7ipawdhcW3xDvW69NeHPh4SICjV3QocRl6VFQxBWukxDBz4sgsQNA0 knZPSU4vjctmA+7zLzJ7NvRigGHCgWl9pLzRz73suImWFU9Evv0qs4sZTqMf1hvIgUM0Lw s55hFbslTmwZS9P5uYccHCoUeA6r2Tw= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1641562704; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=a1M3BktK5WSpmRIcwuj0jxFaJnOUMmNc659XQjpfM3Q=; b=M0yiQefBMQFkRJoGB3w1uJF5qtNaLdycRBG5Iq8ob/08mMzOScifhvAdJbra8tPrWgU7sy ZI4rrJF9j7I/bPBg== Received: from echidna.suse.de (ddiss.udp.ovpn2.nue.suse.de [10.163.47.146]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by relay2.suse.de (Postfix) with ESMTPS id 15CC1A3B89; Fri, 7 Jan 2022 13:38:24 +0000 (UTC) From: David Disseldorp To: linux-fsdevel@vger.kernel.org Cc: Martin Wilck , viro@zeniv.linux.org.uk, willy@infradead.org, David Disseldorp Subject: [PATCH v6 6/6] initramfs: support cpio extraction with file checksums Date: Fri, 7 Jan 2022 14:38:14 +0100 Message-Id: <20220107133814.32655-7-ddiss@suse.de> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20220107133814.32655-1-ddiss@suse.de> References: <20220107133814.32655-1-ddiss@suse.de> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org Add support for extraction of checksum-enabled "070702" cpio archives, specified in Documentation/driver-api/early-userspace/buffer-format.rst. Fail extraction if the calculated file data checksum doesn't match the value carried in the header. Suggested-by: Matthew Wilcox (Oracle) Signed-off-by: David Disseldorp --- init/initramfs.c | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/init/initramfs.c b/init/initramfs.c index 5b4ca8ecadb5..ead3e2839f44 100644 --- a/init/initramfs.c +++ b/init/initramfs.c @@ -19,8 +19,11 @@ #include "initramfs_mtime.h" -static ssize_t __init xwrite(struct file *file, const char *p, size_t count, - loff_t *pos) +static __initdata bool csum_present; +static __initdata u32 io_csum; + +static ssize_t __init xwrite(struct file *file, const unsigned char *p, + size_t count, loff_t *pos) { ssize_t out = 0; @@ -35,6 +38,13 @@ static ssize_t __init xwrite(struct file *file, const char *p, size_t count, } else if (rv == 0) break; + if (csum_present) { + ssize_t i; + + for (i = 0; i < rv; i++) + io_csum += p[i]; + } + p += rv; out += rv; count -= rv; @@ -118,8 +128,6 @@ static void __init free_hash(void) } } -static __initdata time64_t mtime; - /* cpio header parsing */ static __initdata unsigned long ino, major, minor, nlink; @@ -128,15 +136,17 @@ static __initdata unsigned long body_len, name_len; static __initdata uid_t uid; static __initdata gid_t gid; static __initdata unsigned rdev; +static __initdata time64_t mtime; +static __initdata u32 hdr_csum; static void __init parse_header(char *s) { - unsigned long parsed[12]; + unsigned long parsed[13]; char buf[9]; int i; buf[8] = '\0'; - for (i = 0, s += 6; i < 12; i++, s += 8) { + for (i = 0, s += 6; i < 13; i++, s += 8) { memcpy(buf, s, 8); parsed[i] = simple_strtoul(buf, NULL, 16); } @@ -151,6 +161,7 @@ static void __init parse_header(char *s) minor = parsed[8]; rdev = new_encode_dev(MKDEV(parsed[9], parsed[10])); name_len = parsed[11]; + hdr_csum = parsed[12]; } /* FSM */ @@ -219,7 +230,11 @@ static int __init do_collect(void) static int __init do_header(void) { - if (memcmp(collected, "070701", 6)) { + if (!memcmp(collected, "070701", 6)) { + csum_present = false; + } else if (!memcmp(collected, "070702", 6)) { + csum_present = true; + } else { if (memcmp(collected, "070707", 6) == 0) error("incorrect cpio method used: use -H newc option"); else @@ -314,6 +329,7 @@ static int __init do_name(void) if (IS_ERR(wfile)) return 0; wfile_pos = 0; + io_csum = 0; vfs_fchown(wfile, uid, gid); vfs_fchmod(wfile, mode); @@ -346,6 +362,8 @@ static int __init do_copy(void) do_utime_path(&wfile->f_path, mtime); fput(wfile); + if (csum_present && io_csum != hdr_csum) + error("bad data checksum"); eat(body_len); state = SkipIt; return 0;