1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
|
/*
Copyright (C) 2022 The Falco Authors.
This file is dual licensed under either the MIT or GPL 2. See MIT.txt
or GPL2.txt for full copies of the license.
*/
#include "quirks.h"
#include <generated/utsrelease.h>
#include <uapi/linux/bpf.h>
#include <linux/sched.h>
#include "../driver_config.h"
#include "../ppm_events_public.h"
#include "bpf_helpers.h"
#include "types.h"
#include "maps.h"
#include "plumbing_helpers.h"
#include "ring_helpers.h"
#include "filler_helpers.h"
#include "fillers.h"
#include "builtins.h"
#ifdef BPF_SUPPORTS_RAW_TRACEPOINTS
#define BPF_PROBE(prefix, event, type) \
__bpf_section(TP_NAME #event) \
int bpf_##event(struct type *ctx)
#else
#define BPF_PROBE(prefix, event, type) \
__bpf_section(TP_NAME prefix #event) \
int bpf_##event(struct type *ctx)
#endif
BPF_PROBE("raw_syscalls/", sys_enter, sys_enter_args)
{
const struct syscall_evt_pair *sc_evt;
struct scap_bpf_settings *settings;
enum ppm_event_type evt_type;
int drop_flags;
long id;
if (bpf_in_ia32_syscall())
return 0;
id = bpf_syscall_get_nr(ctx);
if (id < 0 || id >= SYSCALL_TABLE_SIZE)
return 0;
settings = get_bpf_settings();
if (!settings)
return 0;
if (!settings->capture_enabled)
return 0;
sc_evt = get_syscall_info(id);
if (!sc_evt)
return 0;
if (sc_evt->flags & UF_UNINTERESTING)
return 0;
if (sc_evt->flags & UF_USED) {
evt_type = sc_evt->enter_event_type;
drop_flags = sc_evt->flags;
} else {
evt_type = PPME_GENERIC_E;
drop_flags = UF_ALWAYS_DROP;
}
#ifdef BPF_SUPPORTS_RAW_TRACEPOINTS
call_filler(ctx, ctx, evt_type, settings, drop_flags);
#else
/* Duplicated here to avoid verifier madness */
struct sys_enter_args stack_ctx;
memcpy(stack_ctx.args, ctx->args, sizeof(ctx->args));
if (stash_args(stack_ctx.args))
return 0;
call_filler(ctx, &stack_ctx, evt_type, settings, drop_flags);
#endif
return 0;
}
BPF_PROBE("raw_syscalls/", sys_exit, sys_exit_args)
{
const struct syscall_evt_pair *sc_evt;
struct scap_bpf_settings *settings;
enum ppm_event_type evt_type;
int drop_flags;
long id;
if (bpf_in_ia32_syscall())
return 0;
id = bpf_syscall_get_nr(ctx);
if (id < 0 || id >= SYSCALL_TABLE_SIZE)
return 0;
settings = get_bpf_settings();
if (!settings)
return 0;
if (!settings->capture_enabled)
return 0;
sc_evt = get_syscall_info(id);
if (!sc_evt)
return 0;
if (sc_evt->flags & UF_UNINTERESTING)
return 0;
if (sc_evt->flags & UF_USED) {
evt_type = sc_evt->exit_event_type;
drop_flags = sc_evt->flags;
} else {
evt_type = PPME_GENERIC_X;
drop_flags = UF_ALWAYS_DROP;
}
call_filler(ctx, ctx, evt_type, settings, drop_flags);
return 0;
}
BPF_PROBE("sched/", sched_process_exit, sched_process_exit_args)
{
struct scap_bpf_settings *settings;
enum ppm_event_type evt_type;
struct task_struct *task;
unsigned int flags;
task = (struct task_struct *)bpf_get_current_task();
flags = _READ(task->flags);
if (flags & PF_KTHREAD)
return 0;
settings = get_bpf_settings();
if (!settings)
return 0;
if (!settings->capture_enabled)
return 0;
evt_type = PPME_PROCEXIT_1_E;
call_filler(ctx, ctx, evt_type, settings, UF_NEVER_DROP);
return 0;
}
BPF_PROBE("sched/", sched_switch, sched_switch_args)
{
struct scap_bpf_settings *settings;
enum ppm_event_type evt_type;
settings = get_bpf_settings();
if (!settings)
return 0;
if (!settings->capture_enabled)
return 0;
evt_type = PPME_SCHEDSWITCH_6_E;
call_filler(ctx, ctx, evt_type, settings, 0);
return 0;
}
static __always_inline int bpf_page_fault(struct page_fault_args *ctx)
{
struct scap_bpf_settings *settings;
enum ppm_event_type evt_type;
settings = get_bpf_settings();
if (!settings)
return 0;
if (!settings->page_faults)
return 0;
if (!settings->capture_enabled)
return 0;
evt_type = PPME_PAGE_FAULT_E;
call_filler(ctx, ctx, evt_type, settings, UF_ALWAYS_DROP);
return 0;
}
BPF_PROBE("exceptions/", page_fault_user, page_fault_args)
{
return bpf_page_fault(ctx);
}
BPF_PROBE("exceptions/", page_fault_kernel, page_fault_args)
{
return bpf_page_fault(ctx);
}
BPF_PROBE("signal/", signal_deliver, signal_deliver_args)
{
struct scap_bpf_settings *settings;
enum ppm_event_type evt_type;
settings = get_bpf_settings();
if (!settings)
return 0;
if (!settings->capture_enabled)
return 0;
evt_type = PPME_SIGNALDELIVER_E;
call_filler(ctx, ctx, evt_type, settings, UF_ALWAYS_DROP);
return 0;
}
#ifndef BPF_SUPPORTS_RAW_TRACEPOINTS
__bpf_section(TP_NAME "sched/sched_process_fork")
int bpf_sched_process_fork(struct sched_process_fork_args *ctx)
{
struct scap_bpf_settings *settings;
enum ppm_event_type evt_type;
struct sys_stash_args args;
unsigned long *argsp;
settings = get_bpf_settings();
if (!settings)
return 0;
if (!settings->capture_enabled)
return 0;
argsp = __unstash_args(ctx->parent_pid);
if (!argsp)
return 0;
memcpy(&args, argsp, sizeof(args));
__stash_args(ctx->child_pid, args.args);
return 0;
}
#endif
char kernel_ver[] __bpf_section("kernel_version") = UTS_RELEASE;
char __license[] __bpf_section("license") = "GPL";
char probe_ver[] __bpf_section("probe_version") = DRIVER_VERSION;
char probe_commit[] __bpf_section("build_commit") = DRIVER_COMMIT;
uint64_t probe_api_ver __bpf_section("api_version") = PPM_API_CURRENT_VERSION;
uint64_t probe_schema_ver __bpf_section("schema_version") = PPM_SCHEMA_CURRENT_VERSION;
|