1 | ; $Id: stackcorruption.asm,v 1.1 2002-04-29 14:24:27 bird Exp $
|
---|
2 | ;
|
---|
3 | ; 'Generic' wrapper function to find stack curruptions.
|
---|
4 | ;
|
---|
5 | ; Copyright (c) 2002 knut st. osmundsen (bird@anduin.net)
|
---|
6 | ;
|
---|
7 | ; Project Odin Software License can be found in LICENSE.TXT
|
---|
8 | ;
|
---|
9 |
|
---|
10 |
|
---|
11 | .386
|
---|
12 | .MODEL FLAT
|
---|
13 |
|
---|
14 |
|
---|
15 | ;
|
---|
16 | ; Defined Constants And Macros
|
---|
17 | ;
|
---|
18 | FUNCTION_NAME equ WriteLog
|
---|
19 | FUNCTION_NAME_WRAPPED equ _WriteLog_
|
---|
20 | COUNT_OF_ARGS equ 30h
|
---|
21 |
|
---|
22 |
|
---|
23 | ;
|
---|
24 | ; Externs
|
---|
25 | ;
|
---|
26 | extrn DosSetMem:near
|
---|
27 | extrn Dos32TIB:abs
|
---|
28 | extrn FUNCTION_NAME_WRAPPED:near
|
---|
29 |
|
---|
30 |
|
---|
31 | CODE32 segment
|
---|
32 |
|
---|
33 |
|
---|
34 | FUNCTION_NAME proc near
|
---|
35 | save_eax = dword ptr -4
|
---|
36 | save_ebx = dword ptr -8
|
---|
37 | save_ecx = dword ptr -12
|
---|
38 | save_edx = dword ptr -16
|
---|
39 | pv = dword ptr -20
|
---|
40 | cb = dword ptr -24
|
---|
41 | push ebp
|
---|
42 | mov ebp, esp
|
---|
43 | sub esp, 30
|
---|
44 |
|
---|
45 | ;
|
---|
46 | ; Save registers.
|
---|
47 | ;
|
---|
48 | mov [ebp + save_eax], eax
|
---|
49 | mov [ebp + save_ebx], ebx
|
---|
50 | mov [ebp + save_ecx], ecx
|
---|
51 | mov [ebp + save_edx], edx
|
---|
52 |
|
---|
53 | ;
|
---|
54 | ; Align stack at page boundary
|
---|
55 | ;
|
---|
56 | lea esp, [esp - (COUNT_OF_ARGS * 4)]
|
---|
57 | and esp, NOT 0fffh
|
---|
58 |
|
---|
59 | ;
|
---|
60 | ; Copy parameters.
|
---|
61 | ;
|
---|
62 | mov ebx, COUNT_OF_ARGS*4
|
---|
63 | copyloop:
|
---|
64 | sub ebx, 4
|
---|
65 | mov eax, [ebp+ebx+8]
|
---|
66 | mov [esp+ebx], eax
|
---|
67 |
|
---|
68 | or ebx, ebx
|
---|
69 | jnz copyloop
|
---|
70 |
|
---|
71 | ;
|
---|
72 | ; Get stack range
|
---|
73 | ;
|
---|
74 | push fs
|
---|
75 | push Dos32TIB
|
---|
76 | pop fs
|
---|
77 | mov eax, fs:[4] ; tib_pstack
|
---|
78 | mov ecx, fs:[8] ; tip_pstacklimit
|
---|
79 | pop fs
|
---|
80 | sub ecx, esp
|
---|
81 | mov [ebp+pv], esp
|
---|
82 | mov [ebp+cb], ecx
|
---|
83 |
|
---|
84 |
|
---|
85 | ;
|
---|
86 | ; Freeze the stack
|
---|
87 | ;
|
---|
88 | push 1 ; PAG_READ
|
---|
89 | push [ebp+cb] ; the size.
|
---|
90 | push [ebp+pv] ; the pointer.
|
---|
91 | call DosSetMem
|
---|
92 | add esp, 0ch
|
---|
93 | test eax, eax
|
---|
94 | jz ok1
|
---|
95 | int 3
|
---|
96 |
|
---|
97 | ok1:
|
---|
98 |
|
---|
99 | ;
|
---|
100 | ; Restore registers.
|
---|
101 | ;
|
---|
102 | mov eax, [ebp-04h]
|
---|
103 | mov ecx, [ebp-08h]
|
---|
104 | mov edx, [ebp-0ch]
|
---|
105 | mov ebx, [ebp-10h]
|
---|
106 |
|
---|
107 | ;
|
---|
108 | ; Call the original function with readonly parameters.
|
---|
109 | ;
|
---|
110 | call FUNCTION_NAME_WRAPPED
|
---|
111 | push eax
|
---|
112 | push ecx
|
---|
113 | push edx
|
---|
114 |
|
---|
115 |
|
---|
116 | ;
|
---|
117 | ; UnFreeze the stack
|
---|
118 | ;
|
---|
119 | push 3 ; PAG_READ | PAG_WRITE
|
---|
120 | push [ebp+cb] ; the size.
|
---|
121 | push [ebp+pv] ; the pointer.
|
---|
122 | call DosSetMem
|
---|
123 | add esp, 0ch
|
---|
124 | test eax, eax
|
---|
125 | jz ok2
|
---|
126 | int 3
|
---|
127 |
|
---|
128 | ok2:
|
---|
129 | ;
|
---|
130 | ; Restore registers and return
|
---|
131 | ;
|
---|
132 | pop edx
|
---|
133 | pop ecx
|
---|
134 | pop eax
|
---|
135 | leave
|
---|
136 | ret
|
---|
137 | FUNCTION_NAME endp
|
---|
138 |
|
---|
139 |
|
---|
140 | CODE32 ends
|
---|
141 |
|
---|
142 |
|
---|
143 | end
|
---|
144 |
|
---|