Skip to content

Commit 983695f

Browse files
borkmannAlexei Starovoitov
authored andcommitted
bpf: fix unconnected udp hooks
Intention of cgroup bind/connect/sendmsg BPF hooks is to act transparently to applications as also stated in original motivation in 7828f20 ("Merge branch 'bpf-cgroup-bind-connect'"). When recently integrating the latter two hooks into Cilium to enable host based load-balancing with Kubernetes, I ran into the issue that pods couldn't start up as DNS got broken. Kubernetes typically sets up DNS as a service and is thus subject to load-balancing. Upon further debugging, it turns out that the cgroupv2 sendmsg BPF hooks API is currently insufficient and thus not usable as-is for standard applications shipped with most distros. To break down the issue we ran into with a simple example: # cat /etc/resolv.conf nameserver 147.75.207.207 nameserver 147.75.207.208 For the purpose of a simple test, we set up above IPs as service IPs and transparently redirect traffic to a different DNS backend server for that node: # cilium service list ID Frontend Backend 1 147.75.207.207:53 1 => 8.8.8.8:53 2 147.75.207.208:53 1 => 8.8.8.8:53 The attached BPF program is basically selecting one of the backends if the service IP/port matches on the cgroup hook. DNS breaks here, because the hooks are not transparent enough to applications which have built-in msg_name address checks: # nslookup 1.1.1.1 ;; reply from unexpected source: 8.8.8.8#53, expected 147.75.207.207#53 ;; reply from unexpected source: 8.8.8.8#53, expected 147.75.207.208#53 ;; reply from unexpected source: 8.8.8.8#53, expected 147.75.207.207#53 [...] ;; connection timed out; no servers could be reached # dig 1.1.1.1 ;; reply from unexpected source: 8.8.8.8#53, expected 147.75.207.207#53 ;; reply from unexpected source: 8.8.8.8#53, expected 147.75.207.208#53 ;; reply from unexpected source: 8.8.8.8#53, expected 147.75.207.207#53 [...] ; <<>> DiG 9.11.3-1ubuntu1.7-Ubuntu <<>> 1.1.1.1 ;; global options: +cmd ;; connection timed out; no servers could be reached For comparison, if none of the service IPs is used, and we tell nslookup to use 8.8.8.8 directly it works just fine, of course: # nslookup 1.1.1.1 8.8.8.8 1.1.1.1.in-addr.arpa name = one.one.one.one. In order to fix this and thus act more transparent to the application, this needs reverse translation on recvmsg() side. A minimal fix for this API is to add similar recvmsg() hooks behind the BPF cgroups static key such that the program can track state and replace the current sockaddr_in{,6} with the original service IP. From BPF side, this basically tracks the service tuple plus socket cookie in an LRU map where the reverse NAT can then be retrieved via map value as one example. Side-note: the BPF cgroups static key should be converted to a per-hook static key in future. Same example after this fix: # cilium service list ID Frontend Backend 1 147.75.207.207:53 1 => 8.8.8.8:53 2 147.75.207.208:53 1 => 8.8.8.8:53 Lookups work fine now: # nslookup 1.1.1.1 1.1.1.1.in-addr.arpa name = one.one.one.one. Authoritative answers can be found from: # dig 1.1.1.1 ; <<>> DiG 9.11.3-1ubuntu1.7-Ubuntu <<>> 1.1.1.1 ;; global options: +cmd ;; Got answer: ;; ->>HEADER<<- opcode: QUERY, status: NXDOMAIN, id: 51550 ;; flags: qr rd ra ad; QUERY: 1, ANSWER: 0, AUTHORITY: 1, ADDITIONAL: 1 ;; OPT PSEUDOSECTION: ; EDNS: version: 0, flags:; udp: 512 ;; QUESTION SECTION: ;1.1.1.1. IN A ;; AUTHORITY SECTION: . 23426 IN SOA a.root-servers.net. nstld.verisign-grs.com. 2019052001 1800 900 604800 86400 ;; Query time: 17 msec ;; SERVER: 147.75.207.207#53(147.75.207.207) ;; WHEN: Tue May 21 12:59:38 UTC 2019 ;; MSG SIZE rcvd: 111 And from an actual packet level it shows that we're using the back end server when talking via 147.75.207.20{7,8} front end: # tcpdump -i any udp [...] 12:59:52.698732 IP foo.42011 > google-public-dns-a.google.com.domain: 18803+ PTR? 1.1.1.1.in-addr.arpa. (38) 12:59:52.698735 IP foo.42011 > google-public-dns-a.google.com.domain: 18803+ PTR? 1.1.1.1.in-addr.arpa. (38) 12:59:52.701208 IP google-public-dns-a.google.com.domain > foo.42011: 18803 1/0/0 PTR one.one.one.one. (67) 12:59:52.701208 IP google-public-dns-a.google.com.domain > foo.42011: 18803 1/0/0 PTR one.one.one.one. (67) [...] In order to be flexible and to have same semantics as in sendmsg BPF programs, we only allow return codes in [1,1] range. In the sendmsg case the program is called if msg->msg_name is present which can be the case in both, connected and unconnected UDP. The former only relies on the sockaddr_in{,6} passed via connect(2) if passed msg->msg_name was NULL. Therefore, on recvmsg side, we act in similar way to call into the BPF program whenever a non-NULL msg->msg_name was passed independent of sk->sk_state being TCP_ESTABLISHED or not. Note that for TCP case, the msg->msg_name is ignored in the regular recvmsg path and therefore not relevant. For the case of ip{,v6}_recv_error() paths, picked up via MSG_ERRQUEUE, the hook is not called. This is intentional as it aligns with the same semantics as in case of TCP cgroup BPF hooks right now. This might be better addressed in future through a different bpf_attach_type such that this case can be distinguished from the regular recvmsg paths, for example. Fixes: 1cedee1 ("bpf: Hooks for sys_sendmsg") Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Acked-by: Andrey Ignatov <rdna@fb.com> Acked-by: Martin KaFai Lau <kafai@fb.com> Acked-by: Martynas Pumputis <m@lambda.lt> Signed-off-by: Alexei Starovoitov <ast@kernel.org>
1 parent 1884c06 commit 983695f

7 files changed

Lines changed: 36 additions & 4 deletions

File tree

‎include/linux/bpf-cgroup.h‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,12 @@ int bpf_percpu_cgroup_storage_update(struct bpf_map *map, void *key,
238238
#define BPF_CGROUP_RUN_PROG_UDP6_SENDMSG_LOCK(sk, uaddr, t_ctx) \
239239
BPF_CGROUP_RUN_SA_PROG_LOCK(sk, uaddr, BPF_CGROUP_UDP6_SENDMSG, t_ctx)
240240

241+
#define BPF_CGROUP_RUN_PROG_UDP4_RECVMSG_LOCK(sk, uaddr) \
242+
BPF_CGROUP_RUN_SA_PROG_LOCK(sk, uaddr, BPF_CGROUP_UDP4_RECVMSG, NULL)
243+
244+
#define BPF_CGROUP_RUN_PROG_UDP6_RECVMSG_LOCK(sk, uaddr) \
245+
BPF_CGROUP_RUN_SA_PROG_LOCK(sk, uaddr, BPF_CGROUP_UDP6_RECVMSG, NULL)
246+
241247
#define BPF_CGROUP_RUN_PROG_SOCK_OPS(sock_ops) \
242248
({ \
243249
int __ret = 0; \
@@ -339,6 +345,8 @@ static inline int bpf_percpu_cgroup_storage_update(struct bpf_map *map,
339345
#define BPF_CGROUP_RUN_PROG_INET6_CONNECT_LOCK(sk, uaddr) ({ 0; })
340346
#define BPF_CGROUP_RUN_PROG_UDP4_SENDMSG_LOCK(sk, uaddr, t_ctx) ({ 0; })
341347
#define BPF_CGROUP_RUN_PROG_UDP6_SENDMSG_LOCK(sk, uaddr, t_ctx) ({ 0; })
348+
#define BPF_CGROUP_RUN_PROG_UDP4_RECVMSG_LOCK(sk, uaddr) ({ 0; })
349+
#define BPF_CGROUP_RUN_PROG_UDP6_RECVMSG_LOCK(sk, uaddr) ({ 0; })
342350
#define BPF_CGROUP_RUN_PROG_SOCK_OPS(sock_ops) ({ 0; })
343351
#define BPF_CGROUP_RUN_PROG_DEVICE_CGROUP(type,major,minor,access) ({ 0; })
344352
#define BPF_CGROUP_RUN_PROG_SYSCTL(head,table,write,buf,count,pos,nbuf) ({ 0; })

‎include/uapi/linux/bpf.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,8 @@ enum bpf_attach_type {
192192
BPF_LIRC_MODE2,
193193
BPF_FLOW_DISSECTOR,
194194
BPF_CGROUP_SYSCTL,
195+
BPF_CGROUP_UDP4_RECVMSG,
196+
BPF_CGROUP_UDP6_RECVMSG,
195197
__MAX_BPF_ATTACH_TYPE
196198
};
197199

‎kernel/bpf/syscall.c‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1581,6 +1581,8 @@ bpf_prog_load_check_attach_type(enum bpf_prog_type prog_type,
15811581
case BPF_CGROUP_INET6_CONNECT:
15821582
case BPF_CGROUP_UDP4_SENDMSG:
15831583
case BPF_CGROUP_UDP6_SENDMSG:
1584+
case BPF_CGROUP_UDP4_RECVMSG:
1585+
case BPF_CGROUP_UDP6_RECVMSG:
15841586
return 0;
15851587
default:
15861588
return -EINVAL;
@@ -1875,6 +1877,8 @@ static int bpf_prog_attach(const union bpf_attr *attr)
18751877
case BPF_CGROUP_INET6_CONNECT:
18761878
case BPF_CGROUP_UDP4_SENDMSG:
18771879
case BPF_CGROUP_UDP6_SENDMSG:
1880+
case BPF_CGROUP_UDP4_RECVMSG:
1881+
case BPF_CGROUP_UDP6_RECVMSG:
18781882
ptype = BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
18791883
break;
18801884
case BPF_CGROUP_SOCK_OPS:
@@ -1960,6 +1964,8 @@ static int bpf_prog_detach(const union bpf_attr *attr)
19601964
case BPF_CGROUP_INET6_CONNECT:
19611965
case BPF_CGROUP_UDP4_SENDMSG:
19621966
case BPF_CGROUP_UDP6_SENDMSG:
1967+
case BPF_CGROUP_UDP4_RECVMSG:
1968+
case BPF_CGROUP_UDP6_RECVMSG:
19631969
ptype = BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
19641970
break;
19651971
case BPF_CGROUP_SOCK_OPS:
@@ -2011,6 +2017,8 @@ static int bpf_prog_query(const union bpf_attr *attr,
20112017
case BPF_CGROUP_INET6_CONNECT:
20122018
case BPF_CGROUP_UDP4_SENDMSG:
20132019
case BPF_CGROUP_UDP6_SENDMSG:
2020+
case BPF_CGROUP_UDP4_RECVMSG:
2021+
case BPF_CGROUP_UDP6_RECVMSG:
20142022
case BPF_CGROUP_SOCK_OPS:
20152023
case BPF_CGROUP_DEVICE:
20162024
case BPF_CGROUP_SYSCTL:

‎kernel/bpf/verifier.c‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5361,9 +5361,12 @@ static int check_return_code(struct bpf_verifier_env *env)
53615361
struct tnum range = tnum_range(0, 1);
53625362

53635363
switch (env->prog->type) {
5364+
case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
5365+
if (env->prog->expected_attach_type == BPF_CGROUP_UDP4_RECVMSG ||
5366+
env->prog->expected_attach_type == BPF_CGROUP_UDP6_RECVMSG)
5367+
range = tnum_range(1, 1);
53645368
case BPF_PROG_TYPE_CGROUP_SKB:
53655369
case BPF_PROG_TYPE_CGROUP_SOCK:
5366-
case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
53675370
case BPF_PROG_TYPE_SOCK_OPS:
53685371
case BPF_PROG_TYPE_CGROUP_DEVICE:
53695372
case BPF_PROG_TYPE_CGROUP_SYSCTL:
@@ -5380,16 +5383,17 @@ static int check_return_code(struct bpf_verifier_env *env)
53805383
}
53815384

53825385
if (!tnum_in(range, reg->var_off)) {
5386+
char tn_buf[48];
5387+
53835388
verbose(env, "At program exit the register R0 ");
53845389
if (!tnum_is_unknown(reg->var_off)) {
5385-
char tn_buf[48];
5386-
53875390
tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
53885391
verbose(env, "has value %s", tn_buf);
53895392
} else {
53905393
verbose(env, "has unknown scalar value");
53915394
}
5392-
verbose(env, " should have been 0 or 1\n");
5395+
tnum_strn(tn_buf, sizeof(tn_buf), range);
5396+
verbose(env, " should have been in %s\n", tn_buf);
53935397
return -EINVAL;
53945398
}
53955399
return 0;

‎net/core/filter.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6748,6 +6748,7 @@ static bool sock_addr_is_valid_access(int off, int size,
67486748
case BPF_CGROUP_INET4_BIND:
67496749
case BPF_CGROUP_INET4_CONNECT:
67506750
case BPF_CGROUP_UDP4_SENDMSG:
6751+
case BPF_CGROUP_UDP4_RECVMSG:
67516752
break;
67526753
default:
67536754
return false;
@@ -6758,6 +6759,7 @@ static bool sock_addr_is_valid_access(int off, int size,
67586759
case BPF_CGROUP_INET6_BIND:
67596760
case BPF_CGROUP_INET6_CONNECT:
67606761
case BPF_CGROUP_UDP6_SENDMSG:
6762+
case BPF_CGROUP_UDP6_RECVMSG:
67616763
break;
67626764
default:
67636765
return false;

‎net/ipv4/udp.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1783,6 +1783,10 @@ int udp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int noblock,
17831783
sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
17841784
memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
17851785
*addr_len = sizeof(*sin);
1786+
1787+
if (cgroup_bpf_enabled)
1788+
BPF_CGROUP_RUN_PROG_UDP4_RECVMSG_LOCK(sk,
1789+
(struct sockaddr *)sin);
17861790
}
17871791

17881792
if (udp_sk(sk)->gro_enabled)

‎net/ipv6/udp.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,10 @@ int udpv6_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
369369
inet6_iif(skb));
370370
}
371371
*addr_len = sizeof(*sin6);
372+
373+
if (cgroup_bpf_enabled)
374+
BPF_CGROUP_RUN_PROG_UDP6_RECVMSG_LOCK(sk,
375+
(struct sockaddr *)sin6);
372376
}
373377

374378
if (udp_sk(sk)->gro_enabled)

0 commit comments

Comments
 (0)