diff mbox series

init/initramfs: Fix argument forwarding to panic() in panic_show_mem()

Message ID 20230320230534.50174-1-bgray@linux.ibm.com (mailing list archive)
State New, archived
Headers show
Series init/initramfs: Fix argument forwarding to panic() in panic_show_mem() | expand

Commit Message

Benjamin Gray March 20, 2023, 11:05 p.m. UTC
Forwarding variadic argument lists can't be done by passing a va_list
to a function with signature foo(...) (as panic() has). It ends up
interpreting the va_list itself as a single argument instead of
iterating it. printf() happily accepts it of course, leading to corrupt
output.

Convert panic_show_mem() to a macro to allow forwarding the arguments.
The function is trivial enough that it's easier than trying to introduce
a vpanic() variant.

Signed-off-by: Benjamin Gray <bgray@linux.ibm.com>

---

After sending these patches [1] I wondered why the kernel accepted a
corrupt archive. The streaming parser makes it difficult to see
where to add a completeness check (possibly can assert the state is
Start or Reset at the end?), but adding an error() to cover my issue
revealed that the error message was never printed.

[1]: https://lore.kernel.org/all/20230320040839.660475-1-bgray@linux.ibm.com/
---
 init/initramfs.c | 11 ++---------
 1 file changed, 2 insertions(+), 9 deletions(-)


base-commit: 065ffaee73892e8a3629b4cfbe635697807a3c6f
prerequisite-patch-id: 6e3cfc6bf9c5686ad29c7feed8e283d30b1957fd
prerequisite-patch-id: 933a7bd2f29223dba7f2cac7e9aa72aae730292d

Comments

Andrew Donnellan March 22, 2023, 5:29 a.m. UTC | #1
On Tue, 2023-03-21 at 10:05 +1100, Benjamin Gray wrote:
> Forwarding variadic argument lists can't be done by passing a va_list
> to a function with signature foo(...) (as panic() has). It ends up
> interpreting the va_list itself as a single argument instead of
> iterating it. printf() happily accepts it of course, leading to
> corrupt
> output.
> 
> Convert panic_show_mem() to a macro to allow forwarding the
> arguments.
> The function is trivial enough that it's easier than trying to
> introduce
> a vpanic() variant.
> 
> Signed-off-by: Benjamin Gray <bgray@linux.ibm.com>

Reviewed-by: Andrew Donnellan <ajd@linux.ibm.com>
Masahiro Yamada April 16, 2023, 8:45 a.m. UTC | #2
On Tue, Mar 21, 2023 at 8:06 AM Benjamin Gray <bgray@linux.ibm.com> wrote:
>
> Forwarding variadic argument lists can't be done by passing a va_list
> to a function with signature foo(...) (as panic() has). It ends up
> interpreting the va_list itself as a single argument instead of
> iterating it. printf() happily accepts it of course, leading to corrupt
> output.
>
> Convert panic_show_mem() to a macro to allow forwarding the arguments.
> The function is trivial enough that it's easier than trying to introduce
> a vpanic() variant.
>
> Signed-off-by: Benjamin Gray <bgray@linux.ibm.com>


Applied to linux-kbuild.
Thanks.




>
> ---
>
> After sending these patches [1] I wondered why the kernel accepted a
> corrupt archive. The streaming parser makes it difficult to see
> where to add a completeness check (possibly can assert the state is
> Start or Reset at the end?), but adding an error() to cover my issue
> revealed that the error message was never printed.
>
> [1]: https://lore.kernel.org/all/20230320040839.660475-1-bgray@linux.ibm.com/
> ---
>  init/initramfs.c | 11 ++---------
>  1 file changed, 2 insertions(+), 9 deletions(-)
>
> diff --git a/init/initramfs.c b/init/initramfs.c
> index f6c112e30bd4..e7a01c2ccd1b 100644
> --- a/init/initramfs.c
> +++ b/init/initramfs.c
> @@ -60,15 +60,8 @@ static void __init error(char *x)
>                 message = x;
>  }
>
> -static void panic_show_mem(const char *fmt, ...)
> -{
> -       va_list args;
> -
> -       show_mem(0, NULL);
> -       va_start(args, fmt);
> -       panic(fmt, args);
> -       va_end(args);
> -}
> +#define panic_show_mem(fmt, ...) \
> +       ({ show_mem(0, NULL); panic(fmt, ##__VA_ARGS__); })
>
>  /* link hash */
>
>
> base-commit: 065ffaee73892e8a3629b4cfbe635697807a3c6f
> prerequisite-patch-id: 6e3cfc6bf9c5686ad29c7feed8e283d30b1957fd
> prerequisite-patch-id: 933a7bd2f29223dba7f2cac7e9aa72aae730292d
> --
> 2.39.2
>
diff mbox series

Patch

diff --git a/init/initramfs.c b/init/initramfs.c
index f6c112e30bd4..e7a01c2ccd1b 100644
--- a/init/initramfs.c
+++ b/init/initramfs.c
@@ -60,15 +60,8 @@  static void __init error(char *x)
 		message = x;
 }
 
-static void panic_show_mem(const char *fmt, ...)
-{
-	va_list args;
-
-	show_mem(0, NULL);
-	va_start(args, fmt);
-	panic(fmt, args);
-	va_end(args);
-}
+#define panic_show_mem(fmt, ...) \
+	({ show_mem(0, NULL); panic(fmt, ##__VA_ARGS__); })
 
 /* link hash */