bpf: add new helper fd2path for mapping a file descriptor to a pathname
Subsystems (7)
PATCH 1/1 bpf: add new helper fd2path for mapping a file descriptor to a pathname
When people want to identify which file system files are being opened, read, and written to, they can use this helper with file descriptor as input to achieve this goal. Other pseudo filesystems are also supported.
Signed-off-by Wenbo Zhang <[email protected]>
--- include/linux/bpf.h | 1 + include/uapi/linux/bpf.h | 12 ++++++++++- kernel/bpf/core.c | 1 + kernel/bpf/helpers.c | 39 ++++++++++++++++++++++++++++++++++ kernel/trace/bpf_trace.c | 2 ++ tools/include/uapi/linux/bpf.h | 12 ++++++++++- 6 files changed, 65 insertions(+), 2 deletions(-)
include/linux/bpf.h +1 -0
@@ -1082,6 +1082,7 @@ extern const struct bpf_func_proto bpf_get_local_storage_proto;extern const struct bpf_func_proto bpf_strtol_proto;extern const struct bpf_func_proto bpf_strtoul_proto;extern const struct bpf_func_proto bpf_tcp_sock_proto; +extern const struct bpf_func_proto bpf_fd2path_proto;/* Shared helpers among cBPF and eBPF. */ void bpf_user_rnd_init_once (void );
include/uapi/linux/bpf.h +11 -1
@@ -2773,6 +2773,15 @@ union bpf_attr {* * This helper is similar to **bpf_perf_event_output**\ () but * restricted to raw_tracepoint bpf programs. +* +* int bpf_fd2path(char *path, u32 size, int fd) +* Description +* Get **file** atrribute from the current task by *fd*, then call +* **d_path** to get it's absolute path and copy it as string into +* *path* of *size*. The **path** also support pseudo filesystems +* (whether or not it can be mounted). The *size* must be strictly +* positive. On success, the helper makes sure that the *path* is +* NUL-terminated. On failure, it is filled with zeroes. * Return * 0 on success, or a negative error in case of failure. */
@@ -2888,7 +2897,8 @@ union bpf_attr {FN (sk_storage_delete), \FN (send_signal), \FN (tcp_gen_syncookie), \ -FN (skb_output), +FN (skb_output), \ +FN (fd2path),/* integer value in 'imm' field of BPF_CALL instruction selects which helper * function eBPF program intends to call
kernel/bpf/core.c +1 -0
@@ -2079,6 +2079,7 @@ const struct bpf_func_proto bpf_get_current_uid_gid_proto __weak;const struct bpf_func_proto bpf_get_current_comm_proto__weak ;const struct bpf_func_proto bpf_get_current_cgroup_id_proto__weak ;const struct bpf_func_proto bpf_get_local_storage_proto__weak ; +const struct bpf_func_proto bpf_fd2path_proto__weak ;const struct bpf_func_proto *__weak bpf_get_trace_printk_proto (void ) {
kernel/bpf/helpers.c +39 -0
@@ -487,3 +487,42 @@ const struct bpf_func_proto bpf_strtoul_proto = { .arg4_type = ARG_PTR_TO_LONG, };#endif + +BPF_CALL_3(bpf_fd2path, char *, dst, u32, size, int, fd) +{ +struct fd f; +int ret; +char *p; + + ret =security_locked_down (LOCKDOWN_BPF_READ ); +if (ret <0 ) + goto out; + + f =fdget_raw (fd); +if (!f.file) + goto out; + + p =d_path (&f.file->f_path, dst, size); +if (IS_ERR_OR_NULL (p)) + ret =PTR_ERR (p); +else { + ret =strlen (p); +memmove (dst, p, ret); + dst[ret] =0 ; + } + +if (unlikely (ret <0 )) +out: +memset (dst,'0' , size); + +return ret; +} + +const struct bpf_func_proto bpf_fd2path_proto = { + .func = bpf_fd2path, + .gpl_only = true, + .ret_type =RET_INTEGER , + .arg1_type =ARG_PTR_TO_UNINIT_MEM , + .arg2_type =ARG_CONST_SIZE , + .arg3_type =ARG_ANYTHING , +};
kernel/trace/bpf_trace.c +2 -0
@@ -735,6 +735,8 @@ tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)#endif case BPF_FUNC_send_signal:return &bpf_send_signal_proto; +case BPF_FUNC_fd2path: +return &bpf_fd2path_proto;default :return NULL ; }
tools/include/uapi/linux/bpf.h +11 -1
@@ -2773,6 +2773,15 @@ union bpf_attr {* * This helper is similar to **bpf_perf_event_output**\ () but * restricted to raw_tracepoint bpf programs. +* +* int bpf_fd2path(char *path, u32 size, int fd) +* Description +* Get **file** atrribute from the current task by *fd*, then call +* **d_path** to get it's absolute path and copy it as string into +* *path* of *size*. The **path** also support pseudo filesystems +* (whether or not it can be mounted). The *size* must be strictly +* positive. On success, the helper makes sure that the *path* is +* NUL-terminated. On failure, it is filled with zeroes. * Return * 0 on success, or a negative error in case of failure. */
@@ -2888,7 +2897,8 @@ union bpf_attr {FN (sk_storage_delete), \FN (send_signal), \FN (tcp_gen_syncookie), \ -FN (skb_output), +FN (skb_output), \ +FN (fd2path),/* integer value in 'imm' field of BPF_CALL instruction selects which helper * function eBPF program intends to call
Signature · 1 line
2.17.1
Quoted 3 lines · Wenbo Zhang
When people want to identify which file system files are being opened, read, and written to, they can use this helper with file descriptor as input to achieve this goal. Other pseudo filesystems are also supported.
I commented on previous version of the patch. Could you add version number (this is v2) to the patch set when you submit next time (which will be v3).
Quoted 156 lines
Signed-off-by Wenbo Zhang <[email protected]>--- include/linux/bpf.h | 1 + include/uapi/linux/bpf.h | 12 ++++++++++- kernel/bpf/core.c | 1 + kernel/bpf/helpers.c | 39 ++++++++++++++++++++++++++++++++++ kernel/trace/bpf_trace.c | 2 ++ tools/include/uapi/linux/bpf.h | 12 ++++++++++- 6 files changed, 65 insertions(+), 2 deletions(-)include/linux/bpf.h +1
-0@@ -1082,6 +1082,7 @@ extern const struct bpf_func_proto bpf_get_local_storage_proto;extern const struct bpf_func_proto bpf_strtol_proto;extern const struct bpf_func_proto bpf_strtoul_proto;extern const struct bpf_func_proto bpf_tcp_sock_proto; +extern const struct bpf_func_proto bpf_fd2path_proto;/* Shared helpers among cBPF and eBPF. */ void bpf_user_rnd_init_once (void );include/uapi/linux/bpf.h +11
-1@@ -2773,6 +2773,15 @@ union bpf_attr {* * This helper is similar to **bpf_perf_event_output**\ () but * restricted to raw_tracepoint bpf programs. +* +* int bpf_fd2path(char *path, u32 size, int fd) +* Description +* Get **file** atrribute from the current task by *fd*, then call +* **d_path** to get it's absolute path and copy it as string into +* *path* of *size*. The **path** also support pseudo filesystems +* (whether or not it can be mounted). The *size* must be strictly +* positive. On success, the helper makes sure that the *path* is +* NUL-terminated. On failure, it is filled with zeroes. * Return * 0 on success, or a negative error in case of failure. */ @@ -2888,7 +2897,8 @@ union bpf_attr {FN (sk_storage_delete), \FN (send_signal), \FN (tcp_gen_syncookie), \ -FN (skb_output), +FN (skb_output), \ +FN (fd2path),/* integer value in 'imm' field of BPF_CALL instruction selects which helper * function eBPF program intends to call kernel/bpf/core.c +1
-0@@ -2079,6 +2079,7 @@ const struct bpf_func_proto bpf_get_current_uid_gid_proto __weak;const struct bpf_func_proto bpf_get_current_comm_proto__weak ;const struct bpf_func_proto bpf_get_current_cgroup_id_proto__weak ;const struct bpf_func_proto bpf_get_local_storage_proto__weak ; +const struct bpf_func_proto bpf_fd2path_proto__weak ;const struct bpf_func_proto *__weak bpf_get_trace_printk_proto (void ) {kernel/bpf/helpers.c +39
-0@@ -487,3 +487,42 @@ const struct bpf_func_proto bpf_strtoul_proto = { .arg4_type = ARG_PTR_TO_LONG, };#endif + +BPF_CALL_3(bpf_fd2path, char *, dst, u32, size, int, fd) +{ +struct fd f; +int ret; +char *p; + + ret =security_locked_down (LOCKDOWN_BPF_READ ); +if (ret <0 ) + goto out; + + f =fdget_raw (fd); +if (!f.file) + goto out; + + p =d_path (&f.file->f_path, dst, size); +if (IS_ERR_OR_NULL (p)) + ret =PTR_ERR (p); +else { + ret =strlen (p); +memmove (dst, p, ret); + dst[ret] =0 ; + } + +if (unlikely (ret <0 )) +out: +memset (dst,'0' , size); + +return ret; +} + +const struct bpf_func_proto bpf_fd2path_proto = { + .func = bpf_fd2path, + .gpl_only = true, + .ret_type =RET_INTEGER , + .arg1_type =ARG_PTR_TO_UNINIT_MEM , + .arg2_type =ARG_CONST_SIZE , + .arg3_type =ARG_ANYTHING , +};kernel/trace/bpf_trace.c +2
-0@@ -735,6 +735,8 @@ tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)#endif case BPF_FUNC_send_signal:return &bpf_send_signal_proto; +case BPF_FUNC_fd2path: +return &bpf_fd2path_proto;default :return NULL ; }tools/include/uapi/linux/bpf.h +11
-1@@ -2773,6 +2773,15 @@ union bpf_attr {* * This helper is similar to **bpf_perf_event_output**\ () but * restricted to raw_tracepoint bpf programs. +* +* int bpf_fd2path(char *path, u32 size, int fd) +* Description +* Get **file** atrribute from the current task by *fd*, then call +* **d_path** to get it's absolute path and copy it as string into +* *path* of *size*. The **path** also support pseudo filesystems +* (whether or not it can be mounted). The *size* must be strictly +* positive. On success, the helper makes sure that the *path* is +* NUL-terminated. On failure, it is filled with zeroes. * Return * 0 on success, or a negative error in case of failure. */ @@ -2888,7 +2897,8 @@ union bpf_attr {FN (sk_storage_delete), \FN (send_signal), \FN (tcp_gen_syncookie), \ -FN (skb_output), +FN (skb_output), \ +FN (fd2path),/* integer value in 'imm' field of BPF_CALL instruction selects which helper * function eBPF program intends to call