-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrgdb.py
408 lines (328 loc) · 13.3 KB
/
grgdb.py
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
#
# Copyright 2021, Xcalar Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Author: Eric D. Cohen
# GDB commands for use with GuardRails.
# (gdb) source grgdb.py
import gdb
import gdb.printing
import re
PAGE_SIZE = 4096
NUM_GUARD_PAGES = 1
GUARD_SIZE = NUM_GUARD_PAGES * PAGE_SIZE
MAGIC_FREE = 0xcd656727bedabb1e
MAGIC_INUSE = 0x4ef9e433f005ba11
MAGIC_GUARD = 0xfd44ba54deadbabe
def checkGr():
try:
gdb.parse_and_eval("grArgs")
except gdb.error as e:
msg = "FATAL GUARDRAILS ERROR: Program not run under guardrails"
print(msg)
sys.stderr.write(msg + "\n")
raise
class GRFindDelayList (gdb.Command):
"""
Search Guard Rails delayed free list for a given address.
(gdb) gr-find-delay-list <delayListAddress> <addressToFind>
Example:
(gdb) gr-find-delay-list &memSlots[0].delay 0x7f607e24dbf0
"""
def __init__ (self):
super (GRFindDelayList, self).__init__ ("gr-find-delay-list", gdb.COMMAND_USER)
def delayListCount(self, head, tail, maxDelayCt):
if head > tail:
return head - tail
else:
return maxDelayCt - (tail - head)
def invokeHelper (self, arg, from_tty):
argv = gdb.string_to_argv(arg)
if len(argv) != 2:
print("Malformed arguments; see help")
return
dlist = gdb.parse_and_eval('(MemFreeDelay *) ' + argv[0])
addrToFind = int(argv[1], 16)
dlistElms = dlist['elms']
maxDelayCt = int(gdb.parse_and_eval('sizeof(((MemFreeDelay *)0x0)->elms)/sizeof(((MemFreeDelay *)0x0)->elms[0])'))
head = dlist['head']
tail = dlist['tail']
cursor = head
delayCount = self.delayListCount(head, tail, maxDelayCt)
ct = 0
while ct < delayCount:
if not ct % 1000:
print("Searching %7d / %d elements" % (ct, delayCount))
elmPtr = dlistElms[cursor]
elm = elmPtr.dereference()
magicStr = str(elm['magic'])
if elm['magic'] != MAGIC_FREE:
print("Delayed free list contains invalid header magic 0x%x" % magicStr)
elmStartAddr = int(elmPtr)
# Consider the full allocation (not just the amount allocated for the
# user), as the errant pointer might land anywhere in that range.
elmEndAddr = elmStartAddr + (1 << int(elm['binNum'])) + GUARD_SIZE - 1
if elmStartAddr <= addrToFind <= elmEndAddr:
print("Found address 0x%x on delayed free list at index %d, header 0x%x :" % (addrToFind, cursor, elmPtr))
# print("%x <= %x <= %x" % (elmStartAddr, addrToFind, elmEndAddr))
print(elm)
# An element should only appear on the list once, so return
return
# Search the list from the head (most recently added) backwards as
# the element of interest is most likely recent (and search can be
# somewhat slow)
if cursor == 0:
cursor = maxDelayCt
cursor -= 1
ct += 1
print("Address 0x%x not found" % (addrToFind))
def invoke(self, arg, from_tty):
try:
checkGr()
self.invokeHelper(arg, from_tty)
except Exception as e:
print(str(e))
traceback.print_exc()
class GRPrintAddrInfo (gdb.Command):
"""
Try to print GuardRails info about an address.
The address must be either the beginning of a header or beginning of a
user allocation. If the address is a random memory address not known
to point to a header or beginning allocation, first try to find the
associated header address using gr-find-header.
If GuardRails was run with -t and/or -T options, traces will also be
dumped for allocations/frees.
(gdb) gr-print-addr-info <address>
Example:
(gdb) gr-print-addr-info 0x7fc6aa5f2000
"""
def __init__ (self):
super (GRPrintAddrInfo, self).__init__ ("gr-print-addr-info", gdb.COMMAND_USER)
def isValid(self, magic):
return magic == MAGIC_INUSE or magic == MAGIC_FREE
def dumpSymTrace(self, trace, offset, maxFrames):
if not maxFrames:
print("Memory tracking not enabled; to enable rerun GuardRails with -t/-T options")
return
for i in range(offset, maxFrames + offset):
addr = str(trace[i]).split(' ')[0]
addrInt = int(addr, 0)
if not addrInt:
continue
sym = str(gdb.execute('info line *' + str(addrInt), to_string=True))
sym = re.sub(r' and ends at .*', r'', sym)
print(sym[0:-1])
def invokeHelper(self, arg, from_tty):
argv = gdb.string_to_argv(arg)
if len(argv) != 1:
print("Malformed arguments; see help")
return
hdrPtr = gdb.parse_and_eval('(ElmHdr *) ' + argv[0])
hdr = hdrPtr.dereference()
grArgs = gdb.parse_and_eval("grArgs")
maxAllocFrames = int(grArgs['maxTrackFrames'])
maxFreeFrames = int(grArgs['maxTrackFreeFrames'])
if self.isValid(hdr['magic']):
print("Address %s is a header" % argv[0])
else:
hdrPtr = gdb.parse_and_eval('*(ElmHdr **)((char *)' + argv[0] + ' - sizeof(void *))')
hdr = hdrPtr.dereference()
if self.isValid(hdr['magic']):
print("Address %s is a user address" % argv[0])
else:
print("Address %s doesn't look valid" % argv[0])
return
if hdr['magic'] == MAGIC_INUSE:
print("Address %s is in-use" % argv[0])
elif hdr['magic'] == MAGIC_FREE:
print("Address %s is free" % argv[0])
print("Header:")
print(hdr)
trace = hdr['allocBt']
print("================ Allocation Trace: ================")
self.dumpSymTrace(trace, 0, maxAllocFrames)
print("================ Free Trace: ================")
self.dumpSymTrace(trace, maxAllocFrames + 1, maxFreeFrames)
def invoke(self, arg, from_tty):
try:
checkGr()
self.invokeHelper(arg, from_tty)
except Exception as e:
print(str(e))
traceback.print_exc()
class GRFindHeader(gdb.Command):
"""
Try to find the GuardRails header address for an arbitrary address
Use this to find the header address associated with an arbitrary memory
address. The output address of this command can be used with
gr-print-addr-info.
(gdb) gr-find-header <address>
Example:
(gdb) gr-find-header 0x7fc6aa5f2327
"""
def __init__ (self):
super (GRFindHeader, self).__init__ ("gr-find-header", gdb.COMMAND_USER)
def isValid(self, magic):
return magic == MAGIC_INUSE or magic == MAGIC_FREE
def invokeHelper(self, arg, from_tty):
argv = gdb.string_to_argv(arg)
if len(argv) != 1 and (len(argv) == 2 and argv[1] != 'quiet') or len(argv) > 2:
print("Malformed arguments; see help")
return
addr = int(argv[0], 16)
quiet = False
if len(argv) == 2:
quiet = argv[1] == 'quiet'
mask = 0xffffffffffffffff - PAGE_SIZE + 1
headerStart = addr & mask
currHeader = headerStart
while (True):
hdrPtr = gdb.parse_and_eval('((ElmHdr *) ' + str(currHeader) + ')')
hdr = hdrPtr.dereference()
if self.isValid(hdr['magic']):
# XXX: Add header sanity checks here
if quiet:
print("0x%x" % currHeader)
else:
print("Found valid header at: 0x%x" % currHeader)
break
assert(currHeader > PAGE_SIZE)
currHeader -= PAGE_SIZE
def invoke(self, arg, from_tty):
try:
checkGr()
self.invokeHelper(arg, from_tty)
except Exception as e:
print(str(e))
traceback.print_exc()
class GRHeapMetaCorruption(gdb.Command):
"""
Try to determine if a faulting address would cause heap corruption.
This command indicates if accessing a given address would cause
corruption by determining if the access falls on a guard page.
(gdb) gr-heap-meta-corruption <address>
Example:
(gdb) gr-heap-meta-corruption 0x7fc6aa5f2327
"""
def __init__ (self):
super (GRHeapMetaCorruption, self).__init__ ("gr-heap-meta-corruption", gdb.COMMAND_USER)
def invokeHelper(self, arg, from_tty):
argv = gdb.string_to_argv(arg)
if len(argv) != 1:
print("Malformed arguments; see help")
return
addr = int(argv[0], 16)
currHeader = gdb.execute("gr-find-header 0x%x quiet" % (addr), to_string=True)
hdrPtr = gdb.parse_and_eval('((ElmHdr *) ' + str(currHeader) + ')')
hdr = hdrPtr.dereference()
hdrPtrAddr = int(hdrPtr)
guardStartAddr = hdrPtrAddr + (1 << hdr['binNum'])
guardEndAddr = guardStartAddr + GUARD_SIZE - 1
verifyGuard = gdb.parse_and_eval("*((uint64_t *) 0x%x)" % guardStartAddr)
assert(verifyGuard == MAGIC_GUARD)
if guardStartAddr <= addr <= guardEndAddr:
print("CORRUPTION: Access of 0x%x appears to corrupt the heap metadata and/or overrun the buffer" % addr)
else:
print("Access of 0x%x doesn't appear to be heap metadata corruption or overrun" % addr)
def invoke(self, arg, from_tty):
try:
checkGr()
self.invokeHelper(arg, from_tty)
except Exception as e:
print(str(e))
traceback.print_exc()
class GRPrintSegv(gdb.Command):
"""
Print address of access that caused the current segfault.
(gdb) gr-print-segv
Example:
(gdb) gr-print-segv
"""
def __init__ (self):
super (GRPrintSegv, self).__init__ ("gr-print-segv", gdb.COMMAND_USER)
def invokeHelper(self, arg, from_tty):
argv = gdb.string_to_argv(arg)
if len(argv) != 0:
print("Malformed arguments; see help")
return
sigInfo = gdb.parse_and_eval("$_siginfo")
if sigInfo['si_signo'] == 7 or sigInfo['si_signo'] == 11:
print("Memory fault at: " + str(sigInfo['_sifields']['_sigfault']['si_addr']))
else:
print("Unknown fault, signal %d" % sigInfo['si_signo'])
def invoke(self, arg, from_tty):
try:
self.invokeHelper(arg, from_tty)
except Exception as e:
print(str(e))
traceback.print_exc()
class GRDumpInFlight(gdb.Command):
"""
Dump allocator metadata.
WARNING: This is mostly for dumping in-flight allocations to a file
for subsequent post-processing with grdump.py. This is NOT recommended
for interactive debugging.
(gdb) gr-dump-in-flight
Example:
(gdb) gr-dump-in-flight
"""
def __init__ (self):
super (GRDumpInFlight, self).__init__ ("gr-dump-in-flight", gdb.COMMAND_USER)
def getTraceCsv(self, trace, offset, maxFrames):
if not maxFrames:
print("Memory tracking not enabled; to enable rerun GuardRails with -t/-T options")
return
ret = ""
for i in range(offset, maxFrames + offset):
addrInt = int(trace[i])
if addrInt:
ret += "0x%x," % addrInt
else:
ret += ","
return ret
def invokeHelper(self, arg, from_tty):
argv = gdb.string_to_argv(arg)
if len(argv) != 0:
print("Malformed arguments; see help")
return
grArgs = gdb.parse_and_eval("grArgs")
numSlots = int(grArgs['numSlots'])
maxAllocFrames = int(grArgs['maxTrackFrames'])
maxFreeFrames = int(grArgs['maxTrackFreeFrames'])
memSlots = gdb.parse_and_eval("memSlots")
print("===== START TRACES =====")
for slotNum in range(0, numSlots):
memBins = memSlots[slotNum]['memBins']
numBins = int(gdb.parse_and_eval('sizeof(((MemSlot *)0x0)->memBins)/sizeof(((MemSlot *)0x0)->memBins)[0]'))
for binNum in range(0, numBins):
memBin = memBins[binNum]
hdr = memBin['headInUse']
while hdr != 0x0:
csvTrace = self.getTraceCsv(hdr['allocBt'], 0, maxAllocFrames)
elmSize = hdr['usrDataSize']
print(str(elmSize) + "," + csvTrace)
hdr = hdr['next']
def invoke(self, arg, from_tty):
try:
checkGr()
self.invokeHelper(arg, from_tty)
except Exception as e:
print(str(e))
traceback.print_exc()
GRFindDelayList()
GRPrintAddrInfo()
GRFindHeader()
GRHeapMetaCorruption()
GRPrintSegv()
GRDumpInFlight()