1 From a1aa5390cc912934fee76ce80af5f940452fa987 Mon Sep 17 00:00:00 2001
2 From: Sergey Shtylyov <s.shtylyov@omp.ru>
3 Date: Wed, 27 Mar 2024 19:52:49 +0300
4 Subject: of: module: prevent NULL pointer dereference in vsnprintf()
6 From: Sergey Shtylyov <s.shtylyov@omp.ru>
8 commit a1aa5390cc912934fee76ce80af5f940452fa987 upstream.
10 In of_modalias(), we can get passed the str and len parameters which would
11 cause a kernel oops in vsnprintf() since it only allows passing a NULL ptr
12 when the length is also 0. Also, we need to filter out the negative values
13 of the len parameter as these will result in a really huge buffer since
14 snprintf() takes size_t parameter while ours is ssize_t...
16 Found by Linux Verification Center (linuxtesting.org) with the Svace static
19 Signed-off-by: Sergey Shtylyov <s.shtylyov@omp.ru>
20 Cc: stable@vger.kernel.org
21 Link: https://lore.kernel.org/r/1d211023-3923-685b-20f0-f3f90ea56e1f@omp.ru
22 Signed-off-by: Rob Herring <robh@kernel.org>
23 Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
25 drivers/of/module.c | 8 ++++++++
26 1 file changed, 8 insertions(+)
28 --- a/drivers/of/module.c
29 +++ b/drivers/of/module.c
30 @@ -16,6 +16,14 @@ ssize_t of_modalias(const struct device_
35 + * Prevent a kernel oops in vsnprintf() -- it only allows passing a
36 + * NULL ptr when the length is also 0. Also filter out the negative
39 + if ((len > 0 && !str) || len < 0)
43 /* %p eats all alphanum characters, so %c must be used here */
44 csize = snprintf(str, len, "of:N%pOFn%c%s", np, 'T',