Skip to contentSkip to conversation

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

PATCH In review Benjamin Gray 3 participants 3 messages Mar 20, 2023 – Apr 16, 2023

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

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

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.

---

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/[email protected]/

---
 init/initramfs.c | 11 ++---------
 1 file changed, 2 insertions(+), 9 deletions(-)
init/initramfs.c +2 -9
index f6c112e30bd4..e7a01c2ccd1b 100644
@@ -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

Signature · 1 line
2.39.2
Reviewed-by on v1 1/1
#1 lore raw
Quoted 7 lines · Benjamin Gray

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.

Quoted 7 lines

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.

Signature · 2 lines
Andrew Donnellan    OzLabs, ADL Canberra
[email protected]   IBM Australia Limited
#2 lore raw
In reply to Benjamin Gray
Quoted 7 lines · Benjamin Gray

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.

Quoted 5 lines

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.

Applied to linux-kbuild.
Thanks.

Quoted 43 lines

---

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/[email protected]/

---
 init/initramfs.c | 11 ++---------
 1 file changed, 2 insertions(+), 9 deletions(-)
init/initramfs.c +2 -9
index f6c112e30bd4..e7a01c2ccd1b 100644
@@ -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

Signature · 2 lines
Best Regards
Masahiro Yamada