Skip to content

Commit 7e9dfcc

Browse files
walaclenticularis39
authored andcommitted
rtla: Replace atoi() with a robust strtoi()
The atoi() function does not perform error checking, which can lead to undefined behavior when parsing invalid or out-of-range strings. This can cause issues when parsing user-provided numerical inputs, such as signal numbers, PIDs, or CPU lists. To address this, introduce a new strtoi() helper function that safely converts a string to an integer. This function validates the input and checks for overflows, returning a negative value on failure. Replace all calls to atoi() with the new strtoi() function and add proper error handling to make the parsing more robust and prevent potential issues. Signed-off-by: Wander Lairson Costa <wander@redhat.com> Link: https://lore.kernel.org/r/20260106133655.249887-5-wander@redhat.com Signed-off-by: Tomas Glozar <tglozar@redhat.com>
1 parent 648634d commit 7e9dfcc

3 files changed

Lines changed: 41 additions & 8 deletions

File tree

tools/tracing/rtla/src/actions.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,12 +181,13 @@ actions_parse(struct actions *self, const char *trigger, const char *tracefn)
181181
/* Takes two arguments, num (signal) and pid */
182182
while (token != NULL) {
183183
if (strlen(token) > 4 && strncmp(token, "num=", 4) == 0) {
184-
signal = atoi(token + 4);
184+
if (strtoi(token + 4, &signal))
185+
return -1;
185186
} else if (strlen(token) > 4 && strncmp(token, "pid=", 4) == 0) {
186187
if (strncmp(token + 4, "parent", 7) == 0)
187188
pid = -1;
188-
else
189-
pid = atoi(token + 4);
189+
else if (strtoi(token + 4, &pid))
190+
return -1;
190191
} else {
191192
/* Invalid argument */
192193
return -1;

tools/tracing/rtla/src/utils.c

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <fcntl.h>
1818
#include <sched.h>
1919
#include <stdio.h>
20+
#include <limits.h>
2021

2122
#include "utils.h"
2223

@@ -127,16 +128,18 @@ int parse_cpu_set(char *cpu_list, cpu_set_t *set)
127128
nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
128129

129130
for (p = cpu_list; *p; ) {
130-
cpu = atoi(p);
131-
if (cpu < 0 || (!cpu && *p != '0') || cpu >= nr_cpus)
131+
if (strtoi(p, &cpu))
132+
goto err;
133+
if (cpu < 0 || cpu >= nr_cpus)
132134
goto err;
133135

134136
while (isdigit(*p))
135137
p++;
136138
if (*p == '-') {
137139
p++;
138-
end_cpu = atoi(p);
139-
if (end_cpu < cpu || (!end_cpu && *p != '0') || end_cpu >= nr_cpus)
140+
if (strtoi(p, &end_cpu))
141+
goto err;
142+
if (end_cpu < cpu || end_cpu >= nr_cpus)
140143
goto err;
141144
while (isdigit(*p))
142145
p++;
@@ -337,6 +340,7 @@ int set_comm_sched_attr(const char *comm_prefix, struct sched_attr *attr)
337340
struct dirent *proc_entry;
338341
DIR *procfs;
339342
int retval;
343+
int pid;
340344

341345
if (strlen(comm_prefix) >= MAX_PATH) {
342346
err_msg("Command prefix is too long: %d < strlen(%s)\n",
@@ -356,8 +360,12 @@ int set_comm_sched_attr(const char *comm_prefix, struct sched_attr *attr)
356360
if (!retval)
357361
continue;
358362

363+
if (strtoi(proc_entry->d_name, &pid)) {
364+
err_msg("'%s' is not a valid pid", proc_entry->d_name);
365+
goto out_err;
366+
}
359367
/* procfs_is_workload_pid confirmed it is a pid */
360-
retval = __set_sched_attr(atoi(proc_entry->d_name), attr);
368+
retval = __set_sched_attr(pid, attr);
361369
if (retval) {
362370
err_msg("Error setting sched attributes for pid:%s\n", proc_entry->d_name);
363371
goto out_err;
@@ -999,3 +1007,25 @@ char *parse_optional_arg(int argc, char **argv)
9991007
return NULL;
10001008
}
10011009
}
1010+
1011+
/*
1012+
* strtoi - convert string to integer with error checking
1013+
*
1014+
* Returns 0 on success, -1 if conversion fails or result is out of int range.
1015+
*/
1016+
int strtoi(const char *s, int *res)
1017+
{
1018+
char *end_ptr;
1019+
long lres;
1020+
1021+
if (!*s)
1022+
return -1;
1023+
1024+
errno = 0;
1025+
lres = strtol(s, &end_ptr, 0);
1026+
if (errno || *end_ptr || lres > INT_MAX || lres < INT_MIN)
1027+
return -1;
1028+
1029+
*res = (int) lres;
1030+
return 0;
1031+
}

tools/tracing/rtla/src/utils.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <stdint.h>
44
#include <time.h>
55
#include <sched.h>
6+
#include <stdbool.h>
67

78
/*
89
* '18446744073709551615\0'
@@ -81,6 +82,7 @@ static inline int set_deepest_cpu_idle_state(unsigned int cpu, unsigned int stat
8182
static inline int have_libcpupower_support(void) { return 0; }
8283
#endif /* HAVE_LIBCPUPOWER_SUPPORT */
8384
int auto_house_keeping(cpu_set_t *monitored_cpus);
85+
__attribute__((__warn_unused_result__)) int strtoi(const char *s, int *res);
8486

8587
#define ns_to_usf(x) (((double)x/1000))
8688
#define ns_to_per(total, part) ((part * 100) / (double)total)

0 commit comments

Comments
 (0)