Skip to content

Commit cd3092c

Browse files
Zashasborkmann
authored andcommitted
bpf: Split lwt inout verifier structures
The new bpf_lwt_push_encap helper should only be accessible within the LWT BPF IN hook, and not the OUT one, as this may lead to a skb under panic. At the moment, both LWT BPF IN and OUT share the same list of helpers, whose calls are authorized by the verifier. This patch separates the verifier ops for the IN and OUT hooks, and allows the IN hook to call the bpf_lwt_push_encap helper. This patch is also the occasion to put all lwt_*_func_proto functions together for clarity. At the moment, socks_op_func_proto is in the middle of lwt_inout_func_proto and lwt_xmit_func_proto. Signed-off-by: Mathieu Xhonneux <m.xhonneux@gmail.com> Acked-by: David Lebrun <dlebrun@google.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
1 parent fe94cc2 commit cd3092c

File tree

2 files changed

+54
-33
lines changed

2 files changed

+54
-33
lines changed

include/linux/bpf_types.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ BPF_PROG_TYPE(BPF_PROG_TYPE_XDP, xdp)
99
BPF_PROG_TYPE(BPF_PROG_TYPE_CGROUP_SKB, cg_skb)
1010
BPF_PROG_TYPE(BPF_PROG_TYPE_CGROUP_SOCK, cg_sock)
1111
BPF_PROG_TYPE(BPF_PROG_TYPE_CGROUP_SOCK_ADDR, cg_sock_addr)
12-
BPF_PROG_TYPE(BPF_PROG_TYPE_LWT_IN, lwt_inout)
13-
BPF_PROG_TYPE(BPF_PROG_TYPE_LWT_OUT, lwt_inout)
12+
BPF_PROG_TYPE(BPF_PROG_TYPE_LWT_IN, lwt_in)
13+
BPF_PROG_TYPE(BPF_PROG_TYPE_LWT_OUT, lwt_out)
1414
BPF_PROG_TYPE(BPF_PROG_TYPE_LWT_XMIT, lwt_xmit)
1515
BPF_PROG_TYPE(BPF_PROG_TYPE_SOCK_OPS, sock_ops)
1616
BPF_PROG_TYPE(BPF_PROG_TYPE_SK_SKB, sk_skb)

net/core/filter.c

Lines changed: 52 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4783,33 +4783,6 @@ xdp_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
47834783
}
47844784
}
47854785

4786-
static const struct bpf_func_proto *
4787-
lwt_inout_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
4788-
{
4789-
switch (func_id) {
4790-
case BPF_FUNC_skb_load_bytes:
4791-
return &bpf_skb_load_bytes_proto;
4792-
case BPF_FUNC_skb_pull_data:
4793-
return &bpf_skb_pull_data_proto;
4794-
case BPF_FUNC_csum_diff:
4795-
return &bpf_csum_diff_proto;
4796-
case BPF_FUNC_get_cgroup_classid:
4797-
return &bpf_get_cgroup_classid_proto;
4798-
case BPF_FUNC_get_route_realm:
4799-
return &bpf_get_route_realm_proto;
4800-
case BPF_FUNC_get_hash_recalc:
4801-
return &bpf_get_hash_recalc_proto;
4802-
case BPF_FUNC_perf_event_output:
4803-
return &bpf_skb_event_output_proto;
4804-
case BPF_FUNC_get_smp_processor_id:
4805-
return &bpf_get_smp_processor_id_proto;
4806-
case BPF_FUNC_skb_under_cgroup:
4807-
return &bpf_skb_under_cgroup_proto;
4808-
default:
4809-
return bpf_base_func_proto(func_id);
4810-
}
4811-
}
4812-
48134786
static const struct bpf_func_proto *
48144787
sock_ops_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
48154788
{
@@ -4875,6 +4848,44 @@ sk_skb_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
48754848
}
48764849
}
48774850

4851+
static const struct bpf_func_proto *
4852+
lwt_out_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
4853+
{
4854+
switch (func_id) {
4855+
case BPF_FUNC_skb_load_bytes:
4856+
return &bpf_skb_load_bytes_proto;
4857+
case BPF_FUNC_skb_pull_data:
4858+
return &bpf_skb_pull_data_proto;
4859+
case BPF_FUNC_csum_diff:
4860+
return &bpf_csum_diff_proto;
4861+
case BPF_FUNC_get_cgroup_classid:
4862+
return &bpf_get_cgroup_classid_proto;
4863+
case BPF_FUNC_get_route_realm:
4864+
return &bpf_get_route_realm_proto;
4865+
case BPF_FUNC_get_hash_recalc:
4866+
return &bpf_get_hash_recalc_proto;
4867+
case BPF_FUNC_perf_event_output:
4868+
return &bpf_skb_event_output_proto;
4869+
case BPF_FUNC_get_smp_processor_id:
4870+
return &bpf_get_smp_processor_id_proto;
4871+
case BPF_FUNC_skb_under_cgroup:
4872+
return &bpf_skb_under_cgroup_proto;
4873+
default:
4874+
return bpf_base_func_proto(func_id);
4875+
}
4876+
}
4877+
4878+
static const struct bpf_func_proto *
4879+
lwt_in_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
4880+
{
4881+
switch (func_id) {
4882+
case BPF_FUNC_lwt_push_encap:
4883+
return &bpf_lwt_push_encap_proto;
4884+
default:
4885+
return lwt_out_func_proto(func_id, prog);
4886+
}
4887+
}
4888+
48784889
static const struct bpf_func_proto *
48794890
lwt_xmit_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
48804891
{
@@ -4906,7 +4917,7 @@ lwt_xmit_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
49064917
case BPF_FUNC_set_hash_invalid:
49074918
return &bpf_set_hash_invalid_proto;
49084919
default:
4909-
return lwt_inout_func_proto(func_id, prog);
4920+
return lwt_out_func_proto(func_id, prog);
49104921
}
49114922
}
49124923

@@ -6587,13 +6598,23 @@ const struct bpf_prog_ops cg_skb_prog_ops = {
65876598
.test_run = bpf_prog_test_run_skb,
65886599
};
65896600

6590-
const struct bpf_verifier_ops lwt_inout_verifier_ops = {
6591-
.get_func_proto = lwt_inout_func_proto,
6601+
const struct bpf_verifier_ops lwt_in_verifier_ops = {
6602+
.get_func_proto = lwt_in_func_proto,
6603+
.is_valid_access = lwt_is_valid_access,
6604+
.convert_ctx_access = bpf_convert_ctx_access,
6605+
};
6606+
6607+
const struct bpf_prog_ops lwt_in_prog_ops = {
6608+
.test_run = bpf_prog_test_run_skb,
6609+
};
6610+
6611+
const struct bpf_verifier_ops lwt_out_verifier_ops = {
6612+
.get_func_proto = lwt_out_func_proto,
65926613
.is_valid_access = lwt_is_valid_access,
65936614
.convert_ctx_access = bpf_convert_ctx_access,
65946615
};
65956616

6596-
const struct bpf_prog_ops lwt_inout_prog_ops = {
6617+
const struct bpf_prog_ops lwt_out_prog_ops = {
65976618
.test_run = bpf_prog_test_run_skb,
65986619
};
65996620

0 commit comments

Comments
 (0)