Skip to contentSkip to conversation

bpf: use strncpy_from_unsafe_strict() in bpf_seq_printf() helper

PATCH Applied Yonghong Song 3 participants 3 messages May 29, 2020 – Jun 1, 2020

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 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 b2a5212fb634 ("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/

Cc Christoph Hellwig <[email protected]>
---
 kernel/trace/bpf_trace.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
kernel/trace/bpf_trace.c +3 -3
index 187cd6995bbb..3a4afbc7f0bc 100644
@@ -585,9 +585,9 @@ BPF_CALL_5(bpf_seq_printf, struct seq_file *, m, char *, fmt, u32, fmt_size,
 				goto out;
 			}
 
-			err = strncpy_from_unsafe(bufs->buf[memcpy_cnt],
-						  (void *) (long) args[fmt_cnt],
-						  MAX_SEQ_PRINTF_STR_LEN);
+			err = strncpy_from_unsafe_strict(bufs->buf[memcpy_cnt],
+							 (void *) (long) args[fmt_cnt],
+							 MAX_SEQ_PRINTF_STR_LEN);
 			if (err < 0)
 				bufs->buf[memcpy_cnt][0] = '\0';
 			params[fmt_cnt] = (u64)(long)bufs->buf[memcpy_cnt];
Signature · 1 line
2.24.1
Acked-by on v1 1/1
#1 lore raw
Quoted 17 lines · Yonghong Song

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 b2a5212fb634 ("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.

Quoted 8 lines

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/

Cc Christoph Hellwig <[email protected]>

I guess we should add:

Fixes 492e639f0c22 ("bpf: Add bpf_seq_printf and bpf_seq_write helpers")
#2 lore raw
Quoted 24 lines · Song Liu
Quoted 24 lines · Yonghong Song

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 b2a5212fb634 ("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/

Quoted 8 lines
Quoted 2 lines
Cc Christoph Hellwig <[email protected]>

I guess we should add:

Fixes 492e639f0c22 ("bpf: Add bpf_seq_printf and bpf_seq_write helpers")

Applied. Thanks