Skip to content

Commit 5fe821a

Browse files
Alexei Starovoitovdavem330
authored andcommitted
net: filter: cleanup invocation of internal BPF
Kernel API for classic BPF socket filters is: sk_unattached_filter_create() - validate classic BPF, convert, JIT SK_RUN_FILTER() - run it sk_unattached_filter_destroy() - destroy socket filter Cleanup internal BPF kernel API as following: sk_filter_select_runtime() - final step of internal BPF creation. Try to JIT internal BPF program, if JIT is not available select interpreter SK_RUN_FILTER() - run it sk_filter_free() - free internal BPF program Disallow direct calls to BPF interpreter. Execution of the BPF program should be done with SK_RUN_FILTER() macro. Example of internal BPF create, run, destroy: struct sk_filter *fp; fp = kzalloc(sk_filter_size(prog_len), GFP_KERNEL); memcpy(fp->insni, prog, prog_len * sizeof(fp->insni[0])); fp->len = prog_len; sk_filter_select_runtime(fp); SK_RUN_FILTER(fp, ctx); sk_filter_free(fp); Sockets, seccomp, testsuite, tracing are using different ways to populate sk_filter, so first steps of program creation are not common. Signed-off-by: Alexei Starovoitov <ast@plumgrid.com> Acked-by: Daniel Borkmann <dborkman@redhat.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 21ea04f commit 5fe821a

4 files changed

Lines changed: 34 additions & 26 deletions

File tree

‎include/linux/filter.h‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,10 +184,8 @@ static inline unsigned int sk_filter_size(unsigned int proglen)
184184

185185
int sk_filter(struct sock *sk, struct sk_buff *skb);
186186

187-
u32 sk_run_filter_int_seccomp(const struct seccomp_data *ctx,
188-
const struct sock_filter_int *insni);
189-
u32 sk_run_filter_int_skb(const struct sk_buff *ctx,
190-
const struct sock_filter_int *insni);
187+
void sk_filter_select_runtime(struct sk_filter *fp);
188+
void sk_filter_free(struct sk_filter *fp);
191189

192190
int sk_convert_filter(struct sock_filter *prog, int len,
193191
struct sock_filter_int *new_prog, int *new_len);

‎kernel/seccomp.c‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -273,10 +273,8 @@ static long seccomp_attach_filter(struct sock_fprog *fprog)
273273

274274
atomic_set(&filter->usage, 1);
275275
filter->prog->len = new_len;
276-
filter->prog->bpf_func = (void *)sk_run_filter_int_seccomp;
277276

278-
/* JIT internal BPF into native HW instructions */
279-
bpf_int_jit_compile(filter->prog);
277+
sk_filter_select_runtime(filter->prog);
280278

281279
/*
282280
* If there is an existing filter, make it the prev and don't drop its
@@ -340,7 +338,7 @@ void put_seccomp_filter(struct task_struct *tsk)
340338
while (orig && atomic_dec_and_test(&orig->usage)) {
341339
struct seccomp_filter *freeme = orig;
342340
orig = orig->prev;
343-
bpf_jit_free(freeme->prog);
341+
sk_filter_free(freeme->prog);
344342
kfree(freeme);
345343
}
346344
}

‎lib/test_bpf.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1489,7 +1489,7 @@ static __init int test_bpf(void)
14891489
memcpy(fp_ext->insns, tests[i].insns_int,
14901490
fprog.len * 8);
14911491
fp->len = fprog.len;
1492-
fp->bpf_func = sk_run_filter_int_skb;
1492+
sk_filter_select_runtime(fp);
14931493
} else {
14941494
err = sk_unattached_filter_create(&fp, &fprog);
14951495
if (tests[i].data_type == EXPECTED_FAIL) {
@@ -1516,7 +1516,7 @@ static __init int test_bpf(void)
15161516
if (tests[i].data_type != SKB_INT)
15171517
sk_unattached_filter_destroy(fp);
15181518
else
1519-
kfree(fp);
1519+
sk_filter_free(fp);
15201520

15211521
if (err) {
15221522
pr_cont("FAIL %d\n", err);

‎net/core/filter.c‎

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ noinline u64 __bpf_call_base(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
153153
* keep, 0 for none. @ctx is the data we are operating on, @insn is the
154154
* array of filter instructions.
155155
*/
156-
unsigned int __sk_run_filter(void *ctx, const struct sock_filter_int *insn)
156+
static unsigned int __sk_run_filter(void *ctx, const struct sock_filter_int *insn)
157157
{
158158
u64 stack[MAX_BPF_STACK / sizeof(u64)];
159159
u64 regs[MAX_BPF_REG], tmp;
@@ -571,15 +571,6 @@ unsigned int __sk_run_filter(void *ctx, const struct sock_filter_int *insn)
571571
return 0;
572572
}
573573

574-
u32 sk_run_filter_int_seccomp(const struct seccomp_data *ctx,
575-
const struct sock_filter_int *insni)
576-
__attribute__ ((alias ("__sk_run_filter")));
577-
578-
u32 sk_run_filter_int_skb(const struct sk_buff *ctx,
579-
const struct sock_filter_int *insni)
580-
__attribute__ ((alias ("__sk_run_filter")));
581-
EXPORT_SYMBOL_GPL(sk_run_filter_int_skb);
582-
583574
/* Helper to find the offset of pkt_type in sk_buff structure. We want
584575
* to make sure its still a 3bit field starting at a byte boundary;
585576
* taken from arch/x86/net/bpf_jit_comp.c.
@@ -1397,7 +1388,7 @@ static void sk_filter_release_rcu(struct rcu_head *rcu)
13971388
struct sk_filter *fp = container_of(rcu, struct sk_filter, rcu);
13981389

13991390
sk_release_orig_filter(fp);
1400-
bpf_jit_free(fp);
1391+
sk_filter_free(fp);
14011392
}
14021393

14031394
/**
@@ -1497,7 +1488,6 @@ static struct sk_filter *__sk_migrate_filter(struct sk_filter *fp,
14971488
goto out_err_free;
14981489
}
14991490

1500-
fp->bpf_func = sk_run_filter_int_skb;
15011491
fp->len = new_len;
15021492

15031493
/* 2nd pass: remap sock_filter insns into sock_filter_int insns. */
@@ -1510,6 +1500,8 @@ static struct sk_filter *__sk_migrate_filter(struct sk_filter *fp,
15101500
*/
15111501
goto out_err_free;
15121502

1503+
sk_filter_select_runtime(fp);
1504+
15131505
kfree(old_prog);
15141506
return fp;
15151507

@@ -1528,6 +1520,29 @@ void __weak bpf_int_jit_compile(struct sk_filter *prog)
15281520
{
15291521
}
15301522

1523+
/**
1524+
* sk_filter_select_runtime - select execution runtime for BPF program
1525+
* @fp: sk_filter populated with internal BPF program
1526+
*
1527+
* try to JIT internal BPF program, if JIT is not available select interpreter
1528+
* BPF program will be executed via SK_RUN_FILTER() macro
1529+
*/
1530+
void sk_filter_select_runtime(struct sk_filter *fp)
1531+
{
1532+
fp->bpf_func = (void *) __sk_run_filter;
1533+
1534+
/* Probe if internal BPF can be JITed */
1535+
bpf_int_jit_compile(fp);
1536+
}
1537+
EXPORT_SYMBOL_GPL(sk_filter_select_runtime);
1538+
1539+
/* free internal BPF program */
1540+
void sk_filter_free(struct sk_filter *fp)
1541+
{
1542+
bpf_jit_free(fp);
1543+
}
1544+
EXPORT_SYMBOL_GPL(sk_filter_free);
1545+
15311546
static struct sk_filter *__sk_prepare_filter(struct sk_filter *fp,
15321547
struct sock *sk)
15331548
{
@@ -1548,12 +1563,9 @@ static struct sk_filter *__sk_prepare_filter(struct sk_filter *fp,
15481563
/* JIT compiler couldn't process this filter, so do the
15491564
* internal BPF translation for the optimized interpreter.
15501565
*/
1551-
if (!fp->jited) {
1566+
if (!fp->jited)
15521567
fp = __sk_migrate_filter(fp, sk);
15531568

1554-
/* Probe if internal BPF can be jit-ed */
1555-
bpf_int_jit_compile(fp);
1556-
}
15571569
return fp;
15581570
}
15591571

0 commit comments

Comments
 (0)