Skip to content

Commit 2bc40f8

Browse files
praritgregkh
authored andcommitted
kdb: Use strscpy with destination buffer size
[ Upstream commit c2b94c7 ] 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: Prarit 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: Daniel Thompson <daniel.thompson@linaro.org> Signed-off-by: Daniel Thompson <daniel.thompson@linaro.org> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 4dc8439 commit 2bc40f8

3 files changed

Lines changed: 15 additions & 12 deletions

File tree

‎kernel/debug/kdb/kdb_io.c‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ static char *kdb_read(char *buffer, size_t bufsize)
216216
int count;
217217
int i;
218218
int diag, dtab_count;
219-
int key;
219+
int key, buf_size, ret;
220220

221221

222222
diag = kdbgetintenv("DTABCOUNT", &dtab_count);
@@ -336,9 +336,8 @@ static char *kdb_read(char *buffer, size_t bufsize)
336336
else
337337
p_tmp = tmpbuffer;
338338
len = strlen(p_tmp);
339-
count = kallsyms_symbol_complete(p_tmp,
340-
sizeof(tmpbuffer) -
341-
(p_tmp - tmpbuffer));
339+
buf_size = sizeof(tmpbuffer) - (p_tmp - tmpbuffer);
340+
count = kallsyms_symbol_complete(p_tmp, buf_size);
342341
if (tab == 2 && count > 0) {
343342
kdb_printf("\n%d symbols are found.", count);
344343
if (count > dtab_count) {
@@ -350,9 +349,13 @@ static char *kdb_read(char *buffer, size_t bufsize)
350349
}
351350
kdb_printf("\n");
352351
for (i = 0; i < count; i++) {
353-
if (WARN_ON(!kallsyms_symbol_next(p_tmp, i)))
352+
ret = kallsyms_symbol_next(p_tmp, i, buf_size);
353+
if (WARN_ON(!ret))
354354
break;
355-
kdb_printf("%s ", p_tmp);
355+
if (ret != -E2BIG)
356+
kdb_printf("%s ", p_tmp);
357+
else
358+
kdb_printf("%s... ", p_tmp);
356359
*(p_tmp + len) = '\0';
357360
}
358361
if (i >= dtab_count)

‎kernel/debug/kdb/kdb_private.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ typedef struct __ksymtab {
8383
unsigned long sym_start;
8484
unsigned long sym_end;
8585
} kdb_symtab_t;
86-
extern int kallsyms_symbol_next(char *prefix_name, int flag);
86+
extern int kallsyms_symbol_next(char *prefix_name, int flag, int buf_size);
8787
extern int kallsyms_symbol_complete(char *prefix_name, int max_len);
8888

8989
/* Exported Symbols for kernel loadable modules to use. */

‎kernel/debug/kdb/kdb_support.c‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -221,11 +221,13 @@ int kallsyms_symbol_complete(char *prefix_name, int max_len)
221221
* Parameters:
222222
* prefix_name prefix of a symbol name to lookup
223223
* flag 0 means search from the head, 1 means continue search.
224+
* buf_size maximum length that can be written to prefix_name
225+
* buffer
224226
* Returns:
225227
* 1 if a symbol matches the given prefix.
226228
* 0 if no string found
227229
*/
228-
int kallsyms_symbol_next(char *prefix_name, int flag)
230+
int kallsyms_symbol_next(char *prefix_name, int flag, int buf_size)
229231
{
230232
int prefix_len = strlen(prefix_name);
231233
static loff_t pos;
@@ -235,10 +237,8 @@ int kallsyms_symbol_next(char *prefix_name, int flag)
235237
pos = 0;
236238

237239
while ((name = kdb_walk_kallsyms(&pos))) {
238-
if (strncmp(name, prefix_name, prefix_len) == 0) {
239-
strncpy(prefix_name, name, strlen(name)+1);
240-
return 1;
241-
}
240+
if (!strncmp(name, prefix_name, prefix_len))
241+
return strscpy(prefix_name, name, buf_size);
242242
}
243243
return 0;
244244
}

0 commit comments

Comments
 (0)