bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH bpf-next 0/2 v2] Add bpf_skc_to_unix_sock() helper
@ 2021-10-07 14:13 Hengqi Chen
  2021-10-07 14:13 ` [PATCH bpf-next 1/2 v2] bpf: " Hengqi Chen
  2021-10-07 14:13 ` [PATCH bpf-next 2/2 v2] selftests/bpf: Test " Hengqi Chen
  0 siblings, 2 replies; 7+ messages in thread
From: Hengqi Chen @ 2021-10-07 14:13 UTC (permalink / raw)
  To: bpf
  Cc: ast, daniel, andrii, yhs, john.fastabend, kafai, songliubraving,
	hengqi.chen

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.

v1->v2:
 - Update selftest, remove trailing spaces changes (Song)

Hengqi Chen (2):
  bpf: Add bpf_skc_to_unix_sock() helper
  selftests/bpf: Test bpf_skc_to_unix_sock() helper

 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                            |  2 +
 tools/include/uapi/linux/bpf.h                |  7 +++
 .../bpf/prog_tests/skc_to_unix_sock.c         | 54 +++++++++++++++++++
 .../bpf/progs/test_skc_to_unix_sock.c         | 29 ++++++++++
 8 files changed, 130 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

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH bpf-next 1/2 v2] bpf: Add bpf_skc_to_unix_sock() helper
  2021-10-07 14:13 [PATCH bpf-next 0/2 v2] Add bpf_skc_to_unix_sock() helper Hengqi Chen
@ 2021-10-07 14:13 ` Hengqi Chen
  2021-10-07 14:13 ` [PATCH bpf-next 2/2 v2] selftests/bpf: Test " Hengqi Chen
  1 sibling, 0 replies; 7+ messages in thread
From: Hengqi Chen @ 2021-10-07 14:13 UTC (permalink / raw)
  To: bpf
  Cc: ast, daniel, andrii, yhs, john.fastabend, kafai, songliubraving,
	hengqi.chen

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.

Suggested-by: Yonghong Song <yhs@fb.com>
Acked-by: Song Liu <songliubraving@fb.com>
Signed-off-by: Hengqi Chen <hengqi.chen@gmail.com>
---
 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             |  2 +
 tools/include/uapi/linux/bpf.h |  7 +++++++
 6 files changed, 47 insertions(+), 5 deletions(-)

diff --git a/include/linux/bpf.h b/include/linux/bpf.h
index d604c8251d88..be3102b4554b 100644
--- a/include/linux/bpf.h
+++ b/include/linux/bpf.h
@@ -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;
diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h
index 6fc59d61937a..22e7a3f38b9f 100644
--- a/include/uapi/linux/bpf.h
+++ b/include/uapi/linux/bpf.h
@@ -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
diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c
index 6b3153841a33..cbcd0d6fca7c 100644
--- a/kernel/trace/bpf_trace.c
+++ b/kernel/trace/bpf_trace.c
@@ -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:
diff --git a/net/core/filter.c b/net/core/filter.c
index 4bace37a6a44..8e8d3b49c297 100644
--- a/net/core/filter.c
+++ b/net/core/filter.c
@@ -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);
 	}
diff --git a/scripts/bpf_doc.py b/scripts/bpf_doc.py
index 00ac7b79cddb..c11335a9b708 100755
--- a/scripts/bpf_doc.py
+++ b/scripts/bpf_doc.py
@@ -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',
diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h
index 6fc59d61937a..22e7a3f38b9f 100644
--- a/tools/include/uapi/linux/bpf.h
+++ b/tools/include/uapi/linux/bpf.h
@@ -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
--
2.25.1

^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH bpf-next 2/2 v2] selftests/bpf: Test bpf_skc_to_unix_sock() helper
  2021-10-07 14:13 [PATCH bpf-next 0/2 v2] Add bpf_skc_to_unix_sock() helper Hengqi Chen
  2021-10-07 14:13 ` [PATCH bpf-next 1/2 v2] bpf: " Hengqi Chen
@ 2021-10-07 14:13 ` Hengqi Chen
  2021-10-19  1:46   ` Alexei Starovoitov
  1 sibling, 1 reply; 7+ messages in thread
From: Hengqi Chen @ 2021-10-07 14:13 UTC (permalink / raw)
  To: bpf
  Cc: ast, daniel, andrii, yhs, john.fastabend, kafai, songliubraving,
	hengqi.chen

Add a new test which triggers unix_listen kernel function
to test bpf_skc_to_unix_sock helper.

Signed-off-by: Hengqi Chen <hengqi.chen@gmail.com>
---
 .../bpf/prog_tests/skc_to_unix_sock.c         | 54 +++++++++++++++++++
 .../bpf/progs/test_skc_to_unix_sock.c         | 29 ++++++++++
 2 files changed, 83 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

diff --git a/tools/testing/selftests/bpf/prog_tests/skc_to_unix_sock.c b/tools/testing/selftests/bpf/prog_tests/skc_to_unix_sock.c
new file mode 100644
index 000000000000..971ef5b948bd
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/skc_to_unix_sock.c
@@ -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_EQ(strcmp(skel->bss->path, sock_path), 0, "bpf_skc_to_unix_sock failed");
+
+cleanup:
+	if (sockfd)
+		close(sockfd);
+	test_skc_to_unix_sock__destroy(skel);
+}
diff --git a/tools/testing/selftests/bpf/progs/test_skc_to_unix_sock.c b/tools/testing/selftests/bpf/progs/test_skc_to_unix_sock.c
new file mode 100644
index 000000000000..098c49c2edce
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/test_skc_to_unix_sock.c
@@ -0,0 +1,29 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/* Copyright (c) 2021 Hengqi Chen */
+
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+#include <bpf/bpf_core_read.h>
+
+const volatile pid_t my_pid = 0;
+char path[256] = {};
+
+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;
+
+	bpf_core_read_str(path, sizeof(path), &unix_sk->addr->name->sun_path);
+	return 0;
+}
+
+char _license[] SEC("license") = "GPL";
--
2.25.1

^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH bpf-next 2/2 v2] selftests/bpf: Test bpf_skc_to_unix_sock() helper
  2021-10-07 14:13 ` [PATCH bpf-next 2/2 v2] selftests/bpf: Test " Hengqi Chen
@ 2021-10-19  1:46   ` Alexei Starovoitov
  2021-10-19 15:23     ` Hengqi Chen
  0 siblings, 1 reply; 7+ messages in thread
From: Alexei Starovoitov @ 2021-10-19  1:46 UTC (permalink / raw)
  To: Hengqi Chen
  Cc: bpf, Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
	Yonghong Song, John Fastabend, Martin KaFai Lau, Song Liu

On Thu, Oct 7, 2021 at 7:14 AM Hengqi Chen <hengqi.chen@gmail.com> wrote:
> +
> +       sockaddr.sun_family = AF_UNIX;
> +       strcpy(sockaddr.sun_path, sock_path);

please use strncpy.

> +       len = sizeof(sockaddr);
> +       unlink(sock_path);

please use abstract socket to avoid unlink and potential race.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH bpf-next 2/2 v2] selftests/bpf: Test bpf_skc_to_unix_sock() helper
  2021-10-19  1:46   ` Alexei Starovoitov
@ 2021-10-19 15:23     ` Hengqi Chen
  2021-10-19 17:02       ` Andrii Nakryiko
  0 siblings, 1 reply; 7+ messages in thread
From: Hengqi Chen @ 2021-10-19 15:23 UTC (permalink / raw)
  To: Alexei Starovoitov
  Cc: bpf, Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
	Yonghong Song, John Fastabend, Martin KaFai Lau, Song Liu

Thanks for the review.

On 10/19/21 9:46 AM, Alexei Starovoitov wrote:
> On Thu, Oct 7, 2021 at 7:14 AM Hengqi Chen <hengqi.chen@gmail.com> wrote:
>> +
>> +       sockaddr.sun_family = AF_UNIX;
>> +       strcpy(sockaddr.sun_path, sock_path);
> 
> please use strncpy.

Will do.

> 
>> +       len = sizeof(sockaddr);
>> +       unlink(sock_path);
> 
> please use abstract socket to avoid unlink and potential race.
> 

Will switch to abstract socket and update the BPF program.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH bpf-next 2/2 v2] selftests/bpf: Test bpf_skc_to_unix_sock() helper
  2021-10-19 15:23     ` Hengqi Chen
@ 2021-10-19 17:02       ` Andrii Nakryiko
  2021-10-20 13:41         ` Hengqi Chen
  0 siblings, 1 reply; 7+ messages in thread
From: Andrii Nakryiko @ 2021-10-19 17:02 UTC (permalink / raw)
  To: Hengqi Chen
  Cc: Alexei Starovoitov, bpf, Alexei Starovoitov, Daniel Borkmann,
	Andrii Nakryiko, Yonghong Song, John Fastabend, Martin KaFai Lau,
	Song Liu

On Tue, Oct 19, 2021 at 8:23 AM Hengqi Chen <hengqi.chen@gmail.com> wrote:
>
> Thanks for the review.
>
> On 10/19/21 9:46 AM, Alexei Starovoitov wrote:
> > On Thu, Oct 7, 2021 at 7:14 AM Hengqi Chen <hengqi.chen@gmail.com> wrote:
> >> +
> >> +       sockaddr.sun_family = AF_UNIX;
> >> +       strcpy(sockaddr.sun_path, sock_path);
> >
> > please use strncpy.
>
> Will do.

please also run checkpatch.pl and confirm you haven't introduced new
styling issues. As one example (and please fix this up in the next
revision), you are using C++-style comments.

>
> >
> >> +       len = sizeof(sockaddr);
> >> +       unlink(sock_path);
> >
> > please use abstract socket to avoid unlink and potential race.
> >
>
> Will switch to abstract socket and update the BPF program.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH bpf-next 2/2 v2] selftests/bpf: Test bpf_skc_to_unix_sock() helper
  2021-10-19 17:02       ` Andrii Nakryiko
@ 2021-10-20 13:41         ` Hengqi Chen
  0 siblings, 0 replies; 7+ messages in thread
From: Hengqi Chen @ 2021-10-20 13:41 UTC (permalink / raw)
  To: Andrii Nakryiko
  Cc: Alexei Starovoitov, bpf, Alexei Starovoitov, Daniel Borkmann,
	Andrii Nakryiko, Yonghong Song, John Fastabend, Martin KaFai Lau,
	Song Liu



On 2021/10/20 1:02 AM, Andrii Nakryiko wrote:
> On Tue, Oct 19, 2021 at 8:23 AM Hengqi Chen <hengqi.chen@gmail.com> wrote:
>>
>> Thanks for the review.
>>
>> On 10/19/21 9:46 AM, Alexei Starovoitov wrote:
>>> On Thu, Oct 7, 2021 at 7:14 AM Hengqi Chen <hengqi.chen@gmail.com> wrote:
>>>> +
>>>> +       sockaddr.sun_family = AF_UNIX;
>>>> +       strcpy(sockaddr.sun_path, sock_path);
>>>
>>> please use strncpy.
>>
>> Will do.
> 
> please also run checkpatch.pl and confirm you haven't introduced new
> styling issues. As one example (and please fix this up in the next
> revision), you are using C++-style comments.
> 

Thanks, Andrii.

Forgot to run checkpatch scripts before sending the email.
Will do that in future patches.

>>
>>>
>>>> +       len = sizeof(sockaddr);
>>>> +       unlink(sock_path);
>>>
>>> please use abstract socket to avoid unlink and potential race.
>>>
>>
>> Will switch to abstract socket and update the BPF program.

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2021-10-20 13:41 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-10-07 14:13 [PATCH bpf-next 0/2 v2] Add bpf_skc_to_unix_sock() helper Hengqi Chen
2021-10-07 14:13 ` [PATCH bpf-next 1/2 v2] bpf: " Hengqi Chen
2021-10-07 14:13 ` [PATCH bpf-next 2/2 v2] selftests/bpf: Test " Hengqi Chen
2021-10-19  1:46   ` Alexei Starovoitov
2021-10-19 15:23     ` Hengqi Chen
2021-10-19 17:02       ` Andrii Nakryiko
2021-10-20 13:41         ` Hengqi Chen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).