gpt4 book ai didi

c - linux内核模块赋值从指针生成整数而不进行强制转换

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:37:58 24 4
gpt4 key购买 nike

这是简单的 sys_call_table Hook 代码

#include <asm/unistd.h>
#include <linux/autoconf.h>
#include <linux/in.h>
#include <linux/init_task.h>
#include <linux/ip.h>
#include <linux/kernel.h>
#include <linux/kmod.h>
#include <linux/mm.h>
#include <linux/module.h>
#include <linux/sched.h>
#include <linux/skbuff.h>
#include <linux/stddef.h>
#include <linux/string.h>
#include <linux/syscalls.h>
#include <linux/tcp.h>
#include <linux/types.h>
#include <linux/unistd.h>
#include <linux/version.h>
#include <linux/workqueue.h>

ssize_t *sys_call_table = (ssize_t *)0xc0026e04;

asmlinkage ssize_t (*orig_open)(const char *pathname, int flags);

asmlinkage ssize_t hacked_open(const char *pathname, int flags)
{
printk(KERN_INFO "SYS_OPEN called : %s\n", pathname);
return orig_open(pathname, flags);
}

int init_module(void)
{
orig_open = sys_call_table[__NR_open]; /* line 33 */
sys_call_table[__NR_open] = hacked_open; /* line 34 */
return 0;
}

void cleanup_module(void)
{
sys_call_table[__NR_open] = orig_open; /* line 40 */
}

MODULE_LICENSE("GPL");

我收到如下警告

这段代码工作正常,但我想删除警告。我该怎么办?

/home/tester/tools/lkm/a.c: In function 'init_module':
/home/tester/tools/lkm/a.c:33: warning: assignment makes integer from pointer without a cast
/home/tester/tools/lkm/a.c:34: warning: assignment makes integer from pointer without a cast
/home/tester/tools/lkm/a.c: In function 'cleanup_module':
/home/tester/tools/lkm/a.c:40: warning: assignment makes integer from pointer without a cast

最佳答案

如果你想让你的编译器沉默,你必须添加类型转换(即使这通常是一个坏主意,这就是你的编译器如何改变它)。

ssize_t *sys_call_table = (ssize_t *)0xc0026e04;

typedef ssize_t (*ftype)(const char *, int);

ftype orig_open;

ssize_t hacked_open(const char *pathname, int flags)
{
printf("SYS_OPEN called : %s\n", pathname);
return orig_open(pathname, flags);
}

int init_module(void)
{
orig_open = (ftype)sys_call_table[__NR_open];
sys_call_table[__NR_open] = (ssize_t)hacked_open;
return 0;
}

关于c - linux内核模块赋值从指针生成整数而不进行强制转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13323455/

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