Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags   |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) #include <linux/filter.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) #include <linux/seccomp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <sys/prctl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <bpf/bpf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <bpf/libbpf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <sys/resource.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include "trace_helpers.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #ifdef __mips__
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #define	MAX_ENTRIES  6000 /* MIPS n64 syscalls start at 5000 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #define	MAX_ENTRIES  1024
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) /* install fake seccomp program to enable seccomp code path inside the kernel,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20)  * so that our kprobe attached to seccomp_phase1() can be triggered
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) static void install_accept_all_seccomp(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 	struct sock_filter filter[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 		BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_ALLOW),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	struct sock_fprog prog = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 		.len = (unsigned short)(sizeof(filter)/sizeof(filter[0])),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 		.filter = filter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	if (prctl(PR_SET_SECCOMP, 2, &prog))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 		perror("prctl");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) int main(int ac, char **argv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	struct bpf_link *link = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	struct bpf_program *prog;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	struct bpf_object *obj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	int key, fd, progs_fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	const char *section;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	char filename[256];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	FILE *f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	setrlimit(RLIMIT_MEMLOCK, &r);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	obj = bpf_object__open_file(filename, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	if (libbpf_get_error(obj)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 		fprintf(stderr, "ERROR: opening BPF object file failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	prog = bpf_object__find_program_by_name(obj, "bpf_prog1");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	if (!prog) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 		printf("finding a prog in obj file failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 		goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	/* load BPF program */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	if (bpf_object__load(obj)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 		fprintf(stderr, "ERROR: loading BPF object file failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 		goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	link = bpf_program__attach(prog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	if (libbpf_get_error(link)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 		fprintf(stderr, "ERROR: bpf_program__attach failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 		link = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 		goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	progs_fd = bpf_object__find_map_fd_by_name(obj, "progs");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	if (progs_fd < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 		fprintf(stderr, "ERROR: finding a map in obj file failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 		goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	bpf_object__for_each_program(prog, obj) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 		section = bpf_program__section_name(prog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 		/* register only syscalls to PROG_ARRAY */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 		if (sscanf(section, "kprobe/%d", &key) != 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 		fd = bpf_program__fd(prog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 		bpf_map_update_elem(progs_fd, &key, &fd, BPF_ANY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	install_accept_all_seccomp();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	f = popen("dd if=/dev/zero of=/dev/null count=5", "r");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	(void) f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	read_trace_pipe();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) cleanup:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	bpf_link__destroy(link);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	bpf_object__close(obj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) }