Skip to content

Commit cf7385c

Browse files
Sergey Shtylyovrobherring
authored andcommitted
of: module: add buffer overflow check in of_modalias()
In of_modalias(), if the buffer happens to be too small even for the 1st snprintf() call, the len parameter will become negative and str parameter (if not NULL initially) will point beyond the buffer's end. Add the buffer overflow check after the 1st snprintf() call and fix such check after the strlen() call (accounting for the terminating NUL char). Fixes: bc57506 ("of/device: use of_property_for_each_string to parse compatible strings") Signed-off-by: Sergey Shtylyov <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Rob Herring <[email protected]>
1 parent 649bad6 commit cf7385c

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

drivers/of/module.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,15 @@ ssize_t of_modalias(const struct device_node *np, char *str, ssize_t len)
2121
csize = snprintf(str, len, "of:N%pOFn%c%s", np, 'T',
2222
of_node_get_device_type(np));
2323
tsize = csize;
24+
if (csize >= len)
25+
csize = len > 0 ? len - 1 : 0;
2426
len -= csize;
25-
if (str)
26-
str += csize;
27+
str += csize;
2728

2829
of_property_for_each_string(np, "compatible", p, compat) {
2930
csize = strlen(compat) + 1;
3031
tsize += csize;
31-
if (csize > len)
32+
if (csize >= len)
3233
continue;
3334

3435
csize = snprintf(str, len, "C%s", compat);

0 commit comments

Comments
 (0)