-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'Fix the incorrect register read for syscalls on x86_64'
Kenta Tada says: ==================== Currently, rcx is read as the fourth parameter of syscall on x86_64. But x86_64 Linux System Call convention uses r10 actually. This commit adds the wrapper for users who want to access to syscall params to analyze the user space. Changelog: ---------- v1 -> v2: - Rebase to current bpf-next https://lore.kernel.org/bpf/20211222213924.1869758-1-andrii@kernel.org/ v2 -> v3: - Modify the definition of SYSCALL macros for only targeted archs. - Define __BPF_TARGET_MISSING variants for completeness. - Remove CORE variants. These macros will not be used. - Add a selftest. v3 -> v4: - Modify a selftest not to use serial tests. - Modify a selftest to use ASSERT_EQ(). - Extract syscall wrapper for all the other tests. - Add CORE variants. v4 -> v5: - Modify the CORE variant macro not to read memory directly. - Remove the unnecessary comment. - Add a selftest for the CORE variant. ==================== Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
- Loading branch information
Showing
5 changed files
with
173 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
tools/testing/selftests/bpf/prog_tests/test_bpf_syscall_macro.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// SPDX-License-Identifier: GPL-2.0 | ||
/* Copyright 2022 Sony Group Corporation */ | ||
#include <sys/prctl.h> | ||
#include <test_progs.h> | ||
#include "bpf_syscall_macro.skel.h" | ||
|
||
void test_bpf_syscall_macro(void) | ||
{ | ||
struct bpf_syscall_macro *skel = NULL; | ||
int err; | ||
int exp_arg1 = 1001; | ||
unsigned long exp_arg2 = 12; | ||
unsigned long exp_arg3 = 13; | ||
unsigned long exp_arg4 = 14; | ||
unsigned long exp_arg5 = 15; | ||
|
||
/* check whether it can open program */ | ||
skel = bpf_syscall_macro__open(); | ||
if (!ASSERT_OK_PTR(skel, "bpf_syscall_macro__open")) | ||
return; | ||
|
||
skel->rodata->filter_pid = getpid(); | ||
|
||
/* check whether it can load program */ | ||
err = bpf_syscall_macro__load(skel); | ||
if (!ASSERT_OK(err, "bpf_syscall_macro__load")) | ||
goto cleanup; | ||
|
||
/* check whether it can attach kprobe */ | ||
err = bpf_syscall_macro__attach(skel); | ||
if (!ASSERT_OK(err, "bpf_syscall_macro__attach")) | ||
goto cleanup; | ||
|
||
/* check whether args of syscall are copied correctly */ | ||
prctl(exp_arg1, exp_arg2, exp_arg3, exp_arg4, exp_arg5); | ||
ASSERT_EQ(skel->bss->arg1, exp_arg1, "syscall_arg1"); | ||
ASSERT_EQ(skel->bss->arg2, exp_arg2, "syscall_arg2"); | ||
ASSERT_EQ(skel->bss->arg3, exp_arg3, "syscall_arg3"); | ||
/* it cannot copy arg4 when uses PT_REGS_PARM4 on x86_64 */ | ||
#ifdef __x86_64__ | ||
ASSERT_NEQ(skel->bss->arg4_cx, exp_arg4, "syscall_arg4_from_cx"); | ||
#else | ||
ASSERT_EQ(skel->bss->arg4_cx, exp_arg4, "syscall_arg4_from_cx"); | ||
#endif | ||
ASSERT_EQ(skel->bss->arg4, exp_arg4, "syscall_arg4"); | ||
ASSERT_EQ(skel->bss->arg5, exp_arg5, "syscall_arg5"); | ||
|
||
/* check whether args of syscall are copied correctly for CORE variants */ | ||
ASSERT_EQ(skel->bss->arg1_core, exp_arg1, "syscall_arg1_core_variant"); | ||
ASSERT_EQ(skel->bss->arg2_core, exp_arg2, "syscall_arg2_core_variant"); | ||
ASSERT_EQ(skel->bss->arg3_core, exp_arg3, "syscall_arg3_core_variant"); | ||
/* it cannot copy arg4 when uses PT_REGS_PARM4_CORE on x86_64 */ | ||
#ifdef __x86_64__ | ||
ASSERT_NEQ(skel->bss->arg4_core_cx, exp_arg4, "syscall_arg4_from_cx_core_variant"); | ||
#else | ||
ASSERT_EQ(skel->bss->arg4_core_cx, exp_arg4, "syscall_arg4_from_cx_core_variant"); | ||
#endif | ||
ASSERT_EQ(skel->bss->arg4_core, exp_arg4, "syscall_arg4_core_variant"); | ||
ASSERT_EQ(skel->bss->arg5_core, exp_arg5, "syscall_arg5_core_variant"); | ||
|
||
cleanup: | ||
bpf_syscall_macro__destroy(skel); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/* SPDX-License-Identifier: GPL-2.0 */ | ||
#ifndef __BPF_MISC_H__ | ||
#define __BPF_MISC_H__ | ||
|
||
#if defined(__TARGET_ARCH_x86) | ||
#define SYSCALL_WRAPPER 1 | ||
#define SYS_PREFIX "__x64_" | ||
#elif defined(__TARGET_ARCH_s390) | ||
#define SYSCALL_WRAPPER 1 | ||
#define SYS_PREFIX "__s390x_" | ||
#elif defined(__TARGET_ARCH_arm64) | ||
#define SYSCALL_WRAPPER 1 | ||
#define SYS_PREFIX "__arm64_" | ||
#else | ||
#define SYSCALL_WRAPPER 0 | ||
#define SYS_PREFIX "" | ||
#endif | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// SPDX-License-Identifier: GPL-2.0 | ||
/* Copyright 2022 Sony Group Corporation */ | ||
#include <vmlinux.h> | ||
|
||
#include <bpf/bpf_core_read.h> | ||
#include <bpf/bpf_helpers.h> | ||
#include <bpf/bpf_tracing.h> | ||
#include "bpf_misc.h" | ||
|
||
int arg1 = 0; | ||
unsigned long arg2 = 0; | ||
unsigned long arg3 = 0; | ||
unsigned long arg4_cx = 0; | ||
unsigned long arg4 = 0; | ||
unsigned long arg5 = 0; | ||
|
||
int arg1_core = 0; | ||
unsigned long arg2_core = 0; | ||
unsigned long arg3_core = 0; | ||
unsigned long arg4_core_cx = 0; | ||
unsigned long arg4_core = 0; | ||
unsigned long arg5_core = 0; | ||
|
||
const volatile pid_t filter_pid = 0; | ||
|
||
SEC("kprobe/" SYS_PREFIX "sys_prctl") | ||
int BPF_KPROBE(handle_sys_prctl) | ||
{ | ||
struct pt_regs *real_regs; | ||
pid_t pid = bpf_get_current_pid_tgid() >> 32; | ||
|
||
if (pid != filter_pid) | ||
return 0; | ||
|
||
real_regs = (struct pt_regs *)PT_REGS_PARM1(ctx); | ||
|
||
/* test for PT_REGS_PARM */ | ||
bpf_probe_read_kernel(&arg1, sizeof(arg1), &PT_REGS_PARM1_SYSCALL(real_regs)); | ||
bpf_probe_read_kernel(&arg2, sizeof(arg2), &PT_REGS_PARM2_SYSCALL(real_regs)); | ||
bpf_probe_read_kernel(&arg3, sizeof(arg3), &PT_REGS_PARM3_SYSCALL(real_regs)); | ||
bpf_probe_read_kernel(&arg4_cx, sizeof(arg4_cx), &PT_REGS_PARM4(real_regs)); | ||
bpf_probe_read_kernel(&arg4, sizeof(arg4), &PT_REGS_PARM4_SYSCALL(real_regs)); | ||
bpf_probe_read_kernel(&arg5, sizeof(arg5), &PT_REGS_PARM5_SYSCALL(real_regs)); | ||
|
||
/* test for the CORE variant of PT_REGS_PARM */ | ||
arg1_core = PT_REGS_PARM1_CORE_SYSCALL(real_regs); | ||
arg2_core = PT_REGS_PARM2_CORE_SYSCALL(real_regs); | ||
arg3_core = PT_REGS_PARM3_CORE_SYSCALL(real_regs); | ||
arg4_core_cx = PT_REGS_PARM4_CORE(real_regs); | ||
arg4_core = PT_REGS_PARM4_CORE_SYSCALL(real_regs); | ||
arg5_core = PT_REGS_PARM5_CORE_SYSCALL(real_regs); | ||
|
||
return 0; | ||
} | ||
|
||
char _license[] SEC("license") = "GPL"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters