Skip to content

Commit 4a482f3

Browse files
iamkafaidavem330
authored andcommitted
cgroup: bpf: Add bpf_skb_in_cgroup_proto
Adds a bpf helper, bpf_skb_in_cgroup, to decide if a skb->sk belongs to a descendant of a cgroup2. It is similar to the feature added in netfilter: commit c38c459 ("netfilter: implement xt_cgroup cgroup2 path match") The user is expected to populate a BPF_MAP_TYPE_CGROUP_ARRAY which will be used by the bpf_skb_in_cgroup. Modifications to the bpf verifier is to ensure BPF_MAP_TYPE_CGROUP_ARRAY and bpf_skb_in_cgroup() are always used together. Signed-off-by: Martin KaFai Lau <[email protected]> Cc: Alexei Starovoitov <[email protected]> Cc: Daniel Borkmann <[email protected]> Cc: Tejun Heo <[email protected]> Acked-by: Alexei Starovoitov <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 4ed8ec5 commit 4a482f3

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-1
lines changed

Diff for: include/uapi/linux/bpf.h

+11
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,17 @@ enum bpf_func_id {
337337
*/
338338
BPF_FUNC_skb_change_type,
339339

340+
/**
341+
* bpf_skb_in_cgroup(skb, map, index) - Check cgroup2 membership of skb
342+
* @skb: pointer to skb
343+
* @map: pointer to bpf_map in BPF_MAP_TYPE_CGROUP_ARRAY type
344+
* @index: index of the cgroup in the bpf_map
345+
* Return:
346+
* == 0 skb failed the cgroup2 descendant test
347+
* == 1 skb succeeded the cgroup2 descendant test
348+
* < 0 error
349+
*/
350+
BPF_FUNC_skb_in_cgroup,
340351
__BPF_FUNC_MAX_ID,
341352
};
342353

Diff for: kernel/bpf/verifier.c

+7-1
Original file line numberDiff line numberDiff line change
@@ -1036,7 +1036,9 @@ static int check_map_func_compatibility(struct bpf_map *map, int func_id)
10361036
goto error;
10371037
break;
10381038
case BPF_MAP_TYPE_CGROUP_ARRAY:
1039-
goto error;
1039+
if (func_id != BPF_FUNC_skb_in_cgroup)
1040+
goto error;
1041+
break;
10401042
default:
10411043
break;
10421044
}
@@ -1056,6 +1058,10 @@ static int check_map_func_compatibility(struct bpf_map *map, int func_id)
10561058
if (map->map_type != BPF_MAP_TYPE_STACK_TRACE)
10571059
goto error;
10581060
break;
1061+
case BPF_FUNC_skb_in_cgroup:
1062+
if (map->map_type != BPF_MAP_TYPE_CGROUP_ARRAY)
1063+
goto error;
1064+
break;
10591065
default:
10601066
break;
10611067
}

Diff for: net/core/filter.c

+38
Original file line numberDiff line numberDiff line change
@@ -2239,6 +2239,40 @@ bpf_get_skb_set_tunnel_proto(enum bpf_func_id which)
22392239
}
22402240
}
22412241

2242+
#ifdef CONFIG_SOCK_CGROUP_DATA
2243+
static u64 bpf_skb_in_cgroup(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
2244+
{
2245+
struct sk_buff *skb = (struct sk_buff *)(long)r1;
2246+
struct bpf_map *map = (struct bpf_map *)(long)r2;
2247+
struct bpf_array *array = container_of(map, struct bpf_array, map);
2248+
struct cgroup *cgrp;
2249+
struct sock *sk;
2250+
u32 i = (u32)r3;
2251+
2252+
sk = skb->sk;
2253+
if (!sk || !sk_fullsock(sk))
2254+
return -ENOENT;
2255+
2256+
if (unlikely(i >= array->map.max_entries))
2257+
return -E2BIG;
2258+
2259+
cgrp = READ_ONCE(array->ptrs[i]);
2260+
if (unlikely(!cgrp))
2261+
return -EAGAIN;
2262+
2263+
return cgroup_is_descendant(sock_cgroup_ptr(&sk->sk_cgrp_data), cgrp);
2264+
}
2265+
2266+
static const struct bpf_func_proto bpf_skb_in_cgroup_proto = {
2267+
.func = bpf_skb_in_cgroup,
2268+
.gpl_only = false,
2269+
.ret_type = RET_INTEGER,
2270+
.arg1_type = ARG_PTR_TO_CTX,
2271+
.arg2_type = ARG_CONST_MAP_PTR,
2272+
.arg3_type = ARG_ANYTHING,
2273+
};
2274+
#endif
2275+
22422276
static const struct bpf_func_proto *
22432277
sk_filter_func_proto(enum bpf_func_id func_id)
22442278
{
@@ -2307,6 +2341,10 @@ tc_cls_act_func_proto(enum bpf_func_id func_id)
23072341
return bpf_get_event_output_proto();
23082342
case BPF_FUNC_get_smp_processor_id:
23092343
return &bpf_get_smp_processor_id_proto;
2344+
#ifdef CONFIG_SOCK_CGROUP_DATA
2345+
case BPF_FUNC_skb_in_cgroup:
2346+
return &bpf_skb_in_cgroup_proto;
2347+
#endif
23102348
default:
23112349
return sk_filter_func_proto(func_id);
23122350
}

0 commit comments

Comments
 (0)