Skip to content

Commit 53e380d

Browse files
daandemeyerMartin KaFai Lau
authored andcommitted
bpf: Add bpf_sock_addr_set_sun_path() to allow writing unix sockaddr from bpf
As prep for adding unix socket support to the cgroup sockaddr hooks, let's add a kfunc bpf_sock_addr_set_sun_path() that allows modifying a unix sockaddr from bpf. While this is already possible for AF_INET and AF_INET6, we'll need this kfunc when we add unix socket support since modifying the address for those requires modifying both the address and the sockaddr length. Signed-off-by: Daan De Meyer <daan.j.demeyer@gmail.com> Link: https://lore.kernel.org/r/20231011185113.140426-4-daan.j.demeyer@gmail.com Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>
1 parent fefba7d commit 53e380d

2 files changed

Lines changed: 35 additions & 1 deletion

File tree

kernel/bpf/btf.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7850,6 +7850,7 @@ static int bpf_prog_type_to_kfunc_hook(enum bpf_prog_type prog_type)
78507850
case BPF_PROG_TYPE_SYSCALL:
78517851
return BTF_KFUNC_HOOK_SYSCALL;
78527852
case BPF_PROG_TYPE_CGROUP_SKB:
7853+
case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
78537854
return BTF_KFUNC_HOOK_CGROUP_SKB;
78547855
case BPF_PROG_TYPE_SCHED_ACT:
78557856
return BTF_KFUNC_HOOK_SCHED_ACT;

net/core/filter.c

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
#include <net/xdp.h>
8282
#include <net/mptcp.h>
8383
#include <net/netfilter/nf_conntrack_bpf.h>
84+
#include <linux/un.h>
8485

8586
static const struct bpf_func_proto *
8687
bpf_sk_base_func_proto(enum bpf_func_id func_id);
@@ -11768,6 +11769,27 @@ __bpf_kfunc int bpf_dynptr_from_xdp(struct xdp_buff *xdp, u64 flags,
1176811769

1176911770
return 0;
1177011771
}
11772+
11773+
__bpf_kfunc int bpf_sock_addr_set_sun_path(struct bpf_sock_addr_kern *sa_kern,
11774+
const u8 *sun_path, u32 sun_path__sz)
11775+
{
11776+
struct sockaddr_un *un;
11777+
11778+
if (sa_kern->sk->sk_family != AF_UNIX)
11779+
return -EINVAL;
11780+
11781+
/* We do not allow changing the address to unnamed or larger than the
11782+
* maximum allowed address size for a unix sockaddr.
11783+
*/
11784+
if (sun_path__sz == 0 || sun_path__sz > UNIX_PATH_MAX)
11785+
return -EINVAL;
11786+
11787+
un = (struct sockaddr_un *)sa_kern->uaddr;
11788+
memcpy(un->sun_path, sun_path, sun_path__sz);
11789+
sa_kern->uaddrlen = offsetof(struct sockaddr_un, sun_path) + sun_path__sz;
11790+
11791+
return 0;
11792+
}
1177111793
__diag_pop();
1177211794

1177311795
int bpf_dynptr_from_skb_rdonly(struct sk_buff *skb, u64 flags,
@@ -11792,6 +11814,10 @@ BTF_SET8_START(bpf_kfunc_check_set_xdp)
1179211814
BTF_ID_FLAGS(func, bpf_dynptr_from_xdp)
1179311815
BTF_SET8_END(bpf_kfunc_check_set_xdp)
1179411816

11817+
BTF_SET8_START(bpf_kfunc_check_set_sock_addr)
11818+
BTF_ID_FLAGS(func, bpf_sock_addr_set_sun_path)
11819+
BTF_SET8_END(bpf_kfunc_check_set_sock_addr)
11820+
1179511821
static const struct btf_kfunc_id_set bpf_kfunc_set_skb = {
1179611822
.owner = THIS_MODULE,
1179711823
.set = &bpf_kfunc_check_set_skb,
@@ -11802,6 +11828,11 @@ static const struct btf_kfunc_id_set bpf_kfunc_set_xdp = {
1180211828
.set = &bpf_kfunc_check_set_xdp,
1180311829
};
1180411830

11831+
static const struct btf_kfunc_id_set bpf_kfunc_set_sock_addr = {
11832+
.owner = THIS_MODULE,
11833+
.set = &bpf_kfunc_check_set_sock_addr,
11834+
};
11835+
1180511836
static int __init bpf_kfunc_init(void)
1180611837
{
1180711838
int ret;
@@ -11816,7 +11847,9 @@ static int __init bpf_kfunc_init(void)
1181611847
ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_LWT_XMIT, &bpf_kfunc_set_skb);
1181711848
ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_LWT_SEG6LOCAL, &bpf_kfunc_set_skb);
1181811849
ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_NETFILTER, &bpf_kfunc_set_skb);
11819-
return ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_XDP, &bpf_kfunc_set_xdp);
11850+
ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_XDP, &bpf_kfunc_set_xdp);
11851+
return ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_CGROUP_SOCK_ADDR,
11852+
&bpf_kfunc_set_sock_addr);
1182011853
}
1182111854
late_initcall(bpf_kfunc_init);
1182211855

0 commit comments

Comments
 (0)