gpt4 book ai didi

linux-kernel - strcmp 内核模块内部崩溃

转载 作者:行者123 更新时间:2023-12-01 23:02:55 30 4
gpt4 key购买 nike

我正在尝试检测内核(Netfilter)模块中的传出数据包。我正在使用 strcmp 函数来实现它。使用 strcmp 函数加载内核模块后,内核总是崩溃。我尝试删除 strcmp 函数 - 加载没有任何问题。我希望问题出在所有字符串函数上,我也尝试过 strstr() - 我的系统崩溃了

其背后的逻辑是,传入数据包将把 eth[0-9]+ 分配给“in->name”,“out->name”将被分配给传出数据包,反之亦然。

有什么见解可以检测传出数据包吗?我知道另一个选择是使用 output_hook 而不是预路由和后路由 Hook 。但在这里我想以不同的方式处理传入和传出的数据包。我使用的内核版本不支持模块内的字符串函数吗?

$ uname -a
Linux vmdsk01 2.6.32-21-generic #32-Ubuntu SMP Fri Apr 16 08:09:38 UTC 2010 x86_64 GNU/Linux

包含部分

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>

#include <linux/netfilter.h>
#include <linux/netfilter_ipv4.h>

#include <linux/skbuff.h>
#include <linux/inet.h>
#include <linux/ip.h>
#include <linux/tcp.h>
#include <net/ip.h>

#include <linux/string.h>

主钩

31 unsigned int main_hook(unsigned int hooknum,  
32 struct sk_buff *skb,
33 const struct net_device *in,
34 const struct net_device *out,
35 int (*okfn)(struct sk_buff*))
36 {
37 if( strcmp(out->name, "<NULL>") == NULL ) // Outgoing packet must not have <NULL>
38 {
39 printk( KERN_INFO "OUTGOING PACKET");
40 }
41 ....

我还尝试用以下内容替换第 37 行,我的系统挂起

37     if( strstr(out->name, "eth") != NULL ) // Outgoing packet must have eth[0-9]+ 

最佳答案

out 结构指针中可能有 NULL 指针。您可以在 main_hook 中添加一些健全性检查,例如:

unsigned int main_hook(unsigned int hooknum,  
struct sk_buff *skb,
const struct net_device *in,
const struct net_device *out,
int (*okfn)(struct sk_buff*))
{
if (!out)
return -EINVAL;

if( strncmp(out->name, "<NULL>", IFNAMSIZ) == 0 ) // Outgoing packet must not have <NULL>
{
printk( KERN_INFO "OUTGOING PACKET");
}
....

因此,我添加了对 out 指针的检查,并使用 strncmp 而不是 strcmp,其中 IFNAMSIZ 是大小include/linux/netdevice.h 中定义的 out->name 。另外,str(n)cmp 不会返回 NULL,而是返回 0

检查并请提供任何崩溃消息。

关于linux-kernel - strcmp 内核模块内部崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23731706/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com