Re: [PATCH bpf-next v1 1/3] bpf: Add bpf_file_d_path helper

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Thu, Jul 18, 2024 at 4:53 AM Lin Yikai <yikai.lin@xxxxxxxx> wrote:
>
> Add the "bpf_file_d_path" helper function
> to retrieve the path from a struct file object.
> But there is no need to include vmlinux.h
> or reference the definition of struct file.
>
> Additionally, update the bpf.h tools uapi header.
>
> Signed-off-by: Lin Yikai <yikai.lin@xxxxxxxx>
> ---
>  include/uapi/linux/bpf.h       | 20 ++++++++++++++++++++
>  kernel/trace/bpf_trace.c       | 34 ++++++++++++++++++++++++++++++++++
>  tools/include/uapi/linux/bpf.h | 20 ++++++++++++++++++++
>  3 files changed, 74 insertions(+)
>
> diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h
> index 35bcf52dbc65..7e5cec61a877 100644
> --- a/include/uapi/linux/bpf.h
> +++ b/include/uapi/linux/bpf.h
> @@ -5792,6 +5792,25 @@ union bpf_attr {
>   *             0 on success.
>   *
>   *             **-ENOENT** if the bpf_local_storage cannot be found.
> + *
> + * long bpf_file_d_path(void *file, char *dst, u32 size)
> + *     Description
> + *             Return full path for the given *file* object.
> + *
> + *             In order to solve issues where certain eBPF programs can not include
> + *             the definition of struct file or vmlinux.h
> + *             due to their complexity and conflicts on some operating system,
> + *             the variable *file* here is declared as type void*
> + *             instead of the traditional struct file*.
> + *             It will be forcibly converted into type struct file* later.
> + *
> + *             If the path is larger than *size*, then only *size*
> + *             bytes will be copied to *dst*
> + *
> + *     Return
> + *             On success, the strictly positive length of the string,
> + *             including the trailing NULL character. On error, a negative
> + *             value.
>   */
>  #define ___BPF_FUNC_MAPPER(FN, ctx...)                 \
>         FN(unspec, 0, ##ctx)                            \
> @@ -6006,6 +6025,7 @@ union bpf_attr {
>         FN(user_ringbuf_drain, 209, ##ctx)              \
>         FN(cgrp_storage_get, 210, ##ctx)                \
>         FN(cgrp_storage_delete, 211, ##ctx)             \
> +       FN(file_d_path, 212, ##ctx)                     \
>         /* */
>
>  /* backwards-compatibility macros for users of __BPF_FUNC_MAPPER that don't
> diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c
> index cd098846e251..70fde7f20e97 100644
> --- a/kernel/trace/bpf_trace.c
> +++ b/kernel/trace/bpf_trace.c
> @@ -1257,6 +1257,38 @@ static const struct bpf_func_proto bpf_get_func_arg_cnt_proto = {
>         .arg1_type      = ARG_PTR_TO_CTX,
>  };
>
> +BPF_CALL_3(bpf_file_d_path, void *, file, char*, dst, u32, size)
> +{
> +       long len;
> +       struct file copy;
> +       char *ptr;
> +
> +       if (!size)
> +               return 0;
> +
> +       len = copy_from_kernel_nofault(&copy, (struct file *)file, sizeof(struct file));
> +       if (len < 0)
> +               return len;
> +
> +       ptr = d_path(&(copy.f_path), dst, size);
> +       if (IS_ERR(ptr)) {
> +               len = PTR_ERR(ptr);
> +       } else {
> +               len = dst + size - ptr;
> +               memmove(dst, ptr, len);
> +       }
> +       return len;
> +}
> +
> +const struct bpf_func_proto bpf_file_d_path_proto = {
> +       .func           = bpf_file_d_path,
> +       .gpl_only       = false,
> +       .ret_type       = RET_INTEGER,
> +       .arg1_type      = ARG_ANYTHING,

you can't just accept any random value as `struct file *`, this is a
complete no-go. It will have to accept some sort of PTR_TRUSTED
argument, be added as kfunc, etc, etc. We had earlier discussion
around this, I don't remember all the details, but this is definitely
not the way forward.

> +       .arg2_type      = ARG_PTR_TO_MEM,
> +       .arg3_type      = ARG_CONST_SIZE_OR_ZERO,
> +};
> +
>  #ifdef CONFIG_KEYS
>  __bpf_kfunc_start_defs();
>
> @@ -1629,6 +1661,8 @@ bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
>                 return &bpf_find_vma_proto;
>         case BPF_FUNC_trace_vprintk:
>                 return bpf_get_trace_vprintk_proto();
> +       case BPF_FUNC_file_d_path:
> +               return &bpf_file_d_path_proto;
>         default:
>                 return bpf_base_func_proto(func_id, prog);
>         }
> diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h
> index 35bcf52dbc65..7e5cec61a877 100644
> --- a/tools/include/uapi/linux/bpf.h
> +++ b/tools/include/uapi/linux/bpf.h
> @@ -5792,6 +5792,25 @@ union bpf_attr {
>   *             0 on success.
>   *
>   *             **-ENOENT** if the bpf_local_storage cannot be found.
> + *
> + * long bpf_file_d_path(void *file, char *dst, u32 size)
> + *     Description
> + *             Return full path for the given *file* object.
> + *
> + *             In order to solve issues where certain eBPF programs can not include
> + *             the definition of struct file or vmlinux.h
> + *             due to their complexity and conflicts on some operating system,
> + *             the variable *file* here is declared as type void*
> + *             instead of the traditional struct file*.
> + *             It will be forcibly converted into type struct file* later.
> + *
> + *             If the path is larger than *size*, then only *size*
> + *             bytes will be copied to *dst*
> + *
> + *     Return
> + *             On success, the strictly positive length of the string,
> + *             including the trailing NULL character. On error, a negative
> + *             value.
>   */
>  #define ___BPF_FUNC_MAPPER(FN, ctx...)                 \
>         FN(unspec, 0, ##ctx)                            \
> @@ -6006,6 +6025,7 @@ union bpf_attr {
>         FN(user_ringbuf_drain, 209, ##ctx)              \
>         FN(cgrp_storage_get, 210, ##ctx)                \
>         FN(cgrp_storage_delete, 211, ##ctx)             \
> +       FN(file_d_path, 212, ##ctx)                     \
>         /* */
>
>  /* backwards-compatibility macros for users of __BPF_FUNC_MAPPER that don't
> --
> 2.34.1
>





[Index of Archives]     [Linux Samsung SoC]     [Linux Rockchip SoC]     [Linux Actions SoC]     [Linux for Synopsys ARC Processors]     [Linux NFS]     [Linux NILFS]     [Linux USB Devel]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]


  Powered by Linux