[51770] | 1 | ; $Id: SUPR3HardenedMainA-win.asm 62490 2016-07-22 18:41:49Z vboxsync $
|
---|
| 2 | ;; @file
|
---|
| 3 | ; VirtualBox Support Library - Hardened main(), Windows assembly bits.
|
---|
| 4 | ;
|
---|
| 5 |
|
---|
| 6 | ;
|
---|
[62490] | 7 | ; Copyright (C) 2012-2016 Oracle Corporation
|
---|
[51770] | 8 | ;
|
---|
| 9 | ; This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
| 10 | ; available from http://www.virtualbox.org. This file is free software;
|
---|
| 11 | ; you can redistribute it and/or modify it under the terms of the GNU
|
---|
| 12 | ; General Public License (GPL) as published by the Free Software
|
---|
| 13 | ; Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
| 14 | ; VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
| 15 | ; hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
| 16 | ;
|
---|
| 17 | ; The contents of this file may alternatively be used under the terms
|
---|
| 18 | ; of the Common Development and Distribution License Version 1.0
|
---|
| 19 | ; (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
| 20 | ; VirtualBox OSE distribution, in which case the provisions of the
|
---|
| 21 | ; CDDL are applicable instead of those of the GPL.
|
---|
| 22 | ;
|
---|
| 23 | ; You may elect to license modified versions of this file under the
|
---|
| 24 | ; terms and conditions of either the GPL or the CDDL or both.
|
---|
| 25 | ;
|
---|
| 26 |
|
---|
| 27 | ;*******************************************************************************
|
---|
| 28 | ;* Header Files *
|
---|
| 29 | ;*******************************************************************************
|
---|
[52523] | 30 | %define RT_ASM_WITH_SEH64
|
---|
[51770] | 31 | %include "iprt/asmdefs.mac"
|
---|
| 32 |
|
---|
| 33 |
|
---|
[52943] | 34 | ; External code.
|
---|
[52949] | 35 | extern NAME(supR3HardenedEarlyProcessInit)
|
---|
[51770] | 36 |
|
---|
[52943] | 37 |
|
---|
[51770] | 38 | BEGINCODE
|
---|
| 39 |
|
---|
| 40 |
|
---|
[52356] | 41 | ;;
|
---|
[52949] | 42 | ; Alternative code for LdrInitializeThunk that performs the early process startup
|
---|
| 43 | ; for the Stub and VM processes.
|
---|
[52943] | 44 | ;
|
---|
| 45 | ; This does not concern itself with any arguments on stack or in registers that
|
---|
| 46 | ; may be passed to the LdrIntializeThunk routine as we just save and restore
|
---|
| 47 | ; them all before we restart the restored LdrInitializeThunk routine.
|
---|
| 48 | ;
|
---|
[52949] | 49 | ; @sa supR3HardenedEarlyProcessInit
|
---|
| 50 | ;
|
---|
| 51 | BEGINPROC supR3HardenedEarlyProcessInitThunk
|
---|
[52943] | 52 | ;
|
---|
| 53 | ; Prologue.
|
---|
| 54 | ;
|
---|
| 55 |
|
---|
| 56 | ; Reserve space for the "return" address.
|
---|
| 57 | push 0
|
---|
| 58 |
|
---|
| 59 | ; Create a stack frame, saving xBP.
|
---|
| 60 | push xBP
|
---|
| 61 | SEH64_PUSH_xBP
|
---|
| 62 | mov xBP, xSP
|
---|
| 63 | SEH64_SET_FRAME_xBP 0 ; probably wrong...
|
---|
| 64 |
|
---|
| 65 | ; Save all volatile registers.
|
---|
| 66 | push xAX
|
---|
| 67 | push xCX
|
---|
| 68 | push xDX
|
---|
| 69 | %ifdef RT_ARCH_AMD64
|
---|
| 70 | push r8
|
---|
| 71 | push r9
|
---|
| 72 | push r10
|
---|
| 73 | push r11
|
---|
| 74 | %endif
|
---|
| 75 |
|
---|
| 76 | ; Reserve spill space and align the stack.
|
---|
| 77 | sub xSP, 20h
|
---|
| 78 | and xSP, ~0fh
|
---|
| 79 | SEH64_END_PROLOGUE
|
---|
| 80 |
|
---|
| 81 | ;
|
---|
| 82 | ; Call the C/C++ code that does the actual work. This returns the
|
---|
| 83 | ; resume address in xAX, which we put in the "return" stack position.
|
---|
| 84 | ;
|
---|
[52949] | 85 | call NAME(supR3HardenedEarlyProcessInit)
|
---|
[52943] | 86 | mov [xBP + xCB], xAX
|
---|
| 87 |
|
---|
| 88 | ;
|
---|
| 89 | ; Restore volatile registers.
|
---|
| 90 | ;
|
---|
| 91 | mov xAX, [xBP - xCB*1]
|
---|
| 92 | mov xCX, [xBP - xCB*2]
|
---|
| 93 | mov xDX, [xBP - xCB*3]
|
---|
| 94 | %ifdef RT_ARCH_AMD64
|
---|
| 95 | mov r8, [xBP - xCB*4]
|
---|
| 96 | mov r9, [xBP - xCB*5]
|
---|
| 97 | mov r10, [xBP - xCB*6]
|
---|
| 98 | mov r11, [xBP - xCB*7]
|
---|
| 99 | %endif
|
---|
| 100 | ;
|
---|
| 101 | ; Use the leave instruction to restore xBP and set up xSP to point at
|
---|
| 102 | ; the resume address. Then use the 'ret' instruction to resume process
|
---|
| 103 | ; initializaton.
|
---|
| 104 | ;
|
---|
| 105 | leave
|
---|
| 106 | ret
|
---|
[52949] | 107 | ENDPROC supR3HardenedEarlyProcessInitThunk
|
---|
[52943] | 108 |
|
---|
| 109 |
|
---|
| 110 |
|
---|
| 111 | ;;
|
---|
[52356] | 112 | ; Composes a standard call name.
|
---|
| 113 | %ifdef RT_ARCH_X86
|
---|
| 114 | %define SUPHNTIMP_STDCALL_NAME(a,b) _ %+ a %+ @ %+ b
|
---|
| 115 | %else
|
---|
| 116 | %define SUPHNTIMP_STDCALL_NAME(a,b) NAME(a)
|
---|
| 117 | %endif
|
---|
| 118 |
|
---|
[52945] | 119 | ;; Concats two litterals.
|
---|
| 120 | %define SUPHNTIMP_CONCAT(a,b) a %+ b
|
---|
[52356] | 121 |
|
---|
[52945] | 122 |
|
---|
[52356] | 123 | ;;
|
---|
| 124 | ; Import data and code for an API call.
|
---|
| 125 | ;
|
---|
| 126 | ; @param 1 The plain API name.
|
---|
| 127 | ; @param 2 The parameter frame size on x86. Multiple of dword.
|
---|
| 128 | ; @param 3 Non-zero expression if system call.
|
---|
[52940] | 129 | ; @param 4 Non-zero expression if early available call
|
---|
[52356] | 130 | ;
|
---|
| 131 | %define SUPHNTIMP_SYSCALL 1
|
---|
[52940] | 132 | %macro SupHardNtImport 4
|
---|
[52356] | 133 | ;
|
---|
| 134 | ; The data.
|
---|
| 135 | ;
|
---|
| 136 | BEGINDATA
|
---|
[52367] | 137 | global __imp_ %+ SUPHNTIMP_STDCALL_NAME(%1,%2) ; The import name used via dllimport.
|
---|
| 138 | __imp_ %+ SUPHNTIMP_STDCALL_NAME(%1,%2):
|
---|
| 139 | GLOBALNAME g_pfn %+ %1 ; The name we like to refer to.
|
---|
[52356] | 140 | RTCCPTR_DEF 0
|
---|
| 141 | %if %3
|
---|
[52367] | 142 | GLOBALNAME g_uApiNo %+ %1
|
---|
[52356] | 143 | RTCCPTR_DEF 0
|
---|
| 144 | %endif
|
---|
| 145 |
|
---|
| 146 | ;
|
---|
| 147 | ; The code: First a call stub.
|
---|
| 148 | ;
|
---|
| 149 | BEGINCODE
|
---|
| 150 | global SUPHNTIMP_STDCALL_NAME(%1, %2)
|
---|
| 151 | SUPHNTIMP_STDCALL_NAME(%1, %2):
|
---|
| 152 | jmp RTCCPTR_PRE [NAME(g_pfn %+ %1) xWrtRIP]
|
---|
| 153 |
|
---|
| 154 | %if %3
|
---|
| 155 | ;
|
---|
| 156 | ; Make system calls.
|
---|
| 157 | ;
|
---|
| 158 | %ifdef RT_ARCH_AMD64
|
---|
| 159 | BEGINPROC %1 %+ _SyscallType1
|
---|
[52523] | 160 | SEH64_END_PROLOGUE
|
---|
[52356] | 161 | mov eax, [NAME(g_uApiNo %+ %1) xWrtRIP]
|
---|
| 162 | mov r10, rcx
|
---|
| 163 | syscall
|
---|
| 164 | ret
|
---|
| 165 | ENDPROC %1 %+ _SyscallType1
|
---|
[57650] | 166 | BEGINPROC %1 %+ _SyscallType2 ; Introduced with build 10525
|
---|
| 167 | SEH64_END_PROLOGUE
|
---|
| 168 | mov eax, [NAME(g_uApiNo %+ %1) xWrtRIP]
|
---|
| 169 | test byte [07ffe0308h], 1 ; SharedUserData!Something
|
---|
| 170 | mov r10, rcx
|
---|
| 171 | jnz .int_alternative
|
---|
| 172 | syscall
|
---|
| 173 | ret
|
---|
| 174 | .int_alternative:
|
---|
| 175 | int 2eh
|
---|
| 176 | ret
|
---|
| 177 | ENDPROC %1 %+ _SyscallType2
|
---|
[52356] | 178 | %else
|
---|
| 179 | BEGINPROC %1 %+ _SyscallType1
|
---|
| 180 | mov edx, 07ffe0300h ; SharedUserData!SystemCallStub
|
---|
| 181 | mov eax, [NAME(g_uApiNo %+ %1) xWrtRIP]
|
---|
[52374] | 182 | call dword [edx]
|
---|
[52356] | 183 | ret %2
|
---|
| 184 | ENDPROC %1 %+ _SyscallType1
|
---|
| 185 | BEGINPROC %1 %+ _SyscallType2
|
---|
| 186 | push .return
|
---|
| 187 | mov edx, esp
|
---|
| 188 | mov eax, [NAME(g_uApiNo %+ %1) xWrtRIP]
|
---|
| 189 | sysenter
|
---|
| 190 | add esp, 4
|
---|
| 191 | .return:
|
---|
| 192 | ret %2
|
---|
| 193 | ENDPROC %1 %+ _SyscallType2
|
---|
| 194 | %endif
|
---|
| 195 | %endif
|
---|
[52940] | 196 |
|
---|
| 197 | %if %4 == 0
|
---|
[52950] | 198 | global NAME(SUPHNTIMP_CONCAT(%1,_Early))
|
---|
| 199 | NAME(SUPHNTIMP_CONCAT(%1,_Early)):
|
---|
[52940] | 200 | int3
|
---|
| 201 | %ifdef RT_ARCH_AMD64
|
---|
| 202 | ret
|
---|
| 203 | %else
|
---|
| 204 | ret %2
|
---|
| 205 | %endif
|
---|
| 206 | %endif
|
---|
[52356] | 207 | %endmacro
|
---|
| 208 |
|
---|
| 209 | %define SUPHARNT_COMMENT(a_Comment)
|
---|
[56732] | 210 | %define SUPHARNT_IMPORT_SYSCALL(a_Name, a_cbParamsX86) SupHardNtImport a_Name, a_cbParamsX86, SUPHNTIMP_SYSCALL, 1
|
---|
| 211 | %define SUPHARNT_IMPORT_STDCALL(a_Name, a_cbParamsX86) SupHardNtImport a_Name, a_cbParamsX86, 0, 0
|
---|
| 212 | %define SUPHARNT_IMPORT_STDCALL_OPTIONAL(a_Name, a_cbParamsX86) SUPHARNT_IMPORT_STDCALL(a_Name, a_cbParamsX86)
|
---|
| 213 | %define SUPHARNT_IMPORT_STDCALL_EARLY(a_Name, a_cbParamsX86) SupHardNtImport a_Name, a_cbParamsX86, 0, 1
|
---|
[52941] | 214 | %define SUPHARNT_IMPORT_STDCALL_EARLY_OPTIONAL(a_Name, a_cbParamsX86) SUPHARNT_IMPORT_STDCALL_EARLY(a_Name, a_cbParamsX86)
|
---|
[52356] | 215 | %include "import-template-ntdll.h"
|
---|
| 216 | %include "import-template-kernel32.h"
|
---|
| 217 |
|
---|
[52403] | 218 |
|
---|
| 219 | ;
|
---|
| 220 | ; For simplified LdrLoadDll patching we define a special writable, readable and
|
---|
| 221 | ; exectuable section of 4KB where we can put jump back code.
|
---|
| 222 | ;
|
---|
| 223 | section .rwxpg bss execute read write align=4096
|
---|
| 224 | GLOBALNAME g_abSupHardReadWriteExecPage
|
---|
| 225 | resb 4096
|
---|
| 226 |
|
---|