Commit 073f4ec1 authored by Stanislav Fomichev's avatar Stanislav Fomichev Committed by Daniel Borkmann
Browse files

bpf: Enable bpf_{g,s}etsockopt in BPF_CGROUP_INET{4,6}_GET{PEER,SOCK}NAME



Those hooks run as BPF_CGROUP_RUN_SA_PROG_LOCK and operate on
a locked socket.

Signed-off-by: default avatarStanislav Fomichev <sdf@google.com>
Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
Link: https://lore.kernel.org/bpf/20210127232853.3753823-3-sdf@google.com
parent 62476cc1
Loading
Loading
Loading
Loading
+8 −0
Original line number Diff line number Diff line
@@ -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;
+8 −0
Original line number Diff line number Diff line
@@ -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);
+8 −0
Original line number Diff line number Diff line
@@ -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);