test_common_load.cpp
Go to the documentation of this file.
1 //
2 // Bareflank Hypervisor
3 //
4 // Copyright (C) 2015 Assured Information Security, Inc.
5 // Author: Rian Quinn <quinnr@ainfosec.com>
6 // Author: Brendan Kerrigan <kerriganb@ainfosec.com>
7 //
8 // This library is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU Lesser General Public
10 // License as published by the Free Software Foundation; either
11 // version 2.1 of the License, or (at your option) any later version.
12 //
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Lesser General Public License for more details.
17 //
18 // You should have received a copy of the GNU Lesser General Public
19 // License along with this library; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 
22 #include <gsl/gsl>
23 
24 #include <test.h>
25 
26 #include <memory.h>
27 #include <common.h>
28 #include <platform.h>
29 #include <driver_entry_interface.h>
30 
31 // -----------------------------------------------------------------------------
32 // Expose Private Functions
33 // -----------------------------------------------------------------------------
34 
35 extern "C"
36 {
37  struct module_t *get_module(uint64_t index);
38  int64_t resolve_symbol(const char *name, void **sym);
39  int64_t execute_symbol(const char *sym, uint64_t arg1, uint64_t arg2, uint64_t cpuid);
40  int64_t add_raw_md_to_memory_manager(uint64_t virt, uint64_t type);
41  int64_t add_md_to_memory_manager(struct module_t *module);
42 }
43 
44 extern uint64_t g_malloc_fails;
45 
46 // -----------------------------------------------------------------------------
47 // Tests
48 // -----------------------------------------------------------------------------
49 
50 void
51 driver_entry_ut::test_common_load_successful_load()
52 {
53  this->expect_true(common_add_module(m_dummy_start_vmm_success.get(), m_dummy_start_vmm_success_length) == BF_SUCCESS);
54  this->expect_true(common_add_module(m_dummy_stop_vmm_success.get(), m_dummy_stop_vmm_success_length) == BF_SUCCESS);
55  this->expect_true(common_add_module(m_dummy_add_md_success.get(), m_dummy_add_md_success_length) == BF_SUCCESS);
56  this->expect_true(common_add_module(m_dummy_misc.get(), m_dummy_misc_length) == BF_SUCCESS);
61 }
62 
63 void
64 driver_entry_ut::test_common_load_load_when_already_loaded()
65 {
66  this->expect_true(common_add_module(m_dummy_start_vmm_success.get(), m_dummy_start_vmm_success_length) == BF_SUCCESS);
67  this->expect_true(common_add_module(m_dummy_stop_vmm_success.get(), m_dummy_stop_vmm_success_length) == BF_SUCCESS);
68  this->expect_true(common_add_module(m_dummy_add_md_success.get(), m_dummy_add_md_success_length) == BF_SUCCESS);
69  this->expect_true(common_add_module(m_dummy_misc.get(), m_dummy_misc_length) == BF_SUCCESS);
75 }
76 
77 void
78 driver_entry_ut::test_common_load_load_when_already_running()
79 {
80  this->expect_true(common_add_module(m_dummy_start_vmm_success.get(), m_dummy_start_vmm_success_length) == BF_SUCCESS);
81  this->expect_true(common_add_module(m_dummy_stop_vmm_success.get(), m_dummy_stop_vmm_success_length) == BF_SUCCESS);
82  this->expect_true(common_add_module(m_dummy_add_md_success.get(), m_dummy_add_md_success_length) == BF_SUCCESS);
83  this->expect_true(common_add_module(m_dummy_misc.get(), m_dummy_misc_length) == BF_SUCCESS);
90 }
91 
92 void
93 driver_entry_ut::test_common_load_load_when_corrupt()
94 {
95  this->expect_true(common_add_module(m_dummy_start_vmm_success.get(), m_dummy_start_vmm_success_length) == BF_SUCCESS);
96  this->expect_true(common_add_module(m_dummy_stop_vmm_failure.get(), m_dummy_stop_vmm_failure_length) == BF_SUCCESS);
97  this->expect_true(common_add_module(m_dummy_add_md_success.get(), m_dummy_add_md_success_length) == BF_SUCCESS);
98  this->expect_true(common_add_module(m_dummy_misc.get(), m_dummy_misc_length) == BF_SUCCESS);
104 
105  common_reset();
106 }
107 
108 void
109 driver_entry_ut::test_common_load_fail_due_to_relocation_error()
110 {
111  this->expect_true(common_add_module(m_dummy_start_vmm_success.get(), m_dummy_start_vmm_success_length) == BF_SUCCESS);
112  this->expect_true(common_add_module(m_dummy_stop_vmm_success.get(), m_dummy_stop_vmm_success_length) == BF_SUCCESS);
115  this->expect_true(common_fini() == BF_SUCCESS);
117 }
118 
119 void
120 driver_entry_ut::test_common_load_fail_due_to_no_modules_added()
121 {
124  this->expect_true(common_fini() == BF_SUCCESS);
126 }
127 
128 void
129 driver_entry_ut::test_common_load_add_md_failed()
130 {
131  this->expect_true(common_add_module(m_dummy_start_vmm_success.get(), m_dummy_start_vmm_success_length) == BF_SUCCESS);
132  this->expect_true(common_add_module(m_dummy_stop_vmm_success.get(), m_dummy_stop_vmm_success_length) == BF_SUCCESS);
133  this->expect_true(common_add_module(m_dummy_add_md_failure.get(), m_dummy_add_md_failure_length) == BF_SUCCESS);
134  this->expect_true(common_add_module(m_dummy_misc.get(), m_dummy_misc_length) == BF_SUCCESS);
136  this->expect_true(common_fini() == BF_SUCCESS);
137 }
138 
139 void
140 driver_entry_ut::test_common_load_add_md_tls_failed()
141 {
142  this->expect_true(common_add_module(m_dummy_start_vmm_success.get(), m_dummy_start_vmm_success_length) == BF_SUCCESS);
143  this->expect_true(common_add_module(m_dummy_stop_vmm_success.get(), m_dummy_stop_vmm_success_length) == BF_SUCCESS);
144  this->expect_true(common_add_module(m_dummy_add_md_success.get(), m_dummy_add_md_success_length) == BF_SUCCESS);
145  this->expect_true(common_add_module(m_dummy_misc.get(), m_dummy_misc_length) == BF_SUCCESS);
146 
147  {
148  MockRepository mocks;
149  mocks.OnCallFunc(add_md_to_memory_manager).Return(0);
150  mocks.ExpectCallFunc(add_raw_md_to_memory_manager).Return(-1);
151 
152  RUN_UNITTEST_WITH_MOCKS(mocks, [&]
153  {
154  this->expect_true(common_load_vmm() == -1);
155  });
156  }
157 
158  this->expect_true(common_fini() == BF_SUCCESS);
159 }
160 
161 void
162 driver_entry_ut::test_common_load_tls_platform_alloc_failed()
163 {
165 
166  auto ___ = gsl::finally([&]
167  { g_malloc_fails = 0; });
168 
169  this->expect_true(common_add_module(m_dummy_start_vmm_success.get(), m_dummy_start_vmm_success_length) == BF_SUCCESS);
170  this->expect_true(common_add_module(m_dummy_stop_vmm_success.get(), m_dummy_stop_vmm_success_length) == BF_SUCCESS);
171  this->expect_true(common_add_module(m_dummy_add_md_success.get(), m_dummy_add_md_success_length) == BF_SUCCESS);
172  this->expect_true(common_add_module(m_dummy_misc.get(), m_dummy_misc_length) == BF_SUCCESS);
174  this->expect_true(common_fini() == BF_SUCCESS);
175 }
176 
177 void
178 driver_entry_ut::test_common_load_stack_platform_alloc_failed()
179 {
181 
182  auto ___ = gsl::finally([&]
183  { g_malloc_fails = 0; });
184 
185  this->expect_true(common_add_module(m_dummy_start_vmm_success.get(), m_dummy_start_vmm_success_length) == BF_SUCCESS);
186  this->expect_true(common_add_module(m_dummy_stop_vmm_success.get(), m_dummy_stop_vmm_success_length) == BF_SUCCESS);
187  this->expect_true(common_add_module(m_dummy_add_md_success.get(), m_dummy_add_md_success_length) == BF_SUCCESS);
188  this->expect_true(common_add_module(m_dummy_misc.get(), m_dummy_misc_length) == BF_SUCCESS);
190  this->expect_true(common_fini() == BF_SUCCESS);
191 }
192 
193 void
194 driver_entry_ut::test_common_load_loader_add_failed()
195 {
196  this->expect_true(common_add_module(m_dummy_start_vmm_success.get(), m_dummy_start_vmm_success_length) == BF_SUCCESS);
197  this->expect_true(common_add_module(m_dummy_stop_vmm_success.get(), m_dummy_stop_vmm_success_length) == BF_SUCCESS);
198  this->expect_true(common_add_module(m_dummy_add_md_success.get(), m_dummy_add_md_success_length) == BF_SUCCESS);
199  this->expect_true(common_add_module(m_dummy_misc.get(), m_dummy_misc_length) == BF_SUCCESS);
200 
201  auto module = get_module(0);
202  module->file.added = 1;
203 
205  this->expect_true(common_fini() == BF_SUCCESS);
206 }
207 
208 void
209 driver_entry_ut::test_common_load_resolve_symbol_failed()
210 {
211  this->expect_true(common_add_module(m_dummy_start_vmm_success.get(), m_dummy_start_vmm_success_length) == BF_SUCCESS);
212  this->expect_true(common_add_module(m_dummy_stop_vmm_success.get(), m_dummy_stop_vmm_success_length) == BF_SUCCESS);
213  this->expect_true(common_add_module(m_dummy_add_md_success.get(), m_dummy_add_md_success_length) == BF_SUCCESS);
214  this->expect_true(common_add_module(m_dummy_misc.get(), m_dummy_misc_length) == BF_SUCCESS);
215 
216  {
217  MockRepository mocks;
218  mocks.ExpectCallFunc(resolve_symbol).Return(-1);
219 
220  RUN_UNITTEST_WITH_MOCKS(mocks, [&]
221  {
222  this->expect_true(common_load_vmm() == -1);
223  });
224  }
225 
226  this->expect_true(common_fini() == BF_SUCCESS);
227 }
228 
229 void
230 driver_entry_ut::test_common_load_execute_symbol_failed()
231 {
232  this->expect_true(common_add_module(m_dummy_start_vmm_success.get(), m_dummy_start_vmm_success_length) == BF_SUCCESS);
233  this->expect_true(common_add_module(m_dummy_stop_vmm_success.get(), m_dummy_stop_vmm_success_length) == BF_SUCCESS);
234  this->expect_true(common_add_module(m_dummy_add_md_success.get(), m_dummy_add_md_success_length) == BF_SUCCESS);
235  this->expect_true(common_add_module(m_dummy_misc.get(), m_dummy_misc_length) == BF_SUCCESS);
236 
237  {
238  MockRepository mocks;
239  mocks.ExpectCallFunc(execute_symbol).Return(-1);
240 
241  RUN_UNITTEST_WITH_MOCKS(mocks, [&]
242  {
243  this->expect_true(common_load_vmm() == -1);
244  });
245  }
246 
247  this->expect_true(common_fini() == BF_SUCCESS);
248 }
#define BF_SUCCESS
Definition: error_codes.h:105
int64_t common_add_module(const char *file, uint64_t fsize)
Definition: common.c:305
#define VMM_LOADED
#define RUN_UNITTEST_WITH_MOCKS(a, b)
Definition: unittest.h:229
#define BFELF_ERROR_INVALID_ARG
Definition: error_codes.h:81
int64_t common_load_vmm(void)
Definition: common.c:357
int64_t common_fini(void)
Definition: common.c:273
#define BFELF_ERROR_NO_SUCH_SYMBOL
Definition: error_codes.h:89
int64_t execute_symbol(const char *sym, uint64_t arg1, uint64_t arg2, uint64_t cpuid)
Definition: common.c:108
#define BF_ERROR_VMM_INVALID_STATE
Definition: error_codes.h:110
#define MEMORY_MANAGER_FAILURE
Definition: error_codes.h:99
uint64_t g_malloc_fails
Definition: platform.c:37
#define THREAD_LOCAL_STORAGE_SIZE
Definition: constants.h:241
auto index(const T virt, const F from)
struct module_t * get_module(uint64_t index)
#define VMM_UNLOADED
constexpr page_table_x64::integer_pointer virt
int64_t common_start_vmm(void)
Definition: common.c:490
int64_t add_md_to_memory_manager(struct module_t *module)
Definition: common.c:152
#define BF_ERROR_OUT_OF_MEMORY
Definition: error_codes.h:113
#define BF_ERROR_NO_MODULES_ADDED
Definition: error_codes.h:108
constexpr const auto name
Definition: cpuid_x64.h:81
#define STACK_SIZE
Definition: constants.h:225
int64_t resolve_symbol(const char *name, void **sym)
Definition: common.c:87
#define VMM_RUNNING
#define VMM_CORRUPT
int64_t add_raw_md_to_memory_manager(uint64_t virt, uint64_t type)
Definition: common.c:135
int64_t common_vmm_status(void)
Definition: common.c:227
#define BF_ERROR_VMM_CORRUPTED
Definition: error_codes.h:114
#define expect_true(a)
int64_t common_reset(void)
Definition: common.c:233