Skip to content

Commit c7dce4c

Browse files
azeemshaikh38kees
authored andcommitted
tracing: Replace all non-returning strlcpy with strscpy
strlcpy() reads the entire source buffer first. This read may exceed the destination size limit. This is both inefficient and can lead to linear read overflows if a source string is not NUL-terminated [1]. In an effort to remove strlcpy() completely [2], replace strlcpy() here with strscpy(). No return values were used, so direct replacement with strlcpy is safe. [1] https://www.kernel.org/doc/html/latest/process/deprecated.html#strlcpy [2] KSPP#89 Signed-off-by: Azeem Shaikh <azeemshaikh38@gmail.com> Acked-by: Masami Hiramatsu (Google) <mhiramat@kernel.org> Reviewed-by: Kees Cook <keescook@chromium.org> Signed-off-by: Kees Cook <keescook@chromium.org> Link: https://lore.kernel.org/r/20230516143956.1367827-1-azeemshaikh38@gmail.com
1 parent 7afbe5d commit c7dce4c

5 files changed

Lines changed: 10 additions & 10 deletions

File tree

kernel/trace/trace.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ static int boot_snapshot_index;
196196

197197
static int __init set_cmdline_ftrace(char *str)
198198
{
199-
strlcpy(bootup_tracer_buf, str, MAX_TRACER_SIZE);
199+
strscpy(bootup_tracer_buf, str, MAX_TRACER_SIZE);
200200
default_bootup_tracer = bootup_tracer_buf;
201201
/* We are using ftrace early, expand it */
202202
ring_buffer_expanded = true;
@@ -281,7 +281,7 @@ static char trace_boot_options_buf[MAX_TRACER_SIZE] __initdata;
281281

282282
static int __init set_trace_boot_options(char *str)
283283
{
284-
strlcpy(trace_boot_options_buf, str, MAX_TRACER_SIZE);
284+
strscpy(trace_boot_options_buf, str, MAX_TRACER_SIZE);
285285
return 1;
286286
}
287287
__setup("trace_options=", set_trace_boot_options);
@@ -291,7 +291,7 @@ static char *trace_boot_clock __initdata;
291291

292292
static int __init set_trace_boot_clock(char *str)
293293
{
294-
strlcpy(trace_boot_clock_buf, str, MAX_TRACER_SIZE);
294+
strscpy(trace_boot_clock_buf, str, MAX_TRACER_SIZE);
295295
trace_boot_clock = trace_boot_clock_buf;
296296
return 1;
297297
}
@@ -2521,7 +2521,7 @@ static void __trace_find_cmdline(int pid, char comm[])
25212521
if (map != NO_CMDLINE_MAP) {
25222522
tpid = savedcmd->map_cmdline_to_pid[map];
25232523
if (tpid == pid) {
2524-
strlcpy(comm, get_saved_cmdlines(map), TASK_COMM_LEN);
2524+
strscpy(comm, get_saved_cmdlines(map), TASK_COMM_LEN);
25252525
return;
25262526
}
25272527
}

kernel/trace/trace_events.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2831,7 +2831,7 @@ static __init int setup_trace_triggers(char *str)
28312831
char *buf;
28322832
int i;
28332833

2834-
strlcpy(bootup_trigger_buf, str, COMMAND_LINE_SIZE);
2834+
strscpy(bootup_trigger_buf, str, COMMAND_LINE_SIZE);
28352835
ring_buffer_expanded = true;
28362836
disable_tracing_selftest("running event triggers");
28372837

@@ -3621,7 +3621,7 @@ static char bootup_event_buf[COMMAND_LINE_SIZE] __initdata;
36213621

36223622
static __init int setup_trace_event(char *str)
36233623
{
3624-
strlcpy(bootup_event_buf, str, COMMAND_LINE_SIZE);
3624+
strscpy(bootup_event_buf, str, COMMAND_LINE_SIZE);
36253625
ring_buffer_expanded = true;
36263626
disable_tracing_selftest("running event tracing");
36273627

kernel/trace/trace_events_inject.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ static int parse_entry(char *str, struct trace_event_call *call, void **pentry)
217217
char *addr = (char *)(unsigned long) val;
218218

219219
if (field->filter_type == FILTER_STATIC_STRING) {
220-
strlcpy(entry + field->offset, addr, field->size);
220+
strscpy(entry + field->offset, addr, field->size);
221221
} else if (field->filter_type == FILTER_DYN_STRING ||
222222
field->filter_type == FILTER_RDYN_STRING) {
223223
int str_len = strlen(addr) + 1;
@@ -232,7 +232,7 @@ static int parse_entry(char *str, struct trace_event_call *call, void **pentry)
232232
}
233233
entry = *pentry;
234234

235-
strlcpy(entry + (entry_size - str_len), addr, str_len);
235+
strscpy(entry + (entry_size - str_len), addr, str_len);
236236
str_item = (u32 *)(entry + field->offset);
237237
if (field->filter_type == FILTER_RDYN_STRING)
238238
str_loc -= field->offset + field->size;

kernel/trace/trace_kprobe.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ static char kprobe_boot_events_buf[COMMAND_LINE_SIZE] __initdata;
3030

3131
static int __init set_kprobe_boot_events(char *str)
3232
{
33-
strlcpy(kprobe_boot_events_buf, str, COMMAND_LINE_SIZE);
33+
strscpy(kprobe_boot_events_buf, str, COMMAND_LINE_SIZE);
3434
disable_tracing_selftest("running kprobe events");
3535

3636
return 1;

kernel/trace/trace_probe.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ int traceprobe_parse_event_name(const char **pevent, const char **pgroup,
254254
trace_probe_log_err(offset, GROUP_TOO_LONG);
255255
return -EINVAL;
256256
}
257-
strlcpy(buf, event, slash - event + 1);
257+
strscpy(buf, event, slash - event + 1);
258258
if (!is_good_system_name(buf)) {
259259
trace_probe_log_err(offset, BAD_GROUP_NAME);
260260
return -EINVAL;

0 commit comments

Comments
 (0)