blob: 64a1f7550d7e9e5d20e9a1e85ac1416b0b4cf2aa [file] [log] [blame]
Alexei Starovoitov5bacd782015-05-19 16:59:05 -07001/* Copyright (c) 2015 PLUMgrid, http://plumgrid.com
2 *
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of version 2 of the GNU General Public
5 * License as published by the Free Software Foundation.
6 */
7#include <linux/ptrace.h>
8#include <linux/version.h>
9#include <uapi/linux/bpf.h>
10#include <uapi/linux/seccomp.h>
Mickaël Salaünaf392a82017-02-08 21:27:44 +010011#include <uapi/linux/unistd.h>
David Daney4b7190e2017-06-13 16:49:38 -070012#include "syscall_nrs.h"
Toke Høiland-Jørgensen7cf245a2020-01-20 14:06:49 +010013#include <bpf/bpf_helpers.h>
14#include <bpf/bpf_tracing.h>
Alexei Starovoitov5bacd782015-05-19 16:59:05 -070015
16#define PROG(F) SEC("kprobe/"__stringify(F)) int bpf_func_##F
17
Daniel T. Lee59929cd2020-05-16 13:06:08 +090018struct {
19 __uint(type, BPF_MAP_TYPE_PROG_ARRAY);
20 __uint(key_size, sizeof(u32));
21 __uint(value_size, sizeof(u32));
David Daney4b7190e2017-06-13 16:49:38 -070022#ifdef __mips__
Daniel T. Lee59929cd2020-05-16 13:06:08 +090023 __uint(max_entries, 6000); /* MIPS n64 syscalls start at 5000 */
David Daney4b7190e2017-06-13 16:49:38 -070024#else
Daniel T. Lee59929cd2020-05-16 13:06:08 +090025 __uint(max_entries, 1024);
David Daney4b7190e2017-06-13 16:49:38 -070026#endif
Daniel T. Lee59929cd2020-05-16 13:06:08 +090027} progs SEC(".maps");
Alexei Starovoitov5bacd782015-05-19 16:59:05 -070028
Naveen N. Rao973d94d2016-09-24 02:10:05 +053029SEC("kprobe/__seccomp_filter")
Alexei Starovoitov5bacd782015-05-19 16:59:05 -070030int bpf_prog1(struct pt_regs *ctx)
31{
Naveen N. Rao973d94d2016-09-24 02:10:05 +053032 int sc_nr = (int)PT_REGS_PARM1(ctx);
Alexei Starovoitov5bacd782015-05-19 16:59:05 -070033
34 /* dispatch into next BPF program depending on syscall number */
Naveen N. Rao973d94d2016-09-24 02:10:05 +053035 bpf_tail_call(ctx, &progs, sc_nr);
Alexei Starovoitov5bacd782015-05-19 16:59:05 -070036
37 /* fall through -> unknown syscall */
Naveen N. Rao973d94d2016-09-24 02:10:05 +053038 if (sc_nr >= __NR_getuid && sc_nr <= __NR_getsid) {
Alexei Starovoitov5bacd782015-05-19 16:59:05 -070039 char fmt[] = "syscall=%d (one of get/set uid/pid/gid)\n";
Naveen N. Rao973d94d2016-09-24 02:10:05 +053040 bpf_trace_printk(fmt, sizeof(fmt), sc_nr);
Alexei Starovoitov5bacd782015-05-19 16:59:05 -070041 }
42 return 0;
43}
44
45/* we jump here when syscall number == __NR_write */
David Daney4b7190e2017-06-13 16:49:38 -070046PROG(SYS__NR_write)(struct pt_regs *ctx)
Alexei Starovoitov5bacd782015-05-19 16:59:05 -070047{
Daniel Borkmann02413ca2016-04-13 00:10:53 +020048 struct seccomp_data sd;
Alexei Starovoitov5bacd782015-05-19 16:59:05 -070049
Ilya Leoshkeviche4d9c232020-07-20 13:48:06 +020050 bpf_probe_read_kernel(&sd, sizeof(sd), (void *)PT_REGS_PARM2(ctx));
Alexei Starovoitov5bacd782015-05-19 16:59:05 -070051 if (sd.args[2] == 512) {
52 char fmt[] = "write(fd=%d, buf=%p, size=%d)\n";
53 bpf_trace_printk(fmt, sizeof(fmt),
54 sd.args[0], sd.args[1], sd.args[2]);
55 }
56 return 0;
57}
58
David Daney4b7190e2017-06-13 16:49:38 -070059PROG(SYS__NR_read)(struct pt_regs *ctx)
Alexei Starovoitov5bacd782015-05-19 16:59:05 -070060{
Daniel Borkmann02413ca2016-04-13 00:10:53 +020061 struct seccomp_data sd;
Alexei Starovoitov5bacd782015-05-19 16:59:05 -070062
Ilya Leoshkeviche4d9c232020-07-20 13:48:06 +020063 bpf_probe_read_kernel(&sd, sizeof(sd), (void *)PT_REGS_PARM2(ctx));
Alexei Starovoitov5bacd782015-05-19 16:59:05 -070064 if (sd.args[2] > 128 && sd.args[2] <= 1024) {
65 char fmt[] = "read(fd=%d, buf=%p, size=%d)\n";
66 bpf_trace_printk(fmt, sizeof(fmt),
67 sd.args[0], sd.args[1], sd.args[2]);
68 }
69 return 0;
70}
71
Ivan Khoronzhukbb4b5c02019-08-15 15:13:56 +030072#ifdef __NR_mmap2
73PROG(SYS__NR_mmap2)(struct pt_regs *ctx)
Alexei Starovoitov5bacd782015-05-19 16:59:05 -070074{
Ivan Khoronzhukbb4b5c02019-08-15 15:13:56 +030075 char fmt[] = "mmap2\n";
76
Alexei Starovoitov5bacd782015-05-19 16:59:05 -070077 bpf_trace_printk(fmt, sizeof(fmt));
78 return 0;
79}
Ivan Khoronzhukbb4b5c02019-08-15 15:13:56 +030080#endif
81
82#ifdef __NR_mmap
83PROG(SYS__NR_mmap)(struct pt_regs *ctx)
84{
85 char fmt[] = "mmap\n";
86
87 bpf_trace_printk(fmt, sizeof(fmt));
88 return 0;
89}
90#endif
Alexei Starovoitov5bacd782015-05-19 16:59:05 -070091
92char _license[] SEC("license") = "GPL";
93u32 _version SEC("version") = LINUX_VERSION_CODE;