]> git.ipfire.org Git - thirdparty/kernel/stable-queue.git/blame - releases/4.14.60/mmc-pwrseq-use-kmalloc_array-instead-of-stack-vla.patch
Fixes for 5.10
[thirdparty/kernel/stable-queue.git] / releases / 4.14.60 / mmc-pwrseq-use-kmalloc_array-instead-of-stack-vla.patch
CommitLineData
a65d4bac
GKH
1From foo@baz Sat Jul 28 10:25:26 CEST 2018
2From: "Tobin C. Harding" <me@tobin.cc>
3Date: Mon, 26 Mar 2018 17:33:14 +1100
4Subject: mmc: pwrseq: Use kmalloc_array instead of stack VLA
5
6From: "Tobin C. Harding" <me@tobin.cc>
7
8[ Upstream commit 486e6661367b40f927aadbed73237693396cbf94 ]
9
10The use of stack Variable Length Arrays needs to be avoided, as they
11can be a vector for stack exhaustion, which can be both a runtime bug
12(kernel Oops) or a security flaw (overwriting memory beyond the
13stack). Also, in general, as code evolves it is easy to lose track of
14how big a VLA can get. Thus, we can end up having runtime failures
15that are hard to debug. As part of the directive[1] to remove all VLAs
16from the kernel, and build with -Wvla.
17
18Currently driver is using a VLA declared using the number of descriptors. This
19array is used to store integer values and is later used as an argument to
20`gpiod_set_array_value_cansleep()` This can be avoided by using
21`kmalloc_array()` to allocate memory for the array of integer values. Memory is
22free'd before return from function.
23
24>From the code it appears that it is safe to sleep so we can use GFP_KERNEL
25(based _cansleep() suffix of function `gpiod_set_array_value_cansleep()`.
26
27It can be expected that this patch will result in a small increase in overhead
28due to the use of `kmalloc_array()`
29
30[1] https://lkml.org/lkml/2018/3/7/621
31
32Signed-off-by: Tobin C. Harding <me@tobin.cc>
33Signed-off-by: Ulf Hansson <ulf.hansson@linaro.org>
34Signed-off-by: Sasha Levin <alexander.levin@microsoft.com>
35Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
36---
37 drivers/mmc/core/pwrseq_simple.c | 14 +++++++++-----
38 1 file changed, 9 insertions(+), 5 deletions(-)
39
40--- a/drivers/mmc/core/pwrseq_simple.c
41+++ b/drivers/mmc/core/pwrseq_simple.c
42@@ -40,14 +40,18 @@ static void mmc_pwrseq_simple_set_gpios_
43 struct gpio_descs *reset_gpios = pwrseq->reset_gpios;
44
45 if (!IS_ERR(reset_gpios)) {
46- int i;
47- int values[reset_gpios->ndescs];
48+ int i, *values;
49+ int nvalues = reset_gpios->ndescs;
50
51- for (i = 0; i < reset_gpios->ndescs; i++)
52+ values = kmalloc_array(nvalues, sizeof(int), GFP_KERNEL);
53+ if (!values)
54+ return;
55+
56+ for (i = 0; i < nvalues; i++)
57 values[i] = value;
58
59- gpiod_set_array_value_cansleep(
60- reset_gpios->ndescs, reset_gpios->desc, values);
61+ gpiod_set_array_value_cansleep(nvalues, reset_gpios->desc, values);
62+ kfree(values);
63 }
64 }
65