Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Catching a system call just before control enters a shared library

I have wrapped a number of system call function like write(), open() etc and LD-PRELOAD is used to override the original system calls. Moreover I have defined a few more functions and made this too a shred library.

I would like to catch all system calls from different application processes to these shared libraries before they enter the shared library. How can i do that?

Thanks

like image 761
Lipika Deka Avatar asked Jun 04 '11 16:06

Lipika Deka


People also ask

What triggers a system call?

A computer program makes a system call when it makes a request to the operating system's kernel. System call provides the services of the operating system to the user programs via Application Program Interface(API).

What is the difference between a system call and a library call?

A system call is a request made by the program to enter into kernel mode to access a process.. A library call is a request made by the program to access a library function defined in a programming library.

How does the kernel know which system call was invoked?

The kernel doesn't monitor the process to detect a system call. Instead, the process generates an interrupt which transfers control to the kernel, because that's what software-generated interrupts do according to the instruction set reference manual.

How system calls are intercepted?

Intercepting a system call means that you want a function of your own to be called instead of the kernel function implementing a given system call everytime the latter in invoked.


2 Answers

LD_PRELOAD is not necessarily a good way to interpose system calls, because a) it only allows you to intercept library calls and b) it only allows you to intercept library calls. ;)

A) While in general, system calls are wrapped by the shared libC in your system, no one prevents you from calling a system call yourself, e.g., but setting up the right register content and then issuing INT 0x80 on an x86 system. If the program you're interested in does so, you'll never catch those with LD_PRELOAD-based libc-interposition.

B) While in general, most programs use the shared libC in your system to make system calls, sometimes applications are linked statically, which means the libC code is part of the application and does not come from the shared lib. In such cases, LD_PRELOAD also does not help.

A comment already suggested to use strace/ltrace -- my generalized advice would be to have a look at ptrace() which both of these tools use and which should give you what you want without the need of modifying the kernel.

like image 66
BjoernD Avatar answered Sep 30 '22 05:09

BjoernD


Patch-free User-level Link-time intercepting of system calls and interposing on library functions may do the trick but I have not tested it.

like image 24
Soroush Avatar answered Sep 30 '22 07:09

Soroush