[PATCH V3] init: Handle bootloader identifier in kernel parameters

Huacai Chen posted 1 patch 2 months, 2 weeks ago
There is a newer version of this series
init/main.c | 12 ++++++++++++
1 file changed, 12 insertions(+)
[PATCH V3] init: Handle bootloader identifier in kernel parameters
Posted by Huacai Chen 2 months, 2 weeks ago
BootLoader (Grub, LILO, etc) may pass an identifier such as "BOOT_IMAGE=
/boot/vmlinuz-x.y.z" to kernel parameters. But these identifiers are not
recognized by the kernel itself so will be passed to user space. However
user space init program also doesn't recognized it.

KEXEC/KDUMP (kexec-tools) may also pass an identifier such as "kexec" on
some architectures.

We cannot change BootLoader's behavior, because this behavior exists for
many years, and there are already user space programs search BOOT_IMAGE=
in /proc/cmdline to obtain the kernel image locations:

https://github.com/linuxdeepin/deepin-ab-recovery/blob/master/util.go
(search getBootOptions)
https://github.com/linuxdeepin/deepin-ab-recovery/blob/master/main.go
(search getKernelReleaseWithBootOption)

So the the best way is handle (ignore) it by the kernel itself, which
can avoid such boot warnings (if we use something like init=/bin/bash,
bootloader identifier can even cause a crash):

Kernel command line: BOOT_IMAGE=(hd0,1)/vmlinuz-6.x root=/dev/sda3 ro console=tty
Unknown kernel command line parameters "BOOT_IMAGE=(hd0,1)/vmlinuz-6.x", will be passed to user space.

Cc: stable@vger.kernel.org
Signed-off-by: Huacai Chen <chenhuacai@loongson.cn>
---
V2: Update comments and commit messages.
V3: Document bootloader identifiers.

 init/main.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/init/main.c b/init/main.c
index 225a58279acd..b25e7da5347a 100644
--- a/init/main.c
+++ b/init/main.c
@@ -545,6 +545,12 @@ static int __init unknown_bootoption(char *param, char *val,
 				     const char *unused, void *arg)
 {
 	size_t len = strlen(param);
+	/*
+	 * Well-known bootloader identifiers:
+	 * 1. LILO/Grub pass "BOOT_IMAGE=...";
+	 * 2. kexec/kdump (kexec-tools) pass "kexec".
+	 */
+	const char *bootloader[] = { "BOOT_IMAGE=", "kexec", NULL };
 
 	/* Handle params aliased to sysctls */
 	if (sysctl_is_alias(param))
@@ -552,6 +558,12 @@ static int __init unknown_bootoption(char *param, char *val,
 
 	repair_env_string(param, val);
 
+	/* Handle bootloader identifier */
+	for (int i = 0; bootloader[i]; i++) {
+		if (!strncmp(param, bootloader[i], strlen(bootloader[i])))
+			return 0;
+	}
+
 	/* Handle obsolete-style parameters */
 	if (obsolete_checksetup(param))
 		return 0;
-- 
2.47.3
Re: [PATCH V3] init: Handle bootloader identifier in kernel parameters
Posted by Andrew Morton 1 month, 3 weeks ago
On Mon, 21 Jul 2025 18:13:43 +0800 Huacai Chen <chenhuacai@loongson.cn> wrote:

> BootLoader (Grub, LILO, etc) may pass an identifier such as "BOOT_IMAGE=
> /boot/vmlinuz-x.y.z" to kernel parameters. But these identifiers are not
> recognized by the kernel itself so will be passed to user space. However
> user space init program also doesn't recognized it.
> 
> KEXEC/KDUMP (kexec-tools) may also pass an identifier such as "kexec" on
> some architectures.
> 
> We cannot change BootLoader's behavior, because this behavior exists for
> many years, and there are already user space programs search BOOT_IMAGE=
> in /proc/cmdline to obtain the kernel image locations:
> 
> https://github.com/linuxdeepin/deepin-ab-recovery/blob/master/util.go
> (search getBootOptions)
> https://github.com/linuxdeepin/deepin-ab-recovery/blob/master/main.go
> (search getKernelReleaseWithBootOption)
> 
> So the the best way is handle (ignore) it by the kernel itself, which
> can avoid such boot warnings (if we use something like init=/bin/bash,
> bootloader identifier can even cause a crash):
> 
> Kernel command line: BOOT_IMAGE=(hd0,1)/vmlinuz-6.x root=/dev/sda3 ro console=tty
> Unknown kernel command line parameters "BOOT_IMAGE=(hd0,1)/vmlinuz-6.x", will be passed to user space.
> 
> Cc: stable@vger.kernel.org

I think I'll keep this in -next until 6.18-rc1 - I suspect any issues
here will take a while to discover.

> --- a/init/main.c
> +++ b/init/main.c
> @@ -545,6 +545,12 @@ static int __init unknown_bootoption(char *param, char *val,
>  				     const char *unused, void *arg)
>  {
>  	size_t len = strlen(param);
> +	/*
> +	 * Well-known bootloader identifiers:
> +	 * 1. LILO/Grub pass "BOOT_IMAGE=...";
> +	 * 2. kexec/kdump (kexec-tools) pass "kexec".
> +	 */
> +	const char *bootloader[] = { "BOOT_IMAGE=", "kexec", NULL };
>  
>  	/* Handle params aliased to sysctls */
>  	if (sysctl_is_alias(param))
> @@ -552,6 +558,12 @@ static int __init unknown_bootoption(char *param, char *val,
>  
>  	repair_env_string(param, val);
>  
> +	/* Handle bootloader identifier */
> +	for (int i = 0; bootloader[i]; i++) {
> +		if (!strncmp(param, bootloader[i], strlen(bootloader[i])))
> +			return 0;
> +	}

We have str_has_prefix().

And strstarts()!  Both of which are awfully similar and both of which
lamely do two passes across a string.
Re: [PATCH V3] init: Handle bootloader identifier in kernel parameters
Posted by Huacai Chen 1 month, 3 weeks ago
On Tue, Aug 12, 2025 at 9:56 AM Andrew Morton <akpm@linux-foundation.org> wrote:
>
> On Mon, 21 Jul 2025 18:13:43 +0800 Huacai Chen <chenhuacai@loongson.cn> wrote:
>
> > BootLoader (Grub, LILO, etc) may pass an identifier such as "BOOT_IMAGE=
> > /boot/vmlinuz-x.y.z" to kernel parameters. But these identifiers are not
> > recognized by the kernel itself so will be passed to user space. However
> > user space init program also doesn't recognized it.
> >
> > KEXEC/KDUMP (kexec-tools) may also pass an identifier such as "kexec" on
> > some architectures.
> >
> > We cannot change BootLoader's behavior, because this behavior exists for
> > many years, and there are already user space programs search BOOT_IMAGE=
> > in /proc/cmdline to obtain the kernel image locations:
> >
> > https://github.com/linuxdeepin/deepin-ab-recovery/blob/master/util.go
> > (search getBootOptions)
> > https://github.com/linuxdeepin/deepin-ab-recovery/blob/master/main.go
> > (search getKernelReleaseWithBootOption)
> >
> > So the the best way is handle (ignore) it by the kernel itself, which
> > can avoid such boot warnings (if we use something like init=/bin/bash,
> > bootloader identifier can even cause a crash):
> >
> > Kernel command line: BOOT_IMAGE=(hd0,1)/vmlinuz-6.x root=/dev/sda3 ro console=tty
> > Unknown kernel command line parameters "BOOT_IMAGE=(hd0,1)/vmlinuz-6.x", will be passed to user space.
> >
> > Cc: stable@vger.kernel.org
>
> I think I'll keep this in -next until 6.18-rc1 - I suspect any issues
> here will take a while to discover.
>
> > --- a/init/main.c
> > +++ b/init/main.c
> > @@ -545,6 +545,12 @@ static int __init unknown_bootoption(char *param, char *val,
> >                                    const char *unused, void *arg)
> >  {
> >       size_t len = strlen(param);
> > +     /*
> > +      * Well-known bootloader identifiers:
> > +      * 1. LILO/Grub pass "BOOT_IMAGE=...";
> > +      * 2. kexec/kdump (kexec-tools) pass "kexec".
> > +      */
> > +     const char *bootloader[] = { "BOOT_IMAGE=", "kexec", NULL };
> >
> >       /* Handle params aliased to sysctls */
> >       if (sysctl_is_alias(param))
> > @@ -552,6 +558,12 @@ static int __init unknown_bootoption(char *param, char *val,
> >
> >       repair_env_string(param, val);
> >
> > +     /* Handle bootloader identifier */
> > +     for (int i = 0; bootloader[i]; i++) {
> > +             if (!strncmp(param, bootloader[i], strlen(bootloader[i])))
> > +                     return 0;
> > +     }
>
> We have str_has_prefix().
>
> And strstarts()!  Both of which are awfully similar and both of which
> lamely do two passes across a string.
OK, I will send V4 with strstarts().

Huacai
Re: [PATCH V3] init: Handle bootloader identifier in kernel parameters
Posted by Huacai Chen 1 month, 3 weeks ago
Hi, Andrew,

Can you take this patch to linux-mm (seems the best tree for it)?

Huacai

On Mon, Jul 21, 2025 at 6:14 PM Huacai Chen <chenhuacai@loongson.cn> wrote:
>
> BootLoader (Grub, LILO, etc) may pass an identifier such as "BOOT_IMAGE=
> /boot/vmlinuz-x.y.z" to kernel parameters. But these identifiers are not
> recognized by the kernel itself so will be passed to user space. However
> user space init program also doesn't recognized it.
>
> KEXEC/KDUMP (kexec-tools) may also pass an identifier such as "kexec" on
> some architectures.
>
> We cannot change BootLoader's behavior, because this behavior exists for
> many years, and there are already user space programs search BOOT_IMAGE=
> in /proc/cmdline to obtain the kernel image locations:
>
> https://github.com/linuxdeepin/deepin-ab-recovery/blob/master/util.go
> (search getBootOptions)
> https://github.com/linuxdeepin/deepin-ab-recovery/blob/master/main.go
> (search getKernelReleaseWithBootOption)
>
> So the the best way is handle (ignore) it by the kernel itself, which
> can avoid such boot warnings (if we use something like init=/bin/bash,
> bootloader identifier can even cause a crash):
>
> Kernel command line: BOOT_IMAGE=(hd0,1)/vmlinuz-6.x root=/dev/sda3 ro console=tty
> Unknown kernel command line parameters "BOOT_IMAGE=(hd0,1)/vmlinuz-6.x", will be passed to user space.
>
> Cc: stable@vger.kernel.org
> Signed-off-by: Huacai Chen <chenhuacai@loongson.cn>
> ---
> V2: Update comments and commit messages.
> V3: Document bootloader identifiers.
>
>  init/main.c | 12 ++++++++++++
>  1 file changed, 12 insertions(+)
>
> diff --git a/init/main.c b/init/main.c
> index 225a58279acd..b25e7da5347a 100644
> --- a/init/main.c
> +++ b/init/main.c
> @@ -545,6 +545,12 @@ static int __init unknown_bootoption(char *param, char *val,
>                                      const char *unused, void *arg)
>  {
>         size_t len = strlen(param);
> +       /*
> +        * Well-known bootloader identifiers:
> +        * 1. LILO/Grub pass "BOOT_IMAGE=...";
> +        * 2. kexec/kdump (kexec-tools) pass "kexec".
> +        */
> +       const char *bootloader[] = { "BOOT_IMAGE=", "kexec", NULL };
>
>         /* Handle params aliased to sysctls */
>         if (sysctl_is_alias(param))
> @@ -552,6 +558,12 @@ static int __init unknown_bootoption(char *param, char *val,
>
>         repair_env_string(param, val);
>
> +       /* Handle bootloader identifier */
> +       for (int i = 0; bootloader[i]; i++) {
> +               if (!strncmp(param, bootloader[i], strlen(bootloader[i])))
> +                       return 0;
> +       }
> +
>         /* Handle obsolete-style parameters */
>         if (obsolete_checksetup(param))
>                 return 0;
> --
> 2.47.3
>