-
Notifications
You must be signed in to change notification settings - Fork 3
/
libc.py
576 lines (488 loc) · 19 KB
/
libc.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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
"""
Common standard C library builtin functions.
"""
from __future__ import annotations
import logging
import re
from typing import TYPE_CHECKING
from ... import constants
from ...call_hooks import builtin_func
if TYPE_CHECKING:
from rugosa.emulation.cpu_context import ProcessorContext
logger = logging.getLogger(__name__)
@builtin_func("_alloca")
@builtin_func("_malloca")
def _alloca(cpu_context, func_name, func_args):
"""
Allocates stack space
NOTE: Our memory controller will automatically allocate pages as it writes.
Therefore, we don't actually need to allocate anything, just provide them with
a pointer.
"""
(size,) = func_args
if size:
page_size = cpu_context.memory.PAGE_SIZE
size = (size + page_size - 1) & ~(page_size - 1) # Align one page up.
logger.debug("Allocating %d bytes of stack space.", size)
# Returns a pointer to our allocated stack space.
return cpu_context.sp - size
@builtin_func("malloc")
@builtin_func("calloc")
def malloc(cpu_context, func_name, func_args):
"""
Allocates memory block
"""
if func_name == "calloc":
num, size = func_args
size = num * size
else:
(size,) = func_args
if size:
logger.debug("Allocating %d bytes of memory.", size)
return cpu_context.memory.alloc(size)
@builtin_func
def new(cpu_context, func_name, func_args):
"""
Allocates memory block
"""
size = func_args[0]
# If we have a second argument it's either a nothrow_value or a ptr to where they
# want us to allocate the space.
# Just assume its a ptr if it's not 0 since the emulator should not produce a nothrow_value correctly
# and therefore be 0.
# TODO: Perhaps provide more context to make a more informed decision.
try:
ptr = func_args[1]
except IndexError:
ptr = 0
if size:
if ptr:
logger.debug("Mapping %d bytes into 0x%X", size, ptr)
# If a pointer was provided, just give it back to them without doing anything.
# Our memory controller will automatically allocate pages as they are being written.
return ptr
logger.debug("Allocating %d bytes of memory.", size)
return cpu_context.memory.alloc(size)
@builtin_func
def realloc(cpu_context, func_name, func_args):
"""
Reallocates memory block
"""
ptr, size = func_args
if ptr and size:
logger.debug("Reallocating 0x%X with %d bytes.", ptr, size)
return cpu_context.memory.realloc(ptr, size)
@builtin_func
def memchr(cpu_context, func_name, func_args):
"""
Locate character in block of memory.
Searches within the first num bytes of the block of memory pointed by data_ptr
for the first occurrence of value (interpreted as an unsigned char) and returns a pointer
to it.
"""
data_ptr, value, num = func_args
ptr = cpu_context.memory.find(value, start=data_ptr, end=data_ptr + num)
if ptr == -1:
return 0
return ptr
@builtin_func("memmove")
@builtin_func("memmove_s")
@builtin_func("wmemmove")
@builtin_func("wmemmove_s")
@builtin_func("memcpy")
@builtin_func("memcpy_s")
@builtin_func("wmemcpy")
@builtin_func("wmemcpy_s")
def memcpy(cpu_context, func_name, func_args):
"""
Copies count characters from src to dst.
"""
secure = func_name.endswith("_s")
wide = func_name.startswith("w")
if secure:
dst, dst_size, src, count = func_args
count = min(dst_size, count)
else:
dst, src, count = func_args
if wide:
count *= 2
if dst and src:
logger.debug("Copying %d bytes from 0x%X to 0x%X", count, src, dst)
cpu_context.memory.copy(src, dst, count)
return 0 if secure else dst
@builtin_func
def memset(cpu_context, func_name, func_args):
"""
Writes count number of ch characters to dst.
"""
dst, ch, count = func_args
if dst:
logger.debug("Writing %r * %d to 0x%X", chr(ch), count, dst)
cpu_context.memory.write(dst, bytes([ch]) * count)
return dst
@builtin_func
def memcmp(cpu_context, func_name, func_args):
lhs, rhs, count = func_args
if lhs and rhs:
logger.debug("Comparing the first %d bytes in 0x%X with 0x%X", count, lhs, rhs)
left = cpu_context.memory.read(lhs, count)
right = cpu_context.memory.read(rhs, count)
if left < right:
return -1
if left > right:
return 1
return 0
@builtin_func("strcat")
@builtin_func("strcat_s")
@builtin_func("wcscat")
@builtin_func("wcscat_s")
@builtin_func("lstrcatA")
@builtin_func("lstrcatW")
def strcat(cpu_context, func_name, func_args):
"""
Concatenates at most count characters from src, stopping if the null character
is found, to the end of the null-terminated byte string pointed to by dst.
The src[0] replaces the null terminator at the end of dst.
"""
secure = func_name.endswith("_s")
wide = func_name.startswith("w") or func_name.endswith("W")
if secure:
dst, dst_size, src = func_args
if wide:
dst_size *= 2
else:
dst, src = func_args
dst_size = None
if dst and src:
logger.debug("Concatenating c string in 0x%X to c string in 0x%X", src, dst)
append_str = cpu_context.memory.read_data(
src, data_type=constants.WIDE_STRING if wide else constants.STRING
)
dest_str = cpu_context.memory.read_data(
dst, data_type=constants.WIDE_STRING if wide else constants.STRING
)
null_offset = dst + len(dest_str)
if dst_size is not None:
dst_size -= null_offset - dst
append_str = append_str[:dst_size]
terminator = b"\0\0" if wide else b"\0"
cpu_context.memory.write(null_offset, append_str + terminator)
return 0 if secure else dst
@builtin_func("strncat")
@builtin_func("strncat_s")
@builtin_func("wcsncat")
@builtin_func("wcsncat_s")
def strncat(cpu_context, func_name, func_args):
"""
Concatenates at most count characters from src, stopping if the null character
is found, to the end of the null-terminated byte string pointed to by dst.
The src[0] replaces the null terminator at the end of dst.
"""
secure = func_name.endswith("_s")
wide = func_name.startswith("w")
if secure:
dst, dst_size, src, count = func_args
if wide:
dst_size *= 2
else:
dst, src, count = func_args
dst_size = None
if wide:
count *= 2
if dst and src:
logger.debug("Concatenating c string in 0x%X to c string in 0x%X", src, dst)
append_str = cpu_context.memory.read_data(
src, data_type=constants.WIDE_STRING if wide else constants.STRING
)[:count]
dest_str = cpu_context.memory.read_data(
dst, data_type=constants.WIDE_STRING if wide else constants.STRING
)
null_offset = dst + len(dest_str)
if dst_size is not None:
dst_size -= null_offset - dst
append_str = append_str[:dst_size]
terminator = b"\0\0" if wide else b"\0"
cpu_context.memory.write(null_offset, append_str + terminator)
return 0 if secure else dst
@builtin_func
def strpbrk(cpu_context, func_name, func_args):
"""
Locate characters in string.
Returns a pointer to the first occurrence of str1 of any of the characters
that are part of str2, or a null pointer if there are no matches.
"""
str1_ptr, str2_ptr = func_args
str1 = cpu_context.memory.read_data(str1_ptr)
str2 = cpu_context.memory.read_data(str2_ptr)
for offset, ch in enumerate(str1):
if ch in str2:
return str1_ptr + offset
return 0
@builtin_func("strchr")
@builtin_func("strrchr")
def strchr(cpu_context, func_name, func_args):
"""
Locate first or last occurrence of character in string.
"""
string_ptr, character = func_args
string = cpu_context.memory.read_data(string_ptr)
if func_name == "strchr":
offset = string.find(character)
else:
offset = string.rfind(character)
if offset == -1:
return 0
return string_ptr + offset
@builtin_func("strcpy")
@builtin_func("strcpy_s")
@builtin_func("wcscpy")
@builtin_func("wcscpy_s")
@builtin_func("lstrcpyA")
@builtin_func("lstrcpyW")
def strcpy(cpu_context, func_name, func_args):
"""
Copies the null-terminated byte string pointed to by src, including the null terminator, to dst.
"""
secure = func_name.endswith("_s")
wide = func_name.startswith("w") or func_name.endswith("W")
if secure:
dst, dst_size, src = func_args
if wide:
dst_size *= 2
else:
dst, src = func_args
dst_size = None
if dst and src:
logger.debug("Copying c string in 0x%X to 0x%X", src, dst)
terminator = b"\0\0" if wide else b"\0"
src_str = cpu_context.memory.read_data(
src, data_type=constants.WIDE_STRING if wide else constants.STRING
) + terminator
size = len(src_str)
if dst_size is not None:
size = min(dst_size, size) # limit to dst_size if secure.
cpu_context.memory.copy(src, dst, size)
return 0 if secure else dst
@builtin_func("strncpy")
@builtin_func("strncpy_s")
@builtin_func("wcsncpy")
@builtin_func("wcsncpy_s")
@builtin_func("lstrcpynA")
@builtin_func("lstrcpynW")
def strncpy(cpu_context, func_name, func_args):
"""
Copies at most count characters of src (including the terminating null character) to dst
but not any of the characters that follow the null character.
For non-secure versions, if count is not reached, additional null characters are written to dst.
"""
secure = func_name.endswith("_s")
wide = func_name.startswith("w") or func_name.endswith("W")
if secure:
# Secure version has an extra dst_size argument used for validation, but we don't care to
# throw errors if we don't have to!
dst, _, src, count = func_args
else:
dst, src, count = func_args
if wide:
count *= 2
if dst and src:
logger.debug("Copying c string in 0x%X to 0x%X", src, dst)
terminator = b"\0\0" if wide else b"\0"
src_str = cpu_context.memory.read_data(
src, data_type=constants.WIDE_STRING if wide else constants.STRING
) + terminator
size = min(count, len(src_str))
cpu_context.memory.copy(src, dst, size)
# Non-secure version also pads the rest with null characters to reach count size
if not secure and size < count:
# As a safety check, we shouldn't be writing more than about 0x1000 of padding.
delta = count - size
if delta > 0x1000:
logger.warning(
"Attempted to write %d bytes of padding. Ignoring request and using %d bytes "
"of padding instead.", delta, 0x1000)
delta = 0x1000
cpu_context.memory.write(src + size, b"\0" * delta)
return 0 if secure else dst
@builtin_func
def strdup(cpu_context, func_name, func_args):
"""
Returns a pointer to a null-terminated byte string, which is a duplicate of the string
pointing to by str.
"""
(str_ptr,) = func_args
if str_ptr:
logger.debug("Copying c string in 0x%X to new pointer.", str_ptr)
null_offset = cpu_context.memory.find(b"\0", start=str_ptr)
size = null_offset - str_ptr
# create new pointer
new_ptr = cpu_context.memory.alloc(size + 1)
# Copy at most size bytes of data then add the null terminator.
cpu_context.memory.copy(str_ptr, new_ptr, size)
cpu_context.memory.write(new_ptr + size, b"\0")
return new_ptr
@builtin_func
def strndup(cpu_context, func_name, func_args):
"""
Returns a pointer to a null-terminated byte string, which contains copies
of at most size bytes from the string pointed to by str.
"""
str_ptr, size = func_args
if str_ptr:
logger.debug("Copying %d bytes of c string in 0x%X to new pointer.", size, str_ptr)
null_offset = cpu_context.memory.find(b"\0", start=str_ptr)
size = min(size, null_offset - str_ptr)
# create new pointer
new_ptr = cpu_context.memory.alloc(size + 1)
# Copy at most size bytes of data then add the null terminator.
cpu_context.memory.copy(str_ptr, new_ptr, size)
cpu_context.memory.write(new_ptr + size, b"\0")
return new_ptr
@builtin_func("strlen")
@builtin_func("strnlen_s")
@builtin_func("wcslen")
@builtin_func("wcsnlen_s")
@builtin_func("lstrlenA")
@builtin_func("lstrlenW")
def strlen(cpu_context, func_name, func_args):
"""
Returns the length of the given null-terminated byte string.
"""
secure = func_name.endswith("_s")
wide = func_name.startswith("w") or func_name.endswith("W")
if secure:
str_ptr, strsz = func_args
else:
(str_ptr,) = func_args
strsz = None
if str_ptr:
logger.debug("Getting length of c string in 0x%X", str_ptr)
sstr = cpu_context.memory.read_data(
str_ptr, data_type=constants.WIDE_STRING if wide else constants.STRING
)
size = len(sstr)
if wide:
size /= 2
# For secure version, strsz is returned if the terminator was not found
# in the first strsz characters.
if strsz is not None and size > strsz:
return strsz
return size
@builtin_func
def strstr(cpu_context, func_name, func_args):
"""
Locate substring.
Returns a pointer to the first occurrence of str2 in str1, or a null pointer if str2 is not part of str1.
"""
str1_ptr, str2_ptr = func_args
str1 = cpu_context.memory.read_data(str1_ptr)
str2 = cpu_context.memory.read_data(str2_ptr)
offset = str1.find(str2)
if offset == -1:
return 0
return str1_ptr + offset
def _format_string(ctx: ProcessorContext, fmt: str, required_args: int, wide: bool = False) -> str:
"""
Handles formatting the string with the function arguments based on the format.
:param ctx: cpu_context object
:param fmt: format string
:param required_args: num of required arguments for the particular format function to skip
:param wide: Whether the strings are wide
:return: The formatted string
"""
# TODO: parse string instead of bytes
# Format using best attempt here. Basically, locate all the format specifiers, and convert them to a python
# supported format string. For each format string, extract the appropriate data from the context, and append it to
# the values list.
fmt_val_re = re.compile(r"""
% # start with percent character
[-+ #0]{0,1} # optional flag character
(\*|[0-9]{1,}){0,} # optional width specifier, though mutually exclusive (either a number or *, not both)
((\.[0-9]{1,})|\.\*){0,} # optional precision specifier, mutually exclusive
[diuoxXfFeEgGaAcspn] # format type
""", re.VERBOSE)
# NOTE: findall() produces empty results so we need to use finditer()
fmt_vals = [match.group() for match in fmt_val_re.finditer(fmt)]
logger.debug("Format vals: %r", fmt_vals)
# Re-pull function arguments with correct number of arguments.
func_args = ctx.get_function_arg_values(num_args=required_args + len(fmt_vals))
format_vals = []
arg_pos = required_args # skip destination and format string
for match in fmt_vals:
value = func_args[arg_pos]
if "*" in match:
# Indicates that one of the parameters is a width, which must be pulled and added to the list first
arg_pos += 1
if match.endswith("c"): # character (will this be the value or a read from the context???
if value <= 0xFF: # assume that the argument contains the character
value = chr(value)
else: # assume it's a pointer that must be dereferenced
value = chr(ctx.memory.read_data(value, size=1))
elif match.endswith("s"): # string value, should be a pointer
value = ctx.memory.read_string(value, wide=wide)
if not value: # If the argument isn't set during parsing, preserve the formatting
logger.debug("Pulled 0 byte format string, reverting")
value = "%s"
# all other numerical types???
format_vals.append(value)
arg_pos += 1
format_vals = tuple(format_vals)
result = fmt % format_vals
logger.debug(f"Formatted string: {fmt!r} % {format_vals!r} -> {result!r}")
return result
@builtin_func("sprintf")
@builtin_func("wsprintfW") # TODO: technically from winuser.h
def sprintf(ctx, func_name, func_args):
"""
Format a string based on provided format string and parameters.
int sprintf (char *s, const char *format, ...);
For sprintf, there's no way to know up front how many args are needed, but there should always be at least
2 (destination and format). We can use the format string to determine how many arguments we need by
counting the format specifiers.
"""
wide = func_name.endswith("W")
if len(func_args) < 2: # Ensure that there are at least 2 arguments, dest and format
func_args = ctx.get_function_arg_values(num_args=2)
dest, fmt_ptr, *_ = func_args
fmt = ctx.memory.read_string(fmt_ptr, wide=wide)
logger.debug("Format string: %s", fmt)
result = _format_string(ctx, fmt, 2, wide)
logger.debug("Writing formatted value %s to 0x%X", result, dest)
ctx.memory.write_string(dest, result + "\0", wide=wide)
return len(result)
@builtin_func("snprintf")
@builtin_func("sprintf_s")
@builtin_func("swprintf_s")
def snprintf(ctx, func_name, func_args):
"""
int snprintf (char *s, size_t n, const char *format, ...);
Format a string using the provided format string and values, truncated if necessary to length n.
"""
wide = func_name.startswith("sw")
if len(func_args) < 3:
func_args = ctx.get_function_arg_values(num_args=3)
dest, n, fmt_ptr, *_ = func_args
fmt = ctx.memory.read_string(fmt_ptr, wide=wide)
logger.debug("Format string: %s", fmt)
result = _format_string(ctx, fmt, 3, wide)[:n - 1]
logger.debug("Writing formatted value %s to 0x%X", result, dest)
ctx.memory.write_string(dest, result + "\0", wide=wide)
return len(result)
@builtin_func
def printf(ctx, func_name, func_args):
"""
int printf (const char * format, ...);
Writes the C string pointed by **format** to the standard output (stdout).
If **format** includes format specifiers (subsequences beginning with %), the
additional arguments following **format** are formatted and inserted in the resulting string replacing their
respective specifiers.
"""
if len(func_args) < 1:
func_args = ctx.get_function_arg_values(num_args=1)
fmt_ptr, *_ = func_args
fmt = ctx.memory.read_string(fmt_ptr)
logger.debug("Format string: %s", fmt)
result = _format_string(ctx, fmt, 1)
logger.debug("Writing formatted value %s to stdout", result)
ctx.stdout += result
return len(result)