Skip to content

Commit b005fd1

Browse files
jrfastabdavem330
authored andcommitted
bpf: introduce new program type for skbs on sockets
A class of programs, run from strparser and soon from a new map type called sock map, are used with skb as the context but on established sockets. By creating a specific program type for these we can use bpf helpers that expect full sockets and get the verifier to ensure these helpers are not used out of context. The new type is BPF_PROG_TYPE_SK_SKB. This patch introduces the infrastructure and type. Signed-off-by: John Fastabend <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent db5980d commit b005fd1

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

include/linux/bpf_types.h

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ BPF_PROG_TYPE(BPF_PROG_TYPE_LWT_IN, lwt_inout_prog_ops)
1111
BPF_PROG_TYPE(BPF_PROG_TYPE_LWT_OUT, lwt_inout_prog_ops)
1212
BPF_PROG_TYPE(BPF_PROG_TYPE_LWT_XMIT, lwt_xmit_prog_ops)
1313
BPF_PROG_TYPE(BPF_PROG_TYPE_SOCK_OPS, sock_ops_prog_ops)
14+
BPF_PROG_TYPE(BPF_PROG_TYPE_SK_SKB, sk_skb_prog_ops)
1415
#endif
1516
#ifdef CONFIG_BPF_EVENTS
1617
BPF_PROG_TYPE(BPF_PROG_TYPE_KPROBE, kprobe_prog_ops)

include/uapi/linux/bpf.h

+1
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ enum bpf_prog_type {
127127
BPF_PROG_TYPE_LWT_OUT,
128128
BPF_PROG_TYPE_LWT_XMIT,
129129
BPF_PROG_TYPE_SOCK_OPS,
130+
BPF_PROG_TYPE_SK_SKB,
130131
};
131132

132133
enum bpf_attach_type {

net/core/filter.c

+36
Original file line numberDiff line numberDiff line change
@@ -3234,6 +3234,20 @@ static const struct bpf_func_proto *
32343234
}
32353235
}
32363236

3237+
static const struct bpf_func_proto *sk_skb_func_proto(enum bpf_func_id func_id)
3238+
{
3239+
switch (func_id) {
3240+
case BPF_FUNC_skb_load_bytes:
3241+
return &bpf_skb_load_bytes_proto;
3242+
case BPF_FUNC_get_socket_cookie:
3243+
return &bpf_get_socket_cookie_proto;
3244+
case BPF_FUNC_get_socket_uid:
3245+
return &bpf_get_socket_uid_proto;
3246+
default:
3247+
return bpf_base_func_proto(func_id);
3248+
}
3249+
}
3250+
32373251
static const struct bpf_func_proto *
32383252
lwt_xmit_func_proto(enum bpf_func_id func_id)
32393253
{
@@ -3525,6 +3539,22 @@ static bool sock_ops_is_valid_access(int off, int size,
35253539
return __is_valid_sock_ops_access(off, size);
35263540
}
35273541

3542+
static bool sk_skb_is_valid_access(int off, int size,
3543+
enum bpf_access_type type,
3544+
struct bpf_insn_access_aux *info)
3545+
{
3546+
switch (off) {
3547+
case bpf_ctx_range(struct __sk_buff, data):
3548+
info->reg_type = PTR_TO_PACKET;
3549+
break;
3550+
case bpf_ctx_range(struct __sk_buff, data_end):
3551+
info->reg_type = PTR_TO_PACKET_END;
3552+
break;
3553+
}
3554+
3555+
return bpf_skb_is_valid_access(off, size, type, info);
3556+
}
3557+
35283558
static u32 bpf_convert_ctx_access(enum bpf_access_type type,
35293559
const struct bpf_insn *si,
35303560
struct bpf_insn *insn_buf,
@@ -3994,6 +4024,12 @@ const struct bpf_verifier_ops sock_ops_prog_ops = {
39944024
.convert_ctx_access = sock_ops_convert_ctx_access,
39954025
};
39964026

4027+
const struct bpf_verifier_ops sk_skb_prog_ops = {
4028+
.get_func_proto = sk_skb_func_proto,
4029+
.is_valid_access = sk_skb_is_valid_access,
4030+
.convert_ctx_access = bpf_convert_ctx_access,
4031+
};
4032+
39974033
int sk_detach_filter(struct sock *sk)
39984034
{
39994035
int ret = -ENOENT;

0 commit comments

Comments
 (0)