Commit 2bc40f89 authored by Prarit Bhargava's avatar Prarit Bhargava Committed by Greg Kroah-Hartman

kdb: Use strscpy with destination buffer size

[ Upstream commit c2b94c72 ]

gcc 8.1.0 warns with:

kernel/debug/kdb/kdb_support.c: In function ‘kallsyms_symbol_next’:
kernel/debug/kdb/kdb_support.c:239:4: warning: ‘strncpy’ specified bound depends on the length of the source argument [-Wstringop-overflow=]
     strncpy(prefix_name, name, strlen(name)+1);
     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
kernel/debug/kdb/kdb_support.c:239:31: note: length computed here

Use strscpy() with the destination buffer size, and use ellipses when
displaying truncated symbols.

v2: Use strscpy()
Signed-off-by: default avatarPrarit Bhargava <prarit@redhat.com>
Cc: Jonathan Toppins <jtoppins@redhat.com>
Cc: Jason Wessel <jason.wessel@windriver.com>
Cc: Daniel Thompson <daniel.thompson@linaro.org>
Cc: kgdb-bugreport@lists.sourceforge.net
Reviewed-by: default avatarDaniel Thompson <daniel.thompson@linaro.org>
Signed-off-by: default avatarDaniel Thompson <daniel.thompson@linaro.org>
Signed-off-by: default avatarSasha Levin <sashal@kernel.org>
parent 4dc84390
...@@ -216,7 +216,7 @@ static char *kdb_read(char *buffer, size_t bufsize) ...@@ -216,7 +216,7 @@ static char *kdb_read(char *buffer, size_t bufsize)
int count; int count;
int i; int i;
int diag, dtab_count; int diag, dtab_count;
int key; int key, buf_size, ret;
diag = kdbgetintenv("DTABCOUNT", &dtab_count); diag = kdbgetintenv("DTABCOUNT", &dtab_count);
...@@ -336,9 +336,8 @@ static char *kdb_read(char *buffer, size_t bufsize) ...@@ -336,9 +336,8 @@ static char *kdb_read(char *buffer, size_t bufsize)
else else
p_tmp = tmpbuffer; p_tmp = tmpbuffer;
len = strlen(p_tmp); len = strlen(p_tmp);
count = kallsyms_symbol_complete(p_tmp, buf_size = sizeof(tmpbuffer) - (p_tmp - tmpbuffer);
sizeof(tmpbuffer) - count = kallsyms_symbol_complete(p_tmp, buf_size);
(p_tmp - tmpbuffer));
if (tab == 2 && count > 0) { if (tab == 2 && count > 0) {
kdb_printf("\n%d symbols are found.", count); kdb_printf("\n%d symbols are found.", count);
if (count > dtab_count) { if (count > dtab_count) {
...@@ -350,9 +349,13 @@ static char *kdb_read(char *buffer, size_t bufsize) ...@@ -350,9 +349,13 @@ static char *kdb_read(char *buffer, size_t bufsize)
} }
kdb_printf("\n"); kdb_printf("\n");
for (i = 0; i < count; i++) { for (i = 0; i < count; i++) {
if (WARN_ON(!kallsyms_symbol_next(p_tmp, i))) ret = kallsyms_symbol_next(p_tmp, i, buf_size);
if (WARN_ON(!ret))
break; break;
if (ret != -E2BIG)
kdb_printf("%s ", p_tmp); kdb_printf("%s ", p_tmp);
else
kdb_printf("%s... ", p_tmp);
*(p_tmp + len) = '\0'; *(p_tmp + len) = '\0';
} }
if (i >= dtab_count) if (i >= dtab_count)
......
...@@ -83,7 +83,7 @@ typedef struct __ksymtab { ...@@ -83,7 +83,7 @@ typedef struct __ksymtab {
unsigned long sym_start; unsigned long sym_start;
unsigned long sym_end; unsigned long sym_end;
} kdb_symtab_t; } kdb_symtab_t;
extern int kallsyms_symbol_next(char *prefix_name, int flag); extern int kallsyms_symbol_next(char *prefix_name, int flag, int buf_size);
extern int kallsyms_symbol_complete(char *prefix_name, int max_len); extern int kallsyms_symbol_complete(char *prefix_name, int max_len);
/* Exported Symbols for kernel loadable modules to use. */ /* Exported Symbols for kernel loadable modules to use. */
......
...@@ -221,11 +221,13 @@ int kallsyms_symbol_complete(char *prefix_name, int max_len) ...@@ -221,11 +221,13 @@ int kallsyms_symbol_complete(char *prefix_name, int max_len)
* Parameters: * Parameters:
* prefix_name prefix of a symbol name to lookup * prefix_name prefix of a symbol name to lookup
* flag 0 means search from the head, 1 means continue search. * flag 0 means search from the head, 1 means continue search.
* buf_size maximum length that can be written to prefix_name
* buffer
* Returns: * Returns:
* 1 if a symbol matches the given prefix. * 1 if a symbol matches the given prefix.
* 0 if no string found * 0 if no string found
*/ */
int kallsyms_symbol_next(char *prefix_name, int flag) int kallsyms_symbol_next(char *prefix_name, int flag, int buf_size)
{ {
int prefix_len = strlen(prefix_name); int prefix_len = strlen(prefix_name);
static loff_t pos; static loff_t pos;
...@@ -235,10 +237,8 @@ int kallsyms_symbol_next(char *prefix_name, int flag) ...@@ -235,10 +237,8 @@ int kallsyms_symbol_next(char *prefix_name, int flag)
pos = 0; pos = 0;
while ((name = kdb_walk_kallsyms(&pos))) { while ((name = kdb_walk_kallsyms(&pos))) {
if (strncmp(name, prefix_name, prefix_len) == 0) { if (!strncmp(name, prefix_name, prefix_len))
strncpy(prefix_name, name, strlen(name)+1); return strscpy(prefix_name, name, buf_size);
return 1;
}
} }
return 0; return 0;
} }
......
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