Skip to content

Commit 376040e

Browse files
kennyyuAlexei Starovoitov
authored and
Alexei Starovoitov
committed
bpf: Add bpf_copy_from_user_task() helper
This adds a helper for bpf programs to read the memory of other tasks. As an example use case at Meta, we are using a bpf task iterator program and this new helper to print C++ async stack traces for all threads of a given process. Signed-off-by: Kenny Yu <[email protected]> Acked-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent b77fb25 commit 376040e

File tree

5 files changed

+59
-0
lines changed

5 files changed

+59
-0
lines changed

include/linux/bpf.h

+1
Original file line numberDiff line numberDiff line change
@@ -2243,6 +2243,7 @@ extern const struct bpf_func_proto bpf_kallsyms_lookup_name_proto;
22432243
extern const struct bpf_func_proto bpf_find_vma_proto;
22442244
extern const struct bpf_func_proto bpf_loop_proto;
22452245
extern const struct bpf_func_proto bpf_strncmp_proto;
2246+
extern const struct bpf_func_proto bpf_copy_from_user_task_proto;
22462247

22472248
const struct bpf_func_proto *tracing_prog_func_proto(
22482249
enum bpf_func_id func_id, const struct bpf_prog *prog);

include/uapi/linux/bpf.h

+11
Original file line numberDiff line numberDiff line change
@@ -5076,6 +5076,16 @@ union bpf_attr {
50765076
* associated to *xdp_md*, at *offset*.
50775077
* Return
50785078
* 0 on success, or a negative error in case of failure.
5079+
*
5080+
* long bpf_copy_from_user_task(void *dst, u32 size, const void *user_ptr, struct task_struct *tsk, u64 flags)
5081+
* Description
5082+
* Read *size* bytes from user space address *user_ptr* in *tsk*'s
5083+
* address space, and stores the data in *dst*. *flags* is not
5084+
* used yet and is provided for future extensibility. This helper
5085+
* can only be used by sleepable programs.
5086+
* Return
5087+
* 0 on success, or a negative error in case of failure. On error
5088+
* *dst* buffer is zeroed out.
50795089
*/
50805090
#define __BPF_FUNC_MAPPER(FN) \
50815091
FN(unspec), \
@@ -5269,6 +5279,7 @@ union bpf_attr {
52695279
FN(xdp_get_buff_len), \
52705280
FN(xdp_load_bytes), \
52715281
FN(xdp_store_bytes), \
5282+
FN(copy_from_user_task), \
52725283
/* */
52735284

52745285
/* integer value in 'imm' field of BPF_CALL instruction selects which helper

kernel/bpf/helpers.c

+34
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <linux/pid_namespace.h>
1717
#include <linux/proc_ns.h>
1818
#include <linux/security.h>
19+
#include <linux/btf_ids.h>
1920

2021
#include "../../lib/kstrtox.h"
2122

@@ -671,6 +672,39 @@ const struct bpf_func_proto bpf_copy_from_user_proto = {
671672
.arg3_type = ARG_ANYTHING,
672673
};
673674

675+
BPF_CALL_5(bpf_copy_from_user_task, void *, dst, u32, size,
676+
const void __user *, user_ptr, struct task_struct *, tsk, u64, flags)
677+
{
678+
int ret;
679+
680+
/* flags is not used yet */
681+
if (unlikely(flags))
682+
return -EINVAL;
683+
684+
if (unlikely(!size))
685+
return 0;
686+
687+
ret = access_process_vm(tsk, (unsigned long)user_ptr, dst, size, 0);
688+
if (ret == size)
689+
return 0;
690+
691+
memset(dst, 0, size);
692+
/* Return -EFAULT for partial read */
693+
return ret < 0 ? ret : -EFAULT;
694+
}
695+
696+
const struct bpf_func_proto bpf_copy_from_user_task_proto = {
697+
.func = bpf_copy_from_user_task,
698+
.gpl_only = false,
699+
.ret_type = RET_INTEGER,
700+
.arg1_type = ARG_PTR_TO_UNINIT_MEM,
701+
.arg2_type = ARG_CONST_SIZE_OR_ZERO,
702+
.arg3_type = ARG_ANYTHING,
703+
.arg4_type = ARG_PTR_TO_BTF_ID,
704+
.arg4_btf_id = &btf_tracing_ids[BTF_TRACING_TYPE_TASK],
705+
.arg5_type = ARG_ANYTHING
706+
};
707+
674708
BPF_CALL_2(bpf_per_cpu_ptr, const void *, ptr, u32, cpu)
675709
{
676710
if (cpu >= nr_cpu_ids)

kernel/trace/bpf_trace.c

+2
Original file line numberDiff line numberDiff line change
@@ -1235,6 +1235,8 @@ bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
12351235
return &bpf_get_task_stack_proto;
12361236
case BPF_FUNC_copy_from_user:
12371237
return prog->aux->sleepable ? &bpf_copy_from_user_proto : NULL;
1238+
case BPF_FUNC_copy_from_user_task:
1239+
return prog->aux->sleepable ? &bpf_copy_from_user_task_proto : NULL;
12381240
case BPF_FUNC_snprintf_btf:
12391241
return &bpf_snprintf_btf_proto;
12401242
case BPF_FUNC_per_cpu_ptr:

tools/include/uapi/linux/bpf.h

+11
Original file line numberDiff line numberDiff line change
@@ -5076,6 +5076,16 @@ union bpf_attr {
50765076
* associated to *xdp_md*, at *offset*.
50775077
* Return
50785078
* 0 on success, or a negative error in case of failure.
5079+
*
5080+
* long bpf_copy_from_user_task(void *dst, u32 size, const void *user_ptr, struct task_struct *tsk, u64 flags)
5081+
* Description
5082+
* Read *size* bytes from user space address *user_ptr* in *tsk*'s
5083+
* address space, and stores the data in *dst*. *flags* is not
5084+
* used yet and is provided for future extensibility. This helper
5085+
* can only be used by sleepable programs.
5086+
* Return
5087+
* 0 on success, or a negative error in case of failure. On error
5088+
* *dst* buffer is zeroed out.
50795089
*/
50805090
#define __BPF_FUNC_MAPPER(FN) \
50815091
FN(unspec), \
@@ -5269,6 +5279,7 @@ union bpf_attr {
52695279
FN(xdp_get_buff_len), \
52705280
FN(xdp_load_bytes), \
52715281
FN(xdp_store_bytes), \
5282+
FN(copy_from_user_task), \
52725283
/* */
52735284

52745285
/* integer value in 'imm' field of BPF_CALL instruction selects which helper

0 commit comments

Comments
 (0)