Add bpf_skc_to_unix_sock() helper
Subsystems (9)
cover Add bpf_skc_to_unix_sock() helper
This patch set adds a new BPF helper bpf_skc_to_unix_sock(). The helper is used in tracing programs to cast a socket pointer to a unix_sock pointer.
Hengqi Chen (2):
bpf: Add bpf_skc_to_unix_sock() helper
selftests/bpf: Test bpf_skc_to_unix_sock()
include/linux/bpf.h | 1 + include/uapi/linux/bpf.h | 7 +++ kernel/trace/bpf_trace.c | 2 + net/core/filter.c | 23 ++++++++ scripts/bpf_doc.py | 12 +++-- tools/include/uapi/linux/bpf.h | 7 +++ .../bpf/prog_tests/skc_to_unix_sock.c | 54 +++++++++++++++++++ .../bpf/progs/test_skc_to_unix_sock.c | 28 ++++++++++ 8 files changed, 129 insertions(+), 5 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/skc_to_unix_sock.c create mode 100644 tools/testing/selftests/bpf/progs/test_skc_to_unix_sock.c
--
2.25.1
PATCH 1/2 bpf: Add bpf_skc_to_unix_sock() helper
The helper is used in tracing programs to cast a socket pointer to a unix_sock pointer.
The return value could be NULL if the casting is illegal.
--- include/linux/bpf.h | 1 + include/uapi/linux/bpf.h | 7 +++++++ kernel/trace/bpf_trace.c | 2 ++ net/core/filter.c | 23 +++++++++++++++++++++++ scripts/bpf_doc.py | 12 +++++++----- tools/include/uapi/linux/bpf.h | 7 +++++++ 6 files changed, 47 insertions(+), 5 deletions(-)
include/linux/bpf.h +1 -0
@@ -2093,6 +2093,7 @@ extern const struct bpf_func_proto bpf_skc_to_tcp_sock_proto;extern const struct bpf_func_proto bpf_skc_to_tcp_timewait_sock_proto;extern const struct bpf_func_proto bpf_skc_to_tcp_request_sock_proto;extern const struct bpf_func_proto bpf_skc_to_udp6_sock_proto; +extern const struct bpf_func_proto bpf_skc_to_unix_sock_proto;extern const struct bpf_func_proto bpf_copy_from_user_proto;extern const struct bpf_func_proto bpf_snprintf_btf_proto;extern const struct bpf_func_proto bpf_snprintf_proto;
include/uapi/linux/bpf.h +7 -0
@@ -4909,6 +4909,12 @@ union bpf_attr {* Return * The number of bytes written to the buffer, or a negative error * in case of failure. +* +* struct unix_sock *bpf_skc_to_unix_sock(void *sk) +* Description +* Dynamically cast a *sk* pointer to a *unix_sock* pointer. +* Return +* *sk* if casting is valid, or **NULL** otherwise. */ #define __BPF_FUNC_MAPPER (FN ) \ FN(unspec), \
@@ -5089,6 +5095,7 @@ union bpf_attr {FN (task_pt_regs), \FN (get_branch_snapshot), \FN (trace_vprintk), \ +FN (skc_to_unix_sock), \/* */ /* integer value in 'imm' field of BPF_CALL instruction selects which helper
kernel/trace/bpf_trace.c +2 -0
@@ -1608,6 +1608,8 @@ tracing_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)return &bpf_skc_to_tcp_request_sock_proto;case BPF_FUNC_skc_to_udp6_sock:return &bpf_skc_to_udp6_sock_proto; +case BPF_FUNC_skc_to_unix_sock: +return &bpf_skc_to_unix_sock_proto;case BPF_FUNC_sk_storage_get:return &bpf_sk_storage_get_tracing_proto;case BPF_FUNC_sk_storage_delete:
net/core/filter.c +23 -0
@@ -10723,6 +10723,26 @@ const struct bpf_func_proto bpf_skc_to_udp6_sock_proto = { .ret_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_UDP6], }; +BPF_CALL_1(bpf_skc_to_unix_sock, struct sock *, sk) +{ +/* unix_sock type is not generated in dwarf and hence btf, +* trigger an explicit type generation here. +*/ +BTF_TYPE_EMIT (struct unix_sock ); +if (sk &&sk_fullsock (sk) && sk->sk_family ==AF_UNIX ) +return (unsigned long )sk; + +return (unsigned long )NULL ; +} + +const struct bpf_func_proto bpf_skc_to_unix_sock_proto = { + .func = bpf_skc_to_unix_sock, + .gpl_only = false, + .ret_type =RET_PTR_TO_BTF_ID_OR_NULL , + .arg1_type =ARG_PTR_TO_BTF_ID_SOCK_COMMON , + .ret_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_UNIX ], +}; + BPF_CALL_1(bpf_sock_from_file, struct file *, file) {return (unsigned long )sock_from_file (file);
@@ -10762,6 +10782,9 @@ bpf_sk_base_func_proto(enum bpf_func_id func_id)case BPF_FUNC_skc_to_udp6_sock: func = &bpf_skc_to_udp6_sock_proto;break ; +case BPF_FUNC_skc_to_unix_sock: + func = &bpf_skc_to_unix_sock_proto; +break ;default :return bpf_base_func_proto (func_id); }
scripts/bpf_doc.py +7 -5
@@ -250,17 +250,17 @@ class PrinterRST(Printer): license = '''\ .. Copyright (C) All BPF authors and contributors from 2014 to present. .. See git log include/uapi/linux/bpf.h in kernel tree for details. -.. +.. .. %%%LICENSE_START(VERBATIM) .. Permission is granted to make and distribute verbatim copies of this .. manual provided the copyright notice and this permission notice are .. preserved on all copies. -.. +.. .. Permission is granted to copy and distribute modified versions of this .. manual under the conditions for verbatim copying, provided that the .. entire resulting derived work is distributed under the terms of a .. permission notice identical to this one. -.. +.. .. Since the Linux kernel and libraries are constantly changing, this .. manual page may be incorrect or out-of-date. The author(s) assume no .. responsibility for errors or omissions, or for damages resulting from
@@ -268,11 +268,11 @@ class PrinterRST(Printer): .. have taken the same level of care in the production of this manual, .. which is licensed free of charge, as they might when working .. professionally. -.. +.. .. Formatted or processed versions of this manual, if unaccompanied by .. the source, must acknowledge the copyright and authors of this work. .. %%%LICENSE_END -.. +.. .. Please do not edit this file. It was generated from the documentation .. located in file include/uapi/linux/bpf.h of the Linux kernel sources .. (helpers description), and from scripts/bpf_doc.py in the same
@@ -537,6 +537,7 @@ class PrinterHelpers(Printer): 'struct tcp_timewait_sock', 'struct tcp_request_sock', 'struct udp6_sock', + 'struct unix_sock', 'struct task_struct', 'struct __sk_buff',
@@ -589,6 +590,7 @@ class PrinterHelpers(Printer): 'struct tcp_timewait_sock', 'struct tcp_request_sock', 'struct udp6_sock', + 'struct unix_sock', 'struct task_struct', 'struct path', 'struct btf_ptr',
tools/include/uapi/linux/bpf.h +7 -0
@@ -4909,6 +4909,12 @@ union bpf_attr {* Return * The number of bytes written to the buffer, or a negative error * in case of failure. +* +* struct unix_sock *bpf_skc_to_unix_sock(void *sk) +* Description +* Dynamically cast a *sk* pointer to a *unix_sock* pointer. +* Return +* *sk* if casting is valid, or **NULL** otherwise. */ #define __BPF_FUNC_MAPPER (FN ) \ FN(unspec), \
@@ -5089,6 +5095,7 @@ union bpf_attr {FN (task_pt_regs), \FN (get_branch_snapshot), \FN (trace_vprintk), \ +FN (skc_to_unix_sock), \/* */ /* integer value in 'imm' field of BPF_CALL instruction selects which helper
Signature · 1 line
2.25.1
Re: [PATCH bpf-next 1/2] bpf: Add bpf_skc_to_unix_sock() helper
Quoted 7 lines · Hengqi Chen
The helper is used in tracing programs to cast a socket pointer to a
unix_sockpointer.
The return value could be NULL if the casting is illegal.Suggested-by Yonghong Song <[email protected]>Signed-off-by Hengqi Chen <[email protected]>
With one nitpick below.
[...]
scripts/bpf_doc.py class PrinterRST(Printer): · line 253
Quoted 8 lines
scripts/bpf_doc.py +1
-1@@ -250,17 +250,17 @@ class PrinterRST(Printer): license = '''\ .. Copyright (C) All BPF authors and contributors from 2014 to present. .. See git log include/uapi/linux/bpf.h in kernel tree for details. -.. +..
nit: We usually exclude these trailing space changes from the patch.
Re: [PATCH bpf-next 1/2] bpf: Add bpf_skc_to_unix_sock() helper
Quoted 14 lines · Song Liu
Quoted 7 lines · Hengqi Chen
The helper is used in tracing programs to cast a socket pointer to a
unix_sockpointer.
The return value could be NULL if the casting is illegal.Suggested-by Yonghong Song <[email protected]>Signed-off-by Hengqi Chen <[email protected]>Acked-by Song Liu <[email protected]>With one nitpick below.
[...]
Quoted 11 lines
scripts/bpf_doc.py class PrinterRST(Printer): · line 253
Quoted 8 lines
scripts/bpf_doc.py +1
-1@@ -250,17 +250,17 @@ class PrinterRST(Printer): license = '''\ .. Copyright (C) All BPF authors and contributors from 2014 to present. .. See git log include/uapi/linux/bpf.h in kernel tree for details. -.. +..nit: We usually exclude these trailing space changes from the patch.
Thanks for the review, will drop these unrelated changes in v2.
PATCH 2/2 selftests/bpf: Test bpf_skc_to_unix_sock()
In reply to Hengqi ChenAdd a new test which triggers unix_listen kernel function to test bpf_skc_to_unix_sock helper.
--- .../bpf/prog_tests/skc_to_unix_sock.c | 54 +++++++++++++++++++ .../bpf/progs/test_skc_to_unix_sock.c | 28 ++++++++++ 2 files changed, 82 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/skc_to_unix_sock.c create mode 100644 tools/testing/selftests/bpf/progs/test_skc_to_unix_sock.c
tools/testing/selftests/bpf/prog_tests/skc_to_unix_sock.c new file +54 -0
@@ -0,0 +1,54 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* Copyright (c) 2021 Hengqi Chen */ + +#include <test_progs.h> +#include <sys/un.h> +#include "test_skc_to_unix_sock.skel.h" + +static const char *sock_path ="/tmp/test.sock" ; + +void test_skc_to_unix_sock (void ) +{ +struct test_skc_to_unix_sock *skel; +struct sockaddr_un sockaddr; +int err, len, sockfd =0 ; + + skel =test_skc_to_unix_sock__open (); +if (!ASSERT_OK_PTR (skel,"could not open BPF object" )) +return ; + + skel->rodata->my_pid =getpid (); + + err =test_skc_to_unix_sock__load (skel); +if (!ASSERT_OK (err,"could not load BPF object" )) + goto cleanup; + + err =test_skc_to_unix_sock__attach (skel); +if (!ASSERT_OK (err,"could not attach BPF object" )) + goto cleanup; + +// trigger unix_listen + sockfd =socket (AF_UNIX ,SOCK_STREAM ,0 ); +if (!ASSERT_GT (sockfd,0 ,"socket failed" )) + goto cleanup; + + sockaddr.sun_family =AF_UNIX ; +strcpy (sockaddr.sun_path, sock_path); + len =sizeof (sockaddr); +unlink (sock_path); + + err =bind (sockfd, (struct sockaddr *)&sockaddr, len); +if (!ASSERT_OK (err,"bind failed" )) + goto cleanup; + + err =listen (sockfd,1 ); +if (!ASSERT_OK (err,"listen failed" )) + goto cleanup; + +ASSERT_GT (skel->bss->ret,0 ,"bpf_skc_to_unix_sock failed" ); + +cleanup: +if (sockfd) +close (sockfd); +test_skc_to_unix_sock__destroy (skel); +}
tools/testing/selftests/bpf/progs/test_skc_to_unix_sock.c new file +28 -0
@@ -0,0 +1,28 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* Copyright (c) 2021 Hengqi Chen */ + +#include "vmlinux.h" +#include <bpf/bpf_helpers.h> +#include <bpf/bpf_tracing.h> + +const volatile pid_t my_pid =0 ; +__u64 ret =0 ; + +SEC("fentry/unix_listen") +int BPF_PROG (unix_listen ,struct socket *sock,int backlog) +{ +pid_t pid =bpf_get_current_pid_tgid () >>32 ; +struct unix_sock *unix_sk; + +if (pid != my_pid) +return 0 ; + + unix_sk = (struct unix_sock *)bpf_skc_to_unix_sock (sock->sk); +if (!unix_sk) +return 0 ; + + ret = (__u64 )unix_sk; +return 0 ; +} + +char _license[] SEC("license") = "GPL";
--
2.25.1
Song Liu
maintainerRe: [PATCH bpf-next 2/2] selftests/bpf: Test bpf_skc_to_unix_sock()
Quoted 92 lines · Hengqi Chen
Add a new test which triggers
unix_listenkernel function to testbpf_skc_to_unix_sockhelper.Signed-off-by Hengqi Chen <[email protected]>--- .../bpf/prog_tests/skc_to_unix_sock.c | 54 +++++++++++++++++++ .../bpf/progs/test_skc_to_unix_sock.c | 28 ++++++++++ 2 files changed, 82 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/skc_to_unix_sock.c create mode 100644 tools/testing/selftests/bpf/progs/test_skc_to_unix_sock.ctools/testing/selftests/bpf/prog_tests/skc_to_unix_sock.c new file +54
-0@@ -0,0 +1,54 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* Copyright (c) 2021 Hengqi Chen */ + +#include <test_progs.h> +#include <sys/un.h> +#include "test_skc_to_unix_sock.skel.h" + +static const char *sock_path ="/tmp/test.sock" ; + +void test_skc_to_unix_sock (void ) +{ +struct test_skc_to_unix_sock *skel; +struct sockaddr_un sockaddr; +int err, len, sockfd =0 ; + + skel =test_skc_to_unix_sock__open (); +if (!ASSERT_OK_PTR (skel,"could not open BPF object" )) +return ; + + skel->rodata->my_pid =getpid (); + + err =test_skc_to_unix_sock__load (skel); +if (!ASSERT_OK (err,"could not load BPF object" )) + goto cleanup; + + err =test_skc_to_unix_sock__attach (skel); +if (!ASSERT_OK (err,"could not attach BPF object" )) + goto cleanup; + +// trigger unix_listen + sockfd =socket (AF_UNIX ,SOCK_STREAM ,0 ); +if (!ASSERT_GT (sockfd,0 ,"socket failed" )) + goto cleanup; + + sockaddr.sun_family =AF_UNIX ; +strcpy (sockaddr.sun_path, sock_path); + len =sizeof (sockaddr); +unlink (sock_path); + + err =bind (sockfd, (struct sockaddr *)&sockaddr, len); +if (!ASSERT_OK (err,"bind failed" )) + goto cleanup; + + err =listen (sockfd,1 ); +if (!ASSERT_OK (err,"listen failed" )) + goto cleanup; + +ASSERT_GT (skel->bss->ret,0 ,"bpf_skc_to_unix_sock failed" ); + +cleanup: +if (sockfd) +close (sockfd); +test_skc_to_unix_sock__destroy (skel); +}tools/testing/selftests/bpf/progs/test_skc_to_unix_sock.c new file +14
-0@@ -0,0 +1,28 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* Copyright (c) 2021 Hengqi Chen */ + +#include "vmlinux.h" +#include <bpf/bpf_helpers.h> +#include <bpf/bpf_tracing.h> + +const volatile pid_t my_pid =0 ; +__u64 ret =0 ; + +SEC("fentry/unix_listen") +int BPF_PROG (unix_listen ,struct socket *sock,int backlog) +{ +pid_t pid =bpf_get_current_pid_tgid () >>32 ;
tools/testing/selftests/bpf/progs/test_skc_to_unix_sock.c line 24
Quoted 10 lines
tools/testing/selftests/bpf/progs/test_skc_to_unix_sock.c new file +10
-0+struct unix_sock *unix_sk; + +if (pid != my_pid) +return 0 ; + + unix_sk = (struct unix_sock *)bpf_skc_to_unix_sock (sock->sk); +if (!unix_sk) +return 0 ; + + ret = (__u64 )unix_sk;
Other than ret !=0, can we verify unix_sk we get is expected? May we can verify unix_sock-> path matches /tmp/test.sock?
Thanks,
Song
Re: [PATCH bpf-next 2/2] selftests/bpf: Test bpf_skc_to_unix_sock()
Quoted 98 lines · Song Liu
Quoted 97 lines · Hengqi Chen
Add a new test which triggers
unix_listenkernel function to testbpf_skc_to_unix_sockhelper.Signed-off-by Hengqi Chen <[email protected]>--- .../bpf/prog_tests/skc_to_unix_sock.c | 54 +++++++++++++++++++ .../bpf/progs/test_skc_to_unix_sock.c | 28 ++++++++++ 2 files changed, 82 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/skc_to_unix_sock.c create mode 100644 tools/testing/selftests/bpf/progs/test_skc_to_unix_sock.ctools/testing/selftests/bpf/prog_tests/skc_to_unix_sock.c new file +54
-0@@ -0,0 +1,54 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* Copyright (c) 2021 Hengqi Chen */ + +#include <test_progs.h> +#include <sys/un.h> +#include "test_skc_to_unix_sock.skel.h" + +static const char *sock_path ="/tmp/test.sock" ; + +void test_skc_to_unix_sock (void ) +{ +struct test_skc_to_unix_sock *skel; +struct sockaddr_un sockaddr; +int err, len, sockfd =0 ; + + skel =test_skc_to_unix_sock__open (); +if (!ASSERT_OK_PTR (skel,"could not open BPF object" )) +return ; + + skel->rodata->my_pid =getpid (); + + err =test_skc_to_unix_sock__load (skel); +if (!ASSERT_OK (err,"could not load BPF object" )) + goto cleanup; + + err =test_skc_to_unix_sock__attach (skel); +if (!ASSERT_OK (err,"could not attach BPF object" )) + goto cleanup; + +// trigger unix_listen + sockfd =socket (AF_UNIX ,SOCK_STREAM ,0 ); +if (!ASSERT_GT (sockfd,0 ,"socket failed" )) + goto cleanup; + + sockaddr.sun_family =AF_UNIX ; +strcpy (sockaddr.sun_path, sock_path); + len =sizeof (sockaddr); +unlink (sock_path); + + err =bind (sockfd, (struct sockaddr *)&sockaddr, len); +if (!ASSERT_OK (err,"bind failed" )) + goto cleanup; + + err =listen (sockfd,1 ); +if (!ASSERT_OK (err,"listen failed" )) + goto cleanup; + +ASSERT_GT (skel->bss->ret,0 ,"bpf_skc_to_unix_sock failed" ); + +cleanup: +if (sockfd) +close (sockfd); +test_skc_to_unix_sock__destroy (skel); +}tools/testing/selftests/bpf/progs/test_skc_to_unix_sock.c new file +19
-0@@ -0,0 +1,28 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* Copyright (c) 2021 Hengqi Chen */ + +#include "vmlinux.h" +#include <bpf/bpf_helpers.h> +#include <bpf/bpf_tracing.h> + +const volatile pid_t my_pid =0 ; +__u64 ret =0 ; + +SEC("fentry/unix_listen") +int BPF_PROG (unix_listen ,struct socket *sock,int backlog) +{ +pid_t pid =bpf_get_current_pid_tgid () >>32 ; +struct unix_sock *unix_sk; + +if (pid != my_pid) +return 0 ; +
Quoted 9 lines
tools/testing/selftests/bpf/progs/test_skc_to_unix_sock.c line 24
Quoted 5 lines
tools/testing/selftests/bpf/progs/test_skc_to_unix_sock.c new file +5
-0+ unix_sk = (struct unix_sock *)bpf_skc_to_unix_sock (sock->sk); +if (!unix_sk) +return 0 ; + + ret = (__u64 )unix_sk;Other than ret !=0, can we verify
unix_skwe get is expected? May we can verify unix_sock-> path matches/tmp/test.sock?
Yes, that would be much more precise. Will do.
Quoted 3 lines
Thanks,
Song