initramfs: get rid of custom hex2bin()
Subsystems (3)
Andy Shevchenko
reviewercover initramfs: get rid of custom hex2bin()
The series that introduced simple_strntoul() had passed into kernel without proper review and hence reinvented a wheel that's not needed. Here is the refactoring to show that. This is assumed to go via PRINTK tree.
I have tested this on x86, but I believe the same result will be on big-endian CPUs (I deduced that from how strtox() works).
Andy Shevchenko (4):
initramfs: Sort headers alphabetically
initramfs: Refactor to use hex2bin() instead of custom approach
vsprintf: Revert "add simple_strntoul"
kstrtox: Drop extern keyword in the simple_strtox() declarations
include/linux/kstrtox.h | 9 +++---- init/initramfs.c | 60 ++++++++++++++++++++++------------------- lib/vsprintf.c | 7 ----- 3 files changed, 36 insertions(+), 40 deletions(-)
Signature · 1 line
2.50.1
Andy Shevchenko
reviewerPATCH 1/4 initramfs: Sort headers alphabetically
Sorting headers alphabetically helps locating duplicates, and makes it easier to figure out where to insert new headers.
--- init/initramfs.c | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-)
init/initramfs.c +12 -12
@@ -1,25 +1,25 @@// SPDX-License-Identifier: GPL-2.0 -#include <linux/init.h> #include <linux/async.h> -#include <linux/export.h> -#include <linux/fs.h> -#include <linux/slab.h> -#include <linux/types.h> -#include <linux/fcntl.h> #include <linux/delay.h> -#include <linux/string.h> #include <linux/dirent.h> -#include <linux/syscalls.h> -#include <linux/utime.h> +#include <linux/export.h> +#include <linux/fcntl.h> #include <linux/file.h> +#include <linux/fs.h> +#include <linux/init.h> +#include <linux/init_syscalls.h> #include <linux/kstrtox.h> #include <linux/memblock.h> #include <linux/mm.h> #include <linux/namei.h> -#include <linux/init_syscalls.h> -#include <linux/umh.h> -#include <linux/security.h> #include <linux/overflow.h> +#include <linux/security.h> +#include <linux/slab.h> +#include <linux/string.h> +#include <linux/syscalls.h> +#include <linux/types.h> +#include <linux/umh.h> +#include <linux/utime.h> #include "do_mounts.h" #include "initramfs_internal.h"
Signature · 1 line
2.50.1
Andy Shevchenko
reviewerPATCH 2/4 initramfs: Refactor to use hex2bin() instead of custom approach
In reply to Andy ShevchenkoThere 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.
--- init/initramfs.c | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-)
init/initramfs.c +20 -16
@@ -6,6 +6,7 @@#include <linux/fcntl.h> #include <linux/file.h> #include <linux/fs.h> +#include <linux/hex.h> #include <linux/init.h> #include <linux/init_syscalls.h> #include <linux/kstrtox.h>
@@ -21,6 +22,8 @@#include <linux/umh.h> #include <linux/utime.h> +#include <asm/byteorder.h> +#include "do_mounts.h" #include "initramfs_internal.h"
@@ -192,24 +195,25 @@ static __initdata u32 hdr_csum;static void __init parse_header (char *s) { -unsigned long parsed[13 ]; -int i; +__be32 header[13 ]; +int ret; -for (i =0 , s +=6 ; i <13 ; i++, s +=8 ) - parsed[i] =simple_strntoul (s,NULL ,16 ,8 ); + ret =hex2bin ((u8 *)header, s +6 ,sizeof (header)); +if (ret) +error ("damaged header" ); - ino = parsed[0 ]; - mode = parsed[1 ]; - uid = parsed[2 ]; - gid = parsed[3 ]; - nlink = parsed[4 ]; - mtime = parsed[5 ];/* breaks in y2106 */ - body_len = parsed[6 ]; - major = parsed[7 ]; - minor = parsed[8 ]; - rdev =new_encode_dev (MKDEV (parsed[9 ], parsed[10 ])); - name_len = parsed[11 ]; - hdr_csum = parsed[12 ]; + ino =be32_to_cpu (header[0 ]); + mode =be32_to_cpu (header[1 ]); + uid =be32_to_cpu (header[2 ]); + gid =be32_to_cpu (header[3 ]); + nlink =be32_to_cpu (header[4 ]); + mtime =be32_to_cpu (header[5 ]);/* breaks in y2106 */ + body_len =be32_to_cpu (header[6 ]); + major =be32_to_cpu (header[7 ]); + minor =be32_to_cpu (header[8 ]); + rdev =new_encode_dev (MKDEV (be32_to_cpu (header[9 ]),be32_to_cpu (header[10 ]))); + name_len =be32_to_cpu (header[11 ]); + hdr_csum =be32_to_cpu (header[12 ]); }/* FSM */
Signature · 1 line
2.50.1
Re: [PATCH v1 2/4] initramfs: Refactor to use hex2bin() instead of custom approach
init/initramfs.c static __initdata u32 hdr_csum; · line 203
Quoted 3 lines · Andy Shevchenko
init/initramfs.c +3
-0+ ret =hex2bin ((u8 *)header, s +6 ,sizeof (header)); +if (ret) +error ("damaged header" );
The changes look reasonable to me on first glance, but I think we really should improve the error handling to abort the state machine on malformed header here.
One further issue that we have is simple_strntoul()'s acceptance of "0x" prefixes for the hex strings - any initramfs which carries such prefixes will now result in an error.
It's a pretty obscure corner case, but cpio is really easy to generate from printf(), so maybe there are some images out there which rely on this.
I've written an initramfs_test regression test for the "0x" prefix handling. I'll send it to the list.
Thanks, David
Andy Shevchenko
reviewerRe: [PATCH v1 2/4] initramfs: Refactor to use hex2bin() instead of custom approach
Quoted 17 lines · David Disseldorp
init/initramfs.c static __initdata u32 hdr_csum; · line 203
Quoted 3 lines · Andy Shevchenko
init/initramfs.c +3
-0+ ret =hex2bin ((u8 *)header, s +6 ,sizeof (header)); +if (ret) +error ("damaged header" );The changes look reasonable to me on first glance, but I think we really should improve the error handling to abort the state machine on malformed header here.
One further issue that we have is simple_strntoul()'s acceptance of "0x" prefixes for the hex strings - any initramfs which carries such prefixes will now result in an error.
It's a pretty obscure corner case, but cpio is really easy to generate fromprintf(), so maybe there are some images out there which rely on this.
Quoted 2 lines
I've written an
initramfs_testregression test for the "0x" prefix handling. I'll send it to the list.
Is it specified?
The standard refers to octal numbers, we seem to use hexadecimal. I don't believe the 0x will ever appear here.
Otherwise, please point out to the specifications.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v1 2/4] initramfs: Refactor to use hex2bin() instead of custom approach
Quoted 21 lines · Andy Shevchenko
Quoted 17 lines · David Disseldorp
init/initramfs.c static __initdata u32 hdr_csum; · line 203
Quoted 3 lines · Andy Shevchenko
init/initramfs.c +3
-0+ ret =hex2bin ((u8 *)header, s +6 ,sizeof (header)); +if (ret) +error ("damaged header" );The changes look reasonable to me on first glance, but I think we really should improve the error handling to abort the state machine on malformed header here.
One further issue that we have is simple_strntoul()'s acceptance of "0x" prefixes for the hex strings - any initramfs which carries such prefixes will now result in an error.
It's a pretty obscure corner case, but cpio is really easy to generate fromprintf(), so maybe there are some images out there which rely on this.Quoted 2 lines
I've written an
initramfs_testregression test for the "0x" prefix handling. I'll send it to the list.
Quoted 6 lines
Is it specified?
The standard refers to octal numbers, we seem to use hexadecimal. I don't believe the 0x will ever appear here.
Otherwise, please point out to the specifications.
The kernel initramfs specification is at Documentation/driver-api/early-userspace/buffer-format.rst :
The structure of the cpio_header is as follows (all fields contain hexadecimal ASCII numbers fully padded with '0' on the left to the full width of the field, for example, the integer 4780 is represented by the ASCII string "000012ac"):
...
I.e. a "0x" isn't specified as valid prefix. I don't feel strongly regarding diverging from existing behaviour, but it should still be considered (and documented) as a potentially user-visible regression.
Cheers, David
Andy Shevchenko
reviewerRe: [PATCH v1 2/4] initramfs: Refactor to use hex2bin() instead of custom approach
Quoted 3 lines · David Disseldorp
Quoted 2 lines · Andy Shevchenko
Quoted 1 line · David Disseldorp
On Mon, 19 Jan 2026 21:38:39 +0100, Andy Shevchenko wrote:
...
Quoted 20 lines
Quoted 20 lines
Quoted 15 lines
init/initramfs.c static __initdata u32 hdr_csum; · line 203
Quoted 3 lines
init/initramfs.c +3
-0+ ret =hex2bin ((u8 *)header, s +6 ,sizeof (header)); +if (ret) +error ("damaged header" );The changes look reasonable to me on first glance, but I think we really should improve the error handling to abort the state machine on malformed header here.
One further issue that we have is simple_strntoul()'s acceptance of "0x" prefixes for the hex strings - any initramfs which carries such prefixes will now result in an error.
It's a pretty obscure corner case, but cpio is really easy to generate fromprintf(), so maybe there are some images out there which rely on this.Quoted 2 lines
I've written an
initramfs_testregression test for the "0x" prefix handling. I'll send it to the list.Is it specified?
Quoted 7 lines
Quoted 4 lines
The standard refers to octal numbers, we seem to use hexadecimal. I don't believe the 0x will ever appear here.
Otherwise, please point out to the specifications.
The kernel initramfs specification is at
Documentation/driver-api/early-userspace/buffer-format.rst:
Thanks!
Quoted 11 lines
The structure of the
cpio_headeris as follows (all fields contain hexadecimal ASCII numbers fully padded with '0' on the left to the full width of the field, for example, the integer 4780 is represented by the ASCII string "000012ac"):
...I.e. a "0x" isn't specified as valid prefix. I don't feel strongly regarding diverging from existing behaviour,
but it should still be
considered (and documented) as a potentially user-visible regression.
I disagree, this is not specified and should not be used. The CPIO archive in the original form doesn't specify leading 0 for octals (at least how I read it, please correct me, if I'm wrong).
https://pubs.opengroup.org/onlinepubs/007908799/xcu/pax.html
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v1 2/4] initramfs: Refactor to use hex2bin() instead of custom approach
Quoted 4 lines · Andy Shevchenko
Quoted 3 lines
I.e. a "0x" isn't specified as valid prefix. I don't feel strongly regarding diverging from existing behaviour,
Quoted 9 lines
Quoted 2 lines
but it should still be
considered (and documented) as a potentially user-visible regression.I disagree, this is not specified and should not be used. The CPIO archive in the original form doesn't specify leading 0 for octals (at least how I read it, please correct me, if I'm wrong).
https://pubs.opengroup.org/onlinepubs/007908799/xcu/pax.html
This is the "newc" or "crc" format used by BSD (header magic 070701 and 070702, respectively), those were never standardized by POSIX.
These formats use 8-character hexadecimal (%08x).
As far as a 0x prefix, or upper case -- no, that is technically not according to spec, but we DO NOT break user space tools that have been working for 20+ years, unless it is either (a) a security problem or (b) is holding back further development (e.g. because the format is ambiguous. This is Postel's law in action.
-hpa
Andy Shevchenko
reviewerPATCH 3/4 vsprintf: Revert "add simple_strntoul"
In reply to Andy ShevchenkoNo users anymore and none should be in the first place.
This reverts commit fcc155008a20fa31b01569e105250490750f0687.
--- include/linux/kstrtox.h | 1 - lib/vsprintf.c | 7 ------- 2 files changed, 8 deletions(-)
include/linux/kstrtox.h +0 -1
@@ -143,7 +143,6 @@ static inline int __must_check kstrtos32_from_user(const char __user *s, size_t*/ extern unsigned long simple_strtoul (const char *,char **,unsigned int ); -extern unsigned long simple_strntoul (const char *,char **,unsigned int ,size_t );extern long simple_strtol (const char *,char **,unsigned int );extern unsigned long long simple_strtoull (const char *,char **,unsigned int );extern long long simple_strtoll (const char *,char **,unsigned int );
lib/vsprintf.c +0 -7
@@ -129,13 +129,6 @@ unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base) }EXPORT_SYMBOL (simple_strtoul); -unsigned long simple_strntoul (const char *cp,char **endp,unsigned int base, -size_t max_chars) -{ -return simple_strntoull (cp, endp, base, max_chars); -} -EXPORT_SYMBOL (simple_strntoul); -/** * simple_strtol - convert a string to a signed long * @cp: The start of the string
Signature · 1 line
2.50.1
Andy Shevchenko
reviewerPATCH 4/4 kstrtox: Drop extern keyword in the simple_strtox() declarations
In reply to Andy ShevchenkoThere is legacy 'extern' keyword for the exported simple_strtox() function which are the artefact that can be removed. So drop it.
While at it, tweak the declaration to provide parameter names.
--- include/linux/kstrtox.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
include/linux/kstrtox.h +4 -4
@@ -142,9 +142,9 @@ static inline int __must_check kstrtos32_from_user(const char __user *s, size_t* Keep in mind above caveat. */ -extern unsigned long simple_strtoul (const char *,char **,unsigned int ); -extern long simple_strtol (const char *,char **,unsigned int ); -extern unsigned long long simple_strtoull (const char *,char **,unsigned int ); -extern long long simple_strtoll (const char *,char **,unsigned int ); +unsigned long simple_strtoul (const char *cp,char **endp,unsigned int base); +long simple_strtol (const char *cp,char **endp,unsigned int base); +unsigned long long simple_strtoull (const char *cp,char **endp,unsigned int base); +long long simple_strtoll (const char *cp,char **endp,unsigned int base);#endif /* _LINUX_KSTRTOX_H */
Signature · 1 line
2.50.1
Christian Brauner
maintainerQuoted 2 lines
Here is the refactoring to show that. This is assumed to go via PRINTK tree.
No, initramfs is maintained by the VFS and we already carry other patches.
If you want the kstrtox changes to go another route then I will take the first two changes in a stable branch that can be merged.
Quoted 3 lines
I have tested this on x86, but I believe the same result will be on big-endian CPUs (I deduced that from how
strtox()works).
Did you rerun the kunit tests the original change was part of or did you do some custom testing?
Andy Shevchenko
reviewerQuoted 4 lines · Christian Brauner
Quoted 2 lines
Here is the refactoring to show that. This is assumed to go via PRINTK tree.
No, initramfs is maintained by the VFS and we already carry other patches.
If this applies cleanly, take them through it, I will be glad, thanks!
Quoted 2 lines
If you want the kstrtox changes to go another route then I will take the first two changes in a stable branch that can be merged.
I am fine with this route as long as the custom approach is gone.
Quoted 5 lines
Quoted 2 lines
I have tested this on x86, but I believe the same result will be on big-endian CPUs (I deduced that from how
strtox()works).Did you rerun the kunit tests the original change was part of or did you do some custom testing?
I'm not sure I understand the point. There were no test cases added for simple_strntoul() AFAICS. Did I miss anything?
(If I didn't that is the second point on why the patches didn't get enough
time for review and not every stakeholder seen them, usually we require the test cases for new APIs.)
Signature · 2 lines
With Best Regards, Andy Shevchenko
Christian Brauner
maintainerQuoted 19 lines · Andy Shevchenko
Quoted 4 lines · Christian Brauner
Quoted 2 lines
Here is the refactoring to show that. This is assumed to go via PRINTK tree.
No, initramfs is maintained by the VFS and we already carry other patches.
If this applies cleanly, take them through it, I will be glad, thanks!
Quoted 2 lines
If you want the kstrtox changes to go another route then I will take the first two changes in a stable branch that can be merged.
I am fine with this route as long as the custom approach is gone.
Quoted 5 lines
Quoted 2 lines
I have tested this on x86, but I believe the same result will be on big-endian CPUs (I deduced that from how
strtox()works).Did you rerun the kunit tests the original change was part of or did you do some custom testing?
Quoted 6 lines
I'm not sure I understand the point. There were no test cases added for
simple_strntoul()AFAICS. Did I miss anything?(If I didn't that is the second point on why the patches didn't get enough
time for review and not every stakeholder seen them, usually we require the test cases for new APIs.)
Sorry, I meant the kunit tests that do test the initramfs unpacking.
Andy Shevchenko
reviewerQuoted 20 lines · Christian Brauner
Quoted 19 lines · Andy Shevchenko
Quoted 4 lines · Christian Brauner
Quoted 2 lines
Here is the refactoring to show that. This is assumed to go via PRINTK tree.
No, initramfs is maintained by the VFS and we already carry other patches.
If this applies cleanly, take them through it, I will be glad, thanks!
Quoted 2 lines
If you want the kstrtox changes to go another route then I will take the first two changes in a stable branch that can be merged.
I am fine with this route as long as the custom approach is gone.
Quoted 5 lines
Quoted 2 lines
I have tested this on x86, but I believe the same result will be on big-endian CPUs (I deduced that from how
strtox()works).Did you rerun the kunit tests the original change was part of or did you do some custom testing?
Quoted 8 lines
Quoted 6 lines
I'm not sure I understand the point. There were no test cases added for
simple_strntoul()AFAICS. Did I miss anything?(If I didn't that is the second point on why the patches didn't get enough
time for review and not every stakeholder seen them, usually we require the test cases for new APIs.)Sorry, I meant the kunit tests that do test the initramfs unpacking.
Nope, I run on the real HW with real initramfs and I saw the difference when code was under development (not working). The version I sent works good. But noted, next time I will run also above mentioned test cases, thanks for pointing that out.
Signature · 2 lines
With Best Regards, Andy Shevchenko