Execute code in kernel mode.


 
Thread Tools Search this Thread
Top Forums Programming Execute code in kernel mode.
# 8  
Old 03-14-2011
You're not writing this in C++, are you? Your gettimeofday function will be named some crazy operator-overloaded C++ hashed name.

[edit] Add this exact code to libkeepalive.c:

Code:
// below #include <netinet/tcp.h>
#include <sys/time.h>

int gettimeofday(struct timeval *tv, struct timezone *tz)
{
        write(2, "LOOOL", 5);
        return(-1);
}

Then run make in the appropriate directory:
Code:
$ ls
AUTHORS    FEEDBACK  Makefile  VERSION  test
ChangeLog  LICENSE   README    src      wtf.cpp
$ make
 make
make -C src/
make[1]: Entering directory `/home/tyler/code/c/1shot/speedhack/libkeepalive-0.2/src'
gcc -fPIC   -c -o libkeepalive.o libkeepalive.c
gcc -shared -Wl,-soname,libkeepalive.so -o libkeepalive.so libkeepalive.o -ldl
rm libkeepalive.o
make[1]: Leaving directory `/home/tyler/code/c/1shot/speedhack/libkeepalive-0.2/src'
make -C test/
make[1]: Entering directory `/home/tyler/code/c/1shot/speedhack/libkeepalive-0.2/test'
gcc     test.c   -o test
make[1]: Leaving directory `/home/tyler/code/c/1shot/speedhack/libkeepalive-0.2/test'
cp src/libkeepalive.so libkeepalive.so
strip -s libkeepalive.so
$ g++ -o wtf wtf.cpp
$

...then use it like so:
Code:
$ export LD_PRELOAD="./libkeepalive.so"
$ ./wtf
LOOOLS: -1216950284 MS: -1076618008
LOOOLS: -1216950284 MS: -1076618008
LOOOLS: -1216950284 MS: -1076618008
LOOOLS: -1216950284 MS: -1076618008
LOOOLS: -1216950284 MS: -1076618008
LOOOLS: -1216950284 MS: -1076618008
LOOOLS: -1216950284 MS: -1076618008
LOOOLS: -1216950284 MS: -1076618008
LOOOLS: -1216950284 MS: -1076618008
....

And be sure to export LD_PRELOAD="" before doing anything else because you will continue to see LOOOL's popping up in bizzare places Smilie

I suspect the difference is in the makefile. Some rather bizzare options are required to prevent gcc from complaining about your symbols conflicting with libc's when you build stuff like this.

Last edited by Corona688; 03-14-2011 at 07:15 PM..
This User Gave Thanks to Corona688 For This Post:
# 9  
Old 03-14-2011
You need a newline:
Code:
printf("LOOOLAZO\n");


And we try to run a G rated forum here... please try to avoid the foul language.
This User Gave Thanks to Perderabo For This Post:
# 10  
Old 03-14-2011
Personally I would try to avoid using any stdio functions inside a libc-level system library...
This User Gave Thanks to Corona688 For This Post:
# 11  
Old 03-14-2011
Quote:
Originally Posted by Corona688
Personally I would try to avoid using any stdio functions inside a libc-level system library...
Good point I guess. But personally, I would not try to modify a system call. Smilie
# 12  
Old 03-15-2011
Can anyone test it for me? Or give me a tip? I can't imagine where to continue if the library is not executed (that seems to be the problem).

Thank you for read.

EDIT: Sorry, I didn't notice the 2nd page. I'm shamed.

I won't use foul language again, I promise haha.

So is the problem that I write it in C++? Could I use something like extend "C"?

Last edited by lilezek; 03-15-2011 at 11:58 AM..
# 13  
Old 03-15-2011
I gave you working, step-by-step instructions in my post at the top of this page, and demonstrated that gettimeofday call was intercepted, and pointed out potential problems in your own code (i.e. C++ functions get named bizzare names, you need to make it a C function).

If my instructions don't work for you or are missing needed details, please describe where you got stuck.

---------- Post updated at 09:19 AM ---------- Previous update was at 09:03 AM ----------

Quote:
Originally Posted by lilezek
So is the problem that I write it in C++? Could I use something like extend "C"?
Boy it's a good thing there's a way to do this in userspace, since you'd have no hope at all of running C++ in the kernel Smilie

extern "C" gettimeofday(...) might work, but remember, you're writing C library code, not a normal userspace program. You should try to limit the high-level calls you make as much as possible. I'd avoid using C stdio, let alone iostream and the like. What if, in your gettimeofday() function, something deep in STL decides to call gettimeofday()? You'll get recursion until crash or swapdeath.

What, precisely, do you need C++ for? There may be lower-level alternatives.
# 14  
Old 03-15-2011
I understand your looping through STL functions. I don't need them for nothing but test purposes. That is, when I finish the library I'll erase the "LOOOL" line.

I don't need C++. Is just the language I always use. I don't know ASM and I didn't go deeper on C, so I prefer C++.

Thank you for replies. The bug seemed to be that the library must be in the same folder, thing that I didn't know.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Precaution to access user mode buffers from kernel

When accessing a user mode buffers from kernel space drivers what precautions must we take and how those precautions need to be implemented? (0 Replies)
Discussion started by: rupeshkp728
0 Replies

2. Programming

HELP!!: CPU resource allocation between kernel modules and user mode process

Hi,all: I run my program which consists of one kernel module and one user mode process on a dual core server. The problem here is the kernel module consumes 100% of one core while the user mode process only consumes 10% of the other core, is there any solution that I can assign some computing... (1 Reply)
Discussion started by: neyshule
1 Replies

3. Programming

which function copies data from user to kernel mode

when transitionaning from user to kernel mode which function copies data from user mode buffer to kernel mode? (5 Replies)
Discussion started by: rupeshkp728
5 Replies

4. SCO

PANIC: k_trap - Kernel mode trap type 0x0000000E

Hi, i'm another question: I'm a directory /usr/data on my server sco unix 5.0.5: # du /usr/data 4386948 /usr/data I'm tried to connect to ftp directory /usr/data to this server and: PANIC: k_trap - Kernel mode trap type 0x0000000E Cannot dump 262040 pages to dumpdev hd(1/41):space... (3 Replies)
Discussion started by: sebpes
3 Replies

5. UNIX for Dummies Questions & Answers

Kernel Mode

Hi all i have queastion. Can anybody pease help me what is user mode and kernel mode and the term "De-mountable volumes" means? Thanks Palash (2 Replies)
Discussion started by: palash2k
2 Replies

6. Programming

Aplication user and kernel mode (data access)

Hi all, I am trying to setup a program to use a device driver and am confusing buffer access between User and Kernel mode. I think all applications running in User space have to communicate with the device drivers using io control calls and then have some functions called back from the driver... (1 Reply)
Discussion started by: Brendan Kennedy
1 Replies

7. Linux

Kernel panic - not syncing: cannot execute a PAE-enabled kernel on PAE-less CPU

ok so I just installed fedora core 6 on my dell inspiron 700m and I go to boot into linux and I get this error. Has anyone seen this before? I also had XP Pro and Vista installed on this pc prior to putting fedora core 6 on the machine. I'm trying to setup a triple boot system. Please Help... (2 Replies)
Discussion started by: dave043
2 Replies

8. SCO

unexpected trap in kernel mode

hi, I am trying to install sco openserver 5.0.4 on an old system. However, l was not able to proceed after putting the bootstr l have this panic message of PANIC: K_trap - kernel mode trap tupe 0x00000006 will someone kindly help to decode this error kayode (1 Reply)
Discussion started by: kayode
1 Replies

9. Filesystems, Disks and Memory

k_trap - kernel mode trap type 0x0000000E

HELP is urgently required, I run on SCO Unix 3 and this is the panic message that I get every time that I reboot 10U k_trap - kernel mode trap type 0x0000000E I have checked the swap already having the following results: #swap -l path dev swaplo blocks free... (3 Replies)
Discussion started by: alex_slb
3 Replies
Login or Register to Ask a Question