Commit 8c2a75e5 authored by Bjorn Andersson's avatar Bjorn Andersson Committed by Rob Herring

of/device: Fix of_device_get_modalias() buffer handling

of_device_request_module() calls of_device_get_modalias() with "len" 0,
to calculate the size of the buffer needed to store the result, but due
to integer promotion the ssize_t "len" will be compared as unsigned with
strlen(compat) and the loop will generally never break. This results in
a call to snprintf() with a negative len, which triggers below warning,
followed by a dereference of a invalid pointer:

  [    3.060067] WARNING: CPU: 0 PID: 51 at lib/vsprintf.c:2122 vsnprintf+0x348/0x6d8
  ...
  [    3.060301] [<ffffff800891ede8>] vsnprintf+0x348/0x6d8
  [    3.060308] [<ffffff800891f248>] snprintf+0x48/0x50
  [    3.060316] [<ffffff80086a7c80>] of_device_get_modalias+0x108/0x160
  [    3.060322] [<ffffff80086a7cf8>] of_device_request_module+0x20/0x88
  ...

Further more of_device_get_modalias() is supposed to return the number
of bytes needed to store the entire modalias, so the loop needs to
continue accumulate the total size even though the buffer is full.

Finally the function is not expected to ensure space for the NUL, nor
include it in the returned size, so only 1 should be added to the length
of "compat" in the loop (to account for the character 'C').

Fixes: bc575064 ("of/device: use of_property_for_each_string to parse compatible strings")
Cc: Rob Herring <robh@kernel.org>
Signed-off-by: default avatarBjorn Andersson <bjorn.andersson@linaro.org>
Signed-off-by: default avatarRob Herring <robh@kernel.org>
parent 08ab58d9
...@@ -195,10 +195,11 @@ EXPORT_SYMBOL(of_device_get_match_data); ...@@ -195,10 +195,11 @@ EXPORT_SYMBOL(of_device_get_match_data);
static ssize_t of_device_get_modalias(struct device *dev, char *str, ssize_t len) static ssize_t of_device_get_modalias(struct device *dev, char *str, ssize_t len)
{ {
const char *compat, *start = str; const char *compat;
char *c; char *c;
struct property *p; struct property *p;
ssize_t csize; ssize_t csize;
ssize_t tsize;
if ((!dev) || (!dev->of_node)) if ((!dev) || (!dev->of_node))
return -ENODEV; return -ENODEV;
...@@ -206,12 +207,16 @@ static ssize_t of_device_get_modalias(struct device *dev, char *str, ssize_t len ...@@ -206,12 +207,16 @@ static ssize_t of_device_get_modalias(struct device *dev, char *str, ssize_t len
/* Name & Type */ /* Name & Type */
csize = snprintf(str, len, "of:N%sT%s", dev->of_node->name, csize = snprintf(str, len, "of:N%sT%s", dev->of_node->name,
dev->of_node->type); dev->of_node->type);
tsize = csize;
len -= csize; len -= csize;
str += csize; if (str)
str += csize;
of_property_for_each_string(dev->of_node, "compatible", p, compat) { of_property_for_each_string(dev->of_node, "compatible", p, compat) {
if (strlen(compat) + 2 > len) csize = strlen(compat) + 1;
break; tsize += csize;
if (csize > len)
continue;
csize = snprintf(str, len, "C%s", compat); csize = snprintf(str, len, "C%s", compat);
for (c = str; c; ) { for (c = str; c; ) {
...@@ -223,7 +228,7 @@ static ssize_t of_device_get_modalias(struct device *dev, char *str, ssize_t len ...@@ -223,7 +228,7 @@ static ssize_t of_device_get_modalias(struct device *dev, char *str, ssize_t len
str += csize; str += csize;
} }
return str - start; return tsize;
} }
int of_device_request_module(struct device *dev) int of_device_request_module(struct device *dev)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment