Skip to content

Commit 971e305

Browse files
committed
Merge git://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf
Daniel Borkmann says: ==================== pull-request: bpf 2021-04-08 The following pull-request contains BPF updates for your *net* tree. We've added 4 non-merge commits during the last 2 day(s) which contain a total of 4 files changed, 31 insertions(+), 10 deletions(-). The main changes are: 1) Validate and reject invalid JIT branch displacements, from Piotr Krysiuk. 2) Fix incorrect unhash restore as well as fwd_alloc memory accounting in sock map, from John Fastabend. ==================== Signed-off-by: David S. Miller <[email protected]>
2 parents ac075bd + 26f55a5 commit 971e305

File tree

4 files changed

+31
-10
lines changed

4 files changed

+31
-10
lines changed

arch/x86/net/bpf_jit_comp.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1689,7 +1689,16 @@ st: if (is_imm8(insn->off))
16891689
}
16901690

16911691
if (image) {
1692-
if (unlikely(proglen + ilen > oldproglen)) {
1692+
/*
1693+
* When populating the image, assert that:
1694+
*
1695+
* i) We do not write beyond the allocated space, and
1696+
* ii) addrs[i] did not change from the prior run, in order
1697+
* to validate assumptions made for computing branch
1698+
* displacements.
1699+
*/
1700+
if (unlikely(proglen + ilen > oldproglen ||
1701+
proglen + ilen != addrs[i])) {
16931702
pr_err("bpf_jit: fatal error\n");
16941703
return -EFAULT;
16951704
}

arch/x86/net/bpf_jit_comp32.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2276,7 +2276,16 @@ emit_cond_jmp: jmp_cond = get_cond_jmp_opcode(BPF_OP(code), false);
22762276
}
22772277

22782278
if (image) {
2279-
if (unlikely(proglen + ilen > oldproglen)) {
2279+
/*
2280+
* When populating the image, assert that:
2281+
*
2282+
* i) We do not write beyond the allocated space, and
2283+
* ii) addrs[i] did not change from the prior run, in order
2284+
* to validate assumptions made for computing branch
2285+
* displacements.
2286+
*/
2287+
if (unlikely(proglen + ilen > oldproglen ||
2288+
proglen + ilen != addrs[i])) {
22802289
pr_err("bpf_jit: fatal error\n");
22812290
return -EFAULT;
22822291
}

include/linux/skmsg.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,8 +349,13 @@ static inline void sk_psock_update_proto(struct sock *sk,
349349
static inline void sk_psock_restore_proto(struct sock *sk,
350350
struct sk_psock *psock)
351351
{
352-
sk->sk_prot->unhash = psock->saved_unhash;
353352
if (inet_csk_has_ulp(sk)) {
353+
/* TLS does not have an unhash proto in SW cases, but we need
354+
* to ensure we stop using the sock_map unhash routine because
355+
* the associated psock is being removed. So use the original
356+
* unhash handler.
357+
*/
358+
WRITE_ONCE(sk->sk_prot->unhash, psock->saved_unhash);
354359
tcp_update_ulp(sk, psock->sk_proto, psock->saved_write_space);
355360
} else {
356361
sk->sk_write_space = psock->saved_write_space;

net/core/skmsg.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,7 @@ static int sk_psock_skb_ingress_self(struct sk_psock *psock, struct sk_buff *skb
488488
if (unlikely(!msg))
489489
return -EAGAIN;
490490
sk_msg_init(msg);
491+
skb_set_owner_r(skb, sk);
491492
return sk_psock_skb_ingress_enqueue(skb, psock, sk, msg);
492493
}
493494

@@ -790,7 +791,6 @@ static void sk_psock_tls_verdict_apply(struct sk_buff *skb, struct sock *sk, int
790791
{
791792
switch (verdict) {
792793
case __SK_REDIRECT:
793-
skb_set_owner_r(skb, sk);
794794
sk_psock_skb_redirect(skb);
795795
break;
796796
case __SK_PASS:
@@ -808,10 +808,6 @@ int sk_psock_tls_strp_read(struct sk_psock *psock, struct sk_buff *skb)
808808
rcu_read_lock();
809809
prog = READ_ONCE(psock->progs.skb_verdict);
810810
if (likely(prog)) {
811-
/* We skip full set_owner_r here because if we do a SK_PASS
812-
* or SK_DROP we can skip skb memory accounting and use the
813-
* TLS context.
814-
*/
815811
skb->sk = psock->sk;
816812
tcp_skb_bpf_redirect_clear(skb);
817813
ret = sk_psock_bpf_run(psock, prog, skb);
@@ -880,12 +876,13 @@ static void sk_psock_strp_read(struct strparser *strp, struct sk_buff *skb)
880876
kfree_skb(skb);
881877
goto out;
882878
}
883-
skb_set_owner_r(skb, sk);
884879
prog = READ_ONCE(psock->progs.skb_verdict);
885880
if (likely(prog)) {
881+
skb->sk = sk;
886882
tcp_skb_bpf_redirect_clear(skb);
887883
ret = sk_psock_bpf_run(psock, prog, skb);
888884
ret = sk_psock_map_verd(ret, tcp_skb_bpf_redirect_fetch(skb));
885+
skb->sk = NULL;
889886
}
890887
sk_psock_verdict_apply(psock, skb, ret);
891888
out:
@@ -956,12 +953,13 @@ static int sk_psock_verdict_recv(read_descriptor_t *desc, struct sk_buff *skb,
956953
kfree_skb(skb);
957954
goto out;
958955
}
959-
skb_set_owner_r(skb, sk);
960956
prog = READ_ONCE(psock->progs.skb_verdict);
961957
if (likely(prog)) {
958+
skb->sk = sk;
962959
tcp_skb_bpf_redirect_clear(skb);
963960
ret = sk_psock_bpf_run(psock, prog, skb);
964961
ret = sk_psock_map_verd(ret, tcp_skb_bpf_redirect_fetch(skb));
962+
skb->sk = NULL;
965963
}
966964
sk_psock_verdict_apply(psock, skb, ret);
967965
out:

0 commit comments

Comments
 (0)