1 // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
2 /* Copyright (c) 2019 Netronome Systems, Inc. */
3 
4 #include <errno.h>
5 #include <fcntl.h>
6 #include <string.h>
7 #include <stdlib.h>
8 #include <unistd.h>
9 #include <net/if.h>
10 #include <sys/utsname.h>
11 
12 #include <linux/btf.h>
13 #include <linux/filter.h>
14 #include <linux/kernel.h>
15 
16 #include "bpf.h"
17 #include "libbpf.h"
18 #include "libbpf_internal.h"
19 
probe_prog_load(enum bpf_prog_type prog_type,const struct bpf_insn * insns,size_t insns_cnt,char * log_buf,size_t log_buf_sz)20 static int probe_prog_load(enum bpf_prog_type prog_type,
21 			   const struct bpf_insn *insns, size_t insns_cnt,
22 			   char *log_buf, size_t log_buf_sz)
23 {
24 	LIBBPF_OPTS(bpf_prog_load_opts, opts,
25 		.log_buf = log_buf,
26 		.log_size = log_buf_sz,
27 		.log_level = log_buf ? 1 : 0,
28 	);
29 	int fd, err, exp_err = 0;
30 	const char *exp_msg = NULL;
31 	char buf[4096];
32 
33 	switch (prog_type) {
34 	case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
35 		opts.expected_attach_type = BPF_CGROUP_INET4_CONNECT;
36 		break;
37 	case BPF_PROG_TYPE_CGROUP_SOCKOPT:
38 		opts.expected_attach_type = BPF_CGROUP_GETSOCKOPT;
39 		break;
40 	case BPF_PROG_TYPE_SK_LOOKUP:
41 		opts.expected_attach_type = BPF_SK_LOOKUP;
42 		break;
43 	case BPF_PROG_TYPE_KPROBE:
44 		opts.kern_version = get_kernel_version();
45 		break;
46 	case BPF_PROG_TYPE_LIRC_MODE2:
47 		opts.expected_attach_type = BPF_LIRC_MODE2;
48 		break;
49 	case BPF_PROG_TYPE_TRACING:
50 	case BPF_PROG_TYPE_LSM:
51 		opts.log_buf = buf;
52 		opts.log_size = sizeof(buf);
53 		opts.log_level = 1;
54 		if (prog_type == BPF_PROG_TYPE_TRACING)
55 			opts.expected_attach_type = BPF_TRACE_FENTRY;
56 		else
57 			opts.expected_attach_type = BPF_MODIFY_RETURN;
58 		opts.attach_btf_id = 1;
59 
60 		exp_err = -EINVAL;
61 		exp_msg = "attach_btf_id 1 is not a function";
62 		break;
63 	case BPF_PROG_TYPE_EXT:
64 		opts.log_buf = buf;
65 		opts.log_size = sizeof(buf);
66 		opts.log_level = 1;
67 		opts.attach_btf_id = 1;
68 
69 		exp_err = -EINVAL;
70 		exp_msg = "Cannot replace kernel functions";
71 		break;
72 	case BPF_PROG_TYPE_SYSCALL:
73 		opts.prog_flags = BPF_F_SLEEPABLE;
74 		break;
75 	case BPF_PROG_TYPE_STRUCT_OPS:
76 		exp_err = -524; /* -ENOTSUPP */
77 		break;
78 	case BPF_PROG_TYPE_UNSPEC:
79 	case BPF_PROG_TYPE_SOCKET_FILTER:
80 	case BPF_PROG_TYPE_SCHED_CLS:
81 	case BPF_PROG_TYPE_SCHED_ACT:
82 	case BPF_PROG_TYPE_TRACEPOINT:
83 	case BPF_PROG_TYPE_XDP:
84 	case BPF_PROG_TYPE_PERF_EVENT:
85 	case BPF_PROG_TYPE_CGROUP_SKB:
86 	case BPF_PROG_TYPE_CGROUP_SOCK:
87 	case BPF_PROG_TYPE_LWT_IN:
88 	case BPF_PROG_TYPE_LWT_OUT:
89 	case BPF_PROG_TYPE_LWT_XMIT:
90 	case BPF_PROG_TYPE_SOCK_OPS:
91 	case BPF_PROG_TYPE_SK_SKB:
92 	case BPF_PROG_TYPE_CGROUP_DEVICE:
93 	case BPF_PROG_TYPE_SK_MSG:
94 	case BPF_PROG_TYPE_RAW_TRACEPOINT:
95 	case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE:
96 	case BPF_PROG_TYPE_LWT_SEG6LOCAL:
97 	case BPF_PROG_TYPE_SK_REUSEPORT:
98 	case BPF_PROG_TYPE_FLOW_DISSECTOR:
99 	case BPF_PROG_TYPE_CGROUP_SYSCTL:
100 		break;
101 	default:
102 		return -EOPNOTSUPP;
103 	}
104 
105 	fd = bpf_prog_load(prog_type, NULL, "GPL", insns, insns_cnt, &opts);
106 	err = -errno;
107 	if (fd >= 0)
108 		close(fd);
109 	if (exp_err) {
110 		if (fd >= 0 || err != exp_err)
111 			return 0;
112 		if (exp_msg && !strstr(buf, exp_msg))
113 			return 0;
114 		return 1;
115 	}
116 	return fd >= 0 ? 1 : 0;
117 }
118 
libbpf_probe_bpf_prog_type(enum bpf_prog_type prog_type,const void * opts)119 int libbpf_probe_bpf_prog_type(enum bpf_prog_type prog_type, const void *opts)
120 {
121 	struct bpf_insn insns[] = {
122 		BPF_MOV64_IMM(BPF_REG_0, 0),
123 		BPF_EXIT_INSN()
124 	};
125 	const size_t insn_cnt = ARRAY_SIZE(insns);
126 	int ret;
127 
128 	if (opts)
129 		return libbpf_err(-EINVAL);
130 
131 	ret = probe_prog_load(prog_type, insns, insn_cnt, NULL, 0);
132 	return libbpf_err(ret);
133 }
134 
libbpf__load_raw_btf(const char * raw_types,size_t types_len,const char * str_sec,size_t str_len)135 int libbpf__load_raw_btf(const char *raw_types, size_t types_len,
136 			 const char *str_sec, size_t str_len)
137 {
138 	struct btf_header hdr = {
139 		.magic = BTF_MAGIC,
140 		.version = BTF_VERSION,
141 		.hdr_len = sizeof(struct btf_header),
142 		.type_len = types_len,
143 		.str_off = types_len,
144 		.str_len = str_len,
145 	};
146 	int btf_fd, btf_len;
147 	__u8 *raw_btf;
148 
149 	btf_len = hdr.hdr_len + hdr.type_len + hdr.str_len;
150 	raw_btf = malloc(btf_len);
151 	if (!raw_btf)
152 		return -ENOMEM;
153 
154 	memcpy(raw_btf, &hdr, sizeof(hdr));
155 	memcpy(raw_btf + hdr.hdr_len, raw_types, hdr.type_len);
156 	memcpy(raw_btf + hdr.hdr_len + hdr.type_len, str_sec, hdr.str_len);
157 
158 	btf_fd = bpf_btf_load(raw_btf, btf_len, NULL);
159 
160 	free(raw_btf);
161 	return btf_fd;
162 }
163 
load_local_storage_btf(void)164 static int load_local_storage_btf(void)
165 {
166 	const char strs[] = "\0bpf_spin_lock\0val\0cnt\0l";
167 	/* struct bpf_spin_lock {
168 	 *   int val;
169 	 * };
170 	 * struct val {
171 	 *   int cnt;
172 	 *   struct bpf_spin_lock l;
173 	 * };
174 	 */
175 	__u32 types[] = {
176 		/* int */
177 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),  /* [1] */
178 		/* struct bpf_spin_lock */                      /* [2] */
179 		BTF_TYPE_ENC(1, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 1), 4),
180 		BTF_MEMBER_ENC(15, 1, 0), /* int val; */
181 		/* struct val */                                /* [3] */
182 		BTF_TYPE_ENC(15, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 2), 8),
183 		BTF_MEMBER_ENC(19, 1, 0), /* int cnt; */
184 		BTF_MEMBER_ENC(23, 2, 32),/* struct bpf_spin_lock l; */
185 	};
186 
187 	return libbpf__load_raw_btf((char *)types, sizeof(types),
188 				     strs, sizeof(strs));
189 }
190 
probe_map_create(enum bpf_map_type map_type)191 static int probe_map_create(enum bpf_map_type map_type)
192 {
193 	LIBBPF_OPTS(bpf_map_create_opts, opts);
194 	int key_size, value_size, max_entries;
195 	__u32 btf_key_type_id = 0, btf_value_type_id = 0;
196 	int fd = -1, btf_fd = -1, fd_inner = -1, exp_err = 0, err = 0;
197 
198 	key_size	= sizeof(__u32);
199 	value_size	= sizeof(__u32);
200 	max_entries	= 1;
201 
202 	switch (map_type) {
203 	case BPF_MAP_TYPE_STACK_TRACE:
204 		value_size	= sizeof(__u64);
205 		break;
206 	case BPF_MAP_TYPE_LPM_TRIE:
207 		key_size	= sizeof(__u64);
208 		value_size	= sizeof(__u64);
209 		opts.map_flags	= BPF_F_NO_PREALLOC;
210 		break;
211 	case BPF_MAP_TYPE_CGROUP_STORAGE:
212 	case BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE:
213 		key_size	= sizeof(struct bpf_cgroup_storage_key);
214 		value_size	= sizeof(__u64);
215 		max_entries	= 0;
216 		break;
217 	case BPF_MAP_TYPE_QUEUE:
218 	case BPF_MAP_TYPE_STACK:
219 		key_size	= 0;
220 		break;
221 	case BPF_MAP_TYPE_SK_STORAGE:
222 	case BPF_MAP_TYPE_INODE_STORAGE:
223 	case BPF_MAP_TYPE_TASK_STORAGE:
224 		btf_key_type_id = 1;
225 		btf_value_type_id = 3;
226 		value_size = 8;
227 		max_entries = 0;
228 		opts.map_flags = BPF_F_NO_PREALLOC;
229 		btf_fd = load_local_storage_btf();
230 		if (btf_fd < 0)
231 			return btf_fd;
232 		break;
233 	case BPF_MAP_TYPE_RINGBUF:
234 	case BPF_MAP_TYPE_USER_RINGBUF:
235 		key_size = 0;
236 		value_size = 0;
237 		max_entries = sysconf(_SC_PAGE_SIZE);
238 		break;
239 	case BPF_MAP_TYPE_STRUCT_OPS:
240 		/* we'll get -ENOTSUPP for invalid BTF type ID for struct_ops */
241 		opts.btf_vmlinux_value_type_id = 1;
242 		exp_err = -524; /* -ENOTSUPP */
243 		break;
244 	case BPF_MAP_TYPE_BLOOM_FILTER:
245 		key_size = 0;
246 		max_entries = 1;
247 		break;
248 	case BPF_MAP_TYPE_HASH:
249 	case BPF_MAP_TYPE_ARRAY:
250 	case BPF_MAP_TYPE_PROG_ARRAY:
251 	case BPF_MAP_TYPE_PERF_EVENT_ARRAY:
252 	case BPF_MAP_TYPE_PERCPU_HASH:
253 	case BPF_MAP_TYPE_PERCPU_ARRAY:
254 	case BPF_MAP_TYPE_CGROUP_ARRAY:
255 	case BPF_MAP_TYPE_LRU_HASH:
256 	case BPF_MAP_TYPE_LRU_PERCPU_HASH:
257 	case BPF_MAP_TYPE_ARRAY_OF_MAPS:
258 	case BPF_MAP_TYPE_HASH_OF_MAPS:
259 	case BPF_MAP_TYPE_DEVMAP:
260 	case BPF_MAP_TYPE_DEVMAP_HASH:
261 	case BPF_MAP_TYPE_SOCKMAP:
262 	case BPF_MAP_TYPE_CPUMAP:
263 	case BPF_MAP_TYPE_XSKMAP:
264 	case BPF_MAP_TYPE_SOCKHASH:
265 	case BPF_MAP_TYPE_REUSEPORT_SOCKARRAY:
266 		break;
267 	case BPF_MAP_TYPE_UNSPEC:
268 	default:
269 		return -EOPNOTSUPP;
270 	}
271 
272 	if (map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS ||
273 	    map_type == BPF_MAP_TYPE_HASH_OF_MAPS) {
274 		fd_inner = bpf_map_create(BPF_MAP_TYPE_HASH, NULL,
275 					  sizeof(__u32), sizeof(__u32), 1, NULL);
276 		if (fd_inner < 0)
277 			goto cleanup;
278 
279 		opts.inner_map_fd = fd_inner;
280 	}
281 
282 	if (btf_fd >= 0) {
283 		opts.btf_fd = btf_fd;
284 		opts.btf_key_type_id = btf_key_type_id;
285 		opts.btf_value_type_id = btf_value_type_id;
286 	}
287 
288 	fd = bpf_map_create(map_type, NULL, key_size, value_size, max_entries, &opts);
289 	err = -errno;
290 
291 cleanup:
292 	if (fd >= 0)
293 		close(fd);
294 	if (fd_inner >= 0)
295 		close(fd_inner);
296 	if (btf_fd >= 0)
297 		close(btf_fd);
298 
299 	if (exp_err)
300 		return fd < 0 && err == exp_err ? 1 : 0;
301 	else
302 		return fd >= 0 ? 1 : 0;
303 }
304 
libbpf_probe_bpf_map_type(enum bpf_map_type map_type,const void * opts)305 int libbpf_probe_bpf_map_type(enum bpf_map_type map_type, const void *opts)
306 {
307 	int ret;
308 
309 	if (opts)
310 		return libbpf_err(-EINVAL);
311 
312 	ret = probe_map_create(map_type);
313 	return libbpf_err(ret);
314 }
315 
libbpf_probe_bpf_helper(enum bpf_prog_type prog_type,enum bpf_func_id helper_id,const void * opts)316 int libbpf_probe_bpf_helper(enum bpf_prog_type prog_type, enum bpf_func_id helper_id,
317 			    const void *opts)
318 {
319 	struct bpf_insn insns[] = {
320 		BPF_EMIT_CALL((__u32)helper_id),
321 		BPF_EXIT_INSN(),
322 	};
323 	const size_t insn_cnt = ARRAY_SIZE(insns);
324 	char buf[4096];
325 	int ret;
326 
327 	if (opts)
328 		return libbpf_err(-EINVAL);
329 
330 	/* we can't successfully load all prog types to check for BPF helper
331 	 * support, so bail out with -EOPNOTSUPP error
332 	 */
333 	switch (prog_type) {
334 	case BPF_PROG_TYPE_TRACING:
335 	case BPF_PROG_TYPE_EXT:
336 	case BPF_PROG_TYPE_LSM:
337 	case BPF_PROG_TYPE_STRUCT_OPS:
338 		return -EOPNOTSUPP;
339 	default:
340 		break;
341 	}
342 
343 	buf[0] = '\0';
344 	ret = probe_prog_load(prog_type, insns, insn_cnt, buf, sizeof(buf));
345 	if (ret < 0)
346 		return libbpf_err(ret);
347 
348 	/* If BPF verifier doesn't recognize BPF helper ID (enum bpf_func_id)
349 	 * at all, it will emit something like "invalid func unknown#181".
350 	 * If BPF verifier recognizes BPF helper but it's not supported for
351 	 * given BPF program type, it will emit "unknown func bpf_sys_bpf#166".
352 	 * In both cases, provided combination of BPF program type and BPF
353 	 * helper is not supported by the kernel.
354 	 * In all other cases, probe_prog_load() above will either succeed (e.g.,
355 	 * because BPF helper happens to accept no input arguments or it
356 	 * accepts one input argument and initial PTR_TO_CTX is fine for
357 	 * that), or we'll get some more specific BPF verifier error about
358 	 * some unsatisfied conditions.
359 	 */
360 	if (ret == 0 && (strstr(buf, "invalid func ") || strstr(buf, "unknown func ")))
361 		return 0;
362 	return 1; /* assume supported */
363 }
364