bpf: Enable bpf_{g,s}etsockopt in BPF_CGROUP_INET{4,6}_GET{PEER,SOCK}NAME
authorStanislav Fomichev <sdf@google.com>
Wed, 27 Jan 2021 23:28:51 +0000 (15:28 -0800)
committerDaniel Borkmann <daniel@iogearbox.net>
Fri, 29 Jan 2021 01:09:05 +0000 (02:09 +0100)
Those hooks run as BPF_CGROUP_RUN_SA_PROG_LOCK and operate on
a locked socket.

Signed-off-by: Stanislav Fomichev <sdf@google.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Link: https://lore.kernel.org/bpf/20210127232853.3753823-3-sdf@google.com
net/core/filter.c
tools/testing/selftests/bpf/progs/connect_force_port4.c
tools/testing/selftests/bpf/progs/connect_force_port6.c

index 3d7f78a..ba436b1 100644 (file)
@@ -7025,6 +7025,10 @@ sock_addr_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
                case BPF_CGROUP_INET6_CONNECT:
                case BPF_CGROUP_UDP4_SENDMSG:
                case BPF_CGROUP_UDP6_SENDMSG:
+               case BPF_CGROUP_INET4_GETPEERNAME:
+               case BPF_CGROUP_INET6_GETPEERNAME:
+               case BPF_CGROUP_INET4_GETSOCKNAME:
+               case BPF_CGROUP_INET6_GETSOCKNAME:
                        return &bpf_sock_addr_setsockopt_proto;
                default:
                        return NULL;
@@ -7037,6 +7041,10 @@ sock_addr_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
                case BPF_CGROUP_INET6_CONNECT:
                case BPF_CGROUP_UDP4_SENDMSG:
                case BPF_CGROUP_UDP6_SENDMSG:
+               case BPF_CGROUP_INET4_GETPEERNAME:
+               case BPF_CGROUP_INET6_GETPEERNAME:
+               case BPF_CGROUP_INET4_GETSOCKNAME:
+               case BPF_CGROUP_INET6_GETSOCKNAME:
                        return &bpf_sock_addr_getsockopt_proto;
                default:
                        return NULL;
index 7396308..a979aae 100644 (file)
@@ -10,6 +10,8 @@
 #include <bpf/bpf_helpers.h>
 #include <bpf/bpf_endian.h>
 
+#include <bpf_sockopt_helpers.h>
+
 char _license[] SEC("license") = "GPL";
 int _version SEC("version") = 1;
 
@@ -58,6 +60,9 @@ int connect4(struct bpf_sock_addr *ctx)
 SEC("cgroup/getsockname4")
 int getsockname4(struct bpf_sock_addr *ctx)
 {
+       if (!get_set_sk_priority(ctx))
+               return 1;
+
        /* Expose local server as 1.2.3.4:60000 to client. */
        if (ctx->user_port == bpf_htons(60123)) {
                ctx->user_ip4 = bpf_htonl(0x01020304);
@@ -71,6 +76,9 @@ int getpeername4(struct bpf_sock_addr *ctx)
 {
        struct svc_addr *orig;
 
+       if (!get_set_sk_priority(ctx))
+               return 1;
+
        /* Expose service 1.2.3.4:60000 as peer instead of backend. */
        if (ctx->user_port == bpf_htons(60123)) {
                orig = bpf_sk_storage_get(&service_mapping, ctx->sk, 0, 0);
index c1a2b55..afc8f1c 100644 (file)
@@ -9,6 +9,8 @@
 #include <bpf/bpf_helpers.h>
 #include <bpf/bpf_endian.h>
 
+#include <bpf_sockopt_helpers.h>
+
 char _license[] SEC("license") = "GPL";
 int _version SEC("version") = 1;
 
@@ -63,6 +65,9 @@ int connect6(struct bpf_sock_addr *ctx)
 SEC("cgroup/getsockname6")
 int getsockname6(struct bpf_sock_addr *ctx)
 {
+       if (!get_set_sk_priority(ctx))
+               return 1;
+
        /* Expose local server as [fc00::1]:60000 to client. */
        if (ctx->user_port == bpf_htons(60124)) {
                ctx->user_ip6[0] = bpf_htonl(0xfc000000);
@@ -79,6 +84,9 @@ int getpeername6(struct bpf_sock_addr *ctx)
 {
        struct svc_addr *orig;
 
+       if (!get_set_sk_priority(ctx))
+               return 1;
+
        /* Expose service [fc00::1]:60000 as peer instead of backend. */
        if (ctx->user_port == bpf_htons(60124)) {
                orig = bpf_sk_storage_get(&service_mapping, ctx->sk, 0, 0);