forked from hillu/go-yara
-
Notifications
You must be signed in to change notification settings - Fork 7
/
filemap.c
393 lines (321 loc) · 9.97 KB
/
filemap.c
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/*
Copyright (c) 2007-2015. The YARA Authors. All Rights Reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <fcntl.h>
#if defined(_WIN32) || defined(__CYGWIN__)
#include <windows.h>
#else
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
#endif
#include <yara_error.h>
#include <yara_filemap.h>
////////////////////////////////////////////////////////////////////////////////
// Maps a whole file into memory.
//
// Args:
// file_path: Path of the file to map.
// pmapped_file: Pointer to a YR_MAPPED_FILE that will be filled with
// information about the mapping.
// Returns:
// ERROR_SUCCESS
// ERROR_INVALID_ARGUMENT
// ERROR_COULD_NOT_OPEN_FILE
// ERROR_COULD_NOT_MAP_FILE
//
YR_API int yr_filemap_map(const char* file_path, YR_MAPPED_FILE* pmapped_file)
{
return yr_filemap_map_ex(file_path, 0, 0, pmapped_file);
}
////////////////////////////////////////////////////////////////////////////////
// Maps a portion of a file (specified by descriptor) into memory.
//
// Args:
// file: File descriptor representing the file to map.
// offset: File offset where the mapping will begin. This offset must be
// multiple of 1MB and not greater than the actual file size.
// size: Number of bytes that will be mapped. If zero or greater than the
// actual file size all content until the end of the file will be
// mapped.
// pmapped_file: Pointer to a YR_MAPPED_FILE struct that will be filled with
// the new mapping.
// Returns:
// ERROR_SUCCESS
// ERROR_INVALID_ARGUMENT
// ERROR_COULD_NOT_OPEN_FILE
// ERROR_COULD_NOT_MAP_FILE
//
#if defined(_WIN32) || defined(__CYGWIN__)
YR_API int yr_filemap_map_fd(
YR_FILE_DESCRIPTOR file,
uint64_t offset,
size_t size,
YR_MAPPED_FILE* pmapped_file)
{
LARGE_INTEGER fs;
size_t file_size;
pmapped_file->file = file;
pmapped_file->mapping = NULL;
pmapped_file->data = NULL;
pmapped_file->size = 0;
// Ensure that offset is aligned to 1MB
if (offset >> 20 << 20 != offset)
return ERROR_INVALID_ARGUMENT;
if (GetFileSizeEx(pmapped_file->file, &fs))
{
#ifdef _WIN64
file_size = fs.QuadPart;
#else
file_size = fs.LowPart;
#endif
}
else
{
pmapped_file->file = INVALID_HANDLE_VALUE;
return ERROR_COULD_NOT_OPEN_FILE;
}
if (offset > (uint64_t) file_size)
return ERROR_COULD_NOT_MAP_FILE;
if (size == 0)
size = (size_t)(file_size - offset);
pmapped_file->size = yr_min(size, (size_t)(file_size - offset));
if (pmapped_file->size != 0)
{
pmapped_file->mapping = CreateFileMapping(
pmapped_file->file, NULL, PAGE_READONLY, 0, 0, NULL);
if (pmapped_file->mapping == NULL)
{
pmapped_file->file = INVALID_HANDLE_VALUE;
pmapped_file->size = 0;
return ERROR_COULD_NOT_MAP_FILE;
}
pmapped_file->data = (const uint8_t*) MapViewOfFile(
pmapped_file->mapping,
FILE_MAP_READ,
offset >> 32,
offset & 0xFFFFFFFF,
pmapped_file->size);
if (pmapped_file->data == NULL)
{
CloseHandle(pmapped_file->mapping);
pmapped_file->mapping = NULL;
pmapped_file->file = INVALID_HANDLE_VALUE;
pmapped_file->size = 0;
return ERROR_COULD_NOT_MAP_FILE;
}
}
else
{
pmapped_file->mapping = NULL;
pmapped_file->data = NULL;
}
return ERROR_SUCCESS;
}
#else // POSIX
#ifdef __linux__
#include <sys/vfs.h>
#define PROC_SUPER_MAGIC 0x9fa0
#endif
#define MAP_EXTRA_FLAGS 0
#if defined(__APPLE__)
// MacOS defines some extra flags for mmap.The MAP_RESILIENT_CODESIGN allows
// to read from binaries whose code signature is invalid, without this flags
// any attempt to read from such binaries causes a crash, see:
// https://github.com/VirusTotal/yara_issues/1309.
//
// Also, reading from files in removable media that becomes unavailable
// crashes the program if the MAP_RESILIENT_MEDIA flag is not set.
#if defined(MAP_RESILIENT_CODESIGN)
#undef MAP_EXTRA_FLAGS
#if defined(MAP_RESILIENT_MEDIA)
#define MAP_EXTRA_FLAGS MAP_RESILIENT_CODESIGN | MAP_RESILIENT_MEDIA
#else
#define MAP_EXTRA_FLAGS MAP_RESILIENT_CODESIGN
#endif
#endif // #if defined(MAP_RESILIENT_CODESIGN)
#endif // #if defined (__APPLE__)
YR_API int yr_filemap_map_fd(
YR_FILE_DESCRIPTOR file,
uint64_t offset,
size_t size,
YR_MAPPED_FILE* pmapped_file)
{
struct stat st;
#ifdef __linux__
struct statfs stfs;
#endif
pmapped_file->file = file;
pmapped_file->data = NULL;
pmapped_file->size = 0;
// Ensure that offset is aligned to 1MB
if (offset >> 20 << 20 != offset)
return ERROR_INVALID_ARGUMENT;
if (fstat(file, &st) != 0 || S_ISDIR(st.st_mode))
return ERROR_COULD_NOT_OPEN_FILE;
if (offset > st.st_size)
return ERROR_COULD_NOT_MAP_FILE;
#ifdef __linux__
if (fstatfs(file, &stfs) != 0)
return ERROR_COULD_NOT_OPEN_FILE;
switch (stfs.f_type) {
case PROC_SUPER_MAGIC:
return ERROR_COULD_NOT_OPEN_FILE;
}
#endif
if (size == 0)
size = (size_t)(st.st_size - offset);
pmapped_file->size = yr_min(size, (size_t)(st.st_size - offset));
if (pmapped_file->size != 0)
{
pmapped_file->data = (const uint8_t*) mmap(
0,
pmapped_file->size,
PROT_READ,
MAP_PRIVATE | MAP_EXTRA_FLAGS,
pmapped_file->file,
offset);
if (pmapped_file->data == MAP_FAILED)
{
pmapped_file->data = NULL;
pmapped_file->size = 0;
pmapped_file->file = -1;
return ERROR_COULD_NOT_MAP_FILE;
}
madvise((void*) pmapped_file->data, pmapped_file->size, MADV_SEQUENTIAL);
}
else
{
pmapped_file->data = NULL;
}
return ERROR_SUCCESS;
}
#endif
////////////////////////////////////////////////////////////////////////////////
// Maps a portion of a file (specified by path) into memory.
//
// Args:
// file_path: Path of the file to map.
// offset: File offset where the mapping will begin. This offset must be
// multiple of 1MB and not greater than the actual file size.
// size: Number of bytes that will be mapped. If zero or greater than the
// actual file size all content until the end of the file will be
// mapped.
// pmapped_file: Pointer to a YR_MAPPED_FILE struct that will be filled with
// the new mapping.
// Returns:
// ERROR_SUCCESS
// ERROR_INVALID_ARGUMENT
// ERROR_COULD_NOT_OPEN_FILE
// ERROR_COULD_NOT_MAP_FILE
//
#if defined(_WIN32) || defined(__CYGWIN__)
YR_API int yr_filemap_map_ex(
const char* file_path,
uint64_t offset,
size_t size,
YR_MAPPED_FILE* pmapped_file)
{
YR_FILE_DESCRIPTOR fd;
int result;
if (file_path == NULL)
return ERROR_INVALID_ARGUMENT;
fd = CreateFileA(
file_path,
GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
NULL,
OPEN_EXISTING,
FILE_FLAG_SEQUENTIAL_SCAN,
NULL);
if (fd == INVALID_HANDLE_VALUE)
return ERROR_COULD_NOT_OPEN_FILE;
result = yr_filemap_map_fd(fd, offset, size, pmapped_file);
if (result != ERROR_SUCCESS)
CloseHandle(fd);
return result;
}
#else // POSIX
YR_API int yr_filemap_map_ex(
const char* file_path,
uint64_t offset,
size_t size,
YR_MAPPED_FILE* pmapped_file)
{
YR_FILE_DESCRIPTOR fd;
int result;
if (file_path == NULL)
return ERROR_INVALID_ARGUMENT;
fd = open(file_path, O_RDONLY);
if (fd == -1)
return ERROR_COULD_NOT_OPEN_FILE;
result = yr_filemap_map_fd(fd, offset, size, pmapped_file);
if (result != ERROR_SUCCESS)
close(fd);
return result;
}
#endif
////////////////////////////////////////////////////////////////////////////////
// Unmaps a file mapping.
//
// Args:
// pmapped_file: Pointer to a YR_MAPPED_FILE that struct.
//
#ifdef WIN32
YR_API void yr_filemap_unmap_fd(YR_MAPPED_FILE* pmapped_file)
{
if (pmapped_file->data != NULL)
UnmapViewOfFile(pmapped_file->data);
if (pmapped_file->mapping != NULL)
CloseHandle(pmapped_file->mapping);
pmapped_file->mapping = NULL;
pmapped_file->data = NULL;
pmapped_file->size = 0;
}
YR_API void yr_filemap_unmap(YR_MAPPED_FILE* pmapped_file)
{
yr_filemap_unmap_fd(pmapped_file);
if (pmapped_file->file != INVALID_HANDLE_VALUE)
{
CloseHandle(pmapped_file->file);
pmapped_file->file = INVALID_HANDLE_VALUE;
}
}
#else // POSIX
YR_API void yr_filemap_unmap_fd(YR_MAPPED_FILE* pmapped_file)
{
if (pmapped_file->data != NULL)
munmap((void*) pmapped_file->data, pmapped_file->size);
pmapped_file->data = NULL;
pmapped_file->size = 0;
}
YR_API void yr_filemap_unmap(YR_MAPPED_FILE* pmapped_file)
{
yr_filemap_unmap_fd(pmapped_file);
if (pmapped_file->file != -1)
{
close(pmapped_file->file);
pmapped_file->file = -1;
}
}
#endif