Skip to content

Commit 59df9f1

Browse files
author
Alexei Starovoitov
committed
Merge branch 'restrict-bpf_probe_read'
Daniel Borkmann says: ==================== Small set of fixes in order to restrict BPF helpers for tracing which are broken on archs with overlapping address ranges as per discussion in [0]. I've targetted this for -bpf tree so they can be routed as fixes. Thanks! v1 -> v2: - switch to reusable %pks, %pus format specifiers (Yonghong) - fixate %s on kernel_ds probing for archs with overlapping addr space [0] https://lore.kernel.org/bpf/CAHk-=wjJKo0GVixYLmqPn-Q22WFu0xHaBSjKEo7e7Yw72y5SPQ@mail.gmail.com/T/ ==================== Signed-off-by: Alexei Starovoitov <[email protected]>
2 parents 6d74f64 + b2a5212 commit 59df9f1

File tree

8 files changed

+101
-35
lines changed

8 files changed

+101
-35
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

arch/arm/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ config ARM
1212
select ARCH_HAS_KEEPINITRD
1313
select ARCH_HAS_KCOV
1414
select ARCH_HAS_MEMBARRIER_SYNC_CORE
15+
select ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE
1516
select ARCH_HAS_PTE_SPECIAL if ARM_LPAE
1617
select ARCH_HAS_PHYS_TO_DMA
1718
select ARCH_HAS_SETUP_DMA_OPS

arch/arm64/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ config ARM64
2020
select ARCH_HAS_KCOV
2121
select ARCH_HAS_KEEPINITRD
2222
select ARCH_HAS_MEMBARRIER_SYNC_CORE
23+
select ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE
2324
select ARCH_HAS_PTE_DEVMAP
2425
select ARCH_HAS_PTE_SPECIAL
2526
select ARCH_HAS_SETUP_DMA_OPS

arch/x86/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ config X86
6868
select ARCH_HAS_KCOV if X86_64
6969
select ARCH_HAS_MEM_ENCRYPT
7070
select ARCH_HAS_MEMBARRIER_SYNC_CORE
71+
select ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE
7172
select ARCH_HAS_PMEM_API if X86_64
7273
select ARCH_HAS_PTE_DEVMAP if X86_64
7374
select ARCH_HAS_PTE_SPECIAL

init/Kconfig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2279,6 +2279,9 @@ config ASN1
22792279

22802280
source "kernel/Kconfig.locks"
22812281

2282+
config ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE
2283+
bool
2284+
22822285
config ARCH_HAS_SYNC_CORE_BEFORE_USERMODE
22832286
bool
22842287

kernel/bpf/verifier.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4340,7 +4340,9 @@ static void do_refine_retval_range(struct bpf_reg_state *regs, int ret_type,
43404340

43414341
if (ret_type != RET_INTEGER ||
43424342
(func_id != BPF_FUNC_get_stack &&
4343-
func_id != BPF_FUNC_probe_read_str))
4343+
func_id != BPF_FUNC_probe_read_str &&
4344+
func_id != BPF_FUNC_probe_read_kernel_str &&
4345+
func_id != BPF_FUNC_probe_read_user_str))
43444346
return;
43454347

43464348
ret_reg->smax_value = meta->msize_max_value;

kernel/trace/bpf_trace.c

Lines changed: 66 additions & 34 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

@@ -825,14 +855,16 @@ bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
825855
return &bpf_probe_read_user_proto;
826856
case BPF_FUNC_probe_read_kernel:
827857
return &bpf_probe_read_kernel_proto;
828-
case BPF_FUNC_probe_read:
829-
return &bpf_probe_read_compat_proto;
830858
case BPF_FUNC_probe_read_user_str:
831859
return &bpf_probe_read_user_str_proto;
832860
case BPF_FUNC_probe_read_kernel_str:
833861
return &bpf_probe_read_kernel_str_proto;
862+
#ifdef CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE
863+
case BPF_FUNC_probe_read:
864+
return &bpf_probe_read_compat_proto;
834865
case BPF_FUNC_probe_read_str:
835866
return &bpf_probe_read_compat_str_proto;
867+
#endif
836868
#ifdef CONFIG_CGROUPS
837869
case BPF_FUNC_get_current_cgroup_id:
838870
return &bpf_get_current_cgroup_id_proto;

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)