Skip to content

Commit 2e273b0

Browse files
committed
Daniel Borkmann says: ==================== bpf 2021-08-10 We've added 5 non-merge commits during the last 2 day(s) which contain a total of 7 files changed, 27 insertions(+), 15 deletions(-). 1) Fix missing bpf_read_lock_trace() context for BPF loader progs, from Yonghong Song. 2) Fix corner case where BPF prog retrieves wrong local storage, also from Yonghong Song. 3) Restrict availability of BPF write_user helper behind lockdown, from Daniel Borkmann. 4) Fix multiple kernel-doc warnings in BPF core, from Randy Dunlap. * https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf: bpf, core: Fix kernel-doc notation bpf: Fix potentially incorrect results with bpf_get_local_storage() bpf: Add missing bpf_read_[un]lock_trace() for syscall program bpf: Add lockdown check for probe_write_user helper bpf: Add _kernel suffix to internal lockdown_bpf_read ==================== Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jakub Kicinski <[email protected]>
2 parents 09c7fd5 + 019d045 commit 2e273b0

File tree

7 files changed

+27
-15
lines changed

7 files changed

+27
-15
lines changed

include/linux/bpf-cgroup.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,8 @@ static inline void bpf_cgroup_storage_unset(void)
201201
{
202202
int i;
203203

204-
for (i = 0; i < BPF_CGROUP_STORAGE_NEST_MAX; i++) {
205-
if (unlikely(this_cpu_read(bpf_cgroup_storage_info[i].task) != current))
204+
for (i = BPF_CGROUP_STORAGE_NEST_MAX - 1; i >= 0; i--) {
205+
if (likely(this_cpu_read(bpf_cgroup_storage_info[i].task) != current))
206206
continue;
207207

208208
this_cpu_write(bpf_cgroup_storage_info[i].task, NULL);

include/linux/security.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,11 @@ enum lockdown_reason {
120120
LOCKDOWN_MMIOTRACE,
121121
LOCKDOWN_DEBUGFS,
122122
LOCKDOWN_XMON_WR,
123+
LOCKDOWN_BPF_WRITE_USER,
123124
LOCKDOWN_INTEGRITY_MAX,
124125
LOCKDOWN_KCORE,
125126
LOCKDOWN_KPROBES,
126-
LOCKDOWN_BPF_READ,
127+
LOCKDOWN_BPF_READ_KERNEL,
127128
LOCKDOWN_PERF,
128129
LOCKDOWN_TRACEFS,
129130
LOCKDOWN_XMON_RW,

kernel/bpf/core.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1362,11 +1362,13 @@ u64 __weak bpf_probe_read_kernel(void *dst, u32 size, const void *unsafe_ptr)
13621362
}
13631363

13641364
/**
1365-
* __bpf_prog_run - run eBPF program on a given context
1365+
* ___bpf_prog_run - run eBPF program on a given context
13661366
* @regs: is the array of MAX_BPF_EXT_REG eBPF pseudo-registers
13671367
* @insn: is the array of eBPF instructions
13681368
*
13691369
* Decode and execute eBPF instructions.
1370+
*
1371+
* Return: whatever value is in %BPF_R0 at program exit
13701372
*/
13711373
static u64 ___bpf_prog_run(u64 *regs, const struct bpf_insn *insn)
13721374
{
@@ -1878,6 +1880,9 @@ static void bpf_prog_select_func(struct bpf_prog *fp)
18781880
*
18791881
* Try to JIT eBPF program, if JIT is not available, use interpreter.
18801882
* The BPF program will be executed via BPF_PROG_RUN() macro.
1883+
*
1884+
* Return: the &fp argument along with &err set to 0 for success or
1885+
* a negative errno code on failure
18811886
*/
18821887
struct bpf_prog *bpf_prog_select_runtime(struct bpf_prog *fp, int *err)
18831888
{

kernel/bpf/helpers.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -397,8 +397,8 @@ BPF_CALL_2(bpf_get_local_storage, struct bpf_map *, map, u64, flags)
397397
void *ptr;
398398
int i;
399399

400-
for (i = 0; i < BPF_CGROUP_STORAGE_NEST_MAX; i++) {
401-
if (unlikely(this_cpu_read(bpf_cgroup_storage_info[i].task) != current))
400+
for (i = BPF_CGROUP_STORAGE_NEST_MAX - 1; i >= 0; i--) {
401+
if (likely(this_cpu_read(bpf_cgroup_storage_info[i].task) != current))
402402
continue;
403403

404404
storage = this_cpu_read(bpf_cgroup_storage_info[i].storage[stype]);
@@ -1070,12 +1070,12 @@ bpf_base_func_proto(enum bpf_func_id func_id)
10701070
case BPF_FUNC_probe_read_user:
10711071
return &bpf_probe_read_user_proto;
10721072
case BPF_FUNC_probe_read_kernel:
1073-
return security_locked_down(LOCKDOWN_BPF_READ) < 0 ?
1073+
return security_locked_down(LOCKDOWN_BPF_READ_KERNEL) < 0 ?
10741074
NULL : &bpf_probe_read_kernel_proto;
10751075
case BPF_FUNC_probe_read_user_str:
10761076
return &bpf_probe_read_user_str_proto;
10771077
case BPF_FUNC_probe_read_kernel_str:
1078-
return security_locked_down(LOCKDOWN_BPF_READ) < 0 ?
1078+
return security_locked_down(LOCKDOWN_BPF_READ_KERNEL) < 0 ?
10791079
NULL : &bpf_probe_read_kernel_str_proto;
10801080
case BPF_FUNC_snprintf_btf:
10811081
return &bpf_snprintf_btf_proto;

kernel/trace/bpf_trace.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -990,28 +990,29 @@ bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
990990
return &bpf_get_numa_node_id_proto;
991991
case BPF_FUNC_perf_event_read:
992992
return &bpf_perf_event_read_proto;
993-
case BPF_FUNC_probe_write_user:
994-
return bpf_get_probe_write_proto();
995993
case BPF_FUNC_current_task_under_cgroup:
996994
return &bpf_current_task_under_cgroup_proto;
997995
case BPF_FUNC_get_prandom_u32:
998996
return &bpf_get_prandom_u32_proto;
997+
case BPF_FUNC_probe_write_user:
998+
return security_locked_down(LOCKDOWN_BPF_WRITE_USER) < 0 ?
999+
NULL : bpf_get_probe_write_proto();
9991000
case BPF_FUNC_probe_read_user:
10001001
return &bpf_probe_read_user_proto;
10011002
case BPF_FUNC_probe_read_kernel:
1002-
return security_locked_down(LOCKDOWN_BPF_READ) < 0 ?
1003+
return security_locked_down(LOCKDOWN_BPF_READ_KERNEL) < 0 ?
10031004
NULL : &bpf_probe_read_kernel_proto;
10041005
case BPF_FUNC_probe_read_user_str:
10051006
return &bpf_probe_read_user_str_proto;
10061007
case BPF_FUNC_probe_read_kernel_str:
1007-
return security_locked_down(LOCKDOWN_BPF_READ) < 0 ?
1008+
return security_locked_down(LOCKDOWN_BPF_READ_KERNEL) < 0 ?
10081009
NULL : &bpf_probe_read_kernel_str_proto;
10091010
#ifdef CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE
10101011
case BPF_FUNC_probe_read:
1011-
return security_locked_down(LOCKDOWN_BPF_READ) < 0 ?
1012+
return security_locked_down(LOCKDOWN_BPF_READ_KERNEL) < 0 ?
10121013
NULL : &bpf_probe_read_compat_proto;
10131014
case BPF_FUNC_probe_read_str:
1014-
return security_locked_down(LOCKDOWN_BPF_READ) < 0 ?
1015+
return security_locked_down(LOCKDOWN_BPF_READ_KERNEL) < 0 ?
10151016
NULL : &bpf_probe_read_compat_str_proto;
10161017
#endif
10171018
#ifdef CONFIG_CGROUPS

net/bpf/test_run.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <linux/vmalloc.h>
88
#include <linux/etherdevice.h>
99
#include <linux/filter.h>
10+
#include <linux/rcupdate_trace.h>
1011
#include <linux/sched/signal.h>
1112
#include <net/bpf_sk_storage.h>
1213
#include <net/sock.h>
@@ -951,7 +952,10 @@ int bpf_prog_test_run_syscall(struct bpf_prog *prog,
951952
goto out;
952953
}
953954
}
955+
956+
rcu_read_lock_trace();
954957
retval = bpf_prog_run_pin_on_cpu(prog, ctx);
958+
rcu_read_unlock_trace();
955959

956960
if (copy_to_user(&uattr->test.retval, &retval, sizeof(u32))) {
957961
err = -EFAULT;

security/security.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,11 @@ const char *const lockdown_reasons[LOCKDOWN_CONFIDENTIALITY_MAX+1] = {
5858
[LOCKDOWN_MMIOTRACE] = "unsafe mmio",
5959
[LOCKDOWN_DEBUGFS] = "debugfs access",
6060
[LOCKDOWN_XMON_WR] = "xmon write access",
61+
[LOCKDOWN_BPF_WRITE_USER] = "use of bpf to write user RAM",
6162
[LOCKDOWN_INTEGRITY_MAX] = "integrity",
6263
[LOCKDOWN_KCORE] = "/proc/kcore access",
6364
[LOCKDOWN_KPROBES] = "use of kprobes",
64-
[LOCKDOWN_BPF_READ] = "use of bpf to read kernel RAM",
65+
[LOCKDOWN_BPF_READ_KERNEL] = "use of bpf to read kernel RAM",
6566
[LOCKDOWN_PERF] = "unsafe use of perf",
6667
[LOCKDOWN_TRACEFS] = "use of tracefs",
6768
[LOCKDOWN_XMON_RW] = "xmon read and write access",

0 commit comments

Comments
 (0)