Skip to content

fix: fix segmentation fault about bashreadline #288

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define _BPFTIME_FRIDA_ATTACH_PRIVATE_DATA_HPP
#include "attach_private_data.hpp"
#include <cstdint>
#include <string>
namespace bpftime
{
namespace attach
Expand All @@ -10,6 +11,8 @@ namespace attach
struct frida_attach_private_data final : public attach_private_data {
// The address to hook
uint64_t addr;
// Saved module name
std::string module_name;
// The input string should be: Either an decimal integer in string format, indicating the function address to hook. Or in format of NAME:OFFSET, where NAME is the module name (empty is ok), OFFSET is the module offset
int initialize_from_string(const std::string_view &sv) override;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ int frida_attach_private_data::initialize_from_string(const std::string_view &sv
addr = (uintptr_t)resolve_function_addr_by_module_offset(
module_part, std::stoul(offset_part));
SPDLOG_DEBUG("Resolved address: {:x} from string {}", addr, sv);
this->module_name = module_part;
}

return 0;
Expand Down
56 changes: 55 additions & 1 deletion attach/frida_uprobe_attach_impl/src/frida_uprobe_attach_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
#include "spdlog/spdlog.h"
#include <algorithm>
#include <cerrno>
#include <cstdio>
#include <filesystem>
#include <fstream>
#include <memory>
#include <string>
#include <typeinfo>
#include <utility>
#include <unistd.h>
Expand Down Expand Up @@ -39,6 +43,10 @@ int frida_attach_impl::attach_at_with_ebpf_callback(void *func_addr,
int frida_attach_impl::attach_at(void *func_addr,
frida_attach_entry_callback &&cb)
{
if (func_addr == nullptr) {
SPDLOG_ERROR("Unable to attach uprobes to address 0");
return -EINVAL;
}
auto itr = internal_attaches.find(func_addr);
int current_attach_type;
if (std::holds_alternative<callback_variant>(cb)) {
Expand Down Expand Up @@ -165,7 +173,53 @@ int frida_attach_impl::create_attach_with_ebpf_callback(
try {
auto &sub = dynamic_cast<const frida_attach_private_data &>(
private_data);

SPDLOG_DEBUG(
"Attaching with ebpf callback, private data offset={:x}, module name={}",
sub.addr, sub.module_name);
// Check if module path exists in the current process's map
// Only check if the module_name is not empty. If it's empty, it
// means we won't rely on module_name
if (!sub.module_name.empty()) {
bool ok = false;
std::ifstream ifs("/proc/self/maps");
std::string line;
while (ifs) {
std::getline(ifs, line);
SPDLOG_DEBUG("Checking map line {}", line);
char *module_path;
if (sscanf(line.c_str(), "%*s%*s%*s%*s%*s%ms",
&module_path) == 1) {
std::string curr_module(module_path);
free(module_path);
SPDLOG_DEBUG("Checking {}",
curr_module);
if (std::filesystem::exists(
curr_module)) {
bool matched = std::filesystem::
equivalent(
sub.module_name,
curr_module);
SPDLOG_DEBUG(
"Checked {}, matched={}",
curr_module, matched);
if (matched) {
ok = true;
break;
}
} else {
SPDLOG_DEBUG(
"{} doesn't exist, skipped",
curr_module);
}
}
}
if (!ok) {
SPDLOG_ERROR(
"Unable to attach: module name {} doesn't exist in current process's memory maps",
sub.module_name);
return -EINVAL;
}
}
ebpf_callback_args args{ .ebpf_cb = cb,
.attach_type = attach_type };
if (attach_type == ATTACH_UPROBE ||
Expand Down
4 changes: 2 additions & 2 deletions tools/bpftimetool/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ int bpftime_run_ebpf_program(int id,
const handler_manager *manager =
shm_holder.global_shared_memory.get_manager();
size_t handler_size = manager->size();
if (id >= handler_size || id < 0) {
if ((size_t) id >= handler_size || id < 0) {
cerr << "Invalid id " << id << " not exist" << endl;
return 1;
}
Expand Down Expand Up @@ -215,4 +215,4 @@ int main(int argc, char *argv[])
return 1;
}
return EXIT_SUCCESS;
}
}