Dynamic System Calls & System Call Hijacking - demo user program

Zoltan Menyhart
2004-04-20T09:08:51Z

[-- Attachment #1: Type: text/plain, Size: 1 bytes --]

[-- Attachment #2: test.c --]
[-- Type: text/plain, Size: 1329 bytes --]

#include <linux/sys.h> /* For NR_syscalls */
#include <asm/unistd.h> /* For __NR_ni_syscall */
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <asm/fcntl.h> /* For O_RDONLY */

#define MY_SYSCALL "/proc/sys/kernel/dynamic_syscalls/foo"

/*
* Read out my actual system call number from "/proc/...".
*
* On error "-1" is returned and "errno" is set accordingly.
*/
static inline
get_my_syscall_no(void)
{
int fd;
int tmp;
char buff[5]; /* Should be enough :-) */

if ((fd = open(MY_SYSCALL, O_RDONLY)) < 0){
errno = ENOSYS;
return -1;
}
tmp = read(fd, buff, sizeof buff - 1);
close(fd);
if (tmp != sizeof buff - 1){
errno = ENOSYS;
return -1;
}
buff[sizeof buff - 1] = '\0';
tmp = atoi(buff);
if (tmp < __NR_ni_syscall || tmp >= __NR_ni_syscall + NR_syscalls){
errno = ENOSYS;
return -1;
}
return tmp;
}

/*
* Wrapper function for my system call.
*/
long
my_syscall(const int arg, const long arg2, const long arg3, const int arg4,
const int arg5)
{
static int syscall_no = -1;

if (syscall_no == -1)
if ((syscall_no = get_my_syscall_no())== -1)
return -1;
return syscall(syscall_no, arg, arg2, arg3, arg4, arg5);
}

main()
{
if (my_syscall(1, 0, 1, 0, 2) == -1)
perror("my syscall");
if (my_syscall(2, 3, 4, 5, 6) == -1)
perror("my syscall");
}