我正在尝试编写一个eBPF程序,用于记录系统上运行的容器中每个特定系统调用的调用。我正在使用bcc并可以使用bpf_get_current_pid_tgid()
检索PID。
从用户空间我可以检查proc文件系统,以确定进程的命名空间是否与根命名空间不同,以猜测它是否是容器进程,但我不知道你是怎么做的内核空间?
答案 0 :(得分:3)
您可以使用(仅限Linux 4.8+)bpf_get_current_task
帮助程序来检索当前进程的struct task_struct
。然后容器内的进程看到的PID在t->nsproxy->pid_ns_for_children->last_pid
。
以下显示了跟踪execve
系统调用时的操作(您可以在容器中使用top
来检查upid是否正确):
from bcc import BPF
BPF(text="""
#include <linux/pid_namespace.h>
int kprobe__sys_execve(void *ctx) {
u32 pid = bpf_get_current_pid_tgid();
struct task_struct *t = (struct task_struct *)bpf_get_current_task();
u32 upid = t->nsproxy->pid_ns_for_children->last_pid;
bpf_trace_printk("pid=%d; upid=%d!\\n", pid, upid);
return 0;
}
""").trace_print()
以下差异(基于a44d26ed3)扩展了bcc的execsnoop.py
来检索upid:
diff --git a/tools/execsnoop.py b/tools/execsnoop.py
index 5711fd1..2134f69 100755
--- a/tools/execsnoop.py
+++ b/tools/execsnoop.py
@@ -53,6 +53,7 @@ bpf_text = """
#include <uapi/linux/ptrace.h>
#include <linux/sched.h>
#include <linux/fs.h>
+#include <linux/pid_namespace.h>
#define ARGSIZE 128
@@ -63,6 +64,7 @@ enum event_type {
struct data_t {
u32 pid; // PID as in the userspace term (i.e. task->tgid in kernel)
+ u32 upid;
char comm[TASK_COMM_LEN];
enum event_type type;
char argv[ARGSIZE];
@@ -119,6 +121,8 @@ int kretprobe__sys_execve(struct pt_regs *ctx)
{
struct data_t data = {};
data.pid = bpf_get_current_pid_tgid() >> 32;
+ struct task_struct *t = (struct task_struct *)bpf_get_current_task();
+ data.upid = t->nsproxy->pid_ns_for_children->last_pid;
bpf_get_current_comm(&data.comm, sizeof(data.comm));
data.type = EVENT_RET;
data.retval = PT_REGS_RC(ctx);
@@ -134,7 +138,7 @@ b = BPF(text=bpf_text.replace("MAXARG", args.max_args))
# header
if args.timestamp:
print("%-8s" % ("TIME(s)"), end="")
-print("%-16s %-6s %-6s %3s %s" % ("PCOMM", "PID", "PPID", "RET", "ARGS"))
+print("%-16s %-6s %-6s %-6s %3s %s" % ("PCOMM", "PID", "UPID", "PPID", "RET", "ARGS"))
TASK_COMM_LEN = 16 # linux/sched.h
ARGSIZE = 128 # should match #define in C above
@@ -142,6 +146,7 @@ ARGSIZE = 128 # should match #define in C above
class Data(ct.Structure):
_fields_ = [
("pid", ct.c_uint),
+ ("upid", ct.c_uint),
("comm", ct.c_char * TASK_COMM_LEN),
("type", ct.c_int),
("argv", ct.c_char * ARGSIZE),
@@ -189,8 +194,8 @@ def print_event(cpu, data, size):
if args.timestamp:
print("%-8.3f" % (time.time() - start_ts), end="")
ppid = get_ppid(event.pid)
- print("%-16s %-6s %-6s %3s %s" % (event.comm.decode(), event.pid,
- ppid if ppid > 0 else "?", event.retval,
+ print("%-16s %-6s %-6s %-6s %3s %s" % (event.comm.decode(), event.pid,
+ event.upid, ppid if ppid > 0 else "?", event.retval,
b' '.join(argv[event.pid]).decode()))
try:
del(argv[event.pid])