* [PATCH] of: unittest: Remove VLA stack usage
@ 2018-03-12 4:27 Tobin C. Harding
2018-03-18 12:48 ` Rob Herring
0 siblings, 1 reply; 2+ messages in thread
From: Tobin C. Harding @ 2018-03-12 4:27 UTC (permalink / raw)
To: Rob Herring, Frank Rowand
Cc: Tobin C. Harding, devicetree, linux-kernel, kernel-hardening,
Tycho Andersen, Kees Cook
The kernel would like to have all stack VLA usage removed[1]. This is a
test function so the execution speed is not critical. We can allocate
memory for this buffer instead of using a VLA. If kmalloc() fails just
return.
Allocate buffer with kmalloc().
[1]: https://lkml.org/lkml/2018/3/7/621
Signed-off-by: Tobin C. Harding <me@tobin.cc>
---
Kees is this annoying you, CC'ing you an all my VLA patches?
drivers/of/unittest.c | 15 +++++++++++----
1 file changed, 11 insertions(+), 4 deletions(-)
diff --git a/drivers/of/unittest.c b/drivers/of/unittest.c
index 1991fe4319f5..38cbc343b7da 100644
--- a/drivers/of/unittest.c
+++ b/drivers/of/unittest.c
@@ -254,12 +254,18 @@ static void __init of_unittest_check_tree_linkage(void)
static void __init of_unittest_printf_one(struct device_node *np, const char *fmt,
const char *expected)
{
- unsigned char buf[strlen(expected)+10];
+ unsigned char *buf;
+ int buf_size;
int size, i;
+ buf_size = strlen(expected) + 10;
+ buf = kmalloc(buf_size, GFP_KERNEL);
+ if (!buf)
+ return;
+
/* Baseline; check conversion with a large size limit */
- memset(buf, 0xff, sizeof(buf));
- size = snprintf(buf, sizeof(buf) - 2, fmt, np);
+ memset(buf, 0xff, buf_size);
+ size = snprintf(buf, buf_size - 2, fmt, np);
/* use strcmp() instead of strncmp() here to be absolutely sure strings match */
unittest((strcmp(buf, expected) == 0) && (buf[size+1] == 0xff),
@@ -270,12 +276,13 @@ static void __init of_unittest_printf_one(struct device_node *np, const char *fm
size++;
for (i = 0; i < 2; i++, size--) {
/* Clear the buffer, and make sure it works correctly still */
- memset(buf, 0xff, sizeof(buf));
+ memset(buf, 0xff, buf_size);
snprintf(buf, size+1, fmt, np);
unittest(strncmp(buf, expected, size) == 0 && (buf[size+1] == 0xff),
"snprintf failed; size=%i fmt='%s' expected='%s' rslt='%s'\n",
size, fmt, expected, buf);
}
+ kfree(buf);
}
static void __init of_unittest_printf(void)
--
2.7.4
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH] of: unittest: Remove VLA stack usage
2018-03-12 4:27 [PATCH] of: unittest: Remove VLA stack usage Tobin C. Harding
@ 2018-03-18 12:48 ` Rob Herring
0 siblings, 0 replies; 2+ messages in thread
From: Rob Herring @ 2018-03-18 12:48 UTC (permalink / raw)
To: Tobin C. Harding
Cc: Frank Rowand, devicetree, linux-kernel, kernel-hardening,
Tycho Andersen, Kees Cook
On Mon, Mar 12, 2018 at 03:27:23PM +1100, Tobin C. Harding wrote:
> The kernel would like to have all stack VLA usage removed[1]. This is a
> test function so the execution speed is not critical. We can allocate
> memory for this buffer instead of using a VLA. If kmalloc() fails just
> return.
>
> Allocate buffer with kmalloc().
>
> [1]: https://lkml.org/lkml/2018/3/7/621
>
> Signed-off-by: Tobin C. Harding <me@tobin.cc>
> ---
>
> Kees is this annoying you, CC'ing you an all my VLA patches?
>
> drivers/of/unittest.c | 15 +++++++++++----
> 1 file changed, 11 insertions(+), 4 deletions(-)
Applied.
Rob
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2018-03-18 12:48 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2018-03-12 4:27 [PATCH] of: unittest: Remove VLA stack usage Tobin C. Harding
2018-03-18 12:48 ` Rob Herring
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).