Skip to content

Commit 8e13810

Browse files
committed
Merge git://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf
Alexei Starovoitov says: ==================== pull-request: bpf 2020-05-15 The following pull-request contains BPF updates for your *net* tree. We've added 9 non-merge commits during the last 2 day(s) which contain a total of 14 files changed, 137 insertions(+), 43 deletions(-). The main changes are: 1) Fix secid_to_secctx LSM hook default value, from Anders. 2) Fix bug in mmap of bpf array, from Andrii. 3) Restrict bpf_probe_read to archs where they work, from Daniel. 4) Enforce returning 0 for fentry/fexit progs, from Yonghong. ==================== Signed-off-by: David S. Miller <[email protected]>
2 parents 9a2dbb5 + 59df9f1 commit 8e13810

File tree

14 files changed

+137
-43
lines changed

14 files changed

+137
-43
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

include/linux/lsm_hook_defs.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ LSM_HOOK(int, -EINVAL, getprocattr, struct task_struct *p, char *name,
243243
char **value)
244244
LSM_HOOK(int, -EINVAL, setprocattr, const char *name, void *value, size_t size)
245245
LSM_HOOK(int, 0, ismaclabel, const char *name)
246-
LSM_HOOK(int, 0, secid_to_secctx, u32 secid, char **secdata,
246+
LSM_HOOK(int, -EOPNOTSUPP, secid_to_secctx, u32 secid, char **secdata,
247247
u32 *seclen)
248248
LSM_HOOK(int, 0, secctx_to_secid, const char *secdata, u32 seclen, u32 *secid)
249249
LSM_HOOK(void, LSM_RET_VOID, release_secctx, char *secdata, u32 seclen)

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/arraymap.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,12 @@ static int array_map_mmap(struct bpf_map *map, struct vm_area_struct *vma)
486486
if (!(map->map_flags & BPF_F_MMAPABLE))
487487
return -EINVAL;
488488

489-
return remap_vmalloc_range(vma, array_map_vmalloc_addr(array), pgoff);
489+
if (vma->vm_pgoff * PAGE_SIZE + (vma->vm_end - vma->vm_start) >
490+
PAGE_ALIGN((u64)array->map.max_entries * array->elem_size))
491+
return -EINVAL;
492+
493+
return remap_vmalloc_range(vma, array_map_vmalloc_addr(array),
494+
vma->vm_pgoff + pgoff);
490495
}
491496

492497
const struct bpf_map_ops array_map_ops = {

kernel/bpf/verifier.c

Lines changed: 20 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;
@@ -7059,6 +7061,23 @@ static int check_return_code(struct bpf_verifier_env *env)
70597061
return 0;
70607062
range = tnum_const(0);
70617063
break;
7064+
case BPF_PROG_TYPE_TRACING:
7065+
switch (env->prog->expected_attach_type) {
7066+
case BPF_TRACE_FENTRY:
7067+
case BPF_TRACE_FEXIT:
7068+
range = tnum_const(0);
7069+
break;
7070+
case BPF_TRACE_RAW_TP:
7071+
case BPF_MODIFY_RETURN:
7072+
return 0;
7073+
default:
7074+
return -ENOTSUPP;
7075+
}
7076+
break;
7077+
case BPF_PROG_TYPE_EXT:
7078+
/* freplace program can return anything as its return value
7079+
* depends on the to-be-replaced kernel func or bpf program.
7080+
*/
70627081
default:
70637082
return 0;
70647083
}

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)