History

kdb: Replace the use of simple_strto with safer kstrto in kdb_main

The simple_str* family of functions perform no error checking in scenarios where the input value overflows the intended output variable. This results in these functions successfully returning even when the output does not match the input string. Or as it was mentioned [1], "...simple_strtol(), simple_strtoll(), simple_strtoul(), and simple_strtoull() functions explicitly ignore overflows, which may lead to unexpected results in callers." Hence, the use of those functions is discouraged. This patch replaces all uses of the simple_strto* series of functions with their safer kstrto* alternatives. Side effects of this patch: - Every string to long or long long conversion using kstrto* is now checked for failure. - kstrto* errors are handled with appropriate `KDB_BADINT` wherever applicable. - A good side effect is that we end up saving a few lines of code since unlike in simple_strto* functions, kstrto functions do not need an additional "end pointer" variable, and the return values of the latter can be directly checked in an "if" statement without the need to define additional `ret` or `err` variables. This, of course, results in cleaner, yet still easy to understand code. [1] https://www.kernel.org/doc/html/latest/process/deprecated.html#simple-strtol-simple-strtoll-simple-strtoul-simple-strtoull

Yuran Pereira committed 2 years ago

Commit 120fb871 parent 81983751 file · +21 −48Browse files

Showing the changes to kernel/debug (1 of 1 file). Show all

Unified · Split

kernel/debug/kdb/kdb_main.c+21−48
@@ -306,8 +306,8 @@ static int kdbgetulenv(const char *match, unsigned long *value) · 305 unmodified lines
306 return KDB_NOTENV;306 return KDB_NOTENV;
307 if (strlen(ep) == 0)307 if (strlen(ep) == 0)
308 return KDB_NOENVVALUE;308 return KDB_NOENVVALUE;
309 309 if (kstrtoul(ep, 0, value))
310 *value = simple_strtoul(ep, NULL, 0);310 return KDB_BADINT;
311 311
312 return 0;312 return 0;
313}313}
@@ -402,42 +402,23 @@ static void kdb_printenv(void) · 88 unmodified lines
402 */402 */
403int kdbgetularg(const char *arg, unsigned long *value)403int kdbgetularg(const char *arg, unsigned long *value)
404{404{
405 char *endp;405 /*
406 unsigned long val;406 * If the first fails, also try base 16, for us
407 407 * folks too lazy to type the leading 0x...
408 val = simple_strtoul(arg, &endp, 0);408 */
409 409 if (kstrtoul(arg, 0, value)) {
410 if (endp == arg) {410 if (kstrtoul(arg, 16, value))
411 /*
412 * Also try base 16, for us folks too lazy to type the
413 * leading 0x...
414 */
415 val = simple_strtoul(arg, &endp, 16);
416 if (endp == arg)
417 return KDB_BADINT;411 return KDB_BADINT;
418 }412 }
419
420 *value = val;
421
422 return 0;413 return 0;
423}414}
424 415
425int kdbgetu64arg(const char *arg, u64 *value)416int kdbgetu64arg(const char *arg, u64 *value)
426{417{
427 char *endp;418 if (kstrtou64(arg, 0, value)) {
428 u64 val;419 if (kstrtou64(arg, 16, value))
429
430 val = simple_strtoull(arg, &endp, 0);
431
432 if (endp == arg) {
433
434 val = simple_strtoull(arg, &endp, 16);
435 if (endp == arg)
436 return KDB_BADINT;420 return KDB_BADINT;
437 }421 }
438
439 *value = val;
440
441 return 0;422 return 0;
442}423}
443 424
@@ -473,10 +454,10 @@ int kdb_set(int argc, const char **argv) · 29 unmodified lines
473 */454 */
474 if (strcmp(argv[1], "KDBDEBUG") == 0) {455 if (strcmp(argv[1], "KDBDEBUG") == 0) {
475 unsigned int debugflags;456 unsigned int debugflags;
476 char *cp;457 int ret;
477 458
478 debugflags = simple_strtoul(argv[2], &cp, 0);459 ret = kstrtouint(argv[2], 0, &debugflags);
479 if (cp == argv[2] || debugflags & ~KDB_DEBUG_FLAG_MASK) {460 if (ret || debugflags & ~KDB_DEBUG_FLAG_MASK) {
480 kdb_printf("kdb: illegal debug flags '%s'\n",461 kdb_printf("kdb: illegal debug flags '%s'\n",
481 argv[2]);462 argv[2]);
482 return 0;463 return 0;
@@ -1619,10 +1600,10 @@ static int kdb_md(int argc, const char **argv) · 1136 unmodified lines
1619 if (!argv[0][3])1600 if (!argv[0][3])
1620 valid = 1;1601 valid = 1;
1621 else if (argv[0][3] == 'c' && argv[0][4]) {1602 else if (argv[0][3] == 'c' && argv[0][4]) {
1622 char *p;1603 if (kstrtouint(argv[0] + 4, 10, &repeat))
1623 repeat = simple_strtoul(argv[0] + 4, &p, 10);1604 return KDB_BADINT;
1624 mdcount = ((repeat * bytesperword) + 15) / 16;1605 mdcount = ((repeat * bytesperword) + 15) / 16;
1625 valid = !*p;1606 valid = 1;
1626 }1607 }
1627 last_repeat = repeat;1608 last_repeat = repeat;
1628 } else if (strcmp(argv[0], "md") == 0)1609 } else if (strcmp(argv[0], "md") == 0)
@@ -2083,15 +2064,10 @@ static int kdb_dmesg(int argc, const char **argv) · 454 unmodified lines
2083 if (argc > 2)2064 if (argc > 2)
2084 return KDB_ARGCOUNT;2065 return KDB_ARGCOUNT;
2085 if (argc) {2066 if (argc) {
2086 char *cp;2067 if (kstrtoint(argv[1], 0, &lines))
2087 lines = simple_strtol(argv[1], &cp, 0);
2088 if (*cp)
2089 lines = 0;2068 lines = 0;
2090 if (argc > 1) {2069 if (argc > 1 && (kstrtoint(argv[2], 0, &adjust) || adjust < 0))
2091 adjust = simple_strtoul(argv[2], &cp, 0);2070 adjust = 0;
2092 if (*cp || adjust < 0)
2093 adjust = 0;
2094 }
2095 }2071 }
2096 2072
2097 /* disable LOGGING if set */2073 /* disable LOGGING if set */
@@ -2428,14 +2404,12 @@ static int kdb_help(int argc, const char **argv) · 330 unmodified lines
2428static int kdb_kill(int argc, const char **argv)2404static int kdb_kill(int argc, const char **argv)
2429{2405{
2430 long sig, pid;2406 long sig, pid;
2431 char *endp;
2432 struct task_struct *p;2407 struct task_struct *p;
2433 2408
2434 if (argc != 2)2409 if (argc != 2)
2435 return KDB_ARGCOUNT;2410 return KDB_ARGCOUNT;
2436 2411
2437 sig = simple_strtol(argv[1], &endp, 0);2412 if (kstrtol(argv[1], 0, &sig))
2438 if (*endp)
2439 return KDB_BADINT;2413 return KDB_BADINT;
2440 if ((sig >= 0) || !valid_signal(-sig)) {2414 if ((sig >= 0) || !valid_signal(-sig)) {
2441 kdb_printf("Invalid signal parameter.<-signal>\n");2415 kdb_printf("Invalid signal parameter.<-signal>\n");
@@ -2443,8 +2417,7 @@ static int kdb_kill(int argc, const char **argv) · 1 unmodified line
2443 }2417 }
2444 sig = -sig;2418 sig = -sig;
2445 2419
2446 pid = simple_strtol(argv[2], &endp, 0);2420 if (kstrtol(argv[2], 0, &pid))
2447 if (*endp)
2448 return KDB_BADINT;2421 return KDB_BADINT;
2449 if (pid <= 0) {2422 if (pid <= 0) {
2450 kdb_printf("Process ID must be large than 0.\n");2423 kdb_printf("Process ID must be large than 0.\n");