Skip to content

Commit 5b7ce93

Browse files
committed
Merge tag 'kgdb-6.18-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/danielt/linux
Pull kgdb updates from Daniel Thompson: "A collection of small cleanups this cycle. Thorsten Blum has replaced a number strcpy() calls with safer alternatives (fixing a pointer aliasing bug in the process). Colin Ian King has simplified things by removing some unreachable code" * tag 'kgdb-6.18-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/danielt/linux: kdb: remove redundant check for scancode 0xe0 kdb: Replace deprecated strcpy() with helper function in kdb_defcmd() kdb: Replace deprecated strcpy() with memcpy() in parse_grep() kdb: Replace deprecated strcpy() with memmove() in vkdb_printf() kdb: Replace deprecated strcpy() with memcpy() in kdb_strdup() kernel: debug: gdbstub: Replace deprecated strcpy() with strscpy()
2 parents f3826aa + fdbdd0c commit 5b7ce93

6 files changed

Lines changed: 60 additions & 36 deletions

File tree

kernel/debug/gdbstub.c

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include <linux/kgdb.h>
3131
#include <linux/kdb.h>
3232
#include <linux/serial_core.h>
33+
#include <linux/string.h>
3334
#include <linux/reboot.h>
3435
#include <linux/uaccess.h>
3536
#include <asm/cacheflush.h>
@@ -547,7 +548,7 @@ static void gdb_cmd_setregs(struct kgdb_state *ks)
547548
error_packet(remcom_out_buffer, -EINVAL);
548549
} else {
549550
gdb_regs_to_pt_regs(gdb_regs, ks->linux_regs);
550-
strcpy(remcom_out_buffer, "OK");
551+
strscpy(remcom_out_buffer, "OK");
551552
}
552553
}
553554

@@ -577,7 +578,7 @@ static void gdb_cmd_memwrite(struct kgdb_state *ks)
577578
if (err)
578579
error_packet(remcom_out_buffer, err);
579580
else
580-
strcpy(remcom_out_buffer, "OK");
581+
strscpy(remcom_out_buffer, "OK");
581582
}
582583

583584
#if DBG_MAX_REG_NUM > 0
@@ -630,7 +631,7 @@ static void gdb_cmd_reg_set(struct kgdb_state *ks)
630631
i = i / 2;
631632
kgdb_hex2mem(ptr, (char *)gdb_regs, i);
632633
dbg_set_reg(regnum, gdb_regs, ks->linux_regs);
633-
strcpy(remcom_out_buffer, "OK");
634+
strscpy(remcom_out_buffer, "OK");
634635
}
635636
#endif /* DBG_MAX_REG_NUM > 0 */
636637

@@ -642,7 +643,7 @@ static void gdb_cmd_binwrite(struct kgdb_state *ks)
642643
if (err)
643644
error_packet(remcom_out_buffer, err);
644645
else
645-
strcpy(remcom_out_buffer, "OK");
646+
strscpy(remcom_out_buffer, "OK");
646647
}
647648

648649
/* Handle the 'D' or 'k', detach or kill packets */
@@ -656,7 +657,7 @@ static void gdb_cmd_detachkill(struct kgdb_state *ks)
656657
if (error < 0) {
657658
error_packet(remcom_out_buffer, error);
658659
} else {
659-
strcpy(remcom_out_buffer, "OK");
660+
strscpy(remcom_out_buffer, "OK");
660661
kgdb_connected = 0;
661662
}
662663
put_packet(remcom_out_buffer);
@@ -676,7 +677,7 @@ static int gdb_cmd_reboot(struct kgdb_state *ks)
676677
/* For now, only honor R0 */
677678
if (strcmp(remcom_in_buffer, "R0") == 0) {
678679
printk(KERN_CRIT "Executing emergency reboot\n");
679-
strcpy(remcom_out_buffer, "OK");
680+
strscpy(remcom_out_buffer, "OK");
680681
put_packet(remcom_out_buffer);
681682

682683
/*
@@ -739,7 +740,7 @@ static void gdb_cmd_query(struct kgdb_state *ks)
739740

740741
case 'C':
741742
/* Current thread id */
742-
strcpy(remcom_out_buffer, "QC");
743+
strscpy(remcom_out_buffer, "QC");
743744
ks->threadid = shadow_pid(current->pid);
744745
int_to_threadref(thref, ks->threadid);
745746
pack_threadid(remcom_out_buffer + 2, thref);
@@ -773,7 +774,7 @@ static void gdb_cmd_query(struct kgdb_state *ks)
773774
int len = strlen(remcom_in_buffer + 6);
774775

775776
if ((len % 2) != 0) {
776-
strcpy(remcom_out_buffer, "E01");
777+
strscpy(remcom_out_buffer, "E01");
777778
break;
778779
}
779780
kgdb_hex2mem(remcom_in_buffer + 6,
@@ -785,14 +786,14 @@ static void gdb_cmd_query(struct kgdb_state *ks)
785786
kdb_parse(remcom_out_buffer);
786787
kdb_common_deinit_state();
787788

788-
strcpy(remcom_out_buffer, "OK");
789+
strscpy(remcom_out_buffer, "OK");
789790
}
790791
break;
791792
#endif
792793
#ifdef CONFIG_HAVE_ARCH_KGDB_QXFER_PKT
793794
case 'S':
794795
if (!strncmp(remcom_in_buffer, "qSupported:", 11))
795-
strcpy(remcom_out_buffer, kgdb_arch_gdb_stub_feature);
796+
strscpy(remcom_out_buffer, kgdb_arch_gdb_stub_feature);
796797
break;
797798
case 'X':
798799
if (!strncmp(remcom_in_buffer, "qXfer:", 6))
@@ -822,7 +823,7 @@ static void gdb_cmd_task(struct kgdb_state *ks)
822823
}
823824
kgdb_usethread = thread;
824825
ks->kgdb_usethreadid = ks->threadid;
825-
strcpy(remcom_out_buffer, "OK");
826+
strscpy(remcom_out_buffer, "OK");
826827
break;
827828
case 'c':
828829
ptr = &remcom_in_buffer[2];
@@ -837,7 +838,7 @@ static void gdb_cmd_task(struct kgdb_state *ks)
837838
}
838839
kgdb_contthread = thread;
839840
}
840-
strcpy(remcom_out_buffer, "OK");
841+
strscpy(remcom_out_buffer, "OK");
841842
break;
842843
}
843844
}
@@ -851,7 +852,7 @@ static void gdb_cmd_thread(struct kgdb_state *ks)
851852
kgdb_hex2long(&ptr, &ks->threadid);
852853
thread = getthread(ks->linux_regs, ks->threadid);
853854
if (thread)
854-
strcpy(remcom_out_buffer, "OK");
855+
strscpy(remcom_out_buffer, "OK");
855856
else
856857
error_packet(remcom_out_buffer, -EINVAL);
857858
}
@@ -913,7 +914,7 @@ static void gdb_cmd_break(struct kgdb_state *ks)
913914
(int) length, *bpt_type - '0');
914915

915916
if (error == 0)
916-
strcpy(remcom_out_buffer, "OK");
917+
strscpy(remcom_out_buffer, "OK");
917918
else
918919
error_packet(remcom_out_buffer, error);
919920
}

kernel/debug/kdb/kdb_io.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -714,8 +714,8 @@ int vkdb_printf(enum kdb_msgsrc src, const char *fmt, va_list ap)
714714
* it, depending on the results of the search.
715715
*/
716716
cp++; /* to byte after the newline */
717-
replaced_byte = *cp; /* remember what/where it was */
718-
cphold = cp;
717+
replaced_byte = *cp; /* remember what it was */
718+
cphold = cp; /* remember where it was */
719719
*cp = '\0'; /* end the string for our search */
720720

721721
/*
@@ -732,8 +732,9 @@ int vkdb_printf(enum kdb_msgsrc src, const char *fmt, va_list ap)
732732
* Shift the buffer left.
733733
*/
734734
*cphold = replaced_byte;
735-
strcpy(kdb_buffer, cphold);
736-
len = strlen(kdb_buffer);
735+
len = strlen(cphold);
736+
/* Use memmove() because the buffers overlap */
737+
memmove(kdb_buffer, cphold, len + 1);
737738
next_avail = kdb_buffer + len;
738739
size_avail = sizeof(kdb_buffer) - len;
739740
goto kdb_print_out;
@@ -872,8 +873,9 @@ int vkdb_printf(enum kdb_msgsrc src, const char *fmt, va_list ap)
872873
*/
873874
if (kdb_grepping_flag && !suspend_grep) {
874875
*cphold = replaced_byte;
875-
strcpy(kdb_buffer, cphold);
876-
len = strlen(kdb_buffer);
876+
len = strlen(cphold);
877+
/* Use memmove() because the buffers overlap */
878+
memmove(kdb_buffer, cphold, len + 1);
877879
next_avail = kdb_buffer + len;
878880
size_avail = sizeof(kdb_buffer) - len;
879881
}

kernel/debug/kdb/kdb_keyboard.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,6 @@ int kdb_get_kbd_char(void)
145145
return CTRL('F');
146146
}
147147

148-
if (scancode == 0xe0)
149-
return -1;
150-
151148
/*
152149
* For Japanese 86/106 keyboards
153150
* See comment in drivers/char/pc_keyb.c.

kernel/debug/kdb/kdb_main.c

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -721,20 +721,12 @@ static int kdb_defcmd(int argc, const char **argv)
721721
mp->name = kdb_strdup(argv[1], GFP_KDB);
722722
if (!mp->name)
723723
goto fail_name;
724-
mp->usage = kdb_strdup(argv[2], GFP_KDB);
724+
mp->usage = kdb_strdup_dequote(argv[2], GFP_KDB);
725725
if (!mp->usage)
726726
goto fail_usage;
727-
mp->help = kdb_strdup(argv[3], GFP_KDB);
727+
mp->help = kdb_strdup_dequote(argv[3], GFP_KDB);
728728
if (!mp->help)
729729
goto fail_help;
730-
if (mp->usage[0] == '"') {
731-
strcpy(mp->usage, argv[2]+1);
732-
mp->usage[strlen(mp->usage)-1] = '\0';
733-
}
734-
if (mp->help[0] == '"') {
735-
strcpy(mp->help, argv[3]+1);
736-
mp->help[strlen(mp->help)-1] = '\0';
737-
}
738730

739731
INIT_LIST_HEAD(&kdb_macro->statements);
740732
defcmd_in_progress = true;
@@ -860,7 +852,7 @@ static void parse_grep(const char *str)
860852
kdb_printf("search string too long\n");
861853
return;
862854
}
863-
strcpy(kdb_grep_string, cp);
855+
memcpy(kdb_grep_string, cp, len + 1);
864856
kdb_grepping_flag++;
865857
return;
866858
}

kernel/debug/kdb/kdb_private.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ extern int kdbgetaddrarg(int, const char **, int*, unsigned long *,
110110
extern int kdbgetsymval(const char *, kdb_symtab_t *);
111111
extern int kdbnearsym(unsigned long, kdb_symtab_t *);
112112
extern char *kdb_strdup(const char *str, gfp_t type);
113+
extern char *kdb_strdup_dequote(const char *str, gfp_t type);
113114
extern void kdb_symbol_print(unsigned long, const kdb_symtab_t *, unsigned int);
114115

115116
/* Routine for debugging the debugger state. */

kernel/debug/kdb/kdb_support.c

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <linux/uaccess.h>
2424
#include <linux/kdb.h>
2525
#include <linux/slab.h>
26+
#include <linux/string.h>
2627
#include <linux/ctype.h>
2728
#include "kdb_private.h"
2829

@@ -246,11 +247,41 @@ void kdb_symbol_print(unsigned long addr, const kdb_symtab_t *symtab_p,
246247
*/
247248
char *kdb_strdup(const char *str, gfp_t type)
248249
{
249-
int n = strlen(str)+1;
250+
size_t n = strlen(str) + 1;
250251
char *s = kmalloc(n, type);
251252
if (!s)
252253
return NULL;
253-
return strcpy(s, str);
254+
memcpy(s, str, n);
255+
return s;
256+
}
257+
258+
/*
259+
* kdb_strdup_dequote - same as kdb_strdup(), but trims surrounding quotes from
260+
* the input string if present.
261+
* Remarks:
262+
* Quotes are only removed if there is both a leading and a trailing quote.
263+
*/
264+
char *kdb_strdup_dequote(const char *str, gfp_t type)
265+
{
266+
size_t len = strlen(str);
267+
char *s;
268+
269+
if (str[0] == '"' && len > 1 && str[len - 1] == '"') {
270+
/* trim both leading and trailing quotes */
271+
str++;
272+
len -= 2;
273+
}
274+
275+
len++; /* add space for NUL terminator */
276+
277+
s = kmalloc(len, type);
278+
if (!s)
279+
return NULL;
280+
281+
memcpy(s, str, len - 1);
282+
s[len - 1] = '\0';
283+
284+
return s;
254285
}
255286

256287
/*

0 commit comments

Comments
 (0)