Skip to content

Commit fd98583

Browse files
committed
trace: handle tracefs path truncation
If the tracefs mountpoint has a very long path we may exceed PATH_MAX. This is a system misconfiguration and the user must resolve it so that applications can perform path-based system calls successfully. This issue does not occur on real-world systems since tracefs is mounted on /sys/kernel/debug/tracing/, but the compiler is smart enough to foresee the possibility and warn about the unchecked snprintf(3) return value. This patch fixes the compiler warning. Reported-by: Markus Armbruster <[email protected]> Signed-off-by: Stefan Hajnoczi <[email protected]> Reviewed-by: Markus Armbruster <[email protected]> Reviewed-by: Liam Merwick <[email protected]> Message-id: [email protected] Message-Id: <[email protected]> Signed-off-by: Stefan Hajnoczi <[email protected]>
1 parent d97a39d commit fd98583

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

trace/ftrace.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,11 @@ bool ftrace_init(void)
5353
}
5454

5555
if (tracefs_found) {
56-
snprintf(path, PATH_MAX, "%s%s/tracing_on", mount_point, subdir);
56+
if (snprintf(path, PATH_MAX, "%s%s/tracing_on", mount_point, subdir)
57+
>= sizeof(path)) {
58+
fprintf(stderr, "Using tracefs mountpoint would exceed PATH_MAX\n");
59+
return false;
60+
}
5761
trace_fd = open(path, O_WRONLY);
5862
if (trace_fd < 0) {
5963
if (errno == EACCES) {
@@ -72,7 +76,11 @@ bool ftrace_init(void)
7276
}
7377
close(trace_fd);
7478
}
75-
snprintf(path, PATH_MAX, "%s%s/trace_marker", mount_point, subdir);
79+
if (snprintf(path, PATH_MAX, "%s%s/trace_marker", mount_point, subdir)
80+
>= sizeof(path)) {
81+
fprintf(stderr, "Using tracefs mountpoint would exceed PATH_MAX\n");
82+
return false;
83+
}
7684
trace_marker_fd = open(path, O_WRONLY);
7785
if (trace_marker_fd < 0) {
7886
perror("Could not open ftrace 'trace_marker' file");

0 commit comments

Comments
 (0)