Skip to content

Commit c5963a0

Browse files
yuranpereirarostedt
authored andcommitted
ftrace: Replaces simple_strtoul in ftrace
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 kstrtoul and kstruint. Callers affected: - add_rec_by_index - set_graph_max_depth_function Side effects of this patch: - Since `fgraph_max_depth` is an `unsigned int`, this patch uses kstrtouint instead of kstrtoul to avoid any compiler warnings that could originate from calling the latter. - This patch ensures that the callers of kstrtou* return accordingly when kstrtoul and kstruint fail for some reason. In this case, both callers this patch is addressing return 0 on error. [1] https://www.kernel.org/doc/html/latest/process/deprecated.html#simple-strtol-simple-strtoll-simple-strtoul-simple-strtoull Link: https://lore.kernel.org/linux-trace-kernel/GV1PR10MB656333529A8D7B8AFB28D238E8B4A@GV1PR10MB6563.EURPRD10.PROD.OUTLOOK.COM Signed-off-by: Yuran Pereira <[email protected]> Reviewed-by: Masami Hiramatsu (Google) <[email protected]> Signed-off-by: Steven Rostedt (Google) <[email protected]>
1 parent dd5a440 commit c5963a0

File tree

1 file changed

+3
-4
lines changed

1 file changed

+3
-4
lines changed

kernel/trace/ftrace.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4202,12 +4202,12 @@ static int
42024202
add_rec_by_index(struct ftrace_hash *hash, struct ftrace_glob *func_g,
42034203
int clear_filter)
42044204
{
4205-
long index = simple_strtoul(func_g->search, NULL, 0);
4205+
long index;
42064206
struct ftrace_page *pg;
42074207
struct dyn_ftrace *rec;
42084208

42094209
/* The index starts at 1 */
4210-
if (--index < 0)
4210+
if (kstrtoul(func_g->search, 0, &index) || --index < 0)
42114211
return 0;
42124212

42134213
do_for_each_ftrace_rec(pg, rec) {
@@ -5817,9 +5817,8 @@ __setup("ftrace_graph_notrace=", set_graph_notrace_function);
58175817

58185818
static int __init set_graph_max_depth_function(char *str)
58195819
{
5820-
if (!str)
5820+
if (!str || kstrtouint(str, 0, &fgraph_max_depth))
58215821
return 0;
5822-
fgraph_max_depth = simple_strtoul(str, NULL, 0);
58235822
return 1;
58245823
}
58255824
__setup("ftrace_graph_max_depth=", set_graph_max_depth_function);

0 commit comments

Comments
 (0)