-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathExecFlat.hpp
More file actions
313 lines (278 loc) · 9.78 KB
/
ExecFlat.hpp
File metadata and controls
313 lines (278 loc) · 9.78 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/**
* @file ExecFlat.hpp
* @author Samsung R&D Poland - Mobile Security Group (srpol.mb.sec@samsung.com)
* @brief ExecFlat library created to provide an easy API for running KFLAT recipes.
*
*/
#ifndef EXECFLAT_HDR
#define EXECFLAT_HDR
#include <sched.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <cstdarg>
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <cstddef>
#include <poll.h>
#include <signal.h>
#include <cerrno>
#include <fcntl.h>
#include <sys/mman.h>
#include <errno.h>
#include <string>
#include <map>
#include <stdexcept>
#include <iostream>
#include <filesystem>
#include <fstream>
#include <vector>
#include <functional>
#include <system_error>
#include <chrono>
#include <ctime>
#include <iomanip>
#include "kflat_uapi.h"
#define ERRNO_TO_EXCEPTION(_comment) { \
std::stringstream __ss; \
__ss << "KFLAT: " << (_comment) << "\nERRNO"; \
throw std::system_error(errno, std::generic_category(), __ss.str()); \
}
#define LOG(msg_level) ExecFlatLogger((msg_level), log_level, start_time)
namespace fs = std::filesystem;
namespace ExecFlatOpts{
/**
* @brief Used as a ExecFlat contructor parameter \n
* that determines the verbosity level of the ExecFlat library.
*
*/
enum ExecFlatVerbosity {
SUPRESS,
ERROR,
WARNING,
INFO,
DEBUG,
};
/**
* @brief Supported types of interfaces for automatic recipe trigger.
*
*/
enum ExecFlatInterface {
READ,
SHOW,
WRITE,
STORE,
IOCTL,
COMPAT_IOCTL,
};
}
using namespace ExecFlatOpts;
class TermColor {
public:
enum ColorCode {
FG_RED = 31,
FG_YELLOW = 33,
FG_GREEN = 32,
FG_BLUE = 34,
FG_CYAN = 36,
FG_DEFAULT = 39,
};
static std::string set(ColorCode code) {
std::stringstream s;
s << "\033[" << code << "m";
return s.str();
}
static std::string clear() {
std::stringstream s;
s << "\033[" << FG_DEFAULT << "m";
return s.str();
}
};
class ExecFlatLogger {
public:
ExecFlatLogger( ExecFlatVerbosity msg_level,
ExecFlatVerbosity log_level,
std::chrono::system_clock::time_point start
) : msg_level(msg_level), log_level(log_level), start_time(start) { }
~ExecFlatLogger() {
if (opened)
std::cerr << std::endl;
opened = false;
}
template<class T>
ExecFlatLogger &operator<<(const T &msg) {
if (log_level >= msg_level) {
if (!opened) {
std::chrono::duration<double> elapsed = std::chrono::system_clock::now() - start_time;
std::cerr
<< TermColor::set(TermColor::FG_GREEN)
<< "[ExecFlat] [" << std::fixed << elapsed.count() << "] "
<< TermColor::set(get_level_color())
<< std::setw(8) << get_level_str() << ": "
<< TermColor::clear();
}
opened = true;
std::cerr << msg;
}
return *this;
}
private:
ExecFlatVerbosity msg_level, log_level;
bool opened = false;
std::chrono::system_clock::time_point start_time;
inline std::string get_level_str() {
switch (msg_level)
{
case WARNING: return "WARNING";
case INFO: return "INFO";
case DEBUG: return "DEBUG";
case ERROR: return "ERROR";
default: return "UNKNOWN";
}
}
inline TermColor::ColorCode get_level_color() {
switch (msg_level)
{
case WARNING: return TermColor::FG_YELLOW;
case INFO: return TermColor::FG_CYAN;
case DEBUG: return TermColor::FG_DEFAULT;
case ERROR: return TermColor::FG_RED;
default: return TermColor::FG_DEFAULT;
}
}
};
/**
* @brief Allow to easily execute KFLAT recipes and save dumps to files.
*
*/
class ExecFlat {
public:
/**
* @brief Construct and initialize a new ExecFlat object.
*
* @param dump_size Max size of kflat memory dump.
* @param log_level One of ExecFlatVerbosity enum members.
*/
ExecFlat(size_t dump_size, ExecFlatVerbosity log_level);
~ExecFlat();
/**
* @brief Run a KFLAT recipe with a given target file. After enabling KFLAT, chosen file operation will be called on the target.
*
* @param interface One of ExecFlatInterface. The type of file operation to perform on TARGET.
* @param target Path to the file to call INTERFACE on.
* @param recipe Name of the KFLAT recipe.
* @param outfile Path to the file where the dump will be saved.
* @param use_stop_machine Execute the KFLAT recipe under kernel's stop_machine mode.
* @param debug Enable KFLAT LKM logging to dmesg.
* @param skip_func_body Skip executing function body after the recipe finishes flattening.
* @param run_recipe_now Execute KFLAT recipe directly from IOCTL without attaching to any kernel function.
* @param target_timeout In seconds. Timeout for INTERFACE call on TARGET.
* @param poll_timeout In miliseconds. Timeout for recipe execution.
*/
void run_recipe(
ExecFlatInterface interface,
const fs::path &target,
const std::string &recipe,
const fs::path &outfile,
bool use_stop_machine=false,
bool debug=true,
bool skip_func_body=false,
bool run_recipe_now=false,
unsigned int target_timeout=0,
int poll_timeout=-1
);
/**
* @brief Run a KFLAT recipe without any specified target. The recipe will wait for an external trigger (e.g. user can manually trigger the target function).
*
* @param recipe Name of the KFLAT recipe.
* @param outfile Path to the file where the dump will be saved.
* @param use_stop_machine Execute the KFLAT recipe under kernel's stop_machine mode.
* @param debug Enable KFLAT LKM logging to dmesg.
* @param skip_func_body Skip executing function body after the recipe finishes flattening.
* @param run_recipe_now Execute KFLAT recipe directly from IOCTL without attaching to any kernel function.
* @param poll_timeout In miliseconds. Timeout for recipe execution.
*/
void run_recipe_no_target(
const std::string &recipe,
const fs::path &outfile,
bool use_stop_machine=false,
bool debug=true,
bool skip_func_body=false,
bool run_recipe_now=false,
int poll_timeout=-1
);
/**
* @brief Run a KFLAT recipe with a custom trigger function.
*
* @param custom_trigger Function with signature int (). Function that will be executed after enabling KFLAT. \n
Executing this should trigger the kernel function with recipe attached.
* @param recipe Name of the KFLAT recipe.
* @param outfile Path to the file where the dump will be saved.
* @param use_stop_machine Execute the KFLAT recipe under kernel's stop_machine mode.
* @param debug Enable KFLAT LKM logging to dmesg.
* @param skip_func_body Skip executing function body after the recipe finishes flattening.
* @param run_recipe_now Execute KFLAT recipe directly from IOCTL without attaching to any kernel function.
* @param target_timeout In seconds. Timeout for INTERFACE call on TARGET.
* @param poll_timeout In miliseconds. Timeout for recipe execution.
*/
void run_recipe_custom_target(
std::function<int ()> custom_trigger,
const std::string &recipe,
const fs::path &outfile,
bool use_stop_machine=false,
bool debug=false,
bool skip_func_body=false,
bool run_recipe_now=false,
unsigned int target_timeout=0,
int poll_timeout=-1
);
/**
* @brief Read all KFLAT recipes available to execute.
*
* @return std::vector<std::string> Vector of strings with names of loaded recipes
*/
std::vector<std::string> get_loaded_recipes();
private:
// Consts
const char *KFLAT_NODE = "/sys/kernel/debug/kflat";
fs::path governor_filepath;
unsigned int current_cpu;
static const std::map<ExecFlatInterface, std::function<int (int)>> interface_mapping;
// Variables
size_t dump_size;
ExecFlatVerbosity log_level;
int kflat_fd;
size_t out_size; // Actual size returned by KFLAT LKM
char *shared_memory;
std::chrono::system_clock::time_point start_time;
std::string saved_governor;
// Interfaces
static int interface_read(int fd);
static int interface_write(int fd);
static int interface_ioctl(int fd);
// Trigger function timeout
static void sigalrm_handler(int signum);
void start_alarm(unsigned int time);
void stop_alarm();
// Initialization steps
void open_kflat_node();
void mmap_kflat();
// Running recipes
void kflat_ioctl_enable(struct kflat_ioctl_enable *opts);
void kflat_ioctl_disable(struct kflat_ioctl_disable *ret);
void execute_interface(const fs::path &target, ExecFlatInterface interface);
void do_enable(
const std::string &recipe,
bool use_stop_machine,
bool debug,
bool skip_func_body,
bool run_recipe_now,
int pid
);
void disable(const fs::path &outfile, int poll_timeout);
// CPU governor stuff
fs::path get_governor_path();
void set_governor(const std::string &targetGovernor);
void restore_governor();
};
#endif // EXECFLAT_HDR