main.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 <vector>
25 #include <string>
26 #include <memory>
27 #include <iostream>
28 #include <exception.h>
29 
30 #include <command_line_parser.h>
31 #include <file.h>
32 #include <ioctl.h>
33 #include <ioctl_driver.h>
34 
35 #if !defined(__CYGWIN__) && !defined(_WIN32)
36 
37 #include <sys/mman.h>
38 
39 void
41 {
42  mlockall(MCL_CURRENT);
43  munlockall();
44 }
45 
46 #else
47 
48 void
49 flush()
50 {
51 }
52 
53 #endif
54 
55 void
57 {
58  std::cerr << "FATAL ERROR: terminate called" << '\n';
59  abort();
60 }
61 
62 void
64 {
65  std::cerr << "FATAL ERROR: out of memory" << '\n';
67 }
68 
69 void
71 {
72  std::cout << "Usage: bfm [OPTION]... load... list_of_modules..." << std::endl;
73  std::cout << " or: bfm [OPTION]... unload..." << std::endl;
74  std::cout << " or: bfm [OPTION]... start..." << std::endl;
75  std::cout << " or: bfm [OPTION]... stop..." << std::endl;
76  std::cout << " or: bfm [OPTION]... dump..." << std::endl;
77  std::cout << " or: bfm [OPTION]... status..." << std::endl;
78  std::cout << " or: bfm [OPTION]... vmcall versions index..." << std::endl;
79  std::cout << " or: bfm [OPTION]... vmcall registers r2 r3...r15" << std::endl;
80  std::cout << " or: bfm [OPTION]... vmcall string type \"\"..." << std::endl;
81  std::cout << " or: bfm [OPTION]... vmcall data type ifile ofile..." << std::endl;
82  std::cout << " or: bfm [OPTION]... vmcall unittest index..." << std::endl;
83  std::cout << " or: bfm [OPTION]... vmcall event index..." << std::endl;
84  std::cout << "Controls or queries the bareflank hypervisor" << std::endl;
85  std::cout << std::endl;
86  std::cout << " -h, --help show this help menu" << std::endl;
87  std::cout << " --cpuid indicate the requested cpuid" << std::endl;
88  std::cout << " --vcpuid indicate the requested vcpuid" << std::endl;
89  std::cout << std::endl;
90  std::cout << " vmcall string types:" << std::endl;
91  std::cout << " unformatted unformatted string" << std::endl;
92  std::cout << " json json formatted string" << std::endl;
93  std::cout << std::endl;
94  std::cout << " vmcall binary types:" << std::endl;
95  std::cout << " unformatted unformatted binary data" << std::endl;
96  std::cout << std::endl;
97  std::cout << " vmcall notes:" << std::endl;
98  std::cout << " - registers are represented in hex" << std::endl;
99  std::cout << " - data / string uuids equal 0" << std::endl;
100 }
101 
102 int
104 {
105  // -------------------------------------------------------------------------
106  // Command Line Parser
107 
108  auto &&clp = std::make_unique<command_line_parser>();
109 
110  try
111  {
112  clp->parse(args);
113  }
114  catch (bfn::general_exception &ge)
115  {
116  std::cerr << "bfm: " << ge << '\n';
117  std::cerr << "Try `bfm --help' for more information." << '\n';
118 
119  return EXIT_FAILURE;
120  }
121 
122  if (clp->cmd() == command_line_parser_command::help)
123  {
124  help();
125  return EXIT_SUCCESS;
126  }
127 
128  // -------------------------------------------------------------------------
129  // IO Controller
130 
131  auto &&ctl = std::make_unique<ioctl>();
132 
133  try
134  {
135  ctl->open();
136  }
137  catch (bfn::general_exception &ge)
138  {
139  std::cerr << "bfm: " << ge << '\n';
140 
141  return EXIT_FAILURE;
142  }
143 
144  // -------------------------------------------------------------------------
145  // Page-In Memory
146 
147  // TODO: In the future, we should create an RAII class that locks just the
148  // pages being mapped to the hypervisor, which will need to be page
149  // aligned. For now, this should work on most system
150 
151  flush();
152 
153  // -------------------------------------------------------------------------
154  // IOCTR Driver
155 
156  try
157  {
158  auto &&f = std::make_unique<file>();
159  auto &&driver = std::make_unique<ioctl_driver>(f.get(), ctl.get(), clp.get());
160 
161  driver->process();
162  }
163  catch (bfn::general_exception &ge)
164  {
165  std::cerr << "bfm: " << ge << '\n';
166 
167  return EXIT_FAILURE;
168  }
169 
170  return EXIT_SUCCESS;
171 }
172 
173 int
174 main(int argc, const char *argv[])
175 {
176  std::set_terminate(terminate);
177  std::set_new_handler(new_handler);
178 
179  try
180  {
182  auto args_span = gsl::make_span(argv, argc);
183 
184  for (auto i = 1; i < argc; i++)
185  args.push_back(args_span[i]);
186 
187  return protected_main(args);
188  }
189  catch (std::exception &e)
190  {
191  std::cerr << "Caught unhandled exception:" << '\n';
192  std::cerr << " - what(): " << e.what() << '\n';
193  }
194  catch (...)
195  {
196  std::cerr << "Caught unknown exception" << '\n';
197  }
198 
199  return EXIT_FAILURE;
200 }
void flush()
Definition: main.cpp:40
void help()
Definition: main.cpp:70
int protected_main(const command_line_parser::arg_list_type &args)
Definition: main.cpp:103
int main(int argc, const char *argv[])
Definition: main.cpp:174
std::vector< arg_type > arg_list_type
void terminate()
Definition: main.cpp:56
void new_handler()
Definition: main.cpp:63