Skip to content

Commit 549beec

Browse files
author
Alexei Starovoitov
committed
Merge branch 'Add bpf_skc_to_unix_sock() helper'
Hengqi Chen says: ==================== 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. v2->v3: - Use abstract socket in selftest (Alexei) - Run checkpatch script over patches (Andrii) v1->v2: - Update selftest, remove trailing spaces changes (Song) ==================== Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2 parents 44ce0ac + b6c4e71 commit 549beec

File tree

8 files changed

+136
-0
lines changed

8 files changed

+136
-0
lines changed

include/linux/bpf.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2093,6 +2093,7 @@ extern const struct bpf_func_proto bpf_skc_to_tcp_sock_proto;
20932093
extern const struct bpf_func_proto bpf_skc_to_tcp_timewait_sock_proto;
20942094
extern const struct bpf_func_proto bpf_skc_to_tcp_request_sock_proto;
20952095
extern const struct bpf_func_proto bpf_skc_to_udp6_sock_proto;
2096+
extern const struct bpf_func_proto bpf_skc_to_unix_sock_proto;
20962097
extern const struct bpf_func_proto bpf_copy_from_user_proto;
20972098
extern const struct bpf_func_proto bpf_snprintf_btf_proto;
20982099
extern const struct bpf_func_proto bpf_snprintf_proto;

include/uapi/linux/bpf.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4909,6 +4909,12 @@ union bpf_attr {
49094909
* Return
49104910
* The number of bytes written to the buffer, or a negative error
49114911
* in case of failure.
4912+
*
4913+
* struct unix_sock *bpf_skc_to_unix_sock(void *sk)
4914+
* Description
4915+
* Dynamically cast a *sk* pointer to a *unix_sock* pointer.
4916+
* Return
4917+
* *sk* if casting is valid, or **NULL** otherwise.
49124918
*/
49134919
#define __BPF_FUNC_MAPPER(FN) \
49144920
FN(unspec), \
@@ -5089,6 +5095,7 @@ union bpf_attr {
50895095
FN(task_pt_regs), \
50905096
FN(get_branch_snapshot), \
50915097
FN(trace_vprintk), \
5098+
FN(skc_to_unix_sock), \
50925099
/* */
50935100

50945101
/* integer value in 'imm' field of BPF_CALL instruction selects which helper

kernel/trace/bpf_trace.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1608,6 +1608,8 @@ tracing_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
16081608
return &bpf_skc_to_tcp_request_sock_proto;
16091609
case BPF_FUNC_skc_to_udp6_sock:
16101610
return &bpf_skc_to_udp6_sock_proto;
1611+
case BPF_FUNC_skc_to_unix_sock:
1612+
return &bpf_skc_to_unix_sock_proto;
16111613
case BPF_FUNC_sk_storage_get:
16121614
return &bpf_sk_storage_get_tracing_proto;
16131615
case BPF_FUNC_sk_storage_delete:

net/core/filter.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10723,6 +10723,26 @@ const struct bpf_func_proto bpf_skc_to_udp6_sock_proto = {
1072310723
.ret_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_UDP6],
1072410724
};
1072510725

10726+
BPF_CALL_1(bpf_skc_to_unix_sock, struct sock *, sk)
10727+
{
10728+
/* unix_sock type is not generated in dwarf and hence btf,
10729+
* trigger an explicit type generation here.
10730+
*/
10731+
BTF_TYPE_EMIT(struct unix_sock);
10732+
if (sk && sk_fullsock(sk) && sk->sk_family == AF_UNIX)
10733+
return (unsigned long)sk;
10734+
10735+
return (unsigned long)NULL;
10736+
}
10737+
10738+
const struct bpf_func_proto bpf_skc_to_unix_sock_proto = {
10739+
.func = bpf_skc_to_unix_sock,
10740+
.gpl_only = false,
10741+
.ret_type = RET_PTR_TO_BTF_ID_OR_NULL,
10742+
.arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
10743+
.ret_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_UNIX],
10744+
};
10745+
1072610746
BPF_CALL_1(bpf_sock_from_file, struct file *, file)
1072710747
{
1072810748
return (unsigned long)sock_from_file(file);
@@ -10762,6 +10782,9 @@ bpf_sk_base_func_proto(enum bpf_func_id func_id)
1076210782
case BPF_FUNC_skc_to_udp6_sock:
1076310783
func = &bpf_skc_to_udp6_sock_proto;
1076410784
break;
10785+
case BPF_FUNC_skc_to_unix_sock:
10786+
func = &bpf_skc_to_unix_sock_proto;
10787+
break;
1076510788
default:
1076610789
return bpf_base_func_proto(func_id);
1076710790
}

scripts/bpf_doc.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,7 @@ def __init__(self, parser):
537537
'struct tcp_timewait_sock',
538538
'struct tcp_request_sock',
539539
'struct udp6_sock',
540+
'struct unix_sock',
540541
'struct task_struct',
541542

542543
'struct __sk_buff',
@@ -589,6 +590,7 @@ def __init__(self, parser):
589590
'struct tcp_timewait_sock',
590591
'struct tcp_request_sock',
591592
'struct udp6_sock',
593+
'struct unix_sock',
592594
'struct task_struct',
593595
'struct path',
594596
'struct btf_ptr',

tools/include/uapi/linux/bpf.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4909,6 +4909,12 @@ union bpf_attr {
49094909
* Return
49104910
* The number of bytes written to the buffer, or a negative error
49114911
* in case of failure.
4912+
*
4913+
* struct unix_sock *bpf_skc_to_unix_sock(void *sk)
4914+
* Description
4915+
* Dynamically cast a *sk* pointer to a *unix_sock* pointer.
4916+
* Return
4917+
* *sk* if casting is valid, or **NULL** otherwise.
49124918
*/
49134919
#define __BPF_FUNC_MAPPER(FN) \
49144920
FN(unspec), \
@@ -5089,6 +5095,7 @@ union bpf_attr {
50895095
FN(task_pt_regs), \
50905096
FN(get_branch_snapshot), \
50915097
FN(trace_vprintk), \
5098+
FN(skc_to_unix_sock), \
50925099
/* */
50935100

50945101
/* integer value in 'imm' field of BPF_CALL instruction selects which helper
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
/* Copyright (c) 2021 Hengqi Chen */
3+
4+
#include <test_progs.h>
5+
#include <sys/un.h>
6+
#include "test_skc_to_unix_sock.skel.h"
7+
8+
static const char *sock_path = "@skc_to_unix_sock";
9+
10+
void test_skc_to_unix_sock(void)
11+
{
12+
struct test_skc_to_unix_sock *skel;
13+
struct sockaddr_un sockaddr;
14+
int err, sockfd = 0;
15+
16+
skel = test_skc_to_unix_sock__open();
17+
if (!ASSERT_OK_PTR(skel, "could not open BPF object"))
18+
return;
19+
20+
skel->rodata->my_pid = getpid();
21+
22+
err = test_skc_to_unix_sock__load(skel);
23+
if (!ASSERT_OK(err, "could not load BPF object"))
24+
goto cleanup;
25+
26+
err = test_skc_to_unix_sock__attach(skel);
27+
if (!ASSERT_OK(err, "could not attach BPF object"))
28+
goto cleanup;
29+
30+
/* trigger unix_listen */
31+
sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
32+
if (!ASSERT_GT(sockfd, 0, "socket failed"))
33+
goto cleanup;
34+
35+
memset(&sockaddr, 0, sizeof(sockaddr));
36+
sockaddr.sun_family = AF_UNIX;
37+
strncpy(sockaddr.sun_path, sock_path, strlen(sock_path));
38+
sockaddr.sun_path[0] = '\0';
39+
40+
err = bind(sockfd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
41+
if (!ASSERT_OK(err, "bind failed"))
42+
goto cleanup;
43+
44+
err = listen(sockfd, 1);
45+
if (!ASSERT_OK(err, "listen failed"))
46+
goto cleanup;
47+
48+
ASSERT_EQ(strcmp(skel->bss->path, sock_path), 0, "bpf_skc_to_unix_sock failed");
49+
50+
cleanup:
51+
if (sockfd)
52+
close(sockfd);
53+
test_skc_to_unix_sock__destroy(skel);
54+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
/* Copyright (c) 2021 Hengqi Chen */
3+
4+
#include "vmlinux.h"
5+
#include <bpf/bpf_helpers.h>
6+
#include <bpf/bpf_tracing.h>
7+
#include "bpf_tracing_net.h"
8+
9+
const volatile pid_t my_pid = 0;
10+
char path[256] = {};
11+
12+
SEC("fentry/unix_listen")
13+
int BPF_PROG(unix_listen, struct socket *sock, int backlog)
14+
{
15+
pid_t pid = bpf_get_current_pid_tgid() >> 32;
16+
struct unix_sock *unix_sk;
17+
int i, len;
18+
19+
if (pid != my_pid)
20+
return 0;
21+
22+
unix_sk = (struct unix_sock *)bpf_skc_to_unix_sock(sock->sk);
23+
if (!unix_sk)
24+
return 0;
25+
26+
if (!UNIX_ABSTRACT(unix_sk))
27+
return 0;
28+
29+
len = unix_sk->addr->len - sizeof(short);
30+
path[0] = '@';
31+
for (i = 1; i < len; i++) {
32+
if (i >= sizeof(struct sockaddr_un))
33+
break;
34+
35+
path[i] = unix_sk->addr->name->sun_path[i];
36+
}
37+
return 0;
38+
}
39+
40+
char _license[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)