Skip to content

Commit b2a5212

Browse files
borkmannAlexei Starovoitov
authored andcommitted
bpf: Restrict bpf_trace_printk()'s %s usage and add %pks, %pus specifier
Usage of plain %s conversion specifier in bpf_trace_printk() suffers from the very same issue as bpf_probe_read{,str}() helpers, that is, it is broken on archs with overlapping address ranges. While the helpers have been addressed through work in 6ae08ae ("bpf: Add probe_read_{user, kernel} and probe_read_{user, kernel}_str helpers"), we need an option for bpf_trace_printk() as well to fix it. Similarly as with the helpers, force users to make an explicit choice by adding %pks and %pus specifier to bpf_trace_printk() which will then pick the corresponding strncpy_from_unsafe*() variant to perform the access under KERNEL_DS or USER_DS. The %pk* (kernel specifier) and %pu* (user specifier) can later also be extended for other objects aside strings that are probed and printed under tracing, and reused out of other facilities like bpf_seq_printf() or BTF based type printing. Existing behavior of %s for current users is still kept working for archs where it is not broken and therefore gated through CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE. For archs not having this property we fall-back to pick probing under KERNEL_DS as a sensible default. Fixes: 8d3b7dc ("bpf: add support for %s specifier to bpf_trace_printk()") Reported-by: Linus Torvalds <[email protected]> Reported-by: Christoph Hellwig <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Cc: Masami Hiramatsu <[email protected]> Cc: Brendan Gregg <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 47cc0ed commit b2a5212

File tree

3 files changed

+88
-32
lines changed

3 files changed

+88
-32
lines changed

Documentation/core-api/printk-formats.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,20 @@ used when printing stack backtraces. The specifier takes into
112112
consideration the effect of compiler optimisations which may occur
113113
when tail-calls are used and marked with the noreturn GCC attribute.
114114

115+
Probed Pointers from BPF / tracing
116+
----------------------------------
117+
118+
::
119+
120+
%pks kernel string
121+
%pus user string
122+
123+
The ``k`` and ``u`` specifiers are used for printing prior probed memory from
124+
either kernel memory (k) or user memory (u). The subsequent ``s`` specifier
125+
results in printing a string. For direct use in regular vsnprintf() the (k)
126+
and (u) annotation is ignored, however, when used out of BPF's bpf_trace_printk(),
127+
for example, it reads the memory it is pointing to without faulting.
128+
115129
Kernel Pointers
116130
---------------
117131

kernel/trace/bpf_trace.c

Lines changed: 62 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -323,17 +323,15 @@ static const struct bpf_func_proto *bpf_get_probe_write_proto(void)
323323

324324
/*
325325
* Only limited trace_printk() conversion specifiers allowed:
326-
* %d %i %u %x %ld %li %lu %lx %lld %lli %llu %llx %p %s
326+
* %d %i %u %x %ld %li %lu %lx %lld %lli %llu %llx %p %pks %pus %s
327327
*/
328328
BPF_CALL_5(bpf_trace_printk, char *, fmt, u32, fmt_size, u64, arg1,
329329
u64, arg2, u64, arg3)
330330
{
331+
int i, mod[3] = {}, fmt_cnt = 0;
332+
char buf[64], fmt_ptype;
333+
void *unsafe_ptr = NULL;
331334
bool str_seen = false;
332-
int mod[3] = {};
333-
int fmt_cnt = 0;
334-
u64 unsafe_addr;
335-
char buf[64];
336-
int i;
337335

338336
/*
339337
* bpf_check()->check_func_arg()->check_stack_boundary()
@@ -359,40 +357,71 @@ BPF_CALL_5(bpf_trace_printk, char *, fmt, u32, fmt_size, u64, arg1,
359357
if (fmt[i] == 'l') {
360358
mod[fmt_cnt]++;
361359
i++;
362-
} else if (fmt[i] == 'p' || fmt[i] == 's') {
360+
} else if (fmt[i] == 'p') {
363361
mod[fmt_cnt]++;
362+
if ((fmt[i + 1] == 'k' ||
363+
fmt[i + 1] == 'u') &&
364+
fmt[i + 2] == 's') {
365+
fmt_ptype = fmt[i + 1];
366+
i += 2;
367+
goto fmt_str;
368+
}
369+
364370
/* disallow any further format extensions */
365371
if (fmt[i + 1] != 0 &&
366372
!isspace(fmt[i + 1]) &&
367373
!ispunct(fmt[i + 1]))
368374
return -EINVAL;
369-
fmt_cnt++;
370-
if (fmt[i] == 's') {
371-
if (str_seen)
372-
/* allow only one '%s' per fmt string */
373-
return -EINVAL;
374-
str_seen = true;
375-
376-
switch (fmt_cnt) {
377-
case 1:
378-
unsafe_addr = arg1;
379-
arg1 = (long) buf;
380-
break;
381-
case 2:
382-
unsafe_addr = arg2;
383-
arg2 = (long) buf;
384-
break;
385-
case 3:
386-
unsafe_addr = arg3;
387-
arg3 = (long) buf;
388-
break;
389-
}
390-
buf[0] = 0;
391-
strncpy_from_unsafe(buf,
392-
(void *) (long) unsafe_addr,
375+
376+
goto fmt_next;
377+
} else if (fmt[i] == 's') {
378+
mod[fmt_cnt]++;
379+
fmt_ptype = fmt[i];
380+
fmt_str:
381+
if (str_seen)
382+
/* allow only one '%s' per fmt string */
383+
return -EINVAL;
384+
str_seen = true;
385+
386+
if (fmt[i + 1] != 0 &&
387+
!isspace(fmt[i + 1]) &&
388+
!ispunct(fmt[i + 1]))
389+
return -EINVAL;
390+
391+
switch (fmt_cnt) {
392+
case 0:
393+
unsafe_ptr = (void *)(long)arg1;
394+
arg1 = (long)buf;
395+
break;
396+
case 1:
397+
unsafe_ptr = (void *)(long)arg2;
398+
arg2 = (long)buf;
399+
break;
400+
case 2:
401+
unsafe_ptr = (void *)(long)arg3;
402+
arg3 = (long)buf;
403+
break;
404+
}
405+
406+
buf[0] = 0;
407+
switch (fmt_ptype) {
408+
case 's':
409+
#ifdef CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE
410+
strncpy_from_unsafe(buf, unsafe_ptr,
393411
sizeof(buf));
412+
break;
413+
#endif
414+
case 'k':
415+
strncpy_from_unsafe_strict(buf, unsafe_ptr,
416+
sizeof(buf));
417+
break;
418+
case 'u':
419+
strncpy_from_unsafe_user(buf,
420+
(__force void __user *)unsafe_ptr,
421+
sizeof(buf));
422+
break;
394423
}
395-
continue;
424+
goto fmt_next;
396425
}
397426

398427
if (fmt[i] == 'l') {
@@ -403,6 +432,7 @@ BPF_CALL_5(bpf_trace_printk, char *, fmt, u32, fmt_size, u64, arg1,
403432
if (fmt[i] != 'i' && fmt[i] != 'd' &&
404433
fmt[i] != 'u' && fmt[i] != 'x')
405434
return -EINVAL;
435+
fmt_next:
406436
fmt_cnt++;
407437
}
408438

lib/vsprintf.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2168,6 +2168,10 @@ char *fwnode_string(char *buf, char *end, struct fwnode_handle *fwnode,
21682168
* f full name
21692169
* P node name, including a possible unit address
21702170
* - 'x' For printing the address. Equivalent to "%lx".
2171+
* - '[ku]s' For a BPF/tracing related format specifier, e.g. used out of
2172+
* bpf_trace_printk() where [ku] prefix specifies either kernel (k)
2173+
* or user (u) memory to probe, and:
2174+
* s a string, equivalent to "%s" on direct vsnprintf() use
21712175
*
21722176
* ** When making changes please also update:
21732177
* Documentation/core-api/printk-formats.rst
@@ -2251,6 +2255,14 @@ char *pointer(const char *fmt, char *buf, char *end, void *ptr,
22512255
if (!IS_ERR(ptr))
22522256
break;
22532257
return err_ptr(buf, end, ptr, spec);
2258+
case 'u':
2259+
case 'k':
2260+
switch (fmt[1]) {
2261+
case 's':
2262+
return string(buf, end, ptr, spec);
2263+
default:
2264+
return error_string(buf, end, "(einval)", spec);
2265+
}
22542266
}
22552267

22562268
/* default is to _not_ leak addresses, hash before printing */

0 commit comments

Comments
 (0)