Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
michael@0 | 2 | // Use of this source code is governed by a BSD-style license that can be |
michael@0 | 3 | // found in the LICENSE file. |
michael@0 | 4 | |
michael@0 | 5 | // Definition of PreamblePatcher |
michael@0 | 6 | |
michael@0 | 7 | #ifndef SANDBOX_SRC_SIDESTEP_PREAMBLE_PATCHER_H__ |
michael@0 | 8 | #define SANDBOX_SRC_SIDESTEP_PREAMBLE_PATCHER_H__ |
michael@0 | 9 | |
michael@0 | 10 | #include <stddef.h> |
michael@0 | 11 | |
michael@0 | 12 | namespace sidestep { |
michael@0 | 13 | |
michael@0 | 14 | // Maximum size of the preamble stub. We overwrite at least the first 5 |
michael@0 | 15 | // bytes of the function. Considering the worst case scenario, we need 4 |
michael@0 | 16 | // bytes + the max instruction size + 5 more bytes for our jump back to |
michael@0 | 17 | // the original code. With that in mind, 32 is a good number :) |
michael@0 | 18 | const size_t kMaxPreambleStubSize = 32; |
michael@0 | 19 | |
michael@0 | 20 | // Possible results of patching/unpatching |
michael@0 | 21 | enum SideStepError { |
michael@0 | 22 | SIDESTEP_SUCCESS = 0, |
michael@0 | 23 | SIDESTEP_INVALID_PARAMETER, |
michael@0 | 24 | SIDESTEP_INSUFFICIENT_BUFFER, |
michael@0 | 25 | SIDESTEP_JUMP_INSTRUCTION, |
michael@0 | 26 | SIDESTEP_FUNCTION_TOO_SMALL, |
michael@0 | 27 | SIDESTEP_UNSUPPORTED_INSTRUCTION, |
michael@0 | 28 | SIDESTEP_NO_SUCH_MODULE, |
michael@0 | 29 | SIDESTEP_NO_SUCH_FUNCTION, |
michael@0 | 30 | SIDESTEP_ACCESS_DENIED, |
michael@0 | 31 | SIDESTEP_UNEXPECTED, |
michael@0 | 32 | }; |
michael@0 | 33 | |
michael@0 | 34 | // Implements a patching mechanism that overwrites the first few bytes of |
michael@0 | 35 | // a function preamble with a jump to our hook function, which is then |
michael@0 | 36 | // able to call the original function via a specially-made preamble-stub |
michael@0 | 37 | // that imitates the action of the original preamble. |
michael@0 | 38 | // |
michael@0 | 39 | // Note that there are a number of ways that this method of patching can |
michael@0 | 40 | // fail. The most common are: |
michael@0 | 41 | // - If there is a jump (jxx) instruction in the first 5 bytes of |
michael@0 | 42 | // the function being patched, we cannot patch it because in the |
michael@0 | 43 | // current implementation we do not know how to rewrite relative |
michael@0 | 44 | // jumps after relocating them to the preamble-stub. Note that |
michael@0 | 45 | // if you really really need to patch a function like this, it |
michael@0 | 46 | // would be possible to add this functionality (but at some cost). |
michael@0 | 47 | // - If there is a return (ret) instruction in the first 5 bytes |
michael@0 | 48 | // we cannot patch the function because it may not be long enough |
michael@0 | 49 | // for the jmp instruction we use to inject our patch. |
michael@0 | 50 | // - If there is another thread currently executing within the bytes |
michael@0 | 51 | // that are copied to the preamble stub, it will crash in an undefined |
michael@0 | 52 | // way. |
michael@0 | 53 | // |
michael@0 | 54 | // If you get any other error than the above, you're either pointing the |
michael@0 | 55 | // patcher at an invalid instruction (e.g. into the middle of a multi- |
michael@0 | 56 | // byte instruction, or not at memory containing executable instructions) |
michael@0 | 57 | // or, there may be a bug in the disassembler we use to find |
michael@0 | 58 | // instruction boundaries. |
michael@0 | 59 | class PreamblePatcher { |
michael@0 | 60 | public: |
michael@0 | 61 | // Patches target_function to point to replacement_function using a provided |
michael@0 | 62 | // preamble_stub of stub_size bytes. |
michael@0 | 63 | // Returns An error code indicating the result of patching. |
michael@0 | 64 | template <class T> |
michael@0 | 65 | static SideStepError Patch(T target_function, T replacement_function, |
michael@0 | 66 | void* preamble_stub, size_t stub_size) { |
michael@0 | 67 | return RawPatchWithStub(target_function, replacement_function, |
michael@0 | 68 | reinterpret_cast<unsigned char*>(preamble_stub), |
michael@0 | 69 | stub_size, NULL); |
michael@0 | 70 | } |
michael@0 | 71 | |
michael@0 | 72 | private: |
michael@0 | 73 | |
michael@0 | 74 | // Patches a function by overwriting its first few bytes with |
michael@0 | 75 | // a jump to a different function. This is similar to the RawPatch |
michael@0 | 76 | // function except that it uses the stub allocated by the caller |
michael@0 | 77 | // instead of allocating it. |
michael@0 | 78 | // |
michael@0 | 79 | // To use this function, you first have to call VirtualProtect to make the |
michael@0 | 80 | // target function writable at least for the duration of the call. |
michael@0 | 81 | // |
michael@0 | 82 | // target_function: A pointer to the function that should be |
michael@0 | 83 | // patched. |
michael@0 | 84 | // |
michael@0 | 85 | // replacement_function: A pointer to the function that should |
michael@0 | 86 | // replace the target function. The replacement function must have |
michael@0 | 87 | // exactly the same calling convention and parameters as the original |
michael@0 | 88 | // function. |
michael@0 | 89 | // |
michael@0 | 90 | // preamble_stub: A pointer to a buffer where the preamble stub |
michael@0 | 91 | // should be copied. The size of the buffer should be sufficient to |
michael@0 | 92 | // hold the preamble bytes. |
michael@0 | 93 | // |
michael@0 | 94 | // stub_size: Size in bytes of the buffer allocated for the |
michael@0 | 95 | // preamble_stub |
michael@0 | 96 | // |
michael@0 | 97 | // bytes_needed: Pointer to a variable that receives the minimum |
michael@0 | 98 | // number of bytes required for the stub. Can be set to NULL if you're |
michael@0 | 99 | // not interested. |
michael@0 | 100 | // |
michael@0 | 101 | // Returns An error code indicating the result of patching. |
michael@0 | 102 | static SideStepError RawPatchWithStub(void* target_function, |
michael@0 | 103 | void *replacement_function, |
michael@0 | 104 | unsigned char* preamble_stub, |
michael@0 | 105 | size_t stub_size, |
michael@0 | 106 | size_t* bytes_needed); |
michael@0 | 107 | }; |
michael@0 | 108 | |
michael@0 | 109 | }; // namespace sidestep |
michael@0 | 110 | |
michael@0 | 111 | #endif // SANDBOX_SRC_SIDESTEP_PREAMBLE_PATCHER_H__ |