1
votes

this is simple sys_call_table hooking code

#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");

i got an warning like below

this code works fine but i want to delete warnings. how can i do?

/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

2
@alk: I've added markers for those line numbers . . . though really, both of those functions are short enough that the markers aren't even needed. - ruakh
Thanks. I feel marking the lines is part of the minimum an OP could provide without any major efforts, when expecting help. - alk

2 Answers

0
votes

If you want to silence your compiler, you have to add typecasts (even if it is often a bad idea, this is how your compiler turns it).

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;
}
0
votes

When you look at line 33 you will see the problem:

orig_open = sys_call_table[__NR_open];

You have defined sys_call_table to be a pointer to integer. That's the reason for the warning. Same problem with the other lines. If you define sys_call_table properly, the warnings will go away.

You should at least define it as an array of pointers or pointer to pointers, because if ssize_t is only 32 bit on 64 bit system, you might truncate the 64 bit addresses to a 32 bit integer.