forked from hillu/go-yara
-
Notifications
You must be signed in to change notification settings - Fork 7
/
proc_linux.c
466 lines (395 loc) · 11.7 KB
/
proc_linux.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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
/*
Copyright (c) 2007-2021. 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.
*/
// https://github.com/VirusTotal/yara/pull/2048#issuecomment-1965580028
#if !defined _FILE_OFFSET_BITS
#define _FILE_OFFSET_BITS 64
#endif
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/sysmacros.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <yara_error.h>
#include <yara_globals.h>
#include <yara_libyara.h>
#include <yara_mem.h>
#include <yara_proc.h>
#include <yara_strutils.h>
typedef struct _YR_PROC_INFO
{
int pid;
int mem_fd;
int pagemap_fd;
FILE* maps;
uint64_t map_offset;
uint64_t next_block_end;
int page_size;
char map_path[PATH_MAX];
uint64_t map_dmaj;
uint64_t map_dmin;
uint64_t map_ino;
} YR_PROC_INFO;
static int page_size = -1;
int _yr_process_attach(int pid, YR_PROC_ITERATOR_CTX* context)
{
char buffer[256];
page_size = sysconf(_SC_PAGE_SIZE);
if (page_size < 0)
page_size = 4096;
YR_PROC_INFO* proc_info = (YR_PROC_INFO*) yr_malloc(sizeof(YR_PROC_INFO));
if (proc_info == NULL)
return ERROR_INSUFFICIENT_MEMORY;
proc_info->pid = pid;
proc_info->maps = NULL;
proc_info->mem_fd = -1;
proc_info->pagemap_fd = -1;
proc_info->next_block_end = 0;
snprintf(buffer, sizeof(buffer), "/proc/%u/maps", pid);
proc_info->maps = fopen(buffer, "r");
if (proc_info->maps == NULL)
goto err;
snprintf(buffer, sizeof(buffer), "/proc/%u/mem", pid);
proc_info->mem_fd = open(buffer, O_RDONLY);
if (proc_info->mem_fd == -1)
goto err;
snprintf(buffer, sizeof(buffer), "/proc/%u/pagemap", pid);
proc_info->pagemap_fd = open(buffer, O_RDONLY);
if (proc_info->pagemap_fd == -1)
goto err;
context->proc_info = proc_info;
return ERROR_SUCCESS;
err:
if (proc_info)
{
if (proc_info->pagemap_fd != -1)
close(proc_info->pagemap_fd);
if (proc_info->mem_fd != -1)
close(proc_info->mem_fd);
if (proc_info->maps != NULL)
fclose(proc_info->maps);
yr_free(proc_info);
}
return ERROR_COULD_NOT_ATTACH_TO_PROCESS;
}
int _yr_process_detach(YR_PROC_ITERATOR_CTX* context)
{
YR_PROC_INFO* proc_info = (YR_PROC_INFO*) context->proc_info;
if (proc_info)
{
fclose(proc_info->maps);
close(proc_info->mem_fd);
close(proc_info->pagemap_fd);
}
if (context->buffer != NULL)
{
munmap((void*) context->buffer, context->buffer_size);
context->buffer = NULL;
context->buffer_size = 0;
}
return ERROR_SUCCESS;
}
YR_API const uint8_t* yr_process_fetch_memory_block_data(YR_MEMORY_BLOCK* block)
{
const uint8_t* result = NULL;
uint64_t* pagemap = NULL;
YR_PROC_ITERATOR_CTX* context = (YR_PROC_ITERATOR_CTX*) block->context;
YR_PROC_INFO* proc_info = (YR_PROC_INFO*) context->proc_info;
if (context->buffer != NULL)
{
munmap((void*) context->buffer, context->buffer_size);
context->buffer = NULL;
context->buffer_size = 0;
}
int fd = -2; // Assume mapping not connected with a file.
// Only try mapping the file if it has a path and belongs to a device
if (strlen(proc_info->map_path) > 0 &&
!(proc_info->map_dmaj == 0 && proc_info->map_dmin == 0))
{
struct stat st;
if (stat(proc_info->map_path, &st) < 0)
{
// Why should stat fail after file open? Treat like missing.
fd = -1;
}
else if (
(major(st.st_dev) != proc_info->map_dmaj) ||
(minor(st.st_dev) != proc_info->map_dmin) ||
(st.st_ino != proc_info->map_ino))
{
// Wrong file, may have been replaced. Treat like missing.
fd = -1;
}
else if (st.st_size < proc_info->map_offset + block->size)
{
// Mapping extends past end of file. Treat like missing.
fd = -1;
}
else if ((st.st_mode & S_IFMT) != S_IFREG)
{
// Correct filesystem object, but not a regular file. Treat like
// uninitialized mapping.
fd = -2;
}
else
{
fd = open(proc_info->map_path, O_RDONLY);
// Double-check against race conditions
struct stat st2;
if (fstat(fd, &st2) < 0)
{
close(fd);
fd = -1;
}
else if ((st.st_dev != st2.st_dev) || (st.st_ino != st2.st_ino))
{
// File has been changed from under us, so ignore.
close(fd);
fd = -1;
}
}
}
if (fd >= 0)
{
context->buffer = mmap(
NULL,
block->size,
PROT_READ | PROT_WRITE,
MAP_PRIVATE,
fd,
proc_info->map_offset);
close(fd);
if (context->buffer == MAP_FAILED)
{
// Notify the code below that we couldn't read from the file
// fallback to pread() from the process
fd = -1;
}
context->buffer_size = block->size;
}
if (fd < 0)
{
context->buffer = mmap(
NULL,
block->size,
PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS,
-1,
0);
if (context->buffer == MAP_FAILED)
{
context->buffer = NULL;
context->buffer_size = 0;
goto _exit;
}
context->buffer_size = block->size;
}
// If mapping can't be accessed through the filesystem, read everything from
// target process VM.
if (fd == -1)
{
if (pread(
proc_info->mem_fd,
(void*) context->buffer,
block->size,
block->base) == -1)
{
goto _exit;
}
}
else
{
pagemap = calloc(block->size / page_size, sizeof(uint64_t));
if (pagemap == NULL)
{
goto _exit;
}
if (pread(
proc_info->pagemap_fd,
pagemap,
sizeof(uint64_t) * block->size / page_size,
sizeof(uint64_t) * block->base / page_size) == -1)
{
goto _exit;
}
for (uint64_t i = 0; i < block->size / page_size; i++)
{
if (pagemap[i] >> 61 == 0)
{
continue;
}
// Overwrite our mapping if the page is present, file-backed, or
// swap-backed and if it differs from our mapping.
uint8_t buffer[page_size];
if (pread(
proc_info->mem_fd,
buffer,
page_size,
block->base + i * page_size) == -1)
{
goto _exit;
}
if (memcmp(
(void*) context->buffer + i * page_size,
(void*) buffer,
page_size) != 0)
{
memcpy(
(void*) context->buffer + i * page_size, (void*) buffer, page_size);
}
}
}
result = context->buffer;
_exit:;
if (pagemap)
{
free(pagemap);
pagemap = NULL;
}
YR_DEBUG_FPRINTF(2, stderr, "- %s() {} = %p\n", __FUNCTION__, result);
return result;
}
YR_API YR_MEMORY_BLOCK* yr_process_get_next_memory_block(
YR_MEMORY_BLOCK_ITERATOR* iterator)
{
YR_PROC_ITERATOR_CTX* context = (YR_PROC_ITERATOR_CTX*) iterator->context;
YR_PROC_INFO* proc_info = (YR_PROC_INFO*) context->proc_info;
char buffer[PATH_MAX];
char perm[5];
uint64_t begin, end;
uint64_t current_begin = context->current_block.base +
context->current_block.size;
uint64_t max_process_memory_chunk;
yr_get_configuration_uint64(
YR_CONFIG_MAX_PROCESS_MEMORY_CHUNK, &max_process_memory_chunk);
iterator->last_error = ERROR_SUCCESS;
if (proc_info->next_block_end <= current_begin)
{
int path_start, n = 0;
char* p;
while (fgets(buffer, sizeof(buffer), proc_info->maps) != NULL)
{
// locate the '\n' character
p = strrchr(buffer, '\n');
// If we haven't read the whole line, skip over the rest.
if (p == NULL)
{
int c;
do
{
c = fgetc(proc_info->maps);
} while (c >= 0 && c != '\n');
}
// otherwise remove '\n' at the end of the line
else
{
*p = '\0';
}
// Each row in /proc/$PID/maps describes a region of contiguous virtual
// memory in a process or thread. Each row has the following fields:
//
// address perms offset dev inode pathname
// 08048000-08056000 r-xp 00000000 03:0c 64593 /usr/sbin/gpm
//
n = sscanf(
buffer,
"%" SCNx64 "-%" SCNx64 " %4s "
"%" SCNx64 " %" SCNx64 ":%" SCNx64 " %" SCNu64 " %n",
&begin,
&end,
perm,
&(proc_info->map_offset),
&(proc_info->map_dmaj),
&(proc_info->map_dmin),
&(proc_info->map_ino),
&path_start);
// If the row was parsed correctly sscan must return 7.
if (n == 7)
{
// skip the memory region that doesn't have read permission.
if (perm[0] != 'r')
{
continue;
}
// path_start contains the offset within buffer where the path starts,
// the path should start with /.
if (buffer[path_start] == '/')
strncpy(
proc_info->map_path,
buffer + path_start,
sizeof(proc_info->map_path) - 1);
else
proc_info->map_path[0] = '\0';
break;
}
}
if (n == 7)
{
current_begin = begin;
proc_info->next_block_end = end;
}
else
{
YR_DEBUG_FPRINTF(2, stderr, "+ %s() = NULL\n", __FUNCTION__);
return NULL;
}
}
context->current_block.base = current_begin;
context->current_block.size = yr_min(
proc_info->next_block_end - current_begin, max_process_memory_chunk);
assert(context->current_block.size > 0);
YR_DEBUG_FPRINTF(
2,
stderr,
"- %s() {} = %p // .base=0x%" PRIx64 " .size=%" PRIu64 "\n",
__FUNCTION__,
context->current_block,
context->current_block.base,
context->current_block.size);
return &context->current_block;
}
YR_API YR_MEMORY_BLOCK* yr_process_get_first_memory_block(
YR_MEMORY_BLOCK_ITERATOR* iterator)
{
YR_DEBUG_FPRINTF(2, stderr, "+ %s() {\n", __FUNCTION__);
YR_MEMORY_BLOCK* result = NULL;
YR_PROC_ITERATOR_CTX* context = (YR_PROC_ITERATOR_CTX*) iterator->context;
YR_PROC_INFO* proc_info = (YR_PROC_INFO*) context->proc_info;
if (fseek(proc_info->maps, 0, SEEK_SET) != 0)
{
result = NULL;
goto _exit;
}
proc_info->next_block_end = 0;
result = yr_process_get_next_memory_block(iterator);
_exit:
if (result == NULL)
iterator->last_error = ERROR_COULD_NOT_READ_PROCESS_MEMORY;
YR_DEBUG_FPRINTF(2, stderr, "} = %p // %s()\n", result, __FUNCTION__);
return result;
}