Skip to content

Commit

Permalink
feature:Prevent immediate Segmentation fault in probe_write and probe…
Browse files Browse the repository at this point in the history
…_read. We will get error-code when Getting fault in probe_write&read.
  • Loading branch information
Sy0307 committed Dec 16, 2024
1 parent 9603ebb commit b8bc301
Showing 1 changed file with 80 additions and 6 deletions.
86 changes: 80 additions & 6 deletions runtime/src/bpf_helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
#include <vector>
#include <bpftime_shm_internal.hpp>
#include <chrono>
#include <setjmp.h>
#include <signal.h>

#define PATH_MAX 4096

Expand Down Expand Up @@ -64,20 +66,92 @@ long bpftime_strncmp(const char *s1, uint64_t s1_sz, const char *s2)
return strncmp(s1, s2, s1_sz);
}

static sigjmp_buf jump_buffer;

// used for sigsetjmp to avoid faulting in probe_write_user & probe_read_user
void segv_handler(int signum)

Check warning on line 72 in runtime/src/bpf_helper.cpp

View check run for this annotation

Codecov / codecov/patch

runtime/src/bpf_helper.cpp#L72

Added line #L72 was not covered by tests
{
siglongjmp(jump_buffer, 1);

Check warning on line 74 in runtime/src/bpf_helper.cpp

View check run for this annotation

Codecov / codecov/patch

runtime/src/bpf_helper.cpp#L74

Added line #L74 was not covered by tests
}


uint64_t bpftime_probe_read(uint64_t dst, uint64_t size, uint64_t ptr, uint64_t,
uint64_t)
{
memcpy((void *)(uintptr_t)dst, (void *)(uintptr_t)ptr,
if(size<=0) {
return -EINVAL;

Check warning on line 82 in runtime/src/bpf_helper.cpp

View check run for this annotation

Codecov / codecov/patch

runtime/src/bpf_helper.cpp#L82

Added line #L82 was not covered by tests
}
struct sigaction sa, old_sa;
uint64_t ret = 0;

Check warning on line 85 in runtime/src/bpf_helper.cpp

View check run for this annotation

Codecov / codecov/patch

runtime/src/bpf_helper.cpp#L84-L85

Added lines #L84 - L85 were not covered by tests

sa.sa_handler = segv_handler; // set signal handler
sigemptyset(&sa.sa_mask); // clear signal set
sa.sa_flags = 0;

Check warning on line 89 in runtime/src/bpf_helper.cpp

View check run for this annotation

Codecov / codecov/patch

runtime/src/bpf_helper.cpp#L87-L89

Added lines #L87 - L89 were not covered by tests

if (sigaction(SIGSEGV, &sa, &old_sa) == -1) {
perror("sigaction");
return -EFAULT;

Check warning on line 93 in runtime/src/bpf_helper.cpp

View check run for this annotation

Codecov / codecov/patch

runtime/src/bpf_helper.cpp#L92-L93

Added lines #L92 - L93 were not covered by tests
}

if (sigsetjmp(jump_buffer, 1) == 0) {
// to avoid faulting for src & dst (which is NULL)
memcpy((void *)(uintptr_t)dst, (void *)(uintptr_t)ptr,

Check warning on line 98 in runtime/src/bpf_helper.cpp

View check run for this annotation

Codecov / codecov/patch

runtime/src/bpf_helper.cpp#L98

Added line #L98 was not covered by tests
(size_t)(uint32_t)(size));
return 0;
spdlog::debug("probe_read: dst={}, src={}, len={}", dst, ptr, size);

Check warning on line 100 in runtime/src/bpf_helper.cpp

View check run for this annotation

Codecov / codecov/patch

runtime/src/bpf_helper.cpp#L100

Added line #L100 was not covered by tests
} else {
spdlog::error("probe_read: failed to read from src={}", ptr);
ret = -EFAULT; // TO BE checked later

Check warning on line 103 in runtime/src/bpf_helper.cpp

View check run for this annotation

Codecov / codecov/patch

runtime/src/bpf_helper.cpp#L102-L103

Added lines #L102 - L103 were not covered by tests
}

// restore the origin signal handler
if (sigaction(SIGSEGV, &old_sa, NULL) == -1) {
perror("sigaction restore");

Check warning on line 108 in runtime/src/bpf_helper.cpp

View check run for this annotation

Codecov / codecov/patch

runtime/src/bpf_helper.cpp#L108

Added line #L108 was not covered by tests
// if failed to restore, return -EFAULT
return -EFAULT;

Check warning on line 110 in runtime/src/bpf_helper.cpp

View check run for this annotation

Codecov / codecov/patch

runtime/src/bpf_helper.cpp#L110

Added line #L110 was not covered by tests
}

return ret;

Check warning on line 113 in runtime/src/bpf_helper.cpp

View check run for this annotation

Codecov / codecov/patch

runtime/src/bpf_helper.cpp#L113

Added line #L113 was not covered by tests
}




uint64_t bpftime_probe_write_user(uint64_t dst, uint64_t src, uint64_t len,
uint64_t, uint64_t)
uint64_t arg4, uint64_t arg5)
{
memcpy((void *)(uintptr_t)dst, (void *)(uintptr_t)src,
(size_t)(uint32_t)(len));
return 0;
if(len<=0) {
return -EINVAL;

Check warning on line 123 in runtime/src/bpf_helper.cpp

View check run for this annotation

Codecov / codecov/patch

runtime/src/bpf_helper.cpp#L123

Added line #L123 was not covered by tests
}
struct sigaction sa, old_sa;
uint64_t ret = 0;

Check warning on line 126 in runtime/src/bpf_helper.cpp

View check run for this annotation

Codecov / codecov/patch

runtime/src/bpf_helper.cpp#L125-L126

Added lines #L125 - L126 were not covered by tests

sa.sa_handler = segv_handler; // set signal handler
sigemptyset(&sa.sa_mask); // clear signal set
sa.sa_flags = 0;

Check warning on line 130 in runtime/src/bpf_helper.cpp

View check run for this annotation

Codecov / codecov/patch

runtime/src/bpf_helper.cpp#L128-L130

Added lines #L128 - L130 were not covered by tests

if (sigaction(SIGSEGV, &sa, &old_sa) == -1) {
perror("sigaction");
return -EFAULT;

Check warning on line 134 in runtime/src/bpf_helper.cpp

View check run for this annotation

Codecov / codecov/patch

runtime/src/bpf_helper.cpp#L133-L134

Added lines #L133 - L134 were not covered by tests
}

if (sigsetjmp(jump_buffer, 1) == 0) {
// to avoid faulting for src & dst (which is NULL)
memcpy((void *)(uintptr_t)dst, (void *)(uintptr_t)src,

Check warning on line 139 in runtime/src/bpf_helper.cpp

View check run for this annotation

Codecov / codecov/patch

runtime/src/bpf_helper.cpp#L139

Added line #L139 was not covered by tests
(size_t)(uint32_t)(len));
spdlog::debug("probe_write_user: dst={}, src={}, len={}", dst, src, len);

Check warning on line 141 in runtime/src/bpf_helper.cpp

View check run for this annotation

Codecov / codecov/patch

runtime/src/bpf_helper.cpp#L141

Added line #L141 was not covered by tests
} else {
spdlog::error("probe_write_user: failed to write to dst={}", dst);
ret = -EFAULT; // TO BE checked later

Check warning on line 144 in runtime/src/bpf_helper.cpp

View check run for this annotation

Codecov / codecov/patch

runtime/src/bpf_helper.cpp#L143-L144

Added lines #L143 - L144 were not covered by tests
}

// restore the origin signal handler
if (sigaction(SIGSEGV, &old_sa, NULL) == -1) {
perror("sigaction restore");

Check warning on line 149 in runtime/src/bpf_helper.cpp

View check run for this annotation

Codecov / codecov/patch

runtime/src/bpf_helper.cpp#L149

Added line #L149 was not covered by tests
// if failed to restore, return -EFAULT
return -EFAULT;

Check warning on line 151 in runtime/src/bpf_helper.cpp

View check run for this annotation

Codecov / codecov/patch

runtime/src/bpf_helper.cpp#L151

Added line #L151 was not covered by tests
}

return ret;

Check warning on line 154 in runtime/src/bpf_helper.cpp

View check run for this annotation

Codecov / codecov/patch

runtime/src/bpf_helper.cpp#L154

Added line #L154 was not covered by tests
}

uint64_t bpftime_get_prandom_u32()
Expand Down

0 comments on commit b8bc301

Please sign in to comment.