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 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
|
/*
Copyright (C) 2021 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.
*/
#ifndef __PLUMBING_HELPERS_H
#define __PLUMBING_HELPERS_H
#include <linux/ptrace.h>
#include <linux/version.h>
#include <linux/fdtable.h>
#include "types.h"
#include "builtins.h"
#define _READ(P) ({ typeof(P) _val; \
memset(&_val, 0, sizeof(_val)); \
bpf_probe_read(&_val, sizeof(_val), &P); \
_val; \
})
#ifdef BPF_DEBUG
#define bpf_printk(fmt, ...) \
do { \
char s[] = fmt; \
bpf_trace_printk(s, sizeof(s), ##__VA_ARGS__); \
} while (0)
#else
#define bpf_printk(fmt, ...)
#endif
#ifndef BPF_SUPPORTS_RAW_TRACEPOINTS
static __always_inline int __stash_args(unsigned long long id,
unsigned long *args)
{
int ret = bpf_map_update_elem(&stash_map, &id, args, BPF_ANY);
if (ret)
bpf_printk("error stashing arguments for %d:%d\n", id, ret);
return ret;
}
static __always_inline int stash_args(unsigned long *args)
{
unsigned long long id = bpf_get_current_pid_tgid() & 0xffffffff;
return __stash_args(id, args);
}
static __always_inline unsigned long *__unstash_args(unsigned long long id)
{
struct sys_stash_args *args;
args = bpf_map_lookup_elem(&stash_map, &id);
if (!args)
return NULL;
return args->args;
}
static __always_inline unsigned long *unstash_args(void)
{
unsigned long long id = bpf_get_current_pid_tgid() & 0xffffffff;
return __unstash_args(id);
}
static __always_inline void delete_args(void)
{
unsigned long long id = bpf_get_current_pid_tgid() & 0xffffffff;
bpf_map_delete_elem(&stash_map, &id);
}
#endif
/* Can be called just from an exit event
*/
static __always_inline long bpf_syscall_get_retval(void *ctx)
{
struct sys_exit_args *args = (struct sys_exit_args *)ctx;
return args->ret;
}
/* Can be called from both enter and exit event, id is at the same
* offset in both struct sys_enter_args and struct sys_exit_args
*/
static __always_inline long bpf_syscall_get_nr(void *ctx)
{
struct sys_enter_args *args = (struct sys_enter_args *)ctx;
long id;
#ifdef BPF_SUPPORTS_RAW_TRACEPOINTS
struct pt_regs *regs = (struct pt_regs *)args->regs;
id = _READ(regs->orig_ax);
#else
id = args->id;
#endif
return id;
}
#ifndef BPF_SUPPORTS_RAW_TRACEPOINTS
static __always_inline unsigned long bpf_syscall_get_argument_from_args(unsigned long *args,
int idx)
{
unsigned long arg;
if (idx <= 5)
arg = args[idx];
else
arg = 0;
return arg;
}
#endif
static __always_inline unsigned long bpf_syscall_get_argument_from_ctx(void *ctx,
int idx)
{
unsigned long arg;
#ifdef BPF_SUPPORTS_RAW_TRACEPOINTS
struct sys_enter_args *args = (struct sys_enter_args *)ctx;
struct pt_regs *regs = (struct pt_regs *)args->regs;
switch (idx) {
case 0:
arg = _READ(regs->di);
break;
case 1:
arg = _READ(regs->si);
break;
case 2:
arg = _READ(regs->dx);
break;
case 3:
arg = _READ(regs->r10);
break;
case 4:
arg = _READ(regs->r8);
break;
case 5:
arg = _READ(regs->r9);
break;
default:
arg = 0;
}
#else
unsigned long *args = unstash_args();
if (args)
arg = bpf_syscall_get_argument_from_args(args, idx);
else
arg = 0;
#endif
return arg;
}
static __always_inline unsigned long bpf_syscall_get_argument(struct filler_data *data,
int idx)
{
#ifdef BPF_SUPPORTS_RAW_TRACEPOINTS
return bpf_syscall_get_argument_from_ctx(data->ctx, idx);
#else
return bpf_syscall_get_argument_from_args(data->args, idx);
#endif
}
static __always_inline char *get_frame_scratch_area(unsigned int cpu)
{
char *scratchp;
scratchp = bpf_map_lookup_elem(&frame_scratch_map, &cpu);
if (!scratchp)
bpf_printk("frame scratch NULL\n");
return scratchp;
}
static __always_inline char *get_tmp_scratch_area(unsigned int cpu)
{
char *scratchp;
scratchp = bpf_map_lookup_elem(&tmp_scratch_map, &cpu);
if (!scratchp)
bpf_printk("tmp scratch NULL\n");
return scratchp;
}
static __always_inline const struct syscall_evt_pair *get_syscall_info(int id)
{
const struct syscall_evt_pair *p =
bpf_map_lookup_elem(&syscall_table, &id);
if (!p)
bpf_printk("no syscall_info for %d\n", id);
return p;
}
static __always_inline const struct ppm_event_info *get_event_info(enum ppm_event_type event_type)
{
const struct ppm_event_info *e =
bpf_map_lookup_elem(&event_info_table, &event_type);
if (!e)
bpf_printk("no event info for %d\n", event_type);
return e;
}
static __always_inline const struct ppm_event_entry *get_event_filler_info(enum ppm_event_type event_type)
{
const struct ppm_event_entry *e;
e = bpf_map_lookup_elem(&fillers_table, &event_type);
if (!e)
bpf_printk("no filler info for %d\n", event_type);
return e;
}
static __always_inline struct scap_bpf_settings *get_bpf_settings(void)
{
struct scap_bpf_settings *settings;
int id = 0;
settings = bpf_map_lookup_elem(&settings_map, &id);
if (!settings)
bpf_printk("settings NULL\n");
return settings;
}
static __always_inline struct scap_bpf_per_cpu_state *get_local_state(unsigned int cpu)
{
struct scap_bpf_per_cpu_state *state;
state = bpf_map_lookup_elem(&local_state_map, &cpu);
if (!state)
bpf_printk("state NULL\n");
return state;
}
static __always_inline bool acquire_local_state(struct scap_bpf_per_cpu_state *state)
{
if (state->in_use) {
bpf_printk("acquire_local_state: already in use\n");
return false;
}
state->in_use = true;
return true;
}
static __always_inline bool release_local_state(struct scap_bpf_per_cpu_state *state)
{
if (!state->in_use) {
bpf_printk("release_local_state: already not in use\n");
return false;
}
state->in_use = false;
return true;
}
static __always_inline int init_filler_data(void *ctx,
struct filler_data *data,
bool is_syscall)
{
unsigned int cpu;
data->ctx = ctx;
data->settings = get_bpf_settings();
if (!data->settings)
return PPM_FAILURE_BUG;
cpu = bpf_get_smp_processor_id();
data->buf = get_frame_scratch_area(cpu);
if (!data->buf)
return PPM_FAILURE_BUG;
data->state = get_local_state(cpu);
if (!data->state)
return PPM_FAILURE_BUG;
data->tmp_scratch = get_tmp_scratch_area(cpu);
if (!data->tmp_scratch)
return PPM_FAILURE_BUG;
data->evt = get_event_info(data->state->tail_ctx.evt_type);
if (!data->evt)
return PPM_FAILURE_BUG;
data->filler_info = get_event_filler_info(data->state->tail_ctx.evt_type);
if (!data->filler_info)
return PPM_FAILURE_BUG;
#ifndef BPF_SUPPORTS_RAW_TRACEPOINTS
if (is_syscall) {
data->args = unstash_args();
if (!data->args)
return PPM_SKIP_EVENT;
}
#endif
data->curarg_already_on_frame = false;
data->fd = -1;
return PPM_SUCCESS;
}
static __always_inline int bpf_test_bit(int nr, unsigned long *addr)
{
return 1UL & (_READ(addr[BIT_WORD(nr)]) >> (nr & (BITS_PER_LONG - 1)));
}
static __always_inline bool drop_event(void *ctx,
struct scap_bpf_per_cpu_state *state,
enum ppm_event_type evt_type,
struct scap_bpf_settings *settings,
enum syscall_flags drop_flags)
{
if (!settings->dropping_mode)
return false;
switch (evt_type) {
case PPME_SYSCALL_CLOSE_X:
case PPME_SOCKET_BIND_X: {
long ret = bpf_syscall_get_retval(ctx);
if (ret < 0)
return true;
break;
}
case PPME_SYSCALL_CLOSE_E: {
struct sys_enter_args *args;
struct files_struct *files;
struct task_struct *task;
unsigned long *open_fds;
struct fdtable *fdt;
int close_fd;
int max_fds;
close_fd = bpf_syscall_get_argument_from_ctx(ctx, 0);
if (close_fd < 0)
return true;
task = (struct task_struct *)bpf_get_current_task();
if (!task)
break;
files = _READ(task->files);
if (!files)
break;
fdt = _READ(files->fdt);
if (!fdt)
break;
max_fds = _READ(fdt->max_fds);
if (close_fd >= max_fds)
return true;
open_fds = _READ(fdt->open_fds);
if (!open_fds)
break;
if (!bpf_test_bit(close_fd, open_fds))
return true;
break;
}
case PPME_SYSCALL_FCNTL_E:
case PPME_SYSCALL_FCNTL_X: {
long cmd = bpf_syscall_get_argument_from_ctx(ctx, 1);
if (cmd != F_DUPFD && cmd != F_DUPFD_CLOEXEC)
return true;
break;
}
default:
break;
}
if (drop_flags & UF_NEVER_DROP)
return false;
if (drop_flags & UF_ALWAYS_DROP)
return true;
if (state->tail_ctx.ts % 1000000000 >= 1000000000 /
settings->sampling_ratio) {
if (!settings->is_dropping) {
settings->is_dropping = true;
state->tail_ctx.evt_type = PPME_DROP_E;
return false;
}
return true;
}
if (settings->is_dropping) {
settings->is_dropping = false;
state->tail_ctx.evt_type = PPME_DROP_X;
return false;
}
return false;
}
static __always_inline void reset_tail_ctx(struct scap_bpf_per_cpu_state *state,
enum ppm_event_type evt_type,
unsigned long long ts)
{
state->tail_ctx.evt_type = evt_type;
state->tail_ctx.ts = ts;
state->tail_ctx.curarg = 0;
state->tail_ctx.curoff = 0;
state->tail_ctx.len = 0;
state->tail_ctx.prev_res = 0;
}
static __always_inline void call_filler(void *ctx,
void *stack_ctx,
enum ppm_event_type evt_type,
struct scap_bpf_settings *settings,
enum syscall_flags drop_flags)
{
const struct ppm_event_entry *filler_info;
struct scap_bpf_per_cpu_state *state;
unsigned long long pid;
unsigned long long ts;
unsigned int cpu;
cpu = bpf_get_smp_processor_id();
state = get_local_state(cpu);
if (!state)
return;
if (!acquire_local_state(state))
return;
if (cpu == 0 && state->hotplug_cpu != 0) {
evt_type = PPME_CPU_HOTPLUG_E;
drop_flags = UF_NEVER_DROP;
}
ts = settings->boot_time + bpf_ktime_get_boot_ns();
reset_tail_ctx(state, evt_type, ts);
/* drop_event can change state->tail_ctx.evt_type */
if (drop_event(stack_ctx, state, evt_type, settings, drop_flags))
goto cleanup;
++state->n_evts;
filler_info = get_event_filler_info(state->tail_ctx.evt_type);
if (!filler_info)
goto cleanup;
bpf_tail_call(ctx, &tail_map, filler_info->filler_id);
bpf_printk("Can't tail call filler evt=%d, filler=%d\n",
state->tail_ctx.evt_type,
filler_info->filler_id);
cleanup:
release_local_state(state);
}
#endif
|