Skip to content

Commit b36e62e

Browse files
yonghong-songAlexei Starovoitov
authored andcommitted
bpf: Use strncpy_from_unsafe_strict() in bpf_seq_printf() helper
In bpf_seq_printf() helper, when user specified a "%s" in the format string, strncpy_from_unsafe() is used to read the actual string to a buffer. The string could be a format string or a string in the kernel data structure. It is really unlikely that the string will reside in the user memory. This is different from Commit b2a5212 ("bpf: Restrict bpf_trace_printk()'s %s usage and add %pks, %pus specifier") which still used strncpy_from_unsafe() for "%s" to preserve the old behavior. If in the future, bpf_seq_printf() indeed needs to read user memory, we can implement "%pus" format string. Based on discussion in [1], if the intent is to read kernel memory, strncpy_from_unsafe_strict() should be used. So this patch changed to use strncpy_from_unsafe_strict(). [1]: https://lore.kernel.org/bpf/[email protected]/T/ Fixes: 492e639 ("bpf: Add bpf_seq_printf and bpf_seq_write helpers") Signed-off-by: Yonghong Song <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Acked-by: Song Liu <[email protected]> Cc: Christoph Hellwig <[email protected]> Link: https://lore.kernel.org/bpf/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent c3c16f2 commit b36e62e

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

kernel/trace/bpf_trace.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -585,9 +585,9 @@ BPF_CALL_5(bpf_seq_printf, struct seq_file *, m, char *, fmt, u32, fmt_size,
585585
goto out;
586586
}
587587

588-
err = strncpy_from_unsafe(bufs->buf[memcpy_cnt],
589-
(void *) (long) args[fmt_cnt],
590-
MAX_SEQ_PRINTF_STR_LEN);
588+
err = strncpy_from_unsafe_strict(bufs->buf[memcpy_cnt],
589+
(void *) (long) args[fmt_cnt],
590+
MAX_SEQ_PRINTF_STR_LEN);
591591
if (err < 0)
592592
bufs->buf[memcpy_cnt][0] = '\0';
593593
params[fmt_cnt] = (u64)(long)bufs->buf[memcpy_cnt];

0 commit comments

Comments
 (0)