bpf: add new helper fd2path for mapping a file descriptor to a pathname
Subsystems (9)
PATCH v3 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.
This requirement is mainly discussed here:
https://github.com/iovisor/bcc/issues/237
v2->v3:
- remove unnecessary LOCKDOWN_BPF_READ
- refactor error handling section for enhanced readability
- provide a test case in tools/testing/selftests/bpf
v1->v2:
- fix backward compatibility
- add this helper description
- fix signed-off name
--- include/linux/bpf.h | 1 + include/uapi/linux/bpf.h | 12 +- kernel/bpf/core.c | 1 + kernel/bpf/helpers.c | 36 +++++ kernel/trace/bpf_trace.c | 2 + tools/include/uapi/linux/bpf.h | 12 +- tools/testing/selftests/bpf/Makefile | 8 +- tools/testing/selftests/bpf/fd2path_loadgen.c | 75 ++++++++++ .../selftests/bpf/prog_tests/fd2path.c | 131 ++++++++++++++++++ .../selftests/bpf/progs/test_fd2path.c | 56 ++++++++ 10 files changed, 330 insertions(+), 4 deletions(-) create mode 100644 tools/testing/selftests/bpf/fd2path_loadgen.c create mode 100644 tools/testing/selftests/bpf/prog_tests/fd2path.c create mode 100644 tools/testing/selftests/bpf/progs/test_fd2path.c
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 +36 -0
@@ -487,3 +487,39 @@ 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; +char *p; +int ret = -EINVAL ; + + f =fdget_raw (fd); +if (!f.file) + goto error; + + p =d_path (&f.file->f_path, dst, size); +if (IS_ERR_OR_NULL (p)) { + ret =PTR_ERR (p); + goto error; + } + + ret =strlen (p); +memmove (dst, p, ret); + dst[ret] ='\0' ; + goto end; + +error: +memset (dst,'0' , size); +end: +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
tools/testing/selftests/bpf/Makefile +6 -2
@@ -72,7 +72,7 @@ TEST_GEN_PROGS_EXTENDED = test_sock_addr test_skb_cgroup_id_user \ flow_dissector_load test_flow_dissector test_tcp_check_syncookie_user \ test_lirc_mode2_user -TEST_CUSTOM_PROGS = urandom_read +TEST_CUSTOM_PROGS = urandom_read fd2path_loadgen include ../lib.mk
@@ -89,6 +89,9 @@ $(notdir $(TEST_GEN_PROGS) \ $(OUTPUT)/urandom_read: urandom_read.c $(CC) -o $@ $< -Wl,--build-id +$(OUTPUT)/fd2path_loadgen: fd2path_loadgen.c + $(CC) -o $@ $< -Wl,--build-id + BPFOBJ := $(OUTPUT)/libbpf.a $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED): $(OUTPUT)/test_stub.o $(BPFOBJ)
@@ -258,7 +261,8 @@ TRUNNER_TESTS_DIR := prog_tests TRUNNER_BPF_PROGS_DIR := progs TRUNNER_EXTRA_SOURCES := test_progs.c cgroup_helpers.c trace_helpers.c \ flow_dissector_load.h -TRUNNER_EXTRA_FILES := $(OUTPUT)/urandom_read \ +TRUNNER_EXTRA_FILES := $(OUTPUT)/urandom_read \ + $(OUTPUT)/fd2path_loadgen \ $(wildcard progs/btf_dump_test_case_*.c) TRUNNER_BPF_BUILD_RULE := CLANG_BPF_BUILD_RULE TRUNNER_BPF_CFLAGS := -I. -I$(OUTPUT) $(BPF_CFLAGS) $(CLANG_CFLAGS)
tools/testing/selftests/bpf/fd2path_loadgen.c new file +75 -0
75 lines not on this page · raw message
tools/testing/selftests/bpf/prog_tests/fd2path.c new file +131 -0
131 lines not on this page · raw message
tools/testing/selftests/bpf/progs/test_fd2path.c new file +56 -0
56 lines not on this page · raw message
Signature · 1 line
2.17.1
Daniel Borkmann
maintainerOn Thu, Oct 24, 2019 at 10:38:56AM -0400, Wenbo Zhang wrote:
[...]
include/linux/bpf.h extern const struct bpf_func_proto bpf_get_local_storage_proto; · line 1085
Quoted 8 lines
index
2c2c29b49845..d73314a7e674100644include/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;
Don't think we need to expose it here. More below.
Quoted 11 lines
include/linux/bpf.h +0
-0/* Shared helpers among cBPF and eBPF. */ void bpf_user_rnd_init_once (void );include/uapi/linux/bpf.h +1
-0@@ -2773,6 +2773,15 @@ union bpf_attr {* * This helper is similar to **bpf_perf_event_output**\ () but * restricted to raw_tracepoint bpf programs. +*
include/uapi/linux/bpf.h union bpf_attr { · line 2786
Quoted 10 lines
include/uapi/linux/bpf.h +8
-0+* 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.
The 'Return' bits are now removed from prior helper description?
Quoted 16 lines
include/uapi/linux/bpf.h +2
-1*/ @@ -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 +0
-0
kernel/bpf/core.c const struct bpf_func_proto bpf_get_current_uid_gid_proto __weak; · line 2082
Quoted 4 lines
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 ;
Ditto, not needed ...
Quoted 8 lines
kernel/bpf/core.c +0
-0const struct bpf_func_proto *__weak bpf_get_trace_printk_proto (void ) {kernel/bpf/helpers.c +0
-0@@ -487,3 +487,39 @@ const struct bpf_func_proto bpf_strtoul_proto = { .arg4_type = ARG_PTR_TO_LONG,
kernel/bpf/helpers.c const struct bpf_func_proto bpf_strtoul_proto = { · line 497
Quoted 10 lines
kernel/bpf/helpers.c +8
-0};#endif + +BPF_CALL_3(bpf_fd2path, char *, dst, u32, size, int, fd) +{ +struct fd f; +char *p; +int ret = -EINVAL ; + + f =fdget_raw (fd);
What's the rationale for using the fdget_raw variant?
Quoted 8 lines
kernel/bpf/helpers.c +8
-0+if (!f.file) + goto error; + + p =d_path (&f.file->f_path, dst, size); +if (IS_ERR_OR_NULL (p)) { + ret =PTR_ERR (p); + goto error; + }
kernel/bpf/helpers.c const struct bpf_func_proto bpf_strtoul_proto = { · line 515
Quoted 10 lines
kernel/bpf/helpers.c +10
-0+ + ret =strlen (p); +memmove (dst, p, ret); + dst[ret] ='\0' ; + goto end; + +error: +memset (dst,'0' , size); +end: +return ret;
Where is fdput(f) in here?
kernel/bpf/helpers.c const struct bpf_func_proto bpf_strtoul_proto = { · line 525
Quoted 10 lines
kernel/bpf/helpers.c +10
-0+} + +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 , +};
... as you can simply add the helper into kernel/trace/bpf_trace.c right away if it's only used form there anyway and make bpf_fd2path_proto static.
tools/testing/selftests/bpf/Makefile
Quoted 47 lines
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 tools/testing/selftests/bpf/Makefile +0
-0
Would be good to split out selftests into a dedicated patch.
Thanks,
Daniel