Commit ab850abb authored by Alexei Starovoitov's avatar Alexei Starovoitov

Merge branch 'Add SEC("ksyscall") support'

Andrii Nakryiko says:

====================

Add SEC("ksyscall")/SEC("kretsyscall") sections and corresponding
bpf_program__attach_ksyscall() API that simplifies tracing kernel syscalls
through kprobe mechanism. Kprobing syscalls isn't trivial due to varying
syscall handler names in the kernel and various ways syscall argument are
passed, depending on kernel architecture and configuration. SEC("ksyscall")
allows user to not care about such details and just get access to syscall
input arguments, while libbpf takes care of necessary feature detection logic.

There are still more quirks that are not straightforward to hide completely
(see comments about mmap(), clone() and compat syscalls), so in such more
advanced scenarios user might need to fall back to plain SEC("kprobe")
approach, but for absolute majority of users SEC("ksyscall") is a big
improvement.

As part of this patch set libbpf adds two more virtual __kconfig externs, in
addition to existing LINUX_KERNEL_VERSION: LINUX_HAS_BPF_COOKIE and
LINUX_HAS_SYSCALL_WRAPPER, which let's libbpf-provided BPF-side code minimize
external dependencies and assumptions and let's user-space part of libbpf to
perform all the feature detection logic. This benefits USDT support code,
which now doesn't depend on BPF CO-RE for its functionality.

v1->v2:
  - normalize extern variable-related warn and debug message formats (Alan);
rfc->v1:
  - drop dependency on kallsyms and speed up SYSCALL_WRAPPER detection (Alexei);
  - drop dependency on /proc/config.gz in bpf_tracing.h (Yaniv);
  - add doc comment and ephasize mmap(), clone() and compat quirks that are
    not supported (Ilya);
  - use mechanism similar to LINUX_KERNEL_VERSION to also improve USDT code.
====================
Reviewed-by: default avatarStanislav Fomichev <sdf@google.com>
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
parents 9ff5efde d814ed62
...@@ -2,6 +2,8 @@ ...@@ -2,6 +2,8 @@
#ifndef __BPF_TRACING_H__ #ifndef __BPF_TRACING_H__
#define __BPF_TRACING_H__ #define __BPF_TRACING_H__
#include <bpf/bpf_helpers.h>
/* Scan the ARCH passed in from ARCH env variable (see Makefile) */ /* Scan the ARCH passed in from ARCH env variable (see Makefile) */
#if defined(__TARGET_ARCH_x86) #if defined(__TARGET_ARCH_x86)
#define bpf_target_x86 #define bpf_target_x86
...@@ -140,7 +142,7 @@ struct pt_regs___s390 { ...@@ -140,7 +142,7 @@ struct pt_regs___s390 {
#define __PT_RC_REG gprs[2] #define __PT_RC_REG gprs[2]
#define __PT_SP_REG gprs[15] #define __PT_SP_REG gprs[15]
#define __PT_IP_REG psw.addr #define __PT_IP_REG psw.addr
#define PT_REGS_PARM1_SYSCALL(x) ({ _Pragma("GCC error \"use PT_REGS_PARM1_CORE_SYSCALL() instead\""); 0l; }) #define PT_REGS_PARM1_SYSCALL(x) PT_REGS_PARM1_CORE_SYSCALL(x)
#define PT_REGS_PARM1_CORE_SYSCALL(x) BPF_CORE_READ((const struct pt_regs___s390 *)(x), orig_gpr2) #define PT_REGS_PARM1_CORE_SYSCALL(x) BPF_CORE_READ((const struct pt_regs___s390 *)(x), orig_gpr2)
#elif defined(bpf_target_arm) #elif defined(bpf_target_arm)
...@@ -174,7 +176,7 @@ struct pt_regs___arm64 { ...@@ -174,7 +176,7 @@ struct pt_regs___arm64 {
#define __PT_RC_REG regs[0] #define __PT_RC_REG regs[0]
#define __PT_SP_REG sp #define __PT_SP_REG sp
#define __PT_IP_REG pc #define __PT_IP_REG pc
#define PT_REGS_PARM1_SYSCALL(x) ({ _Pragma("GCC error \"use PT_REGS_PARM1_CORE_SYSCALL() instead\""); 0l; }) #define PT_REGS_PARM1_SYSCALL(x) PT_REGS_PARM1_CORE_SYSCALL(x)
#define PT_REGS_PARM1_CORE_SYSCALL(x) BPF_CORE_READ((const struct pt_regs___arm64 *)(x), orig_x0) #define PT_REGS_PARM1_CORE_SYSCALL(x) BPF_CORE_READ((const struct pt_regs___arm64 *)(x), orig_x0)
#elif defined(bpf_target_mips) #elif defined(bpf_target_mips)
...@@ -493,39 +495,62 @@ typeof(name(0)) name(struct pt_regs *ctx) \ ...@@ -493,39 +495,62 @@ typeof(name(0)) name(struct pt_regs *ctx) \
} \ } \
static __always_inline typeof(name(0)) ____##name(struct pt_regs *ctx, ##args) static __always_inline typeof(name(0)) ____##name(struct pt_regs *ctx, ##args)
/* If kernel has CONFIG_ARCH_HAS_SYSCALL_WRAPPER, read pt_regs directly */
#define ___bpf_syscall_args0() ctx #define ___bpf_syscall_args0() ctx
#define ___bpf_syscall_args1(x) ___bpf_syscall_args0(), (void *)PT_REGS_PARM1_CORE_SYSCALL(regs) #define ___bpf_syscall_args1(x) ___bpf_syscall_args0(), (void *)PT_REGS_PARM1_SYSCALL(regs)
#define ___bpf_syscall_args2(x, args...) ___bpf_syscall_args1(args), (void *)PT_REGS_PARM2_CORE_SYSCALL(regs) #define ___bpf_syscall_args2(x, args...) ___bpf_syscall_args1(args), (void *)PT_REGS_PARM2_SYSCALL(regs)
#define ___bpf_syscall_args3(x, args...) ___bpf_syscall_args2(args), (void *)PT_REGS_PARM3_CORE_SYSCALL(regs) #define ___bpf_syscall_args3(x, args...) ___bpf_syscall_args2(args), (void *)PT_REGS_PARM3_SYSCALL(regs)
#define ___bpf_syscall_args4(x, args...) ___bpf_syscall_args3(args), (void *)PT_REGS_PARM4_CORE_SYSCALL(regs) #define ___bpf_syscall_args4(x, args...) ___bpf_syscall_args3(args), (void *)PT_REGS_PARM4_SYSCALL(regs)
#define ___bpf_syscall_args5(x, args...) ___bpf_syscall_args4(args), (void *)PT_REGS_PARM5_CORE_SYSCALL(regs) #define ___bpf_syscall_args5(x, args...) ___bpf_syscall_args4(args), (void *)PT_REGS_PARM5_SYSCALL(regs)
#define ___bpf_syscall_args(args...) ___bpf_apply(___bpf_syscall_args, ___bpf_narg(args))(args) #define ___bpf_syscall_args(args...) ___bpf_apply(___bpf_syscall_args, ___bpf_narg(args))(args)
/* If kernel doesn't have CONFIG_ARCH_HAS_SYSCALL_WRAPPER, we have to BPF_CORE_READ from pt_regs */
#define ___bpf_syswrap_args0() ctx
#define ___bpf_syswrap_args1(x) ___bpf_syswrap_args0(), (void *)PT_REGS_PARM1_CORE_SYSCALL(regs)
#define ___bpf_syswrap_args2(x, args...) ___bpf_syswrap_args1(args), (void *)PT_REGS_PARM2_CORE_SYSCALL(regs)
#define ___bpf_syswrap_args3(x, args...) ___bpf_syswrap_args2(args), (void *)PT_REGS_PARM3_CORE_SYSCALL(regs)
#define ___bpf_syswrap_args4(x, args...) ___bpf_syswrap_args3(args), (void *)PT_REGS_PARM4_CORE_SYSCALL(regs)
#define ___bpf_syswrap_args5(x, args...) ___bpf_syswrap_args4(args), (void *)PT_REGS_PARM5_CORE_SYSCALL(regs)
#define ___bpf_syswrap_args(args...) ___bpf_apply(___bpf_syswrap_args, ___bpf_narg(args))(args)
/* /*
* BPF_KPROBE_SYSCALL is a variant of BPF_KPROBE, which is intended for * BPF_KSYSCALL is a variant of BPF_KPROBE, which is intended for
* tracing syscall functions, like __x64_sys_close. It hides the underlying * tracing syscall functions, like __x64_sys_close. It hides the underlying
* platform-specific low-level way of getting syscall input arguments from * platform-specific low-level way of getting syscall input arguments from
* struct pt_regs, and provides a familiar typed and named function arguments * struct pt_regs, and provides a familiar typed and named function arguments
* syntax and semantics of accessing syscall input parameters. * syntax and semantics of accessing syscall input parameters.
* *
* Original struct pt_regs* context is preserved as 'ctx' argument. This might * Original struct pt_regs * context is preserved as 'ctx' argument. This might
* be necessary when using BPF helpers like bpf_perf_event_output(). * be necessary when using BPF helpers like bpf_perf_event_output().
* *
* This macro relies on BPF CO-RE support. * At the moment BPF_KSYSCALL does not handle all the calling convention
* quirks for mmap(), clone() and compat syscalls transparrently. This may or
* may not change in the future. User needs to take extra measures to handle
* such quirks explicitly, if necessary.
*
* This macro relies on BPF CO-RE support and virtual __kconfig externs.
*/ */
#define BPF_KPROBE_SYSCALL(name, args...) \ #define BPF_KSYSCALL(name, args...) \
name(struct pt_regs *ctx); \ name(struct pt_regs *ctx); \
extern _Bool LINUX_HAS_SYSCALL_WRAPPER __kconfig; \
static __attribute__((always_inline)) typeof(name(0)) \ static __attribute__((always_inline)) typeof(name(0)) \
____##name(struct pt_regs *ctx, ##args); \ ____##name(struct pt_regs *ctx, ##args); \
typeof(name(0)) name(struct pt_regs *ctx) \ typeof(name(0)) name(struct pt_regs *ctx) \
{ \ { \
struct pt_regs *regs = PT_REGS_SYSCALL_REGS(ctx); \ struct pt_regs *regs = LINUX_HAS_SYSCALL_WRAPPER \
? (struct pt_regs *)PT_REGS_PARM1(ctx) \
: ctx; \
_Pragma("GCC diagnostic push") \ _Pragma("GCC diagnostic push") \
_Pragma("GCC diagnostic ignored \"-Wint-conversion\"") \ _Pragma("GCC diagnostic ignored \"-Wint-conversion\"") \
if (LINUX_HAS_SYSCALL_WRAPPER) \
return ____##name(___bpf_syswrap_args(args)); \
else \
return ____##name(___bpf_syscall_args(args)); \ return ____##name(___bpf_syscall_args(args)); \
_Pragma("GCC diagnostic pop") \ _Pragma("GCC diagnostic pop") \
} \ } \
static __attribute__((always_inline)) typeof(name(0)) \ static __attribute__((always_inline)) typeof(name(0)) \
____##name(struct pt_regs *ctx, ##args) ____##name(struct pt_regs *ctx, ##args)
#define BPF_KPROBE_SYSCALL BPF_KSYSCALL
#endif #endif
This diff is collapsed.
...@@ -457,6 +457,52 @@ bpf_program__attach_kprobe_multi_opts(const struct bpf_program *prog, ...@@ -457,6 +457,52 @@ bpf_program__attach_kprobe_multi_opts(const struct bpf_program *prog,
const char *pattern, const char *pattern,
const struct bpf_kprobe_multi_opts *opts); const struct bpf_kprobe_multi_opts *opts);
struct bpf_ksyscall_opts {
/* size of this struct, for forward/backward compatiblity */
size_t sz;
/* custom user-provided value fetchable through bpf_get_attach_cookie() */
__u64 bpf_cookie;
/* attach as return probe? */
bool retprobe;
size_t :0;
};
#define bpf_ksyscall_opts__last_field retprobe
/**
* @brief **bpf_program__attach_ksyscall()** attaches a BPF program
* to kernel syscall handler of a specified syscall. Optionally it's possible
* to request to install retprobe that will be triggered at syscall exit. It's
* also possible to associate BPF cookie (though options).
*
* Libbpf automatically will determine correct full kernel function name,
* which depending on system architecture and kernel version/configuration
* could be of the form __<arch>_sys_<syscall> or __se_sys_<syscall>, and will
* attach specified program using kprobe/kretprobe mechanism.
*
* **bpf_program__attach_ksyscall()** is an API counterpart of declarative
* **SEC("ksyscall/<syscall>")** annotation of BPF programs.
*
* At the moment **SEC("ksyscall")** and **bpf_program__attach_ksyscall()** do
* not handle all the calling convention quirks for mmap(), clone() and compat
* syscalls. It also only attaches to "native" syscall interfaces. If host
* system supports compat syscalls or defines 32-bit syscalls in 64-bit
* kernel, such syscall interfaces won't be attached to by libbpf.
*
* These limitations may or may not change in the future. Therefore it is
* recommended to use SEC("kprobe") for these syscalls or if working with
* compat and 32-bit interfaces is required.
*
* @param prog BPF program to attach
* @param syscall_name Symbolic name of the syscall (e.g., "bpf")
* @param opts Additional options (see **struct bpf_ksyscall_opts**)
* @return Reference to the newly created BPF link; or NULL is returned on
* error, error code is stored in errno
*/
LIBBPF_API struct bpf_link *
bpf_program__attach_ksyscall(const struct bpf_program *prog,
const char *syscall_name,
const struct bpf_ksyscall_opts *opts);
struct bpf_uprobe_opts { struct bpf_uprobe_opts {
/* size of this struct, for forward/backward compatiblity */ /* size of this struct, for forward/backward compatiblity */
size_t sz; size_t sz;
......
...@@ -356,6 +356,7 @@ LIBBPF_0.8.0 { ...@@ -356,6 +356,7 @@ LIBBPF_0.8.0 {
LIBBPF_1.0.0 { LIBBPF_1.0.0 {
global: global:
bpf_prog_query_opts; bpf_prog_query_opts;
bpf_program__attach_ksyscall;
btf__add_enum64; btf__add_enum64;
btf__add_enum64_value; btf__add_enum64_value;
libbpf_bpf_attach_type_str; libbpf_bpf_attach_type_str;
......
...@@ -352,6 +352,8 @@ enum kern_feature_id { ...@@ -352,6 +352,8 @@ enum kern_feature_id {
FEAT_BPF_COOKIE, FEAT_BPF_COOKIE,
/* BTF_KIND_ENUM64 support and BTF_KIND_ENUM kflag support */ /* BTF_KIND_ENUM64 support and BTF_KIND_ENUM kflag support */
FEAT_BTF_ENUM64, FEAT_BTF_ENUM64,
/* Kernel uses syscall wrapper (CONFIG_ARCH_HAS_SYSCALL_WRAPPER) */
FEAT_SYSCALL_WRAPPER,
__FEAT_CNT, __FEAT_CNT,
}; };
......
...@@ -6,7 +6,6 @@ ...@@ -6,7 +6,6 @@
#include <linux/errno.h> #include <linux/errno.h>
#include <bpf/bpf_helpers.h> #include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h> #include <bpf/bpf_tracing.h>
#include <bpf/bpf_core_read.h>
/* Below types and maps are internal implementation details of libbpf's USDT /* Below types and maps are internal implementation details of libbpf's USDT
* support and are subjects to change. Also, bpf_usdt_xxx() API helpers should * support and are subjects to change. Also, bpf_usdt_xxx() API helpers should
...@@ -30,14 +29,6 @@ ...@@ -30,14 +29,6 @@
#ifndef BPF_USDT_MAX_IP_CNT #ifndef BPF_USDT_MAX_IP_CNT
#define BPF_USDT_MAX_IP_CNT (4 * BPF_USDT_MAX_SPEC_CNT) #define BPF_USDT_MAX_IP_CNT (4 * BPF_USDT_MAX_SPEC_CNT)
#endif #endif
/* We use BPF CO-RE to detect support for BPF cookie from BPF side. This is
* the only dependency on CO-RE, so if it's undesirable, user can override
* BPF_USDT_HAS_BPF_COOKIE to specify whether to BPF cookie is supported or not.
*/
#ifndef BPF_USDT_HAS_BPF_COOKIE
#define BPF_USDT_HAS_BPF_COOKIE \
bpf_core_enum_value_exists(enum bpf_func_id___usdt, BPF_FUNC_get_attach_cookie___usdt)
#endif
enum __bpf_usdt_arg_type { enum __bpf_usdt_arg_type {
BPF_USDT_ARG_CONST, BPF_USDT_ARG_CONST,
...@@ -83,15 +74,12 @@ struct { ...@@ -83,15 +74,12 @@ struct {
__type(value, __u32); __type(value, __u32);
} __bpf_usdt_ip_to_spec_id SEC(".maps") __weak; } __bpf_usdt_ip_to_spec_id SEC(".maps") __weak;
/* don't rely on user's BPF code to have latest definition of bpf_func_id */ extern const _Bool LINUX_HAS_BPF_COOKIE __kconfig;
enum bpf_func_id___usdt {
BPF_FUNC_get_attach_cookie___usdt = 0xBAD, /* value doesn't matter */
};
static __always_inline static __always_inline
int __bpf_usdt_spec_id(struct pt_regs *ctx) int __bpf_usdt_spec_id(struct pt_regs *ctx)
{ {
if (!BPF_USDT_HAS_BPF_COOKIE) { if (!LINUX_HAS_BPF_COOKIE) {
long ip = PT_REGS_IP(ctx); long ip = PT_REGS_IP(ctx);
int *spec_id_ptr; int *spec_id_ptr;
......
...@@ -39,6 +39,7 @@ static struct test_case { ...@@ -39,6 +39,7 @@ static struct test_case {
"CONFIG_STR=\"abracad\"\n" "CONFIG_STR=\"abracad\"\n"
"CONFIG_MISSING=0", "CONFIG_MISSING=0",
.data = { .data = {
.unkn_virt_val = 0,
.bpf_syscall = false, .bpf_syscall = false,
.tristate_val = TRI_MODULE, .tristate_val = TRI_MODULE,
.bool_val = true, .bool_val = true,
...@@ -121,7 +122,7 @@ static struct test_case { ...@@ -121,7 +122,7 @@ static struct test_case {
void test_core_extern(void) void test_core_extern(void)
{ {
const uint32_t kern_ver = get_kernel_version(); const uint32_t kern_ver = get_kernel_version();
int err, duration = 0, i, j; int err, i, j;
struct test_core_extern *skel = NULL; struct test_core_extern *skel = NULL;
uint64_t *got, *exp; uint64_t *got, *exp;
int n = sizeof(*skel->data) / sizeof(uint64_t); int n = sizeof(*skel->data) / sizeof(uint64_t);
...@@ -136,19 +137,17 @@ void test_core_extern(void) ...@@ -136,19 +137,17 @@ void test_core_extern(void)
continue; continue;
skel = test_core_extern__open_opts(&opts); skel = test_core_extern__open_opts(&opts);
if (CHECK(!skel, "skel_open", "skeleton open failed\n")) if (!ASSERT_OK_PTR(skel, "skel_open"))
goto cleanup; goto cleanup;
err = test_core_extern__load(skel); err = test_core_extern__load(skel);
if (t->fails) { if (t->fails) {
CHECK(!err, "skel_load", ASSERT_ERR(err, "skel_load_should_fail");
"shouldn't succeed open/load of skeleton\n");
goto cleanup; goto cleanup;
} else if (CHECK(err, "skel_load", } else if (!ASSERT_OK(err, "skel_load")) {
"failed to open/load skeleton\n")) {
goto cleanup; goto cleanup;
} }
err = test_core_extern__attach(skel); err = test_core_extern__attach(skel);
if (CHECK(err, "attach_raw_tp", "failed attach: %d\n", err)) if (!ASSERT_OK(err, "attach_raw_tp"))
goto cleanup; goto cleanup;
usleep(1); usleep(1);
...@@ -158,9 +157,7 @@ void test_core_extern(void) ...@@ -158,9 +157,7 @@ void test_core_extern(void)
got = (uint64_t *)skel->data; got = (uint64_t *)skel->data;
exp = (uint64_t *)&t->data; exp = (uint64_t *)&t->data;
for (j = 0; j < n; j++) { for (j = 0; j < n; j++) {
CHECK(got[j] != exp[j], "check_res", ASSERT_EQ(got[j], exp[j], "result");
"result #%d: expected %llx, but got %llx\n",
j, (__u64)exp[j], (__u64)got[j]);
} }
cleanup: cleanup:
test_core_extern__destroy(skel); test_core_extern__destroy(skel);
......
...@@ -64,8 +64,8 @@ int BPF_KPROBE(handle_sys_prctl) ...@@ -64,8 +64,8 @@ int BPF_KPROBE(handle_sys_prctl)
return 0; return 0;
} }
SEC("kprobe/" SYS_PREFIX "sys_prctl") SEC("ksyscall/prctl")
int BPF_KPROBE_SYSCALL(prctl_enter, int option, unsigned long arg2, int BPF_KSYSCALL(prctl_enter, int option, unsigned long arg2,
unsigned long arg3, unsigned long arg4, unsigned long arg5) unsigned long arg3, unsigned long arg4, unsigned long arg5)
{ {
pid_t pid = bpf_get_current_pid_tgid() >> 32; pid_t pid = bpf_get_current_pid_tgid() >> 32;
......
// SPDX-License-Identifier: GPL-2.0 // SPDX-License-Identifier: GPL-2.0
// Copyright (c) 2017 Facebook // Copyright (c) 2017 Facebook
#include <linux/ptrace.h> #include "vmlinux.h"
#include <linux/bpf.h>
#include <bpf/bpf_helpers.h> #include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h> #include <bpf/bpf_tracing.h>
#include <stdbool.h> #include <bpf/bpf_core_read.h>
#include "bpf_misc.h" #include "bpf_misc.h"
int kprobe_res = 0; int kprobe_res = 0;
...@@ -31,8 +30,8 @@ int handle_kprobe(struct pt_regs *ctx) ...@@ -31,8 +30,8 @@ int handle_kprobe(struct pt_regs *ctx)
return 0; return 0;
} }
SEC("kprobe/" SYS_PREFIX "sys_nanosleep") SEC("ksyscall/nanosleep")
int BPF_KPROBE(handle_kprobe_auto) int BPF_KSYSCALL(handle_kprobe_auto, struct __kernel_timespec *req, struct __kernel_timespec *rem)
{ {
kprobe2_res = 11; kprobe2_res = 11;
return 0; return 0;
...@@ -56,11 +55,11 @@ int handle_kretprobe(struct pt_regs *ctx) ...@@ -56,11 +55,11 @@ int handle_kretprobe(struct pt_regs *ctx)
return 0; return 0;
} }
SEC("kretprobe/" SYS_PREFIX "sys_nanosleep") SEC("kretsyscall/nanosleep")
int BPF_KRETPROBE(handle_kretprobe_auto) int BPF_KRETPROBE(handle_kretprobe_auto, int ret)
{ {
kretprobe2_res = 22; kretprobe2_res = 22;
return 0; return ret;
} }
SEC("uprobe") SEC("uprobe")
......
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
static int (*bpf_missing_helper)(const void *arg1, int arg2) = (void *) 999; static int (*bpf_missing_helper)(const void *arg1, int arg2) = (void *) 999;
extern int LINUX_KERNEL_VERSION __kconfig; extern int LINUX_KERNEL_VERSION __kconfig;
extern int LINUX_UNKNOWN_VIRTUAL_EXTERN __kconfig __weak;
extern bool CONFIG_BPF_SYSCALL __kconfig; /* strong */ extern bool CONFIG_BPF_SYSCALL __kconfig; /* strong */
extern enum libbpf_tristate CONFIG_TRISTATE __kconfig __weak; extern enum libbpf_tristate CONFIG_TRISTATE __kconfig __weak;
extern bool CONFIG_BOOL __kconfig __weak; extern bool CONFIG_BOOL __kconfig __weak;
...@@ -22,6 +23,7 @@ extern const char CONFIG_STR[8] __kconfig __weak; ...@@ -22,6 +23,7 @@ extern const char CONFIG_STR[8] __kconfig __weak;
extern uint64_t CONFIG_MISSING __kconfig __weak; extern uint64_t CONFIG_MISSING __kconfig __weak;
uint64_t kern_ver = -1; uint64_t kern_ver = -1;
uint64_t unkn_virt_val = -1;
uint64_t bpf_syscall = -1; uint64_t bpf_syscall = -1;
uint64_t tristate_val = -1; uint64_t tristate_val = -1;
uint64_t bool_val = -1; uint64_t bool_val = -1;
...@@ -38,6 +40,7 @@ int handle_sys_enter(struct pt_regs *ctx) ...@@ -38,6 +40,7 @@ int handle_sys_enter(struct pt_regs *ctx)
int i; int i;
kern_ver = LINUX_KERNEL_VERSION; kern_ver = LINUX_KERNEL_VERSION;
unkn_virt_val = LINUX_UNKNOWN_VIRTUAL_EXTERN;
bpf_syscall = CONFIG_BPF_SYSCALL; bpf_syscall = CONFIG_BPF_SYSCALL;
tristate_val = CONFIG_TRISTATE; tristate_val = CONFIG_TRISTATE;
bool_val = CONFIG_BOOL; bool_val = CONFIG_BOOL;
......
// SPDX-License-Identifier: GPL-2.0 // SPDX-License-Identifier: GPL-2.0
#include "vmlinux.h"
#include <linux/ptrace.h>
#include <linux/bpf.h>
#include <netinet/in.h>
#include <bpf/bpf_helpers.h> #include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h> #include <bpf/bpf_tracing.h>
#include <bpf/bpf_core_read.h>
#include "bpf_misc.h" #include "bpf_misc.h"
static struct sockaddr_in old; static struct sockaddr_in old;
SEC("kprobe/" SYS_PREFIX "sys_connect") SEC("ksyscall/connect")
int BPF_KPROBE(handle_sys_connect) int BPF_KSYSCALL(handle_sys_connect, int fd, struct sockaddr_in *uservaddr, int addrlen)
{ {
#if SYSCALL_WRAPPER == 1
struct pt_regs *real_regs;
#endif
struct sockaddr_in new; struct sockaddr_in new;
void *ptr;
#if SYSCALL_WRAPPER == 0
ptr = (void *)PT_REGS_PARM2(ctx);
#else
real_regs = (struct pt_regs *)PT_REGS_PARM1(ctx);
bpf_probe_read_kernel(&ptr, sizeof(ptr), &PT_REGS_PARM2(real_regs));
#endif
bpf_probe_read_user(&old, sizeof(old), ptr); bpf_probe_read_user(&old, sizeof(old), uservaddr);
__builtin_memset(&new, 0xab, sizeof(new)); __builtin_memset(&new, 0xab, sizeof(new));
bpf_probe_write_user(ptr, &new, sizeof(new)); bpf_probe_write_user(uservaddr, &new, sizeof(new));
return 0; return 0;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment