c - LKM 从 netfilter 挂钩运行 call_usermodehelper
问题描述
我正在尝试通过使用 call_usermodehelper 从 linux 5.10 中的 linux 内核模块执行 linux 终端命令,该命令是从 netfilter 挂钩中调用的,但是由于我知道它在 softirq 上下文中运行,所以我似乎无法以任何一种方式执行它。使用“UMH_WAIT_EXEC”,我会scheduling while atomic: nc/16886/0x00000101
在发送我正在使用过滤器监视的 udp 数据包时得到,如果我使用“UMH_NO_WAIT”,我会得到一个空引用。代码看起来像这样:
static unsigned int hfunc(void *priv, struct sk_buff *skb, const struct nf_hook_state *state)
{
// --snipp--
if (ntohs(udph->dest) == 1337) {
char *argv[4];
char *envp[4];
argv[0] = "/bin/bash";
argv[1] = "-c";
argv[2] = "/bin/ls";
argv[3] = NULL;
envp[0] = "HOME=/";
envp[1] = "TERM=linux";
envp[2] = "PATH=/sbin:/usr/sbin:/bin:/usr/bin";
envp[3] = NULL;
call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
}
}
如何从 netfilter 钩子的 softirq 上下文运行该程序?
解决方案
我通过使用调度解决了这个问题。
static struct nf_hook_ops *nfho = NULL;
struct work_arg_struct {
struct work_struct work;
char *data;
};
static struct work_arg_struct my_work;
void bash_work_handler(struct work_struct *work){
struct work_arg_struct *work_arg;
work_arg = container_of(work, struct work_arg_struct, work);
// --snip--
call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
return;
}
static unsigned int hfunc(void *priv, struct sk_buff *skb, const struct nf_hook_state *state)
{
if (ntohs(udph->dest) == 1337) {
// setup arguments
schedule_work(&my_work.work);
}
}
--snip--
static int __init module_init(void)
{
//--snip--
INIT_WORK(&my_work.work, bash_work_handler);
}
推荐阅读
- windows - 无法在容器中运行最简单的命令
- sql-server - 从 AWS Sage Maker Studio 远程连接到 SQL Server
- arrays - 如何多阶数组多维
- python - 在python中排序txt文件时字符串索引超出范围
- database - 如何更改 MongoDB v3.4 maxWriteBatchSize 限制
- python - 为什么yolo检测不到图片
- wordpress - Google Compute Engine 中的 Wordpress 网站突然无法访问
- fft - Healpix / Healpy:我如何使用 healpy 对天空进行 SHT 处理?
- c - 我正在用 c 制作井字游戏,wincheck 功能但在下面的代码中不起作用
- vue.js - Vue 应用程序在浏览器中运行通过 CURL 抛出“Javascript not enabled”