Skip to content

Commit

Permalink
add syscall override retuyrn value
Browse files Browse the repository at this point in the history
  • Loading branch information
yunwei37 committed Nov 22, 2023
1 parent 89e1565 commit 8255576
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 29 deletions.
4 changes: 2 additions & 2 deletions example/error-inject/error_inject_syscall.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ int do_override_inject_syscall_sys_enter_openat(
{
int rand = bpf_get_prandom_u32();
if (rand % 2 == 0) {
bpf_printk("bpf: Inject error. Target func will not exec.\n");
bpf_printk("bpf: Inject error. Open will failed.\n");
bpf_override_return(ctx, -1);
return 0;
}
Expand All @@ -23,7 +23,7 @@ int do_override_inject_syscall_sys_enter_open(
{
int rand = bpf_get_prandom_u32();
if (rand % 2 == 0) {
bpf_printk("bpf: Inject error. Target func will not exec.\n");
bpf_printk("bpf: Inject error. Open will failed.\n");
bpf_override_return(ctx, -1);
return 0;
}
Expand Down
6 changes: 4 additions & 2 deletions example/error-inject/victim.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
int target_func()
{
int res = open("/dev/null");
printf("target_func is running\n");
close(res);
printf("target_func is running, open res = %d\n", res);
if (res > 0) {
close(res);
}
return 0;
}

Expand Down
2 changes: 1 addition & 1 deletion runtime/src/attach/attach_manager/base_attach_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ extern "C" uint64_t bpftime_override_return(uint64_t ctx, uint64_t value)
curr_thread_override_return_callback.value()(ctx, value);
} else {
SPDLOG_ERROR(
"Called bpftime_set_retval, but no retval callback was set");
"Called bpftime_override_return, but no retval callback was set");
assert(false);
}
return 0;
Expand Down
71 changes: 47 additions & 24 deletions runtime/src/attach/bpf_attach_syscall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,16 @@ int bpf_attach_ctx::create_tracepoint(int tracepoint_id, int perf_fd,
}
if (progs.empty()) {
SPDLOG_ERROR("bpf_link for perf event {} not found",
perf_fd);
perf_fd);
return perf_fd;
}
const auto &name = itr->second;
if (name.starts_with("sys_enter_")) {
auto syscall_name = name.substr(10);
auto syscall_id = id_table.find(syscall_name);
if (syscall_id == id_table.end()) {
SPDLOG_ERROR(
"Syscall id not found for name {}",
syscall_name);
SPDLOG_ERROR("Syscall id not found for name {}",
syscall_name);
return -1;
}
for (auto p : progs)
Expand All @@ -80,9 +79,8 @@ int bpf_attach_ctx::create_tracepoint(int tracepoint_id, int perf_fd,
auto syscall_name = name.substr(9);
auto syscall_id = id_table.find(syscall_name);
if (syscall_id == id_table.end()) {
SPDLOG_ERROR(
"Syscall id not found for name {}",
syscall_name);
SPDLOG_ERROR("Syscall id not found for name {}",
syscall_name);
return -1;
}
for (auto p : progs)
Expand All @@ -107,7 +105,7 @@ int bpf_attach_ctx::create_tracepoint(int tracepoint_id, int perf_fd,
return perf_fd;
} else {
SPDLOG_ERROR("Unexpected syscall tracepoint name {}",
name);
name);
return -1;
}
} else {
Expand Down Expand Up @@ -179,8 +177,16 @@ int64_t bpf_attach_ctx::run_syscall_hooker(int64_t sys_nr, int64_t arg1,
if (sys_nr == __NR_exit_group || sys_nr == __NR_exit)
return orig_syscall(sys_nr, arg1, arg2, arg3, arg4, arg5, arg6);
SPDLOG_DEBUG("Syscall callback {} {} {} {} {} {} {}", sys_nr, arg1,
arg2, arg3, arg4, arg5, arg6);
curr_thread_override_return_callback =
arg2, arg3, arg4, arg5, arg6);
bool is_overrided = false;
uint64_t user_ret = 0;
uint64_t user_ret_ctx = 0;
curr_thread_override_return_callback =
override_return_set_callback([&](uint64_t ctx, uint64_t v) {
is_overrided = true;
user_ret = v;
user_ret_ctx = ctx;
});
if (!sys_enter_progs[sys_nr].empty() ||
!global_sys_enter_progs.empty()) {
trace_event_raw_sys_enter ctx;
Expand All @@ -192,40 +198,57 @@ int64_t bpf_attach_ctx::run_syscall_hooker(int64_t sys_nr, int64_t arg1,
ctx.args[3] = arg4;
ctx.args[4] = arg5;
ctx.args[5] = arg6;
const auto exec = [&](const bpftime_prog *prog) {
for (const auto prog : sys_enter_progs[sys_nr]) {
SPDLOG_DEBUG("Call {}", prog->prog_name());
auto lctx = ctx;
// Avoid polluting other ebpf programs..
uint64_t ret;
int err = prog->bpftime_prog_exec(&lctx, sizeof(lctx),
&ret);
assert(err >= 0);
};
for (const auto &item : sys_enter_progs[sys_nr]) {
exec(item);
SPDLOG_DEBUG("ret {}", ret);
}
for (auto item : global_sys_enter_progs) {
exec(item);
for (const auto prog : global_sys_enter_progs) {
SPDLOG_DEBUG("Call {}", prog->prog_name());
auto lctx = ctx;
// Avoid polluting other ebpf programs..
uint64_t ret;
int err = prog->bpftime_prog_exec(&lctx, sizeof(lctx),
&ret);
SPDLOG_DEBUG("ret {}", ret);
}
}
if (is_overrided) {
curr_thread_override_return_callback.reset();
return user_ret;
}
int64_t ret = orig_syscall(sys_nr, arg1, arg2, arg3, arg4, arg5, arg6);
if (!sys_exit_progs[sys_nr].empty() || !global_sys_exit_progs.empty()) {
trace_event_raw_sys_exit ctx;
memset(&ctx, 0, sizeof(ctx));
ctx.id = sys_nr;
ctx.ret = ret;
const auto exec = [&](const bpftime_prog *prog) {
for (const auto prog : sys_exit_progs[sys_nr]) {
SPDLOG_DEBUG("Call {}", prog->prog_name());
auto lctx = ctx;
// Avoid polluting other ebpf programs..
uint64_t ret;
int err = prog->bpftime_prog_exec(&lctx, sizeof(lctx),
&ret);
SPDLOG_DEBUG("ret {}", ret);
}
for (const auto prog : global_sys_exit_progs) {
SPDLOG_DEBUG("Call {}", prog->prog_name());
auto lctx = ctx;
// Avoid polluting other ebpf programs..
uint64_t ret;
int err = prog->bpftime_prog_exec(&lctx, sizeof(lctx),
&ret);
assert(err >= 0);
};
for (const auto &item : sys_exit_progs[sys_nr])
exec(item);
for (auto item : global_sys_exit_progs)
exec(item);
SPDLOG_DEBUG("ret {}", ret);
}
}
if (is_overrided) {
curr_thread_override_return_callback.reset();
return user_ret;
}
return ret;
}
Expand Down

0 comments on commit 8255576

Please sign in to comment.