Skip to content

Commit d8c3859

Browse files
borkmanngregkh
authored andcommitted
bpf: Add lockdown check for probe_write_user helper
commit 51e1bb9 upstream. Back then, commit 96ae522 ("bpf: Add bpf_probe_write_user BPF helper to be called in tracers") added the bpf_probe_write_user() helper in order to allow to override user space memory. Its original goal was to have a facility to "debug, divert, and manipulate execution of semi-cooperative processes" under CAP_SYS_ADMIN. Write to kernel was explicitly disallowed since it would otherwise tamper with its integrity. One use case was shown in cf9b119 ("samples/bpf: Add test/example of using bpf_probe_write_user bpf helper") where the program DNATs traffic at the time of connect(2) syscall, meaning, it rewrites the arguments to a syscall while they're still in userspace, and before the syscall has a chance to copy the argument into kernel space. These days we have better mechanisms in BPF for achieving the same (e.g. for load-balancers), but without having to write to userspace memory. Of course the bpf_probe_write_user() helper can also be used to abuse many other things for both good or bad purpose. Outside of BPF, there is a similar mechanism for ptrace(2) such as PTRACE_PEEK{TEXT,DATA} and PTRACE_POKE{TEXT,DATA}, but would likely require some more effort. Commit 96ae522 explicitly dedicated the helper for experimentation purpose only. Thus, move the helper's availability behind a newly added LOCKDOWN_BPF_WRITE_USER lockdown knob so that the helper is disabled under the "integrity" mode. More fine-grained control can be implemented also from LSM side with this change. Fixes: 96ae522 ("bpf: Add bpf_probe_write_user BPF helper to be called in tracers") Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Acked-by: Andrii Nakryiko <andrii@kernel.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 5b5064e commit d8c3859

3 files changed

Lines changed: 5 additions & 2 deletions

File tree

‎include/linux/security.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ 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,

‎kernel/trace/bpf_trace.c‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1272,12 +1272,13 @@ bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
12721272
return &bpf_get_numa_node_id_proto;
12731273
case BPF_FUNC_perf_event_read:
12741274
return &bpf_perf_event_read_proto;
1275-
case BPF_FUNC_probe_write_user:
1276-
return bpf_get_probe_write_proto();
12771275
case BPF_FUNC_current_task_under_cgroup:
12781276
return &bpf_current_task_under_cgroup_proto;
12791277
case BPF_FUNC_get_prandom_u32:
12801278
return &bpf_get_prandom_u32_proto;
1279+
case BPF_FUNC_probe_write_user:
1280+
return security_locked_down(LOCKDOWN_BPF_WRITE_USER) < 0 ?
1281+
NULL : bpf_get_probe_write_proto();
12811282
case BPF_FUNC_probe_read_user:
12821283
return &bpf_probe_read_user_proto;
12831284
case BPF_FUNC_probe_read_kernel:

‎security/security.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ 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",

0 commit comments

Comments
 (0)