Skip to content

Commit 6cab5e9

Browse files
Alexei Starovoitovborkmann
Alexei Starovoitov
authored andcommitted
bpf: run bpf programs with preemption disabled
Disabled preemption is necessary for proper access to per-cpu maps from BPF programs. But the sender side of socket filters didn't have preemption disabled: unix_dgram_sendmsg->sk_filter->sk_filter_trim_cap->bpf_prog_run_save_cb->BPF_PROG_RUN and a combination of af_packet with tun device didn't disable either: tpacket_snd->packet_direct_xmit->packet_pick_tx_queue->ndo_select_queue-> tun_select_queue->tun_ebpf_select_queue->bpf_prog_run_clear_cb->BPF_PROG_RUN Disable preemption before executing BPF programs (both classic and extended). Reported-by: Jann Horn <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Acked-by: Song Liu <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]>
1 parent 1bb54c4 commit 6cab5e9

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

include/linux/filter.h

+18-3
Original file line numberDiff line numberDiff line change
@@ -591,8 +591,8 @@ static inline u8 *bpf_skb_cb(struct sk_buff *skb)
591591
return qdisc_skb_cb(skb)->data;
592592
}
593593

594-
static inline u32 bpf_prog_run_save_cb(const struct bpf_prog *prog,
595-
struct sk_buff *skb)
594+
static inline u32 __bpf_prog_run_save_cb(const struct bpf_prog *prog,
595+
struct sk_buff *skb)
596596
{
597597
u8 *cb_data = bpf_skb_cb(skb);
598598
u8 cb_saved[BPF_SKB_CB_LEN];
@@ -611,15 +611,30 @@ static inline u32 bpf_prog_run_save_cb(const struct bpf_prog *prog,
611611
return res;
612612
}
613613

614+
static inline u32 bpf_prog_run_save_cb(const struct bpf_prog *prog,
615+
struct sk_buff *skb)
616+
{
617+
u32 res;
618+
619+
preempt_disable();
620+
res = __bpf_prog_run_save_cb(prog, skb);
621+
preempt_enable();
622+
return res;
623+
}
624+
614625
static inline u32 bpf_prog_run_clear_cb(const struct bpf_prog *prog,
615626
struct sk_buff *skb)
616627
{
617628
u8 *cb_data = bpf_skb_cb(skb);
629+
u32 res;
618630

619631
if (unlikely(prog->cb_access))
620632
memset(cb_data, 0, BPF_SKB_CB_LEN);
621633

622-
return BPF_PROG_RUN(prog, skb);
634+
preempt_disable();
635+
res = BPF_PROG_RUN(prog, skb);
636+
preempt_enable();
637+
return res;
623638
}
624639

625640
static __always_inline u32 bpf_prog_run_xdp(const struct bpf_prog *prog,

kernel/bpf/cgroup.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,7 @@ int __cgroup_bpf_run_filter_skb(struct sock *sk,
572572
bpf_compute_and_save_data_end(skb, &saved_data_end);
573573

574574
ret = BPF_PROG_RUN_ARRAY(cgrp->bpf.effective[type], skb,
575-
bpf_prog_run_save_cb);
575+
__bpf_prog_run_save_cb);
576576
bpf_restore_data_end(skb, saved_data_end);
577577
__skb_pull(skb, offset);
578578
skb->sk = save_sk;

0 commit comments

Comments
 (0)