* [PATCH v4 0/3] Replace the use of simple_strtol/ul functions with kstrto
@ 2024-10-28 19:17 Nir Lichtman
2024-10-28 19:19 ` [PATCH v4 1/3] kdb: Replace the use of simple_strto with safer kstrto in kdb_main Nir Lichtman
` (3 more replies)
0 siblings, 4 replies; 7+ messages in thread
From: Nir Lichtman @ 2024-10-28 19:17 UTC (permalink / raw)
To: kgdb-bugreport, linux-trace-kernel
Cc: yuran.pereira, jason.wessel, daniel.thompson, dianders, rostedt,
mhiramat, linux-kernel, linux-kernel-mentees
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 function 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 series replaces uses of the simple_strto* series of function
with the safer kstrto* alternatives.
[1] https://www.kernel.org/doc/html/latest/process/deprecated.html#simple-strtol-simple-strtoll-simple-strtoul-simple-strtoull
Yuran Pereira (2):
kdb: Replace the use of simple_strto with safer kstrto in kdb_main
trace: kdb: Replace simple_strtoul with kstrtoul in kdb_ftdump
Nir Lichtman (1):
kdb: Remove fallback interpretation of arbitrary numbers as hex
This patch is originally based upon
https://lore.kernel.org/all/GV1PR10MB65635561FB160078C3744B5FE8B4A@GV1PR10MB6563.EURPRD10.PROD.OUTLOOK.COM/
Since the original thread was left with a review and the comments have not been addressed,
decided to continue the minor work left to move this patch forward.
v2:
- Styling fixes
- Fix an invalid conversion to unsigned int instead of signed as it was supposed to be
- Fix one of the conversions to return an error in case of failure, instead of silently falling back to a default value
- Add Douglas's suggestion for removing the hex interpretation fallback
v3:
- Split to 3 parts instead of 2 (in which the 3rd part now contains the hex interpretation fix)
- Fix the patch series to properly reference the cover
- Fix credit tags
v4:
- Fix regression between v2 to v3 in which I accidentaly
reverted an improper unsigned/signed conversion
- Fix nit of spacing in if condition parens split into multiple lines
kernel/debug/kdb/kdb_main.c | 69 +++++++++--------------------------
kernel/trace/trace_kdb.c | 15 +++-----
2 files changed, 23 insertions(+), 61 deletions(-)
--
2.39.2
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH v4 1/3] kdb: Replace the use of simple_strto with safer kstrto in kdb_main
2024-10-28 19:17 [PATCH v4 0/3] Replace the use of simple_strtol/ul functions with kstrto Nir Lichtman
@ 2024-10-28 19:19 ` Nir Lichtman
2024-10-28 21:09 ` Doug Anderson
2024-10-28 19:21 ` [PATCH v4 2/3] trace: kdb: Replace simple_strtoul with kstrtoul in kdb_ftdump Nir Lichtman
` (2 subsequent siblings)
3 siblings, 1 reply; 7+ messages in thread
From: Nir Lichtman @ 2024-10-28 19:19 UTC (permalink / raw)
To: kgdb-bugreport, linux-trace-kernel
Cc: yuran.pereira, jason.wessel, daniel.thompson, dianders, rostedt,
mhiramat, linux-kernel, linux-kernel-mentees
From: Yuran Pereira <yuran.pereira@hotmail.com>
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
Signed-off-by: Yuran Pereira <yuran.pereira@hotmail.com>
[nir: addressed review comments by fixing styling, invalid conversion and a missing error return]
Signed-off-by: Nir Lichtman <nir@lichtman.org>
---
kernel/debug/kdb/kdb_main.c | 69 +++++++++++--------------------------
1 file changed, 21 insertions(+), 48 deletions(-)
diff --git a/kernel/debug/kdb/kdb_main.c b/kernel/debug/kdb/kdb_main.c
index f5f7d7fb5936..f8703ab760d9 100644
--- a/kernel/debug/kdb/kdb_main.c
+++ b/kernel/debug/kdb/kdb_main.c
@@ -306,8 +306,8 @@ static int kdbgetulenv(const char *match, unsigned long *value)
return KDB_NOTENV;
if (strlen(ep) == 0)
return KDB_NOENVVALUE;
-
- *value = simple_strtoul(ep, NULL, 0);
+ if (kstrtoul(ep, 0, value))
+ return KDB_BADINT;
return 0;
}
@@ -402,42 +402,23 @@ static void kdb_printenv(void)
*/
int kdbgetularg(const char *arg, unsigned long *value)
{
- char *endp;
- unsigned long val;
-
- val = simple_strtoul(arg, &endp, 0);
-
- if (endp == arg) {
- /*
- * Also try base 16, for us folks too lazy to type the
- * leading 0x...
- */
- val = simple_strtoul(arg, &endp, 16);
- if (endp == arg)
+ /*
+ * If the first fails, also try base 16, for us
+ * folks too lazy to type the leading 0x...
+ */
+ if (kstrtoul(arg, 0, value)) {
+ if (kstrtoul(arg, 16, value))
return KDB_BADINT;
}
-
- *value = val;
-
return 0;
}
int kdbgetu64arg(const char *arg, u64 *value)
{
- char *endp;
- u64 val;
-
- val = simple_strtoull(arg, &endp, 0);
-
- if (endp == arg) {
-
- val = simple_strtoull(arg, &endp, 16);
- if (endp == arg)
+ if (kstrtou64(arg, 0, value)) {
+ if (kstrtou64(arg, 16, value))
return KDB_BADINT;
}
-
- *value = val;
-
return 0;
}
@@ -473,10 +454,10 @@ int kdb_set(int argc, const char **argv)
*/
if (strcmp(argv[1], "KDBDEBUG") == 0) {
unsigned int debugflags;
- char *cp;
+ int ret;
- debugflags = simple_strtoul(argv[2], &cp, 0);
- if (cp == argv[2] || debugflags & ~KDB_DEBUG_FLAG_MASK) {
+ ret = kstrtouint(argv[2], 0, &debugflags);
+ if (ret || debugflags & ~KDB_DEBUG_FLAG_MASK) {
kdb_printf("kdb: illegal debug flags '%s'\n",
argv[2]);
return 0;
@@ -1619,10 +1600,10 @@ static int kdb_md(int argc, const char **argv)
if (!argv[0][3])
valid = 1;
else if (argv[0][3] == 'c' && argv[0][4]) {
- char *p;
- repeat = simple_strtoul(argv[0] + 4, &p, 10);
+ if (kstrtouint(argv[0] + 4, 10, &repeat))
+ return KDB_BADINT;
mdcount = ((repeat * bytesperword) + 15) / 16;
- valid = !*p;
+ valid = 1;
}
last_repeat = repeat;
} else if (strcmp(argv[0], "md") == 0)
@@ -2083,15 +2064,10 @@ static int kdb_dmesg(int argc, const char **argv)
if (argc > 2)
return KDB_ARGCOUNT;
if (argc) {
- char *cp;
- lines = simple_strtol(argv[1], &cp, 0);
- if (*cp)
+ if (kstrtoint(argv[1], 0, &lines))
lines = 0;
- if (argc > 1) {
- adjust = simple_strtoul(argv[2], &cp, 0);
- if (*cp || adjust < 0)
- adjust = 0;
- }
+ if (argc > 1 && (kstrtoint(argv[2], 0, &adjust) || adjust < 0))
+ adjust = 0;
}
/* disable LOGGING if set */
@@ -2428,14 +2404,12 @@ static int kdb_help(int argc, const char **argv)
static int kdb_kill(int argc, const char **argv)
{
long sig, pid;
- char *endp;
struct task_struct *p;
if (argc != 2)
return KDB_ARGCOUNT;
- sig = simple_strtol(argv[1], &endp, 0);
- if (*endp)
+ if (kstrtol(argv[1], 0, &sig))
return KDB_BADINT;
if ((sig >= 0) || !valid_signal(-sig)) {
kdb_printf("Invalid signal parameter.<-signal>\n");
@@ -2443,8 +2417,7 @@ static int kdb_kill(int argc, const char **argv)
}
sig = -sig;
- pid = simple_strtol(argv[2], &endp, 0);
- if (*endp)
+ if (kstrtol(argv[2], 0, &pid))
return KDB_BADINT;
if (pid <= 0) {
kdb_printf("Process ID must be large than 0.\n");
--
2.39.2
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [PATCH v4 2/3] trace: kdb: Replace simple_strtoul with kstrtoul in kdb_ftdump
2024-10-28 19:17 [PATCH v4 0/3] Replace the use of simple_strtol/ul functions with kstrto Nir Lichtman
2024-10-28 19:19 ` [PATCH v4 1/3] kdb: Replace the use of simple_strto with safer kstrto in kdb_main Nir Lichtman
@ 2024-10-28 19:21 ` Nir Lichtman
2024-10-28 19:22 ` [PATCH v4 3/3] kdb: Remove fallback interpretation of arbitrary numbers as hex Nir Lichtman
2024-11-08 8:38 ` [PATCH v4 0/3] Replace the use of simple_strtol/ul functions with kstrto Daniel Thompson
3 siblings, 0 replies; 7+ messages in thread
From: Nir Lichtman @ 2024-10-28 19:21 UTC (permalink / raw)
To: kgdb-bugreport, linux-trace-kernel
Cc: yuran.pereira, jason.wessel, daniel.thompson, dianders, rostedt,
mhiramat, linux-kernel, linux-kernel-mentees
From: Yuran Pereira <yuran.pereira@hotmail.com>
The function simple_strtoul performs no error checking in scenarios
where the input value overflows the intended output variable.
This results in this function successfully returning, even when the
output does not match the input string (aka the function returns
successfully even when the result is wrong).
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_strtoul with the safer
alternatives kstrtoint and kstrtol.
[1] https://www.kernel.org/doc/html/latest/process/deprecated.html#simple-strtol-simple-strtoll-simple-strtoul-simple-strtoull
Signed-off-by: Yuran Pereira <yuran.pereira@hotmail.com>
Reviewed-by: Douglas Anderson <dianders@chromium.org>
Acked-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
[nir: style fixes]
Signed-off-by: Nir Lichtman <nir@lichtman.org>
---
kernel/trace/trace_kdb.c | 13 +++++--------
1 file changed, 5 insertions(+), 8 deletions(-)
diff --git a/kernel/trace/trace_kdb.c b/kernel/trace/trace_kdb.c
index 59857a1ee44c..1e72d20b3c2f 100644
--- a/kernel/trace/trace_kdb.c
+++ b/kernel/trace/trace_kdb.c
@@ -96,22 +96,19 @@ static int kdb_ftdump(int argc, const char **argv)
{
int skip_entries = 0;
long cpu_file;
- char *cp;
+ int err;
int cnt;
int cpu;
if (argc > 2)
return KDB_ARGCOUNT;
- if (argc) {
- skip_entries = simple_strtol(argv[1], &cp, 0);
- if (*cp)
- skip_entries = 0;
- }
+ if (argc && kstrtoint(argv[1], 0, &skip_entries))
+ return KDB_BADINT;
if (argc == 2) {
- cpu_file = simple_strtol(argv[2], &cp, 0);
- if (*cp || cpu_file >= NR_CPUS || cpu_file < 0 ||
+ err = kstrtol(argv[2], 0, &cpu_file);
+ if (err || cpu_file >= NR_CPUS || cpu_file < 0 ||
!cpu_online(cpu_file))
return KDB_BADINT;
} else {
--
2.39.2
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [PATCH v4 3/3] kdb: Remove fallback interpretation of arbitrary numbers as hex
2024-10-28 19:17 [PATCH v4 0/3] Replace the use of simple_strtol/ul functions with kstrto Nir Lichtman
2024-10-28 19:19 ` [PATCH v4 1/3] kdb: Replace the use of simple_strto with safer kstrto in kdb_main Nir Lichtman
2024-10-28 19:21 ` [PATCH v4 2/3] trace: kdb: Replace simple_strtoul with kstrtoul in kdb_ftdump Nir Lichtman
@ 2024-10-28 19:22 ` Nir Lichtman
2024-11-08 8:38 ` [PATCH v4 0/3] Replace the use of simple_strtol/ul functions with kstrto Daniel Thompson
3 siblings, 0 replies; 7+ messages in thread
From: Nir Lichtman @ 2024-10-28 19:22 UTC (permalink / raw)
To: kgdb-bugreport, linux-trace-kernel
Cc: yuran.pereira, jason.wessel, daniel.thompson, dianders, rostedt,
mhiramat, linux-kernel, linux-kernel-mentees
Remove logic that enables a fallback of interpreting numbers supplied in KDB CLI
to be interpreted as hex without explicit "0x" prefix as this can be confusing
for the end users.
Suggested-by: Douglas Anderson <dianders@chromium.org>
Reviewed-by: Douglas Anderson <dianders@chromium.org>
Signed-off-by: Nir Lichtman <nir@lichtman.org>
---
kernel/debug/kdb/kdb_main.c | 16 ++++------------
1 file changed, 4 insertions(+), 12 deletions(-)
diff --git a/kernel/debug/kdb/kdb_main.c b/kernel/debug/kdb/kdb_main.c
index f8703ab760d9..5f4be507d79f 100644
--- a/kernel/debug/kdb/kdb_main.c
+++ b/kernel/debug/kdb/kdb_main.c
@@ -402,23 +402,15 @@ static void kdb_printenv(void)
*/
int kdbgetularg(const char *arg, unsigned long *value)
{
- /*
- * If the first fails, also try base 16, for us
- * folks too lazy to type the leading 0x...
- */
- if (kstrtoul(arg, 0, value)) {
- if (kstrtoul(arg, 16, value))
- return KDB_BADINT;
- }
+ if (kstrtoul(arg, 0, value))
+ return KDB_BADINT;
return 0;
}
int kdbgetu64arg(const char *arg, u64 *value)
{
- if (kstrtou64(arg, 0, value)) {
- if (kstrtou64(arg, 16, value))
- return KDB_BADINT;
- }
+ if (kstrtou64(arg, 0, value))
+ return KDB_BADINT;
return 0;
}
--
2.39.2
^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [PATCH v4 1/3] kdb: Replace the use of simple_strto with safer kstrto in kdb_main
2024-10-28 19:19 ` [PATCH v4 1/3] kdb: Replace the use of simple_strto with safer kstrto in kdb_main Nir Lichtman
@ 2024-10-28 21:09 ` Doug Anderson
0 siblings, 0 replies; 7+ messages in thread
From: Doug Anderson @ 2024-10-28 21:09 UTC (permalink / raw)
To: Nir Lichtman
Cc: kgdb-bugreport, linux-trace-kernel, yuran.pereira, jason.wessel,
daniel.thompson, rostedt, mhiramat, linux-kernel,
linux-kernel-mentees
Hi,
On Mon, Oct 28, 2024 at 12:19 PM Nir Lichtman <nir@lichtman.org> wrote:
>
> From: Yuran Pereira <yuran.pereira@hotmail.com>
>
> 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
>
> Signed-off-by: Yuran Pereira <yuran.pereira@hotmail.com>
> [nir: addressed review comments by fixing styling, invalid conversion and a missing error return]
> Signed-off-by: Nir Lichtman <nir@lichtman.org>
> ---
> kernel/debug/kdb/kdb_main.c | 69 +++++++++++--------------------------
> 1 file changed, 21 insertions(+), 48 deletions(-)
Reviewed-by: Douglas Anderson <dianders@chromium.org>
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH v4 0/3] Replace the use of simple_strtol/ul functions with kstrto
2024-10-28 19:17 [PATCH v4 0/3] Replace the use of simple_strtol/ul functions with kstrto Nir Lichtman
` (2 preceding siblings ...)
2024-10-28 19:22 ` [PATCH v4 3/3] kdb: Remove fallback interpretation of arbitrary numbers as hex Nir Lichtman
@ 2024-11-08 8:38 ` Daniel Thompson
2024-11-08 11:06 ` Nir Lichtman
3 siblings, 1 reply; 7+ messages in thread
From: Daniel Thompson @ 2024-11-08 8:38 UTC (permalink / raw)
To: kgdb-bugreport, linux-trace-kernel, Nir Lichtman
Cc: yuran.pereira, jason.wessel, dianders, rostedt, mhiramat,
linux-kernel, linux-kernel-mentees
On Mon, 28 Oct 2024 19:17:00 +0000, Nir Lichtman wrote:
> 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 function 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.
>
> [...]
Applied, thanks!
[1/3] kdb: Replace the use of simple_strto with safer kstrto in kdb_main
commit: fe0c87871fc0b97f6d374b670c81f7c4087eebc5
[2/3] trace: kdb: Replace simple_strtoul with kstrtoul in kdb_ftdump
commit: c56642c737fc0bd9bcc3a22a2bf8ed6f5900a660
[3/3] kdb: Remove fallback interpretation of arbitrary numbers as hex
commit: 5f4ca702e36893a276fccb0aa55ab36e19dfbb50
Best regards,
--
Daniel Thompson <daniel.thompson@linaro.org>
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH v4 0/3] Replace the use of simple_strtol/ul functions with kstrto
2024-11-08 8:38 ` [PATCH v4 0/3] Replace the use of simple_strtol/ul functions with kstrto Daniel Thompson
@ 2024-11-08 11:06 ` Nir Lichtman
0 siblings, 0 replies; 7+ messages in thread
From: Nir Lichtman @ 2024-11-08 11:06 UTC (permalink / raw)
To: Daniel Thompson
Cc: kgdb-bugreport, linux-trace-kernel, yuran.pereira, jason.wessel,
dianders, rostedt, mhiramat, linux-kernel, linux-kernel-mentees
On Fri, Nov 08, 2024 at 08:38:35AM +0000, Daniel Thompson wrote:
>
> On Mon, 28 Oct 2024 19:17:00 +0000, Nir Lichtman wrote:
> > 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 function 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.
> >
> > [...]
>
> Applied, thanks!
Thanks!
Nir
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2024-11-08 11:06 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-10-28 19:17 [PATCH v4 0/3] Replace the use of simple_strtol/ul functions with kstrto Nir Lichtman
2024-10-28 19:19 ` [PATCH v4 1/3] kdb: Replace the use of simple_strto with safer kstrto in kdb_main Nir Lichtman
2024-10-28 21:09 ` Doug Anderson
2024-10-28 19:21 ` [PATCH v4 2/3] trace: kdb: Replace simple_strtoul with kstrtoul in kdb_ftdump Nir Lichtman
2024-10-28 19:22 ` [PATCH v4 3/3] kdb: Remove fallback interpretation of arbitrary numbers as hex Nir Lichtman
2024-11-08 8:38 ` [PATCH v4 0/3] Replace the use of simple_strtol/ul functions with kstrto Daniel Thompson
2024-11-08 11:06 ` Nir Lichtman
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).