Back to home page

OSCL-LXR

 
 

    


0001 /* Copyright (c) 2015 PLUMgrid, http://plumgrid.com
0002  *
0003  * This program is free software; you can redistribute it and/or
0004  * modify it under the terms of version 2 of the GNU General Public
0005  * License as published by the Free Software Foundation.
0006  */
0007 #include <linux/ptrace.h>
0008 #include <linux/version.h>
0009 #include <uapi/linux/bpf.h>
0010 #include <uapi/linux/seccomp.h>
0011 #include <uapi/linux/unistd.h>
0012 #include "syscall_nrs.h"
0013 #include <bpf/bpf_helpers.h>
0014 #include <bpf/bpf_tracing.h>
0015 
0016 #define PROG(F) SEC("kprobe/"__stringify(F)) int bpf_func_##F
0017 
0018 struct {
0019     __uint(type, BPF_MAP_TYPE_PROG_ARRAY);
0020     __uint(key_size, sizeof(u32));
0021     __uint(value_size, sizeof(u32));
0022 #ifdef __mips__
0023     __uint(max_entries, 6000); /* MIPS n64 syscalls start at 5000 */
0024 #else
0025     __uint(max_entries, 1024);
0026 #endif
0027 } progs SEC(".maps");
0028 
0029 SEC("kprobe/__seccomp_filter")
0030 int bpf_prog1(struct pt_regs *ctx)
0031 {
0032     int sc_nr = (int)PT_REGS_PARM1(ctx);
0033 
0034     /* dispatch into next BPF program depending on syscall number */
0035     bpf_tail_call(ctx, &progs, sc_nr);
0036 
0037     /* fall through -> unknown syscall */
0038     if (sc_nr >= __NR_getuid && sc_nr <= __NR_getsid) {
0039         char fmt[] = "syscall=%d (one of get/set uid/pid/gid)\n";
0040         bpf_trace_printk(fmt, sizeof(fmt), sc_nr);
0041     }
0042     return 0;
0043 }
0044 
0045 /* we jump here when syscall number == __NR_write */
0046 PROG(SYS__NR_write)(struct pt_regs *ctx)
0047 {
0048     struct seccomp_data sd;
0049 
0050     bpf_probe_read_kernel(&sd, sizeof(sd), (void *)PT_REGS_PARM2(ctx));
0051     if (sd.args[2] == 512) {
0052         char fmt[] = "write(fd=%d, buf=%p, size=%d)\n";
0053         bpf_trace_printk(fmt, sizeof(fmt),
0054                  sd.args[0], sd.args[1], sd.args[2]);
0055     }
0056     return 0;
0057 }
0058 
0059 PROG(SYS__NR_read)(struct pt_regs *ctx)
0060 {
0061     struct seccomp_data sd;
0062 
0063     bpf_probe_read_kernel(&sd, sizeof(sd), (void *)PT_REGS_PARM2(ctx));
0064     if (sd.args[2] > 128 && sd.args[2] <= 1024) {
0065         char fmt[] = "read(fd=%d, buf=%p, size=%d)\n";
0066         bpf_trace_printk(fmt, sizeof(fmt),
0067                  sd.args[0], sd.args[1], sd.args[2]);
0068     }
0069     return 0;
0070 }
0071 
0072 #ifdef __NR_mmap2
0073 PROG(SYS__NR_mmap2)(struct pt_regs *ctx)
0074 {
0075     char fmt[] = "mmap2\n";
0076 
0077     bpf_trace_printk(fmt, sizeof(fmt));
0078     return 0;
0079 }
0080 #endif
0081 
0082 #ifdef __NR_mmap
0083 PROG(SYS__NR_mmap)(struct pt_regs *ctx)
0084 {
0085     char fmt[] = "mmap\n";
0086 
0087     bpf_trace_printk(fmt, sizeof(fmt));
0088     return 0;
0089 }
0090 #endif
0091 
0092 char _license[] SEC("license") = "GPL";
0093 u32 _version SEC("version") = LINUX_VERSION_CODE;