From nobody Mon Apr 6 11:53:27 2026 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.223.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 890DB274B39 for ; Sun, 29 Mar 2026 10:53:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=195.135.223.131 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774781630; cv=none; b=KTbTf/XMT1HiGlmM5+QvGpiMvbT4yOpdJGGP2U8aqTYl6l+Hbmm/0CPPirS/OjUMJFQwPd7qldUkXk+9IJUGL0bR8VbBefxO/a4mM1M0Whv1SLXpcjFY47E+giM/Dpm2MCbfLrzLhWELtEUhmTvOOtO+n9BTCIHjD22OTM17jtM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774781630; c=relaxed/simple; bh=tQHosTlLcdVnbXjQy+SS/ZFvnKIisj97Lu1fOzti6qE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=I25bpdVVZcIF2ju+VIvwuA1bfN6HS4L1cVGpWmP2urVXcQmwpUc+YCavKFqVOH9Xq5cz74YKWE+VjmTDghnhgE0Q8ZjsyXpXthzVBYChvScvy7k5AGTTi4e5AfTiX61h6rTFFM0zjd2fgJhVM1FnHRyzRqGWeOZdxIKJdj81vHM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=suse.de; spf=pass smtp.mailfrom=suse.de; arc=none smtp.client-ip=195.135.223.131 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=suse.de Received: from imap1.dmz-prg2.suse.org (imap1.dmz-prg2.suse.org [IPv6:2a07:de40:b281:104:10:150:64:97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id EFFA25BCE6; Sun, 29 Mar 2026 10:53:44 +0000 (UTC) Authentication-Results: smtp-out2.suse.de; none Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id 011E74A0A2; Sun, 29 Mar 2026 10:53:40 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id QN0CK7QEyWkqcwAAD6G6ig (envelope-from ); Sun, 29 Mar 2026 10:53:40 +0000 From: David Disseldorp To: Andy Shevchenko , David Disseldorp , Petr Mladek , linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org Cc: Al Viro , Christian Brauner , Jan Kara , Steven Rostedt , Rasmus Villemoes , Sergey Senozhatsky , Andrew Morton Subject: [PATCH v4 4/6] initramfs: Refactor to use hex2bin() instead of custom approach Date: Sun, 29 Mar 2026 21:30:26 +1100 Message-ID: <20260329105307.2876-5-ddiss@suse.de> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20260329105307.2876-1-ddiss@suse.de> References: <20260329105307.2876-1-ddiss@suse.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Rspamd-Pre-Result: action=no action; module=replies; Message is reply to one we originated X-Rspamd-Server: rspamd2.dmz-prg2.suse.org X-Spamd-Result: default: False [-4.00 / 50.00]; REPLY(-4.00)[] X-Rspamd-Queue-Id: EFFA25BCE6 X-Rspamd-Pre-Result: action=no action; module=replies; Message is reply to one we originated X-Rspamd-Action: no action X-Spam-Flag: NO X-Spam-Score: -4.00 X-Spam-Level: Content-Type: text/plain; charset="utf-8" From: Andy Shevchenko There is a simple_strntoul() function used solely as a shortcut for hex2bin() with proper endianess conversions. Replace that and drop the unneeded function in the next changes. This implementation will abort if we fail to parse the cpio header, instead of using potentially bogus header values. Co-developed-by: David Disseldorp Signed-off-by: David Disseldorp Signed-off-by: Andy Shevchenko --- init/initramfs.c | 44 +++++++++++++++++++++++++------------------ init/initramfs_test.c | 23 ++++------------------ 2 files changed, 30 insertions(+), 37 deletions(-) diff --git a/init/initramfs.c b/init/initramfs.c index 4ed796566cf35..0d38ea8e63a29 100644 --- a/init/initramfs.c +++ b/init/initramfs.c @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -21,6 +22,8 @@ #include #include =20 +#include + #include "do_mounts.h" #include "initramfs_internal.h" =20 @@ -190,26 +193,30 @@ static __initdata gid_t gid; static __initdata unsigned rdev; static __initdata u32 hdr_csum; =20 -static void __init parse_header(char *s) +static int __init parse_header(char *s) { - unsigned long parsed[13]; - int i; + __be32 header[13]; + int ret; =20 - for (i =3D 0, s +=3D 6; i < 13; i++, s +=3D 8) - parsed[i] =3D simple_strntoul(s, NULL, 16, 8); + ret =3D hex2bin((u8 *)header, s + 6, sizeof(header)); + if (ret) { + error("damaged header"); + return ret; + } =20 - ino =3D parsed[0]; - mode =3D parsed[1]; - uid =3D parsed[2]; - gid =3D parsed[3]; - nlink =3D parsed[4]; - mtime =3D parsed[5]; /* breaks in y2106 */ - body_len =3D parsed[6]; - major =3D parsed[7]; - minor =3D parsed[8]; - rdev =3D new_encode_dev(MKDEV(parsed[9], parsed[10])); - name_len =3D parsed[11]; - hdr_csum =3D parsed[12]; + ino =3D be32_to_cpu(header[0]); + mode =3D be32_to_cpu(header[1]); + uid =3D be32_to_cpu(header[2]); + gid =3D be32_to_cpu(header[3]); + nlink =3D be32_to_cpu(header[4]); + mtime =3D be32_to_cpu(header[5]); /* breaks in y2106 */ + body_len =3D be32_to_cpu(header[6]); + major =3D be32_to_cpu(header[7]); + minor =3D be32_to_cpu(header[8]); + rdev =3D new_encode_dev(MKDEV(be32_to_cpu(header[9]), be32_to_cpu(header[= 10]))); + name_len =3D be32_to_cpu(header[11]); + hdr_csum =3D be32_to_cpu(header[12]); + return 0; } =20 /* FSM */ @@ -289,7 +296,8 @@ static int __init do_header(void) error("no cpio magic"); return 1; } - parse_header(collected); + if (parse_header(collected)) + return 1; next_header =3D this_header + N_ALIGN(name_len) + body_len; next_header =3D (next_header + 3) & ~3; state =3D SkipIt; diff --git a/init/initramfs_test.c b/init/initramfs_test.c index 4a7a85560ee50..4785423f601c2 100644 --- a/init/initramfs_test.c +++ b/init/initramfs_test.c @@ -31,7 +31,8 @@ struct initramfs_test_cpio { #define CPIO_HDR_FMT "%s%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%0= 8x%s" /* * bogus newc header with "0x" prefixes on the uid, gid and namesize value= s. - * parse_header()/simple_str[n]toul() accept this. + * parse_header()/simple_str[n]toul() accepted this, contrary to the initr= amfs + * specification. hex2bin() now fails. */ #define CPIO_HDR_OX_INJECT \ "%s%08x%08x0x%06x0X%06x%08x%08x%08x%08x%08x%08x%08x0x%06x%08x%s" @@ -507,8 +508,7 @@ static void __init initramfs_test_hdr_hex(struct kunit = *test) { char *err; size_t len; - struct kstat st0, st1; - char fdata[] =3D "this file data will be unpacked"; + char fdata[] =3D "this file data will not be unpacked"; struct initramfs_test_bufs { char cpio_src[(CPIO_HDRLEN + PATH_MAX + 3 + sizeof(fdata)) * 2]; } *tbufs =3D kzalloc(sizeof(struct initramfs_test_bufs), GFP_KERNEL); @@ -538,22 +538,7 @@ static void __init initramfs_test_hdr_hex(struct kunit= *test) len =3D fill_cpio(c, ARRAY_SIZE(c), true, tbufs->cpio_src); =20 err =3D unpack_to_rootfs(tbufs->cpio_src, len); - KUNIT_EXPECT_NULL(test, err); - - KUNIT_EXPECT_EQ(test, init_stat(c[0].fname, &st0, 0), 0); - KUNIT_EXPECT_EQ(test, init_stat(c[1].fname, &st1, 0), 0); - - KUNIT_EXPECT_TRUE(test, - uid_eq(st0.uid, make_kuid(current_user_ns(), (uid_t)0x123456))); - KUNIT_EXPECT_TRUE(test, - gid_eq(st0.gid, make_kgid(current_user_ns(), (gid_t)0x123457))); - KUNIT_EXPECT_TRUE(test, - uid_eq(st1.uid, make_kuid(current_user_ns(), (uid_t)0x56))); - KUNIT_EXPECT_TRUE(test, - gid_eq(st1.gid, make_kgid(current_user_ns(), (gid_t)0x57))); - - KUNIT_EXPECT_EQ(test, init_unlink(c[0].fname), 0); - KUNIT_EXPECT_EQ(test, init_rmdir(c[1].fname), 0); + KUNIT_EXPECT_NOT_NULL(test, err); =20 kfree(tbufs); } --=20 2.51.0