|
1 | 1 | #!/usr/bin/env python3 |
2 | | -# |
| 2 | +# |
3 | 3 | # Cross Platform and Multi Architecture Advanced Binary Emulation Framework |
4 | 4 | # |
5 | 5 |
|
@@ -53,31 +53,63 @@ def hook_mcount(ql, address, params): |
53 | 53 | return 0 |
54 | 54 |
|
55 | 55 |
|
56 | | -@linux_kernel_api(params={ |
57 | | - "Ptr": POINTER |
58 | | -}) |
59 | | -def hook___x86_indirect_thunk_rax(ql, address, params): |
60 | | - return 0 |
| 56 | +def __x86_indirect_thunk(ql: Qiling, dest: int): |
| 57 | + ql.log.debug('retpoline to %#010x', dest) |
61 | 58 |
|
| 59 | + ql.arch.regs.arch_pc = dest |
| 60 | + |
| 61 | +# using passthru as a hack to avoid syscall handler overwrite instruction pointer |
| 62 | +@linux_kernel_api(passthru=True) |
| 63 | +def hook___x86_indirect_thunk_rax(ql: Qiling, address: int, params): |
| 64 | + __x86_indirect_thunk(ql, ql.arch.regs.rax) |
62 | 65 |
|
63 | | -@linux_kernel_api(params={ |
64 | | - "Ptr": POINTER |
65 | | -}) |
66 | | -def hook__copy_to_user(ql, address, params): |
67 | | - return 0 |
| 66 | + |
| 67 | +@linux_kernel_api(passthru=True) |
| 68 | +def hook___x86_indirect_thunk_r14(ql, address, params): |
| 69 | + __x86_indirect_thunk(ql, ql.arch.regs.r14) |
68 | 70 |
|
69 | 71 |
|
70 | 72 | @linux_kernel_api(params={ |
71 | | - "Ptr": POINTER |
| 73 | + "ubuf": POINTER, |
| 74 | + "kbuf": POINTER, |
| 75 | + "count": SIZE_T |
72 | 76 | }) |
73 | | -def hook__copy_from_user(ql, address, params): |
| 77 | +def hook__copy_to_user(ql: Qiling, address: int, params) -> int: |
| 78 | + ubuf = params['ubuf'] |
| 79 | + kbuf = params['kbuf'] |
| 80 | + count = params['count'] |
| 81 | + |
| 82 | + # if user-mode buffer is not available, fail |
| 83 | + # TODO: also fail if destination is not writeable |
| 84 | + if not ql.mem.is_mapped(ubuf, count): |
| 85 | + return count |
| 86 | + |
| 87 | + data = ql.mem.read(kbuf, count) |
| 88 | + |
| 89 | + ql.mem.write(ubuf, data) |
| 90 | + |
74 | 91 | return 0 |
75 | 92 |
|
76 | 93 |
|
77 | 94 | @linux_kernel_api(params={ |
78 | | - "Ptr": POINTER |
| 95 | + "kbuf": POINTER, |
| 96 | + "ubuf": POINTER, |
| 97 | + "count": SIZE_T |
79 | 98 | }) |
80 | | -def hook___x86_indirect_thunk_r14(ql, address, params): |
| 99 | +def hook__copy_from_user(ql: Qiling, address: int, params) -> int: |
| 100 | + ubuf = params['ubuf'] |
| 101 | + kbuf = params['kbuf'] |
| 102 | + count = params['count'] |
| 103 | + |
| 104 | + # if user-mode buffer is not available, fail |
| 105 | + # TODO: also fail if source is not readable |
| 106 | + if not ql.mem.is_mapped(ubuf, count): |
| 107 | + return count |
| 108 | + |
| 109 | + data = ql.mem.read(ubuf, count) |
| 110 | + |
| 111 | + ql.mem.write(kbuf, data) |
| 112 | + |
81 | 113 | return 0 |
82 | 114 |
|
83 | 115 |
|
|
0 commit comments