xref: /openbmc/linux/samples/bpf/tracex5.bpf.c (revision 11430421)
14a0ee788SDaniel T. Lee /* Copyright (c) 2015 PLUMgrid, http://plumgrid.com
24a0ee788SDaniel T. Lee  *
34a0ee788SDaniel T. Lee  * This program is free software; you can redistribute it and/or
44a0ee788SDaniel T. Lee  * modify it under the terms of version 2 of the GNU General Public
54a0ee788SDaniel T. Lee  * License as published by the Free Software Foundation.
64a0ee788SDaniel T. Lee  */
74a0ee788SDaniel T. Lee #include "vmlinux.h"
84a0ee788SDaniel T. Lee #include "syscall_nrs.h"
94a0ee788SDaniel T. Lee #include <linux/version.h>
104a0ee788SDaniel T. Lee #include <uapi/linux/unistd.h>
114a0ee788SDaniel T. Lee #include <bpf/bpf_helpers.h>
124a0ee788SDaniel T. Lee #include <bpf/bpf_tracing.h>
13*11430421SDaniel T. Lee #include <bpf/bpf_core_read.h>
144a0ee788SDaniel T. Lee 
154a0ee788SDaniel T. Lee #define __stringify(x) #x
164a0ee788SDaniel T. Lee #define PROG(F) SEC("kprobe/"__stringify(F)) int bpf_func_##F
174a0ee788SDaniel T. Lee 
184a0ee788SDaniel T. Lee struct {
194a0ee788SDaniel T. Lee 	__uint(type, BPF_MAP_TYPE_PROG_ARRAY);
204a0ee788SDaniel T. Lee 	__uint(key_size, sizeof(u32));
214a0ee788SDaniel T. Lee 	__uint(value_size, sizeof(u32));
224a0ee788SDaniel T. Lee #ifdef __mips__
234a0ee788SDaniel T. Lee 	__uint(max_entries, 6000); /* MIPS n64 syscalls start at 5000 */
244a0ee788SDaniel T. Lee #else
254a0ee788SDaniel T. Lee 	__uint(max_entries, 1024);
264a0ee788SDaniel T. Lee #endif
274a0ee788SDaniel T. Lee } progs SEC(".maps");
284a0ee788SDaniel T. Lee 
294a0ee788SDaniel T. Lee SEC("kprobe/__seccomp_filter")
bpf_prog1(struct pt_regs * ctx)304a0ee788SDaniel T. Lee int bpf_prog1(struct pt_regs *ctx)
314a0ee788SDaniel T. Lee {
324a0ee788SDaniel T. Lee 	int sc_nr = (int)PT_REGS_PARM1(ctx);
334a0ee788SDaniel T. Lee 
344a0ee788SDaniel T. Lee 	/* dispatch into next BPF program depending on syscall number */
354a0ee788SDaniel T. Lee 	bpf_tail_call(ctx, &progs, sc_nr);
364a0ee788SDaniel T. Lee 
374a0ee788SDaniel T. Lee 	/* fall through -> unknown syscall */
384a0ee788SDaniel T. Lee 	if (sc_nr >= __NR_getuid && sc_nr <= __NR_getsid) {
394a0ee788SDaniel T. Lee 		char fmt[] = "syscall=%d (one of get/set uid/pid/gid)\n";
404a0ee788SDaniel T. Lee 		bpf_trace_printk(fmt, sizeof(fmt), sc_nr);
414a0ee788SDaniel T. Lee 	}
424a0ee788SDaniel T. Lee 	return 0;
434a0ee788SDaniel T. Lee }
444a0ee788SDaniel T. Lee 
454a0ee788SDaniel T. Lee /* we jump here when syscall number == __NR_write */
PROG(SYS__NR_write)464a0ee788SDaniel T. Lee PROG(SYS__NR_write)(struct pt_regs *ctx)
474a0ee788SDaniel T. Lee {
484a0ee788SDaniel T. Lee 	struct seccomp_data sd;
494a0ee788SDaniel T. Lee 
50*11430421SDaniel T. Lee 	bpf_core_read(&sd, sizeof(sd), (void *)PT_REGS_PARM2(ctx));
514a0ee788SDaniel T. Lee 	if (sd.args[2] == 512) {
524a0ee788SDaniel T. Lee 		char fmt[] = "write(fd=%d, buf=%p, size=%d)\n";
534a0ee788SDaniel T. Lee 		bpf_trace_printk(fmt, sizeof(fmt),
544a0ee788SDaniel T. Lee 				 sd.args[0], sd.args[1], sd.args[2]);
554a0ee788SDaniel T. Lee 	}
564a0ee788SDaniel T. Lee 	return 0;
574a0ee788SDaniel T. Lee }
584a0ee788SDaniel T. Lee 
PROG(SYS__NR_read)594a0ee788SDaniel T. Lee PROG(SYS__NR_read)(struct pt_regs *ctx)
604a0ee788SDaniel T. Lee {
614a0ee788SDaniel T. Lee 	struct seccomp_data sd;
624a0ee788SDaniel T. Lee 
63*11430421SDaniel T. Lee 	bpf_core_read(&sd, sizeof(sd), (void *)PT_REGS_PARM2(ctx));
644a0ee788SDaniel T. Lee 	if (sd.args[2] > 128 && sd.args[2] <= 1024) {
654a0ee788SDaniel T. Lee 		char fmt[] = "read(fd=%d, buf=%p, size=%d)\n";
664a0ee788SDaniel T. Lee 		bpf_trace_printk(fmt, sizeof(fmt),
674a0ee788SDaniel T. Lee 				 sd.args[0], sd.args[1], sd.args[2]);
684a0ee788SDaniel T. Lee 	}
694a0ee788SDaniel T. Lee 	return 0;
704a0ee788SDaniel T. Lee }
714a0ee788SDaniel T. Lee 
724a0ee788SDaniel T. Lee #ifdef __NR_mmap2
PROG(SYS__NR_mmap2)734a0ee788SDaniel T. Lee PROG(SYS__NR_mmap2)(struct pt_regs *ctx)
744a0ee788SDaniel T. Lee {
754a0ee788SDaniel T. Lee 	char fmt[] = "mmap2\n";
764a0ee788SDaniel T. Lee 
774a0ee788SDaniel T. Lee 	bpf_trace_printk(fmt, sizeof(fmt));
784a0ee788SDaniel T. Lee 	return 0;
794a0ee788SDaniel T. Lee }
804a0ee788SDaniel T. Lee #endif
814a0ee788SDaniel T. Lee 
824a0ee788SDaniel T. Lee #ifdef __NR_mmap
PROG(SYS__NR_mmap)834a0ee788SDaniel T. Lee PROG(SYS__NR_mmap)(struct pt_regs *ctx)
844a0ee788SDaniel T. Lee {
854a0ee788SDaniel T. Lee 	char fmt[] = "mmap\n";
864a0ee788SDaniel T. Lee 
874a0ee788SDaniel T. Lee 	bpf_trace_printk(fmt, sizeof(fmt));
884a0ee788SDaniel T. Lee 	return 0;
894a0ee788SDaniel T. Lee }
904a0ee788SDaniel T. Lee #endif
914a0ee788SDaniel T. Lee 
924a0ee788SDaniel T. Lee char _license[] SEC("license") = "GPL";
934a0ee788SDaniel T. Lee u32 _version SEC("version") = LINUX_VERSION_CODE;
94