Skip to content

Commit

Permalink
of: module: prevent NULL pointer dereference in vsnprintf()
Browse files Browse the repository at this point in the history
commit a1aa539 upstream.

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 <s.shtylyov@omp.ru>
Cc: stable@vger.kernel.org
Link: https://lore.kernel.org/r/1d211023-3923-685b-20f0-f3f90ea56e1f@omp.ru
Signed-off-by: Rob Herring <robh@kernel.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
  • Loading branch information
Sergey Shtylyov authored and gregkh committed Apr 10, 2024
1 parent ae6d76e commit 544561d
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions drivers/of/module.c
Expand Up @@ -16,6 +16,14 @@ ssize_t of_modalias(const struct device_node *np, char *str, ssize_t len)
ssize_t csize;
ssize_t tsize;

/*
* Prevent a kernel oops in vsnprintf() -- it only allows passing a
* NULL ptr when the length is also 0. Also filter out the negative
* lengths...
*/
if ((len > 0 && !str) || len < 0)
return -EINVAL;

/* Name & Type */
/* %p eats all alphanum characters, so %c must be used here */
csize = snprintf(str, len, "of:N%pOFn%c%s", np, 'T',
Expand Down

0 comments on commit 544561d

Please sign in to comment.