Port ptrace library injection to C++ as a reusable library

Add a C ABI library (libptrace_inject), a thin CLI (ptrace-inject), and
C/C++ examples that inject a .so into a running x86-64 Linux process.

Injection flow:
- stop every thread with PTRACE_ATTACH, re-scanning /proc/<pid>/task for
  threads created while attaching
- patch a 6-byte shellcode (nop; nop; call r9; int3) into a private,
  file-backed executable mapping, then call target malloc/dlopen/free via
  PTRACE_SETREGS + PTRACE_CONT and read the result from rax
- write the library path with /proc/<pid>/mem, free it, restore the
  original bytes and registers, then PTRACE_DETACH all threads

Safety:
- validate before modifying anything: library path, libc/libdl build
  identity via device:inode, relocated symbol addresses are executable,
  patch site avoids stopped threads' RIPs, stack pointer is writable
- only accept the SIGTRAP whose RIP is shellcode+6
- forward unexpected signals back to the target on detach
- best-effort rollback of code and registers on failure
- shared library exports only the two public entry points

API: ptrace_inject_by_pid() and ptrace_inject_by_name(), with an optional
log callback and an error buffer; silent by default.

Build with CMake (no install rules); docs in README.md (EN) and
README.ZH.md (ZH).
This commit is contained in:
xiaozhou233 committed 2026-10-18 21:38:52 +08:00
commit b78aa07f78
27 files changed
+2141

No files matched your search

+47
View File
@@ -0,0 +1,47 @@
// C++ example: inject a shared library into a process found by name and print
// the resolved PID.
#include <cstdio>
#include "ptrace_inject.h"
static void on_log(int level, const char* message, void*) {
const char* prefix = "[*]";
switch (level) {
case PTRACE_INJECT_LOG_DEBUG:
prefix = "[debug]";
break;
case PTRACE_INJECT_LOG_WARN:
prefix = "[!]";
break;
case PTRACE_INJECT_LOG_ERROR:
prefix = "[error]";
break;
default:
break;
}
std::fprintf(stderr, "%s %s\n", prefix, message);
}
int main(int argc, char** argv) {
if (argc != 3) {
std::fprintf(stderr, "usage: %s <process-name> <library.so>\n", argv[0]);
return 1;
}
ptrace_inject_options options{};
options.log_fn = on_log;
pid_t resolved_pid = -1;
char error[512] = {};
const int result = ptrace_inject_by_name(
argv[1], argv[2], &options, &resolved_pid, error, sizeof(error));
if (result != PTRACE_INJECT_OK) {
std::fprintf(stderr, "inject failed: %s\n", error);
return 1;
}
std::printf("injected into pid %d\n", static_cast<int>(resolved_pid));
return 0;
}
+44
View File
@@ -0,0 +1,44 @@
/*
* Minimal C example: inject a shared library into a PID.
*
* Build against the installed library:
* cc inject_by_pid.c -lptrace_inject -o inject_by_pid
*
* Or against the build tree:
* cc inject_by_pid.c -I../include ../build/libptrace_inject.a -ldl -lstdc++
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ptrace_inject.h"
static void on_log(int level, const char* message, void* user_data) {
(void)user_data;
const char* prefix = level >= PTRACE_INJECT_LOG_WARN ? "[!]" : "[*]";
fprintf(stderr, "%s %s\n", prefix, message);
}
int main(int argc, char** argv) {
if (argc != 3) {
fprintf(stderr, "usage: %s <pid> <library.so>\n", argv[0]);
return 1;
}
ptrace_inject_options options;
memset(&options, 0, sizeof(options));
options.log_fn = on_log;
const long pid = strtol(argv[1], NULL, 10);
char error[512] = {0};
const int result =
ptrace_inject_by_pid((pid_t)pid, argv[2], &options, error, sizeof(error));
if (result != PTRACE_INJECT_OK) {
fprintf(stderr, "inject failed: %s\n",
error[0] != '\0' ? error : "(no error message)");
return 1;
}
return 0;
}
+26
View File
@@ -0,0 +1,26 @@
/*
* A sample shared library to inject. Its constructor runs inside the target
* process as soon as dlopen() loads it.
*
* Build (done automatically by the CMake examples target):
* cc -shared -fPIC -o ptrace_inject_sample.so examples/sample_library.c
*/
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#define SAMPLE_OUTPUT_FILE "/tmp/ptrace_inject_sample.txt"
__attribute__((constructor)) static void on_load(void) {
const char* message = "sample_library: constructor ran\n";
(void)!write(STDOUT_FILENO, message, strlen(message));
const int fd =
open(SAMPLE_OUTPUT_FILE, O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (fd >= 0) {
(void)!write(fd, message, strlen(message));
close(fd);
}
}