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
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
#include <stdio.h>
#include <windows.h>
#include <array>
#include <utility>
#include "mozilla/WindowsStackCookie.h"
#if defined(DEBUG) && defined(_M_X64) && !defined(__MINGW64__)
uint64_t NoStackCookie(const uint64_t* aArray, size_t aSize) {
uint64_t result = 0;
for (size_t i = 0; i < aSize; ++i) {
result += aArray[i];
}
result /= aSize;
return result;
}
// We expect the following instructions to be generated:
//
// 48 8b 05 XX XX XX XX mov rax,qword ptr [__security_cookie]
// 48 31 e0 xor rax,rsp 48
// 89 44 24 38 mov qword ptr [rsp+38h],rax
uint64_t StackCookieWithSmallStackSpace(const uint64_t* aArray, size_t aSize) {
uint64_t array[0x2]{};
for (size_t i = 0; i < aSize; ++i) {
array[aArray[i]] += aArray[i];
}
return array[0] + array[1];
}
// We expect the following instructions to be generated:
//
// 48 8b 05 XX XX XX XX mov rax,qword ptr [__security_cookie]
// 48 31 e0 xor rax,rsp
// 48 89 84 24 28 40 00 00 mov qword ptr [rsp+4028h],rax
uint64_t StackCookieWithLargeStackSpace(const uint64_t* aArray, size_t aSize) {
uint64_t array[0x800]{};
for (size_t i = 0; i < aSize; ++i) {
array[aArray[i]] += aArray[i];
}
return array[0] + array[0x7FF];
}
bool TestStackCookieCheck() {
std::array<std::pair<uintptr_t, bool>, 3> testCases{
std::make_pair<uintptr_t, bool>(
reinterpret_cast<uintptr_t>(NoStackCookie), false),
std::make_pair<uintptr_t, bool>(
reinterpret_cast<uintptr_t>(StackCookieWithSmallStackSpace), true),
std::make_pair<uintptr_t, bool>(
reinterpret_cast<uintptr_t>(StackCookieWithLargeStackSpace), true),
};
for (auto [functionAddress, expectStackCookieCheck] : testCases) {
if (mozilla::HasStackCookieCheck(functionAddress) !=
expectStackCookieCheck) {
printf(
"TEST-FAILED | StackCookie | Wrong output from HasStackCookieCheck "
"for function at %p (expected %d).\n",
reinterpret_cast<void*>(functionAddress), expectStackCookieCheck);
return false;
}
printf(
"TEST-PASS | StackCookie | Correct output from HasStackCookieCheck for "
"function at %p (expected %d).\n",
reinterpret_cast<void*>(functionAddress), expectStackCookieCheck);
}
return true;
}
#endif // defined(DEBUG) && defined(_M_X64) && !defined(__MINGW64__)
int wmain(int argc, wchar_t* argv[]) {
#if defined(DEBUG) && defined(_M_X64) && !defined(__MINGW64__)
if (!TestStackCookieCheck()) {
return 1;
}
#endif // defined(DEBUG) && defined(_M_X64) && !defined(__MINGW64__)
printf("TEST-PASS | StackCookie | All tests ran successfully\n");
return 0;
}
|