forked from piyush-kansal/uCrypt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
68 lines (59 loc) · 2.06 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <errno.h>
#include "syscall_handler.h"
/***
* enter_flag: state the status
* 0 : not being intercepted
* 1 : inside the interception, right before making the system call
* 2 : making the system call
* 3 : inside the interception, right after making the system call
*
* You may use libc functions inside the intercepted library.
* enter_flag is used to make distinguish the two cases such that
* functions made inside the intercepted library won't be
* intercepted again.
**/
__thread int enter_flag = 0;
/***
* It is important not to mess up the errno
* Save it before our interception, restore it before returning
* from our interception.
**/
__thread int saved_errno = 0;
/***
* To save the syscall number, so that we can use it in the
* func_exit. eax, which originally stores the syscall no, will
* be replaced by the return value of the syscall.
**/
__thread int syscall_no = 0;
/***
* func_enter - entry point, syscall will be made afterward
* Important: Do Not Modify the function signature. It is hardcoded
* into the intercepted library.
**/
void func_enter( unsigned int edi, unsigned int esi, unsigned int bp, unsigned int sp, unsigned int ebx, unsigned int edx, unsigned int ecx, unsigned int eax ) {
// If any file handling primitive is called for any of the STDIN/STDOUT/STDERR descriptors, then do not intercept them
if( ( 0 == ebx ) || ( 1 == ebx ) || ( 2 == ebx ) )
return;
if( enter_flag == 0 ) {
saved_errno = errno;
enter_flag = 1;
syscall_handler_pre( &eax, &ebx, &ecx, &edx, &esi, &edi );
syscall_no = eax;
enter_flag = 2;
}
return;
}
/***
* func_exit - exit point, syscall already made
* Important: Do Not Modify the function signature. It is hardcoded
* into the intercepted library.
**/
void func_exit( unsigned int edi, unsigned int esi, unsigned int bp, unsigned int sp, unsigned int ebx, unsigned int edx, unsigned int ecx, unsigned int eax ) {
if ( enter_flag == 2 ) {
enter_flag = 3;
syscall_handler_post( syscall_no, &eax, &ebx, &ecx, &edx, &esi, &edi );
enter_flag = 0;
errno = saved_errno;
}
return;
}