c - 使用 BPF 工具对“struct mnt_namespace”类型的不完整定义和前向声明?
问题描述
我想要的是:
向密件抄送工具添加网络名称空间选项以execsnoop
仅跟踪具有指定网络名称空间的日志,就像我们在许多其他密件抄送工具中具有过滤器 PID 选项一样。例如:execsnoop -N "ns_id"
我尝试了什么:
int syscall__execve(struct pt_regs *ctx,
const char __user *filename,
const char __user *const __user *__argv,
const char __user *const __user *__envp)
{
// create data here and pass to submit_arg to save stack space (#555)
struct data_t data = {};
struct task_struct *task;
data.pid = bpf_get_current_pid_tgid() >> 32;
task = (struct task_struct *)bpf_get_current_task();
// Some kernels, like Ubuntu 4.13.0-generic, return 0
// as the real_parent->tgid.
// We use the get_ppid function as a fallback in those cases. (#1883)
data.ppid = task->real_parent->tgid;
data.netns = task->nsproxy->mnt_ns->ns.inum; // I tried to mount namespace here
bpf_get_current_comm(&data.comm, sizeof(data.comm));
data.type = EVENT_ARG;
__submit_arg(ctx, (void *)filename, &data);
// skip first arg, as we submitted filename
#pragma unroll
for (int i = 1; i < MAXARG; i++) {
if (submit_arg(ctx, (void *)&__argv[i], &data) == 0)
goto out;
}
// handle truncated argument list
char ellipsis[] = "...";
__submit_arg(ctx, (void *)ellipsis, &data);
out:
return 0;
}
收到错误:
/virtual/main.c:98:39: error: incomplete definition of type 'struct mnt_namespace'
data.netns = task->nsproxy->mnt_ns->ns.inum;
~~~~~~~~~~~~~~~~~~~~~^
include/linux/nsproxy.h:8:8: note: forward declaration of 'struct mnt_namespace'
struct mnt_namespace;
^
1 error generated.
Traceback (most recent call last):
File "./execsnoop", line 230, in <module>
b = BPF(text=bpf_text)
File "/usr/lib/python2.7/dist-packages/bcc/__init__.py", line 325, in __init__
raise Exception("Failed to compile BPF text")
Exception: Failed to compile BPF text
我也尝试包含 mnt_namespace.h 头文件,但没有解决。
解决方案
密件抄送工具mountsnoop.py
似乎可以完成您想要实现的目标。他们重新定义了一些结构,并带有以下注释:
/*
* XXX: struct mnt_namespace is defined in fs/mount.h, which is private to the
* VFS and not installed in any kernel-devel packages. So, let's duplicate the
* important part of the definition. There are actually more members in the
* real struct, but we don't need them, and they're more likely to change.
*/
struct mnt_namespace {
atomic_t count;
struct ns_common ns;
};
实际上fs/mount.h中的结构没有导出。所以我怀疑你只需要在你的代码中做同样的事情:部分重新定义struct mnt_namespace
。
推荐阅读
- xml - 使用元素名称获取子值
- javascript - 当用户登陆页面时,如何调整此代码以按地理位置填充地址
- javascript - t.substring 不是 ApexCharts 中的函数
- python - 通过 Python 更新 PostgreSQL 中的行 - pandas.to_sql:更新?
- ruby-on-rails - 从 Ruby on Rails 以 sudo 形式读取文件
- ruby-on-rails - 如何显示通过环境变量配置的 ruby 垃圾收集设置
- java - A* 寻路问题处理(Java)
- pandas - 将 pandas 数据框导出到 Excel 工作表
- ruby - Marshal::load 中“proc”参数的目的是什么?
- javascript - 仅在一个组件上反应切换类