Skip to content

Commit a29430c

Browse files
walaclenticularis39
authored andcommitted
rtla: Replace magic number with MAX_PATH
The trace functions use a buffer to manipulate strings that will be written to tracefs files. These buffers are defined with a magic number of 1024, which is a common source of vulnerabilities. Replace the magic number 1024 with the MAX_PATH macro to make the code safer and more readable. While at it, replace other instances of the magic number with ARRAY_SIZE() when the buffer is locally defined. Signed-off-by: Wander Lairson Costa <wander@redhat.com> Link: https://lore.kernel.org/r/20260309195040.1019085-6-wander@redhat.com Signed-off-by: Tomas Glozar <tglozar@redhat.com>
1 parent a50c538 commit a29430c

3 files changed

Lines changed: 13 additions & 13 deletions

File tree

tools/tracing/rtla/src/osnoise.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ int osnoise_set_cpus(struct osnoise_context *context, char *cpus)
6262
if (!context->curr_cpus)
6363
return -1;
6464

65-
snprintf(buffer, 1024, "%s\n", cpus);
65+
snprintf(buffer, ARRAY_SIZE(buffer), "%s\n", cpus);
6666

6767
debug_msg("setting cpus to %s from %s", cpus, context->orig_cpus);
6868

tools/tracing/rtla/src/timerlat_u.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
static int timerlat_u_main(int cpu, struct timerlat_u_params *params)
3333
{
3434
struct sched_param sp = { .sched_priority = 95 };
35-
char buffer[1024];
35+
char buffer[MAX_PATH];
3636
int timerlat_fd;
3737
cpu_set_t set;
3838
int retval;
@@ -83,7 +83,7 @@ static int timerlat_u_main(int cpu, struct timerlat_u_params *params)
8383

8484
/* add should continue with a signal handler */
8585
while (true) {
86-
retval = read(timerlat_fd, buffer, 1024);
86+
retval = read(timerlat_fd, buffer, ARRAY_SIZE(buffer));
8787
if (retval < 0)
8888
break;
8989
}

tools/tracing/rtla/src/trace.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ void trace_event_add_trigger(struct trace_events *event, char *trigger)
313313
static void trace_event_disable_filter(struct trace_instance *instance,
314314
struct trace_events *tevent)
315315
{
316-
char filter[1024];
316+
char filter[MAX_PATH];
317317
int retval;
318318

319319
if (!tevent->filter)
@@ -325,7 +325,7 @@ static void trace_event_disable_filter(struct trace_instance *instance,
325325
debug_msg("Disabling %s:%s filter %s\n", tevent->system,
326326
tevent->event ? : "*", tevent->filter);
327327

328-
snprintf(filter, 1024, "!%s\n", tevent->filter);
328+
snprintf(filter, ARRAY_SIZE(filter), "!%s\n", tevent->filter);
329329

330330
retval = tracefs_event_file_write(instance->inst, tevent->system,
331331
tevent->event, "filter", filter);
@@ -344,7 +344,7 @@ static void trace_event_save_hist(struct trace_instance *instance,
344344
{
345345
int retval, index, out_fd;
346346
mode_t mode = 0644;
347-
char path[1024];
347+
char path[MAX_PATH];
348348
char *hist;
349349

350350
if (!tevent)
@@ -359,7 +359,7 @@ static void trace_event_save_hist(struct trace_instance *instance,
359359
if (retval)
360360
return;
361361

362-
snprintf(path, 1024, "%s_%s_hist.txt", tevent->system, tevent->event);
362+
snprintf(path, ARRAY_SIZE(path), "%s_%s_hist.txt", tevent->system, tevent->event);
363363

364364
printf(" Saving event %s:%s hist to %s\n", tevent->system, tevent->event, path);
365365

@@ -391,7 +391,7 @@ static void trace_event_save_hist(struct trace_instance *instance,
391391
static void trace_event_disable_trigger(struct trace_instance *instance,
392392
struct trace_events *tevent)
393393
{
394-
char trigger[1024];
394+
char trigger[MAX_PATH];
395395
int retval;
396396

397397
if (!tevent->trigger)
@@ -405,7 +405,7 @@ static void trace_event_disable_trigger(struct trace_instance *instance,
405405

406406
trace_event_save_hist(instance, tevent);
407407

408-
snprintf(trigger, 1024, "!%s\n", tevent->trigger);
408+
snprintf(trigger, ARRAY_SIZE(trigger), "!%s\n", tevent->trigger);
409409

410410
retval = tracefs_event_file_write(instance->inst, tevent->system,
411411
tevent->event, "trigger", trigger);
@@ -444,7 +444,7 @@ void trace_events_disable(struct trace_instance *instance,
444444
static int trace_event_enable_filter(struct trace_instance *instance,
445445
struct trace_events *tevent)
446446
{
447-
char filter[1024];
447+
char filter[MAX_PATH];
448448
int retval;
449449

450450
if (!tevent->filter)
@@ -456,7 +456,7 @@ static int trace_event_enable_filter(struct trace_instance *instance,
456456
return 1;
457457
}
458458

459-
snprintf(filter, 1024, "%s\n", tevent->filter);
459+
snprintf(filter, ARRAY_SIZE(filter), "%s\n", tevent->filter);
460460

461461
debug_msg("Enabling %s:%s filter %s\n", tevent->system,
462462
tevent->event ? : "*", tevent->filter);
@@ -479,7 +479,7 @@ static int trace_event_enable_filter(struct trace_instance *instance,
479479
static int trace_event_enable_trigger(struct trace_instance *instance,
480480
struct trace_events *tevent)
481481
{
482-
char trigger[1024];
482+
char trigger[MAX_PATH];
483483
int retval;
484484

485485
if (!tevent->trigger)
@@ -491,7 +491,7 @@ static int trace_event_enable_trigger(struct trace_instance *instance,
491491
return 1;
492492
}
493493

494-
snprintf(trigger, 1024, "%s\n", tevent->trigger);
494+
snprintf(trigger, ARRAY_SIZE(trigger), "%s\n", tevent->trigger);
495495

496496
debug_msg("Enabling %s:%s trigger %s\n", tevent->system,
497497
tevent->event ? : "*", tevent->trigger);

0 commit comments

Comments
 (0)