Skip to content

Commit a1aa539

Browse files
Sergey Shtylyovrobherring
authored andcommitted
of: module: prevent NULL pointer dereference in vsnprintf()
In of_modalias(), we can get passed the str and len parameters which would cause a kernel oops in vsnprintf() since it only allows passing a NULL ptr when the length is also 0. Also, we need to filter out the negative values of the len parameter as these will result in a really huge buffer since snprintf() takes size_t parameter while ours is ssize_t... Found by Linux Verification Center (linuxtesting.org) with the Svace static analysis tool. Signed-off-by: Sergey Shtylyov <[email protected]> Cc: [email protected] Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Rob Herring <[email protected]>
1 parent b5237d0 commit a1aa539

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

drivers/of/module.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ ssize_t of_modalias(const struct device_node *np, char *str, ssize_t len)
1616
ssize_t csize;
1717
ssize_t tsize;
1818

19+
/*
20+
* Prevent a kernel oops in vsnprintf() -- it only allows passing a
21+
* NULL ptr when the length is also 0. Also filter out the negative
22+
* lengths...
23+
*/
24+
if ((len > 0 && !str) || len < 0)
25+
return -EINVAL;
26+
1927
/* Name & Type */
2028
/* %p eats all alphanum characters, so %c must be used here */
2129
csize = snprintf(str, len, "of:N%pOFn%c%s", np, 'T',

0 commit comments

Comments
 (0)