aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSargun Dhillon <sargun@sargun.me>2016-07-25 08:54:46 -0400
committerDavid S. Miller <davem@davemloft.net>2016-07-25 21:07:48 -0400
commit96ae52279594470622ff0585621a13e96b700600 (patch)
tree72b6be55be49c626dfd6d1b1ac2673b4a0cd649b
parent9b022a6e0f26af108b9105b16b310393c898d9bd (diff)
bpf: Add bpf_probe_write_user BPF helper to be called in tracers
This allows user memory to be written to during the course of a kprobe. It shouldn't be used to implement any kind of security mechanism because of TOC-TOU attacks, but rather to debug, divert, and manipulate execution of semi-cooperative processes. Although it uses probe_kernel_write, we limit the address space the probe can write into by checking the space with access_ok. We do this as opposed to calling copy_to_user directly, in order to avoid sleeping. In addition we ensure the threads's current fs / segment is USER_DS and the thread isn't exiting nor a kernel thread. Given this feature is meant for experiments, and it has a risk of crashing the system, and running programs, we print a warning on when a proglet that attempts to use this helper is installed, along with the pid and process name. Signed-off-by: Sargun Dhillon <sargun@sargun.me> Cc: Alexei Starovoitov <ast@kernel.org> Cc: Daniel Borkmann <daniel@iogearbox.net> Acked-by: Alexei Starovoitov <ast@kernel.org> Signed-off-by: David S. Miller <davem@davemloft.net>
-rw-r--r--include/uapi/linux/bpf.h10
-rw-r--r--kernel/trace/bpf_trace.c45
-rw-r--r--samples/bpf/bpf_helpers.h2
3 files changed, 57 insertions, 0 deletions
diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h
index 2b7076f5b5ad..da218fec6056 100644
--- a/include/uapi/linux/bpf.h
+++ b/include/uapi/linux/bpf.h
@@ -365,6 +365,16 @@ enum bpf_func_id {
365 */ 365 */
366 BPF_FUNC_get_current_task, 366 BPF_FUNC_get_current_task,
367 367
368 /**
369 * bpf_probe_write_user(void *dst, void *src, int len)
370 * safely attempt to write to a location
371 * @dst: destination address in userspace
372 * @src: source address on stack
373 * @len: number of bytes to copy
374 * Return: 0 on success or negative error
375 */
376 BPF_FUNC_probe_write_user,
377
368 __BPF_FUNC_MAX_ID, 378 __BPF_FUNC_MAX_ID,
369}; 379};
370 380
diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c
index a12bbd32c0a6..b20438fdb029 100644
--- a/kernel/trace/bpf_trace.c
+++ b/kernel/trace/bpf_trace.c
@@ -81,6 +81,49 @@ static const struct bpf_func_proto bpf_probe_read_proto = {
81 .arg3_type = ARG_ANYTHING, 81 .arg3_type = ARG_ANYTHING,
82}; 82};
83 83
84static u64 bpf_probe_write_user(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
85{
86 void *unsafe_ptr = (void *) (long) r1;
87 void *src = (void *) (long) r2;
88 int size = (int) r3;
89
90 /*
91 * Ensure we're in user context which is safe for the helper to
92 * run. This helper has no business in a kthread.
93 *
94 * access_ok() should prevent writing to non-user memory, but in
95 * some situations (nommu, temporary switch, etc) access_ok() does
96 * not provide enough validation, hence the check on KERNEL_DS.
97 */
98
99 if (unlikely(in_interrupt() ||
100 current->flags & (PF_KTHREAD | PF_EXITING)))
101 return -EPERM;
102 if (unlikely(segment_eq(get_fs(), KERNEL_DS)))
103 return -EPERM;
104 if (!access_ok(VERIFY_WRITE, unsafe_ptr, size))
105 return -EPERM;
106
107 return probe_kernel_write(unsafe_ptr, src, size);
108}
109
110static const struct bpf_func_proto bpf_probe_write_user_proto = {
111 .func = bpf_probe_write_user,
112 .gpl_only = true,
113 .ret_type = RET_INTEGER,
114 .arg1_type = ARG_ANYTHING,
115 .arg2_type = ARG_PTR_TO_STACK,
116 .arg3_type = ARG_CONST_STACK_SIZE,
117};
118
119static const struct bpf_func_proto *bpf_get_probe_write_proto(void)
120{
121 pr_warn_ratelimited("%s[%d] is installing a program with bpf_probe_write_user helper that may corrupt user memory!",
122 current->comm, task_pid_nr(current));
123
124 return &bpf_probe_write_user_proto;
125}
126
84/* 127/*
85 * limited trace_printk() 128 * limited trace_printk()
86 * only %d %u %x %ld %lu %lx %lld %llu %llx %p %s conversion specifiers allowed 129 * only %d %u %x %ld %lu %lx %lld %llu %llx %p %s conversion specifiers allowed
@@ -362,6 +405,8 @@ static const struct bpf_func_proto *tracing_func_proto(enum bpf_func_id func_id)
362 return &bpf_get_smp_processor_id_proto; 405 return &bpf_get_smp_processor_id_proto;
363 case BPF_FUNC_perf_event_read: 406 case BPF_FUNC_perf_event_read:
364 return &bpf_perf_event_read_proto; 407 return &bpf_perf_event_read_proto;
408 case BPF_FUNC_probe_write_user:
409 return bpf_get_probe_write_proto();
365 default: 410 default:
366 return NULL; 411 return NULL;
367 } 412 }
diff --git a/samples/bpf/bpf_helpers.h b/samples/bpf/bpf_helpers.h
index 84e3fd919a06..217c8d507f2e 100644
--- a/samples/bpf/bpf_helpers.h
+++ b/samples/bpf/bpf_helpers.h
@@ -41,6 +41,8 @@ static int (*bpf_perf_event_output)(void *ctx, void *map, int index, void *data,
41 (void *) BPF_FUNC_perf_event_output; 41 (void *) BPF_FUNC_perf_event_output;
42static int (*bpf_get_stackid)(void *ctx, void *map, int flags) = 42static int (*bpf_get_stackid)(void *ctx, void *map, int flags) =
43 (void *) BPF_FUNC_get_stackid; 43 (void *) BPF_FUNC_get_stackid;
44static int (*bpf_probe_write_user)(void *dst, void *src, int size) =
45 (void *) BPF_FUNC_probe_write_user;
44 46
45/* llvm builtin functions that eBPF C program may use to 47/* llvm builtin functions that eBPF C program may use to
46 * emit BPF_LD_ABS and BPF_LD_IND instructions 48 * emit BPF_LD_ABS and BPF_LD_IND instructions