1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2021 Facebook */
3
4 #include <linux/bpf.h>
5 #include <bpf/bpf_helpers.h>
6 #include "bpf_misc.h"
7
8 char _license[] SEC("license") = "GPL";
9
10 struct bpf_map;
11
12 struct {
13 __uint(type, BPF_MAP_TYPE_ARRAY);
14 __type(key, __u32);
15 __type(value, __u32);
16 __uint(max_entries, 1000);
17 } map_random_data SEC(".maps");
18
19 struct map_bloom_type {
20 __uint(type, BPF_MAP_TYPE_BLOOM_FILTER);
21 __type(value, __u32);
22 __uint(max_entries, 10000);
23 __uint(map_extra, 5);
24 } map_bloom SEC(".maps");
25
26 struct {
27 __uint(type, BPF_MAP_TYPE_ARRAY_OF_MAPS);
28 __type(key, int);
29 __type(value, int);
30 __uint(max_entries, 1);
31 __array(values, struct map_bloom_type);
32 } outer_map SEC(".maps");
33
34 struct callback_ctx {
35 struct bpf_map *map;
36 };
37
38 int error = 0;
39
40 static __u64
check_elem(struct bpf_map * map,__u32 * key,__u32 * val,struct callback_ctx * data)41 check_elem(struct bpf_map *map, __u32 *key, __u32 *val,
42 struct callback_ctx *data)
43 {
44 int err;
45
46 err = bpf_map_peek_elem(data->map, val);
47 if (err) {
48 error |= 1;
49 return 1; /* stop the iteration */
50 }
51
52 return 0;
53 }
54
55 SEC("fentry/" SYS_PREFIX "sys_getpgid")
inner_map(void * ctx)56 int inner_map(void *ctx)
57 {
58 struct bpf_map *inner_map;
59 struct callback_ctx data;
60 int key = 0;
61
62 inner_map = bpf_map_lookup_elem(&outer_map, &key);
63 if (!inner_map) {
64 error |= 2;
65 return 0;
66 }
67
68 data.map = inner_map;
69 bpf_for_each_map_elem(&map_random_data, check_elem, &data, 0);
70
71 return 0;
72 }
73
74 SEC("fentry/" SYS_PREFIX "sys_getpgid")
check_bloom(void * ctx)75 int check_bloom(void *ctx)
76 {
77 struct callback_ctx data;
78
79 data.map = (struct bpf_map *)&map_bloom;
80 bpf_for_each_map_elem(&map_random_data, check_elem, &data, 0);
81
82 return 0;
83 }
84