Subject: Re: [PATCH RFC] bpf: Add support for reading user pointers

From: Masami Hiramatsu <[email-masked]>
Date: Mon, 6 May 2019 23:47:51 +0900

Hi Joel,

On Thu,  2 May 2019 16:49:58 -0400
"Joel Fernandes (Google)" <[email-masked]> wrote:

> The eBPF based opensnoop tool fails to read the file path string passed
> to the do_sys_open function. This is because it is a pointer to
> userspace address and causes an -EFAULT when read with
> probe_kernel_read. This is not an issue when running the tool on x86 but
> is an issue on arm64. This patch adds a new bpf function call based
> which calls the recently proposed probe_user_read function [1].
> Using this function call from opensnoop fixes the issue on arm64.
> 
> [1] https://lore.kernel.org/patchwork/patch/1051588/

Anyway, this series is still out-of-tree. We have to push this or similar
update into kernel at first. I can resend v7 on the latest -tip tree including
this patch if you update the description.

Thank you,

> 
> Cc: Michal Gregorczyk <[email-masked]>
> Cc: Adrian Ratiu <[email-masked]>
> Cc: Mohammad Husain <[email-masked]>
> Cc: Qais Yousef <[email-masked]>
> Cc: Srinivas Ramana <[email-masked]>
> Cc: duyuchao <[email-masked]>
> Cc: Manjo Raja Rao <[email-masked]>
> Cc: Karim Yaghmour <[email-masked]>
> Cc: Tamir Carmeli <[email-masked]>
> Cc: Yonghong Song <[email-masked]>
> Cc: Alexei Starovoitov <[email-masked]>
> Cc: Brendan Gregg <[email-masked]>
> Cc: Masami Hiramatsu <[email-masked]>
> Cc: Peter Ziljstra <[email-masked]>
> Cc: Steven Rostedt <[email-masked]>
> Cc: Kees Cook <[email-masked]>
> Cc: [email-masked]
> Signed-off-by: Joel Fernandes (Google) <[email-masked]>
> ---
>  include/uapi/linux/bpf.h       |  7 ++++++-
>  kernel/trace/bpf_trace.c       | 22 ++++++++++++++++++++++
>  tools/include/uapi/linux/bpf.h |  7 ++++++-
>  3 files changed, 34 insertions(+), 2 deletions(-)
> 
> diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h
> index e99e3e6f8b37..6fec701eaa46 100644
> --- a/include/uapi/linux/bpf.h
> +++ b/include/uapi/linux/bpf.h
> @@ -539,6 +539,10 @@ union bpf_attr {
>   *     @mode: operation mode (enum bpf_adj_room_mode)
>   *     @flags: reserved for future use
>   *     Return: 0 on success or negative error code
> + *
> + * int bpf_probe_read_user(void *dst, int size, void *src)
> + *     Read a userspace pointer safely.
> + *     Return: 0 on success or negative error
>   */
>  #define __BPF_FUNC_MAPPER(FN)		\
>  	FN(unspec),			\
> @@ -591,7 +595,8 @@ union bpf_attr {
>  	FN(get_socket_uid),		\
>  	FN(set_hash),			\
>  	FN(setsockopt),			\
> -	FN(skb_adjust_room),
> +	FN(skb_adjust_room),		\
> +	FN(probe_read_user),
>  
>  /* integer value in 'imm' field of BPF_CALL instruction selects which helper
>   * function eBPF program intends to call
> diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c
> index dc498b605d5d..1e1a11d9faa8 100644
> --- a/kernel/trace/bpf_trace.c
> +++ b/kernel/trace/bpf_trace.c
> @@ -81,6 +81,26 @@ static const struct bpf_func_proto bpf_probe_read_proto = {
>  	.arg3_type	= ARG_ANYTHING,
>  };
>  
> +BPF_CALL_3(bpf_probe_read_user, void *, dst, u32, size, const void *, unsafe_ptr)
> +{
> +	int ret;
> +
> +	ret = probe_user_read(dst, unsafe_ptr, size);
> +	if (unlikely(ret < 0))
> +		memset(dst, 0, size);
> +
> +	return ret;
> +}
> +
> +static const struct bpf_func_proto bpf_probe_read_user_proto = {
> +	.func		= bpf_probe_read_user,
> +	.gpl_only	= true,
> +	.ret_type	= RET_INTEGER,
> +	.arg1_type	= ARG_PTR_TO_UNINIT_MEM,
> +	.arg2_type	= ARG_CONST_SIZE,
> +	.arg3_type	= ARG_ANYTHING,
> +};
> +
>  BPF_CALL_3(bpf_probe_write_user, void *, unsafe_ptr, const void *, src,
>  	   u32, size)
>  {
> @@ -459,6 +479,8 @@ static const struct bpf_func_proto *tracing_func_proto(enum bpf_func_id func_id)
>  		return &bpf_map_delete_elem_proto;
>  	case BPF_FUNC_probe_read:
>  		return &bpf_probe_read_proto;
> +	case BPF_FUNC_probe_read_user:
> +		return &bpf_probe_read_user_proto;
>  	case BPF_FUNC_ktime_get_ns:
>  		return &bpf_ktime_get_ns_proto;
>  	case BPF_FUNC_tail_call:
> diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h
> index e99e3e6f8b37..6fec701eaa46 100644
> --- a/tools/include/uapi/linux/bpf.h
> +++ b/tools/include/uapi/linux/bpf.h
> @@ -539,6 +539,10 @@ union bpf_attr {
>   *     @mode: operation mode (enum bpf_adj_room_mode)
>   *     @flags: reserved for future use
>   *     Return: 0 on success or negative error code
> + *
> + * int bpf_probe_read_user(void *dst, int size, void *src)
> + *     Read a userspace pointer safely.
> + *     Return: 0 on success or negative error
>   */
>  #define __BPF_FUNC_MAPPER(FN)		\
>  	FN(unspec),			\
> @@ -591,7 +595,8 @@ union bpf_attr {
>  	FN(get_socket_uid),		\
>  	FN(set_hash),			\
>  	FN(setsockopt),			\
> -	FN(skb_adjust_room),
> +	FN(skb_adjust_room),		\
> +	FN(probe_read_user),
>  
>  /* integer value in 'imm' field of BPF_CALL instruction selects which helper
>   * function eBPF program intends to call
> -- 
> 2.21.0.593.g511ec345e18-goog
> 

-- 
Masami Hiramatsu <[email-masked]>