Coverage Report

Created: 2026-12-04 07:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/yara/libyara/modules/elf/elf.c
Line
Count
Source
1
/*
2
Copyright (c) 2014. The YARA Authors. All Rights Reserved.
3
4
Redistribution and use in source and binary forms, with or without modification,
5
are permitted provided that the following conditions are met:
6
7
1. Redistributions of source code must retain the above copyright notice, this
8
list of conditions and the following disclaimer.
9
10
2. Redistributions in binary form must reproduce the above copyright notice,
11
this list of conditions and the following disclaimer in the documentation and/or
12
other materials provided with the distribution.
13
14
3. Neither the name of the copyright holder nor the names of its contributors
15
may be used to endorse or promote products derived from this software without
16
specific prior written permission.
17
18
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
22
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
25
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
*/
29
30
#include <limits.h>
31
#include <stdlib.h>
32
#include <string.h>
33
#include <tlshc/tlsh.h>
34
#include <yara/elf.h>
35
#include <yara/elf_utils.h>
36
#include <yara/endian.h>
37
#include <yara/mem.h>
38
#include <yara/modules.h>
39
#include <yara/simple_str.h>
40
#include <yara/utils.h>
41
#include "../crypto.h"
42
#include "../exception.h"
43
44
#define MODULE_NAME elf
45
46
49.6k
#define CLASS_DATA(c, d) ((c << 8) | d)
47
48
static int sort_strcmp(const void* a, const void* b)
49
0
{
50
0
  return strcmp(*(const char**) a, *(const char**) b);
51
0
}
52
53
define_function(telfhash)
54
0
{
55
0
  YR_OBJECT* obj = yr_module();
56
0
  ELF* elf = (ELF*) obj->data;
57
0
  if (elf == NULL)
58
0
    return_string(YR_UNDEFINED);
59
60
0
  if (elf->telfhash)
61
0
    return_string(elf->telfhash);
62
63
  /* We prefer dynsym if possible */
64
0
  ELF_SYMBOL_LIST* list = elf->dynsym ? elf->dynsym : elf->symtab;
65
0
  if (!list)
66
0
    return_string(YR_UNDEFINED);
67
68
  /* exclusions are based on the original implementation
69
     https://github.com/trendmicro/telfhash/blob/master/telfhash/telfhash.py */
70
0
  char* exclude_strings[] = {
71
0
      "__libc_start_main",
72
0
      "main",
73
0
      "abort",
74
0
      "cachectl",
75
0
      "cacheflush",
76
0
      "puts",
77
0
      "atol",
78
0
      "malloc_trim"};
79
80
0
  SIMPLE_STR* sstr = NULL;
81
0
  Tlsh* tlsh = NULL;
82
83
0
  int symbol_count = 0;
84
0
  char** clean_names = yr_calloc(list->count, sizeof(*clean_names));
85
86
0
  if (clean_names == NULL && list->count > 0)
87
0
    return_string(YR_UNDEFINED);
88
89
0
  for (ELF_SYMBOL* i = list->symbols; i != NULL; i = i->next)
90
0
  {
91
0
    char* name = i->name;
92
93
0
    if (!name)
94
0
      continue;
95
96
    /* Use only global code symbols */
97
0
    if (i->bind != ELF_STB_GLOBAL || i->type != ELF_STT_FUNC ||
98
0
        i->visibility != ELF_STV_DEFAULT)
99
0
      continue;
100
101
    /* ignore:
102
        x86-64 specific functions
103
        string functions (str.* and mem.*), gcc changes them depending on arch
104
        symbols starting with . or _ */
105
0
    bool is_bad_prefix = name[0] == '.' || name[0] == '_';
106
0
    size_t namelen = strlen(name);
107
0
    bool is_x86_64 = namelen >= 2 && strncmp(name + namelen - 2, "64", 2) == 0;
108
0
    bool is_mem_or_str = strncmp(name, "str", 3) == 0 ||
109
0
                         strncmp(name, "mem", 3) == 0;
110
111
0
    if (is_bad_prefix || is_x86_64 || is_mem_or_str)
112
0
      continue;
113
114
    /* Exclude any symbols that match the excluded ones */
115
0
    bool is_excluded = false;
116
0
    for (int i = 0; i < sizeof(exclude_strings) / sizeof(*exclude_strings); i++)
117
0
    {
118
0
      if (strcmp(name, exclude_strings[i]) == 0)
119
0
      {
120
0
        is_excluded = true;
121
0
        break;
122
0
      }
123
0
    }
124
125
0
    if (is_excluded)
126
0
      continue;
127
128
0
    clean_names[symbol_count] = yr_malloc(namelen + 1);
129
130
0
    if (!clean_names[symbol_count])
131
0
      goto cleanup;
132
133
    /* Convert it to lowercase */
134
0
    size_t j;
135
0
    for (j = 0; j < namelen; j++)
136
0
      clean_names[symbol_count][j] = tolower((unsigned char) name[j]);
137
138
0
    clean_names[symbol_count][j] = '\0';
139
140
0
    symbol_count++;
141
0
  }
142
143
0
  if (!symbol_count)
144
0
    goto cleanup;
145
146
  /* Now we have all the valid symbols, sort them, concat them */
147
0
  qsort(clean_names, symbol_count, sizeof(*clean_names), &sort_strcmp);
148
149
0
  sstr = sstr_newf("%s", clean_names[0]);
150
0
  if (!sstr)
151
0
    goto cleanup;
152
153
  /* We've already written first symbol, start at 1 */
154
0
  for (int i = 1; i < symbol_count; ++i)
155
0
  {
156
0
    if (!sstr_appendf(sstr, ",%s", clean_names[i]))
157
0
      goto cleanup;
158
0
  }
159
160
0
  tlsh = tlsh_new();
161
0
  if (!tlsh)
162
0
    goto cleanup;
163
164
0
  tlsh_final(tlsh, (const unsigned char*) sstr->str, sstr->len, 0);
165
166
0
  const char* telfhash = tlsh_get_hash(tlsh, true);
167
0
  elf->telfhash = yr_strdup(telfhash);  // cache it
168
0
  if (!elf->telfhash)
169
0
    goto cleanup;
170
171
0
  for (int i = 0; i < symbol_count; ++i) yr_free(clean_names[i]);
172
0
  yr_free(clean_names);
173
0
  sstr_free(sstr);
174
0
  tlsh_free(tlsh);
175
176
0
  return_string(elf->telfhash);
177
178
0
cleanup:
179
0
  for (int i = 0; i < symbol_count; ++i) yr_free(clean_names[i]);
180
0
  yr_free(clean_names);
181
0
  sstr_free(sstr);
182
0
  tlsh_free(tlsh);
183
184
0
  return_string(YR_UNDEFINED);
185
0
}
186
187
#if defined(HAVE_LIBCRYPTO) || defined(HAVE_WINCRYPT_H) || \
188
    defined(HAVE_COMMONCRYPTO_COMMONCRYPTO_H)
189
190
define_function(import_md5)
191
{
192
  YR_OBJECT* obj = yr_module();
193
  ELF* elf = (ELF*) obj->data;
194
  if (elf == NULL)
195
    return_string(YR_UNDEFINED);
196
197
  if (elf->import_hash)
198
    return_string(elf->import_hash);
199
200
  ELF_SYMBOL_LIST* list = elf->dynsym ? elf->dynsym : elf->symtab;
201
  if (!list)
202
    return_string(YR_UNDEFINED);
203
204
  SIMPLE_STR* sstr = NULL;
205
206
  int symbol_count = 0;
207
  char** clean_names = yr_calloc(list->count, sizeof(*clean_names));
208
  if (clean_names == NULL && list->count > 0)
209
    return_string(YR_UNDEFINED);
210
211
  for (ELF_SYMBOL* i = list->symbols; i != NULL; i = i->next)
212
  {
213
    char* name = i->name;
214
215
    if (!name)
216
      continue;
217
218
    if (i->shndx != ELF_SHN_UNDEF)
219
      continue;
220
221
    size_t namelen = strlen(name);
222
223
    // skip empty names
224
    if (namelen == 0)
225
      continue;
226
227
    clean_names[symbol_count] = yr_malloc(namelen + 1);
228
    if (!clean_names[symbol_count])
229
      goto cleanup;
230
231
    /* Convert it to lowercase */
232
    size_t j;
233
    for (j = 0; j < namelen; j++)
234
      clean_names[symbol_count][j] = tolower((unsigned char) name[j]);
235
236
    clean_names[symbol_count][j] = '\0';
237
238
    symbol_count++;
239
  }
240
241
  if (!symbol_count)
242
    goto cleanup;
243
244
  /* Now we have all the valid symbols, sort them, concat them */
245
  qsort(clean_names, symbol_count, sizeof(*clean_names), &sort_strcmp);
246
247
  sstr = sstr_newf("%s", clean_names[0]);
248
  if (!sstr)
249
    goto cleanup;
250
251
  /* We've already written first symbol, start at 1 */
252
  for (int i = 1; i < symbol_count; ++i)
253
  {
254
    if (!sstr_appendf(sstr, ",%s", clean_names[i]))
255
      goto cleanup;
256
  }
257
258
  unsigned char hash[YR_MD5_LEN];
259
260
  yr_md5_ctx ctx;
261
  if (!yr_md5_init(&ctx))
262
    goto cleanup;
263
264
  yr_md5_update(&ctx, sstr->str, sstr->len);
265
  yr_md5_final(hash, &ctx);
266
267
  elf->import_hash = yr_malloc(YR_MD5_LEN * 2 + 1);
268
  if (!elf->import_hash)
269
    goto cleanup;
270
271
  for (int i = 0; i < YR_MD5_LEN; ++i)
272
    sprintf(elf->import_hash + (i * 2), "%02x", hash[i]);
273
274
  for (int i = 0; i < symbol_count; ++i) yr_free(clean_names[i]);
275
  yr_free(clean_names);
276
  sstr_free(sstr);
277
278
  return_string(elf->import_hash);
279
280
cleanup:
281
  for (int i = 0; i < symbol_count; ++i) yr_free(clean_names[i]);
282
  yr_free(clean_names);
283
  sstr_free(sstr);
284
285
  return_string(YR_UNDEFINED);
286
}
287
288
#endif  // defined(HAVE_LIBCRYPTO) || defined(HAVE_WINCRYPT_H)
289
290
int get_elf_class_data(const uint8_t* buffer, size_t buffer_length)
291
7.28k
{
292
7.28k
  elf_ident_t* elf_ident;
293
294
7.28k
  if (buffer_length < sizeof(elf_ident_t))
295
17
    return 0;
296
297
7.27k
  elf_ident = (elf_ident_t*) buffer;
298
299
7.27k
  if (yr_le32toh(elf_ident->magic) == ELF_MAGIC)
300
6.95k
  {
301
6.95k
    return CLASS_DATA(elf_ident->_class, elf_ident->data);
302
6.95k
  }
303
319
  else
304
319
  {
305
319
    return 0;
306
319
  }
307
7.27k
}
308
309
static bool is_valid_ptr(
310
    const void* base,
311
    size_t size,
312
    const void* ptr,
313
    uint64_t ptr_size)  // ptr_size can be 64bit even in 32bit systems.
314
5.13M
{
315
5.13M
  return ptr >= base && ptr_size <= size &&
316
5.12M
         ((char*) ptr) + ptr_size <= ((char*) base) + size;
317
5.13M
}
318
319
#define IS_VALID_PTR(base, size, ptr) \
320
5.14M
  is_valid_ptr(base, size, ptr, sizeof(*ptr))
321
322
//
323
// Returns a string table entry for the index or NULL if the entry is out
324
// of bounds. A non-null return value will be a null-terminated C string.
325
//
326
static const char* str_table_entry(
327
    const char* str_table_base,
328
    const char* str_table_limit,
329
    int index)
330
687k
{
331
687k
  size_t len;
332
687k
  const char* str_entry;
333
334
687k
  if (str_table_base >= str_table_limit)
335
66.1k
    return NULL;
336
337
  // The first entry in the string table must be a null character, if not the
338
  // string table is probably corrupted.
339
621k
  if (*str_table_base != '\0')
340
61.5k
    return NULL;
341
342
559k
  if (index < 0)
343
82.7k
    return NULL;
344
345
  // Bound the index against the table size before forming the pointer. On
346
  // 32-bit builds str_table_base + index can wrap past str_table_limit for a
347
  // large index and pass the check below, letting strnlen read out of bounds.
348
476k
  if ((size_t) index >= (size_t) (str_table_limit - str_table_base))
349
182k
    return NULL;
350
351
293k
  str_entry = str_table_base + index;
352
353
293k
  len = strnlen(str_entry, str_table_limit - str_entry);
354
355
  // Entry is clamped by extent of string table, not null-terminated.
356
293k
  if (str_entry + len == str_table_limit)
357
3.60k
    return NULL;
358
359
290k
  return str_entry;
360
293k
}
361
362
#define ELF_SIZE_OF_SECTION_TABLE(bits, bo, h) \
363
8.42k
  (sizeof(elf##bits##_section_header_t) * yr_##bo##16toh(h->sh_entry_count))
364
365
#define ELF_SIZE_OF_PROGRAM_TABLE(bits, bo, h) \
366
2.08k
  (sizeof(elf##bits##_program_header_t) * yr_##bo##16toh(h->ph_entry_count))
367
368
#define ELF_RVA_TO_OFFSET(bits, bo)                                                \
369
  uint64_t elf_rva_to_offset_##bits##_##bo(                                        \
370
      elf##bits##_header_t* elf_header, uint64_t rva, size_t elf_size)             \
371
5.94k
  {                                                                                \
372
5.94k
    if (yr_##bo##16toh(elf_header->type) == ELF_ET_EXEC)                           \
373
5.94k
    {                                                                              \
374
1.29k
      int i;                                                                       \
375
1.29k
                                                                                   \
376
1.29k
      elf##bits##_program_header_t* program;                                       \
377
1.29k
                                                                                   \
378
1.29k
      /* check that ph_offset doesn't wrap when added to SIZE_OF_PROGRAM_TABLE     \
379
1.29k
       */                                                                          \
380
1.29k
                                                                                   \
381
1.29k
      if (ULONG_MAX - yr_##bo##bits##toh(elf_header->ph_offset) <                  \
382
1.29k
          ELF_SIZE_OF_PROGRAM_TABLE(bits, bo, elf_header))                         \
383
1.29k
      {                                                                            \
384
7
        return YR_UNDEFINED;                                                       \
385
7
      }                                                                            \
386
1.29k
                                                                                   \
387
1.29k
      if (yr_##bo##bits##toh(elf_header->ph_offset) == 0 ||                        \
388
1.28k
          yr_##bo##bits##toh(elf_header->ph_offset) > elf_size ||                  \
389
1.28k
          yr_##bo##bits##toh(elf_header->ph_offset) +                              \
390
795
                  ELF_SIZE_OF_PROGRAM_TABLE(bits, bo, elf_header) >                \
391
795
              elf_size ||                                                          \
392
1.28k
          yr_##bo##16toh(elf_header->ph_entry_count) == 0)                         \
393
1.28k
      {                                                                            \
394
624
        return YR_UNDEFINED;                                                       \
395
624
      }                                                                            \
396
1.28k
                                                                                   \
397
1.28k
      program = (elf##bits##_program_header_t*)                                  \
398
661
        ((uint8_t*) elf_header + yr_##bo##bits##toh(elf_header->ph_offset)); \
399
661
                                                                                   \
400
55.1k
      for (i = 0; i < yr_##bo##16toh(elf_header->ph_entry_count); i++)             \
401
54.7k
      {                                                                            \
402
54.7k
        if (rva >= yr_##bo##bits##toh(program->virt_addr) &&                       \
403
54.7k
            rva < yr_##bo##bits##toh(program->virt_addr) +                         \
404
36.3k
                      yr_##bo##bits##toh(program->mem_size))                       \
405
54.7k
        {                                                                          \
406
175
          return yr_##bo##bits##toh(program->offset) +                             \
407
175
                 (rva - yr_##bo##bits##toh(program->virt_addr));                   \
408
175
        }                                                                          \
409
54.7k
                                                                                   \
410
54.7k
        program++;                                                                 \
411
54.5k
      }                                                                            \
412
661
    }                                                                              \
413
5.94k
    else                                                                           \
414
5.94k
    {                                                                              \
415
4.64k
      int i;                                                                       \
416
4.64k
                                                                                   \
417
4.64k
      elf##bits##_section_header_t* section;                                       \
418
4.64k
                                                                                   \
419
4.64k
      /* check that sh_offset doesn't wrap when added to SIZE_OF_SECTION_TABLE     \
420
4.64k
       */                                                                          \
421
4.64k
                                                                                   \
422
4.64k
      if (ULONG_MAX - yr_##bo##bits##toh(elf_header->sh_offset) <                  \
423
4.64k
          ELF_SIZE_OF_SECTION_TABLE(bits, bo, elf_header))                         \
424
4.64k
      {                                                                            \
425
16
        return YR_UNDEFINED;                                                       \
426
16
      }                                                                            \
427
4.64k
                                                                                   \
428
4.64k
      if (yr_##bo##bits##toh(elf_header->sh_offset) == 0 ||                        \
429
4.63k
          yr_##bo##bits##toh(elf_header->sh_offset) > elf_size ||                  \
430
4.63k
          yr_##bo##bits##toh(elf_header->sh_offset) +                              \
431
3.77k
                  ELF_SIZE_OF_SECTION_TABLE(bits, bo, elf_header) >                \
432
3.77k
              elf_size ||                                                          \
433
4.63k
          yr_##bo##16toh(elf_header->sh_entry_count) == 0)                         \
434
4.63k
      {                                                                            \
435
1.04k
        return YR_UNDEFINED;                                                       \
436
1.04k
      }                                                                            \
437
4.63k
                                                                                   \
438
4.63k
      section = (elf##bits##_section_header_t*)                                  \
439
3.58k
        ((uint8_t*) elf_header + yr_##bo##bits##toh(elf_header->sh_offset)); \
440
3.58k
                                                                                   \
441
86.5k
      for (i = 0; i < yr_##bo##16toh(elf_header->sh_entry_count); i++)             \
442
84.5k
      {                                                                            \
443
84.5k
        if (yr_##bo##32toh(section->type) != ELF_SHT_NULL &&                       \
444
84.5k
            yr_##bo##32toh(section->type) != ELF_SHT_NOBITS &&                     \
445
84.5k
            rva >= yr_##bo##bits##toh(section->addr) &&                            \
446
84.5k
            rva < yr_##bo##bits##toh(section->addr) +                              \
447
52.0k
                      yr_##bo##bits##toh(section->size))                           \
448
84.5k
        {                                                                          \
449
1.51k
          return yr_##bo##bits##toh(section->offset) +                             \
450
1.51k
                 (rva - yr_##bo##bits##toh(section->addr));                        \
451
1.51k
        }                                                                          \
452
84.5k
                                                                                   \
453
84.5k
        section++;                                                                 \
454
83.0k
      }                                                                            \
455
3.58k
    }                                                                              \
456
5.94k
    return YR_UNDEFINED;                                                           \
457
5.94k
  }
458
459
#define PARSE_ELF_HEADER(bits, bo)                                                        \
460
  int parse_elf_header_##bits##_##bo(                                                     \
461
      ELF* elf_data,                                                                      \
462
      elf##bits##_header_t* elf,                                                          \
463
      uint64_t base_address,                                                              \
464
      size_t elf_size,                                                                    \
465
      int flags,                                                                          \
466
      YR_OBJECT* elf_obj)                                                                 \
467
6.14k
  {                                                                                       \
468
6.14k
    unsigned int i, j, m;                                                                 \
469
6.14k
    const char* elf_raw = (const char*) elf;                                              \
470
6.14k
    uint16_t str_table_index = yr_##bo##16toh(elf->sh_str_table_index);                   \
471
6.14k
                                                                                          \
472
6.14k
    const char* sym_table = NULL;                                                         \
473
6.14k
    const char* sym_str_table = NULL;                                                     \
474
6.14k
    const char* dyn_sym_table = NULL;                                                     \
475
6.14k
    const char* dyn_sym_str_table = NULL;                                                 \
476
6.14k
                                                                                          \
477
6.14k
    uint##bits##_t sym_table_size = 0;                                                    \
478
6.14k
    uint##bits##_t sym_str_table_size = 0;                                                \
479
6.14k
    uint##bits##_t dyn_sym_table_size = 0;                                                \
480
6.14k
    uint##bits##_t dyn_sym_str_table_size = 0;                                            \
481
6.14k
                                                                                          \
482
6.14k
    elf_data->symtab = elf_data->dynsym = NULL;                                           \
483
6.14k
                                                                                          \
484
6.14k
    elf##bits##_section_header_t* section_table;                                          \
485
6.14k
    elf##bits##_section_header_t* section;                                                \
486
6.14k
    elf##bits##_program_header_t* segment;                                                \
487
6.14k
                                                                                          \
488
6.14k
    yr_set_integer(yr_##bo##16toh(elf->type), elf_obj, "type");                           \
489
6.14k
    yr_set_integer(yr_##bo##16toh(elf->machine), elf_obj, "machine");                     \
490
6.14k
    yr_set_integer(yr_##bo##bits##toh(elf->sh_offset), elf_obj, "sh_offset");             \
491
6.14k
    yr_set_integer(                                                                       \
492
6.14k
        yr_##bo##16toh(elf->sh_entry_size), elf_obj, "sh_entry_size");                    \
493
6.14k
    yr_set_integer(                                                                       \
494
6.14k
        yr_##bo##16toh(elf->sh_entry_count), elf_obj, "number_of_sections");              \
495
6.14k
    yr_set_integer(yr_##bo##bits##toh(elf->ph_offset), elf_obj, "ph_offset");             \
496
6.14k
    yr_set_integer(                                                                       \
497
6.14k
        yr_##bo##16toh(elf->ph_entry_size), elf_obj, "ph_entry_size");                    \
498
6.14k
    yr_set_integer(                                                                       \
499
6.14k
        yr_##bo##16toh(elf->ph_entry_count), elf_obj, "number_of_segments");              \
500
6.14k
                                                                                          \
501
6.14k
    if (yr_##bo##bits##toh(elf->entry) != 0)                                              \
502
6.14k
    {                                                                                     \
503
5.94k
      yr_set_integer(                                                                     \
504
5.94k
          flags& SCAN_FLAGS_PROCESS_MEMORY                                                \
505
5.94k
              ? base_address + yr_##bo##bits##toh(elf->entry)                             \
506
5.94k
              : elf_rva_to_offset_##bits##_##bo(                                          \
507
5.94k
                    elf, yr_##bo##bits##toh(elf->entry), elf_size),                       \
508
5.94k
          elf_obj,                                                                        \
509
5.94k
          "entry_point");                                                                 \
510
5.94k
    }                                                                                     \
511
6.14k
                                                                                          \
512
6.14k
    if (yr_##bo##16toh(elf->sh_entry_count) < ELF_SHN_LORESERVE &&                        \
513
6.14k
        str_table_index < yr_##bo##16toh(elf->sh_entry_count) &&                          \
514
6.14k
        yr_##bo##bits##toh(elf->sh_offset) < elf_size &&                                  \
515
6.14k
        yr_##bo##bits##toh(elf->sh_offset) +                                              \
516
4.05k
                yr_##bo##16toh(elf->sh_entry_count) *                                     \
517
4.05k
                    sizeof(elf##bits##_section_header_t) <=                               \
518
4.05k
            elf_size)                                                                     \
519
6.14k
    {                                                                                     \
520
3.85k
      const char* str_table = NULL;                                                       \
521
3.85k
                                                                                          \
522
3.85k
      section_table =                                                                     \
523
3.85k
          (elf##bits##_section_header_t*) (elf_raw + yr_##bo##bits##toh(elf->sh_offset)); \
524
3.85k
                                                                                          \
525
3.85k
      if (yr_##bo##bits##toh(section_table[str_table_index].offset) <                     \
526
3.85k
          elf_size)                                                                       \
527
3.85k
      {                                                                                   \
528
2.03k
        str_table = elf_raw +                                                             \
529
2.03k
                    yr_##bo##bits##toh(section_table[str_table_index].offset);            \
530
2.03k
      }                                                                                   \
531
3.85k
                                                                                          \
532
3.85k
      section = section_table;                                                            \
533
3.85k
                                                                                          \
534
1.00M
      for (i = 0; i < yr_##bo##16toh(elf->sh_entry_count); i++, section++)                \
535
1.00M
      {                                                                                   \
536
1.00M
        yr_set_integer(                                                                   \
537
1.00M
            yr_##bo##32toh(section->type), elf_obj, "sections[%i].type", i);              \
538
1.00M
        yr_set_integer(                                                                   \
539
1.00M
            yr_##bo##bits##toh(section->flags),                                           \
540
1.00M
            elf_obj,                                                                      \
541
1.00M
            "sections[%i].flags",                                                         \
542
1.00M
            i);                                                                           \
543
1.00M
        yr_set_integer(                                                                   \
544
1.00M
            yr_##bo##bits##toh(section->addr),                                            \
545
1.00M
            elf_obj,                                                                      \
546
1.00M
            "sections[%i].address",                                                       \
547
1.00M
            i);                                                                           \
548
1.00M
        yr_set_integer(                                                                   \
549
1.00M
            yr_##bo##bits##toh(section->size),                                            \
550
1.00M
            elf_obj,                                                                      \
551
1.00M
            "sections[%i].size",                                                          \
552
1.00M
            i);                                                                           \
553
1.00M
        yr_set_integer(                                                                   \
554
1.00M
            yr_##bo##bits##toh(section->offset),                                          \
555
1.00M
            elf_obj,                                                                      \
556
1.00M
            "sections[%i].offset",                                                        \
557
1.00M
            i);                                                                           \
558
1.00M
                                                                                          \
559
1.00M
        if (yr_##bo##32toh(section->name) < elf_size && str_table > elf_raw)              \
560
1.00M
        {                                                                                 \
561
204k
          const char* section_name = str_table_entry(                                     \
562
204k
              str_table, elf_raw + elf_size, yr_##bo##32toh(section->name));              \
563
204k
                                                                                          \
564
204k
          if (section_name)                                                               \
565
204k
            yr_set_string(section_name, elf_obj, "sections[%i].name", i);                 \
566
204k
        }                                                                                 \
567
1.00M
                                                                                          \
568
1.00M
        if (yr_##bo##32toh(section->type) == ELF_SHT_SYMTAB &&                            \
569
1.00M
            yr_##bo##32toh(section->link) <                                               \
570
20.7k
                yr_##bo##16toh(elf->sh_entry_count))                                      \
571
1.00M
        {                                                                                 \
572
12.6k
          elf##bits##_section_header_t* string_section = section_table +                  \
573
12.6k
                                                         yr_##bo##32toh(                  \
574
12.6k
                                                             section->link);              \
575
12.6k
                                                                                          \
576
12.6k
          if (IS_VALID_PTR(elf, elf_size, string_section) &&                              \
577
12.6k
              yr_##bo##32toh(string_section->type) == ELF_SHT_STRTAB)                     \
578
12.6k
          {                                                                               \
579
7.53k
            sym_table = elf_raw + yr_##bo##bits##toh(section->offset);                    \
580
7.53k
            sym_str_table = elf_raw +                                                     \
581
7.53k
                            yr_##bo##bits##toh(string_section->offset);                   \
582
7.53k
            sym_table_size = yr_##bo##bits##toh(section->size);                           \
583
7.53k
            sym_str_table_size = yr_##bo##bits##toh(string_section->size);                \
584
7.53k
          }                                                                               \
585
12.6k
        }                                                                                 \
586
1.00M
                                                                                          \
587
1.00M
        if (yr_##bo##32toh(section->type) == ELF_SHT_DYNSYM &&                            \
588
1.00M
            yr_##bo##32toh(section->link) <                                               \
589
8.09k
                yr_##bo##16toh(elf->sh_entry_count))                                      \
590
1.00M
        {                                                                                 \
591
4.72k
          elf##bits##_section_header_t* dynstr_section = section_table +                  \
592
4.72k
                                                         yr_##bo##32toh(                  \
593
4.72k
                                                             section->link);              \
594
4.72k
                                                                                          \
595
4.72k
          if (IS_VALID_PTR(elf, elf_size, dynstr_section) &&                              \
596
4.72k
              yr_##bo##32toh(dynstr_section->type) == ELF_SHT_STRTAB)                     \
597
4.72k
          {                                                                               \
598
2.84k
            dyn_sym_table = elf_raw + yr_##bo##bits##toh(section->offset);                \
599
2.84k
            dyn_sym_str_table = elf_raw +                                                 \
600
2.84k
                                yr_##bo##bits##toh(dynstr_section->offset);               \
601
2.84k
            dyn_sym_table_size = yr_##bo##bits##toh(section->size);                       \
602
2.84k
            dyn_sym_str_table_size = yr_##bo##bits##toh(dynstr_section->size);            \
603
2.84k
          }                                                                               \
604
4.72k
        }                                                                                 \
605
1.00M
      }                                                                                   \
606
3.85k
                                                                                          \
607
3.85k
      if (is_valid_ptr(elf, elf_size, sym_str_table, sym_str_table_size) &&               \
608
3.85k
          is_valid_ptr(elf, elf_size, sym_table, sym_table_size))                         \
609
3.85k
      {                                                                                   \
610
609
        elf##bits##_sym_t* sym = (elf##bits##_sym_t*) sym_table;                          \
611
609
        elf_data->symtab = (ELF_SYMBOL_LIST*) yr_malloc(                                  \
612
609
            sizeof(ELF_SYMBOL_LIST));                                                     \
613
609
                                                                                          \
614
609
        if (elf_data->symtab == NULL)                                                     \
615
609
          return ERROR_INSUFFICIENT_MEMORY;                                               \
616
609
                                                                                          \
617
609
        ELF_SYMBOL** symbol = &(elf_data->symtab->symbols);                               \
618
609
        *symbol = NULL;                                                                   \
619
609
                                                                                          \
620
292k
        for (j = 0; j < sym_table_size / sizeof(elf##bits##_sym_t);                       \
621
291k
             j++, sym++)                                                                  \
622
291k
        {                                                                                 \
623
291k
          *symbol = (ELF_SYMBOL*) yr_malloc(sizeof(ELF_SYMBOL));                          \
624
291k
          if (*symbol == NULL)                                                            \
625
291k
            return ERROR_INSUFFICIENT_MEMORY;                                             \
626
291k
                                                                                          \
627
291k
          (*symbol)->name = NULL;                                                         \
628
291k
          (*symbol)->next = NULL;                                                         \
629
291k
                                                                                          \
630
291k
          const char* sym_name = str_table_entry(                                         \
631
291k
              sym_str_table,                                                              \
632
291k
              sym_str_table + sym_str_table_size,                                         \
633
291k
              yr_##bo##32toh(sym->name));                                                 \
634
291k
                                                                                          \
635
291k
          if (sym_name)                                                                   \
636
291k
          {                                                                               \
637
70.6k
            yr_set_string(sym_name, elf_obj, "symtab[%i].name", j);                       \
638
70.6k
            (*symbol)->name = (char*) yr_malloc(strlen(sym_name) + 1);                    \
639
70.6k
            if ((*symbol)->name == NULL)                                                  \
640
70.6k
              return ERROR_INSUFFICIENT_MEMORY;                                           \
641
70.6k
                                                                                          \
642
70.6k
            strcpy((*symbol)->name, sym_name);                                            \
643
70.6k
          }                                                                               \
644
291k
                                                                                          \
645
291k
          int bind = sym->info >> 4;                                                      \
646
291k
          (*symbol)->bind = bind;                                                         \
647
291k
          yr_set_integer(bind, elf_obj, "symtab[%i].bind", j);                            \
648
291k
                                                                                          \
649
291k
          int type = sym->info & 0xf;                                                     \
650
291k
          (*symbol)->type = type;                                                         \
651
291k
          yr_set_integer(type, elf_obj, "symtab[%i].type", j);                            \
652
291k
                                                                                          \
653
291k
          int shndx = yr_##bo##16toh(sym->shndx);                                         \
654
291k
          (*symbol)->shndx = shndx;                                                       \
655
291k
          yr_set_integer(shndx, elf_obj, "symtab[%i].shndx", j);                          \
656
291k
                                                                                          \
657
291k
          int value = yr_##bo##bits##toh(sym->value);                                     \
658
291k
          (*symbol)->value = value;                                                       \
659
291k
          yr_set_integer(                                                                 \
660
291k
              yr_##bo##bits##toh(sym->value), elf_obj, "symtab[%i].value", j);            \
661
291k
                                                                                          \
662
291k
          int size = yr_##bo##bits##toh(sym->size);                                       \
663
291k
          (*symbol)->size = size;                                                         \
664
291k
          yr_set_integer(                                                                 \
665
291k
              yr_##bo##bits##toh(sym->size), elf_obj, "symtab[%i].size", j);              \
666
291k
                                                                                          \
667
291k
          (*symbol)->visibility = sym->other & 0x3;                                       \
668
291k
                                                                                          \
669
291k
          symbol = &((*symbol)->next);                                                    \
670
291k
        }                                                                                 \
671
609
                                                                                          \
672
609
        elf_data->symtab->count = j;                                                      \
673
609
        yr_set_integer(j, elf_obj, "symtab_entries");                                     \
674
609
      }                                                                                   \
675
3.85k
                                                                                          \
676
3.85k
      if (is_valid_ptr(                                                                   \
677
3.85k
              elf, elf_size, dyn_sym_str_table, dyn_sym_str_table_size) &&                \
678
3.85k
          is_valid_ptr(elf, elf_size, dyn_sym_table, dyn_sym_table_size))                 \
679
3.85k
      {                                                                                   \
680
582
        elf##bits##_sym_t* dynsym = (elf##bits##_sym_t*) dyn_sym_table;                   \
681
582
                                                                                          \
682
582
        elf_data->dynsym = (ELF_SYMBOL_LIST*) yr_malloc(                                  \
683
582
            sizeof(ELF_SYMBOL_LIST));                                                     \
684
582
                                                                                          \
685
582
        if (elf_data->dynsym == NULL)                                                     \
686
582
          return ERROR_INSUFFICIENT_MEMORY;                                               \
687
582
                                                                                          \
688
582
        ELF_SYMBOL** symbol = &(elf_data->dynsym->symbols);                               \
689
582
        *symbol = NULL;                                                                   \
690
582
                                                                                          \
691
190k
        for (m = 0; m < dyn_sym_table_size / sizeof(elf##bits##_sym_t);                   \
692
190k
             m++, dynsym++)                                                               \
693
190k
        {                                                                                 \
694
190k
          *symbol = (ELF_SYMBOL*) yr_malloc(sizeof(ELF_SYMBOL));                          \
695
190k
          if (*symbol == NULL)                                                            \
696
190k
            return ERROR_INSUFFICIENT_MEMORY;                                             \
697
190k
                                                                                          \
698
190k
          (*symbol)->name = NULL;                                                         \
699
190k
          (*symbol)->next = NULL;                                                         \
700
190k
                                                                                          \
701
190k
          const char* dynsym_name = str_table_entry(                                      \
702
190k
              dyn_sym_str_table,                                                          \
703
190k
              dyn_sym_str_table + dyn_sym_str_table_size,                                 \
704
190k
              yr_##bo##32toh(dynsym->name));                                              \
705
190k
                                                                                          \
706
190k
          if (dynsym_name)                                                                \
707
190k
          {                                                                               \
708
52.4k
            yr_set_string(dynsym_name, elf_obj, "dynsym[%i].name", m);                    \
709
52.4k
            (*symbol)->name = (char*) yr_malloc(strlen(dynsym_name) + 1);                 \
710
52.4k
            if ((*symbol)->name == NULL)                                                  \
711
52.4k
              return ERROR_INSUFFICIENT_MEMORY;                                           \
712
52.4k
                                                                                          \
713
52.4k
            strcpy((*symbol)->name, dynsym_name);                                         \
714
52.4k
          }                                                                               \
715
190k
                                                                                          \
716
190k
          int bind = dynsym->info >> 4;                                                   \
717
190k
          (*symbol)->bind = bind;                                                         \
718
190k
          yr_set_integer(dynsym->info >> 4, elf_obj, "dynsym[%i].bind", m);               \
719
190k
                                                                                          \
720
190k
          int type = dynsym->info & 0xf;                                                  \
721
190k
          (*symbol)->type = type;                                                         \
722
190k
          yr_set_integer(dynsym->info & 0xf, elf_obj, "dynsym[%i].type", m);              \
723
190k
                                                                                          \
724
190k
          int shndx = yr_##bo##16toh(dynsym->shndx);                                      \
725
190k
          (*symbol)->shndx = shndx;                                                       \
726
190k
          yr_set_integer(                                                                 \
727
190k
              yr_##bo##16toh(dynsym->shndx), elf_obj, "dynsym[%i].shndx", m);             \
728
190k
                                                                                          \
729
190k
          int value = yr_##bo##bits##toh(dynsym->value);                                  \
730
190k
          (*symbol)->value = value;                                                       \
731
190k
          yr_set_integer(                                                                 \
732
190k
              yr_##bo##bits##toh(dynsym->value),                                          \
733
190k
              elf_obj,                                                                    \
734
190k
              "dynsym[%i].value",                                                         \
735
190k
              m);                                                                         \
736
190k
                                                                                          \
737
190k
          int size = yr_##bo##bits##toh(dynsym->size);                                    \
738
190k
          (*symbol)->size = size;                                                         \
739
190k
          yr_set_integer(                                                                 \
740
190k
              yr_##bo##bits##toh(dynsym->size),                                           \
741
190k
              elf_obj,                                                                    \
742
190k
              "dynsym[%i].size",                                                          \
743
190k
              m);                                                                         \
744
190k
                                                                                          \
745
190k
          (*symbol)->visibility = dynsym->other & 0x3;                                    \
746
190k
                                                                                          \
747
190k
          symbol = &((*symbol)->next);                                                    \
748
190k
        }                                                                                 \
749
582
                                                                                          \
750
582
        elf_data->dynsym->count = m;                                                      \
751
582
        yr_set_integer(m, elf_obj, "dynsym_entries");                                     \
752
582
      }                                                                                   \
753
3.85k
    }                                                                                     \
754
6.14k
                                                                                          \
755
6.14k
    if (yr_##bo##16toh(elf->ph_entry_count) > 0 &&                                        \
756
6.14k
        yr_##bo##16toh(elf->ph_entry_count) < ELF_PN_XNUM &&                              \
757
6.14k
        yr_##bo##bits##toh(elf->ph_offset) < elf_size &&                                  \
758
6.14k
        yr_##bo##bits##toh(elf->ph_offset) +                                              \
759
2.28k
                yr_##bo##16toh(elf->ph_entry_count) *                                     \
760
2.28k
                    sizeof(elf##bits##_program_header_t) <=                               \
761
2.28k
            elf_size)                                                                     \
762
6.14k
    {                                                                                     \
763
1.32k
      segment =                                                                           \
764
1.32k
          (elf##bits##_program_header_t*) (elf_raw + yr_##bo##bits##toh(elf->ph_offset)); \
765
1.32k
                                                                                          \
766
765k
      for (i = 0; i < yr_##bo##16toh(elf->ph_entry_count); i++, segment++)                \
767
764k
      {                                                                                   \
768
764k
        yr_set_integer(                                                                   \
769
764k
            yr_##bo##32toh(segment->type), elf_obj, "segments[%i].type", i);              \
770
764k
        yr_set_integer(                                                                   \
771
764k
            yr_##bo##32toh(segment->flags), elf_obj, "segments[%i].flags", i);            \
772
764k
        yr_set_integer(                                                                   \
773
764k
            yr_##bo##bits##toh(segment->offset),                                          \
774
764k
            elf_obj,                                                                      \
775
764k
            "segments[%i].offset",                                                        \
776
764k
            i);                                                                           \
777
764k
        yr_set_integer(                                                                   \
778
764k
            yr_##bo##bits##toh(segment->virt_addr),                                       \
779
764k
            elf_obj,                                                                      \
780
764k
            "segments[%i].virtual_address",                                               \
781
764k
            i);                                                                           \
782
764k
        yr_set_integer(                                                                   \
783
764k
            yr_##bo##bits##toh(segment->phys_addr),                                       \
784
764k
            elf_obj,                                                                      \
785
764k
            "segments[%i].physical_address",                                              \
786
764k
            i);                                                                           \
787
764k
        yr_set_integer(                                                                   \
788
764k
            yr_##bo##bits##toh(segment->file_size),                                       \
789
764k
            elf_obj,                                                                      \
790
764k
            "segments[%i].file_size",                                                     \
791
764k
            i);                                                                           \
792
764k
        yr_set_integer(                                                                   \
793
764k
            yr_##bo##bits##toh(segment->mem_size),                                        \
794
764k
            elf_obj,                                                                      \
795
764k
            "segments[%i].memory_size",                                                   \
796
764k
            i);                                                                           \
797
764k
        yr_set_integer(                                                                   \
798
764k
            yr_##bo##bits##toh(segment->alignment),                                       \
799
764k
            elf_obj,                                                                      \
800
764k
            "segments[%i].alignment",                                                     \
801
764k
            i);                                                                           \
802
764k
                                                                                          \
803
764k
        if (yr_##bo##32toh(segment->type) == ELF_PT_DYNAMIC)                              \
804
764k
        {                                                                                 \
805
14.4k
          j = 0;                                                                          \
806
14.4k
          if (yr_##bo##bits##toh(segment->offset) < elf_size)                             \
807
14.4k
          {                                                                               \
808
8.02k
            elf##bits##_dyn_t* dyn =                                                      \
809
8.02k
                (elf##bits##_dyn_t*) (elf_raw + yr_##bo##bits##toh(segment->offset));     \
810
8.02k
                                                                                          \
811
5.10M
            for (j = 0; IS_VALID_PTR(elf, elf_size, dyn); dyn++, j++)                     \
812
5.10M
            {                                                                             \
813
5.10M
              yr_set_integer(                                                             \
814
5.10M
                  yr_##bo##bits##toh(dyn->tag), elf_obj, "dynamic[%i].type", j);          \
815
5.10M
              yr_set_integer(                                                             \
816
5.10M
                  yr_##bo##bits##toh(dyn->val), elf_obj, "dynamic[%i].val", j);           \
817
5.10M
                                                                                          \
818
5.10M
              if (dyn->tag == ELF_DT_NULL)                                                \
819
5.10M
              {                                                                           \
820
6.17k
                j++;                                                                      \
821
6.17k
                break;                                                                    \
822
6.17k
              }                                                                           \
823
5.10M
            }                                                                             \
824
8.02k
          }                                                                               \
825
14.4k
          yr_set_integer(j, elf_obj, "dynamic_section_entries");                          \
826
14.4k
        }                                                                                 \
827
764k
      }                                                                                   \
828
1.32k
    }                                                                                     \
829
6.14k
    return ERROR_SUCCESS;                                                                 \
830
6.14k
  }
831
832
1.14k
ELF_RVA_TO_OFFSET(32, le);
833
1.90k
ELF_RVA_TO_OFFSET(64, le);
834
1.05k
ELF_RVA_TO_OFFSET(32, be);
835
1.83k
ELF_RVA_TO_OFFSET(64, be);
836
837
1.23k
PARSE_ELF_HEADER(32, le);
838
1.92k
PARSE_ELF_HEADER(64, le);
839
1.09k
PARSE_ELF_HEADER(32, be);
840
1.88k
PARSE_ELF_HEADER(64, be);
841
842
7.29k
begin_declarations
843
7.29k
  declare_integer("ET_NONE");
844
7.29k
  declare_integer("ET_REL");
845
7.29k
  declare_integer("ET_EXEC");
846
7.29k
  declare_integer("ET_DYN");
847
7.29k
  declare_integer("ET_CORE");
848
849
7.29k
  declare_integer("EM_NONE");
850
7.29k
  declare_integer("EM_M32");
851
7.29k
  declare_integer("EM_SPARC");
852
7.29k
  declare_integer("EM_386");
853
7.29k
  declare_integer("EM_68K");
854
7.29k
  declare_integer("EM_88K");
855
7.29k
  declare_integer("EM_860");
856
7.29k
  declare_integer("EM_MIPS");
857
7.29k
  declare_integer("EM_MIPS_RS3_LE");
858
7.29k
  declare_integer("EM_PARISC");
859
7.29k
  declare_integer("EM_SPARC32PLUS");
860
7.29k
  declare_integer("EM_PPC");
861
7.29k
  declare_integer("EM_PPC64");
862
7.29k
  declare_integer("EM_S390");
863
7.29k
  declare_integer("EM_MCORE");
864
7.29k
  declare_integer("EM_RCE");
865
7.29k
  declare_integer("EM_ARM");
866
7.29k
  declare_integer("EM_SH");
867
7.29k
  declare_integer("EM_SPARCV9");
868
7.29k
  declare_integer("EM_X86_64");
869
7.29k
  declare_integer("EM_ARC_COMPACT");
870
7.29k
  declare_integer("EM_AARCH64");
871
7.29k
  declare_integer("EM_BPF");
872
7.29k
  declare_integer("EM_LOONGARCH");
873
874
7.29k
  declare_integer("SHT_NULL");
875
7.29k
  declare_integer("SHT_PROGBITS");
876
7.29k
  declare_integer("SHT_SYMTAB");
877
7.29k
  declare_integer("SHT_STRTAB");
878
7.29k
  declare_integer("SHT_RELA");
879
7.29k
  declare_integer("SHT_HASH");
880
7.29k
  declare_integer("SHT_DYNAMIC");
881
7.29k
  declare_integer("SHT_NOTE");
882
7.29k
  declare_integer("SHT_NOBITS");
883
7.29k
  declare_integer("SHT_REL");
884
7.29k
  declare_integer("SHT_SHLIB");
885
7.29k
  declare_integer("SHT_DYNSYM");
886
887
7.29k
  declare_integer("SHF_WRITE");
888
7.29k
  declare_integer("SHF_ALLOC");
889
7.29k
  declare_integer("SHF_EXECINSTR");
890
891
7.29k
  declare_integer("type");
892
7.29k
  declare_integer("machine");
893
7.29k
  declare_integer("entry_point");
894
895
7.29k
  declare_integer("number_of_sections");
896
7.29k
  declare_integer("sh_offset");
897
7.29k
  declare_integer("sh_entry_size");
898
899
7.29k
  declare_integer("number_of_segments");
900
7.29k
  declare_integer("ph_offset");
901
7.29k
  declare_integer("ph_entry_size");
902
903
21.8k
  begin_struct_array("sections")
904
7.29k
    declare_integer("type");
905
7.29k
    declare_integer("flags");
906
7.29k
    declare_integer("address");
907
7.29k
    declare_string("name");
908
7.29k
    declare_integer("size");
909
7.29k
    declare_integer("offset");
910
14.5k
  end_struct_array("sections")
911
912
7.29k
  declare_integer("PT_NULL");
913
7.29k
  declare_integer("PT_LOAD");
914
7.29k
  declare_integer("PT_DYNAMIC");
915
7.29k
  declare_integer("PT_INTERP");
916
7.29k
  declare_integer("PT_NOTE");
917
7.29k
  declare_integer("PT_SHLIB");
918
7.29k
  declare_integer("PT_PHDR");
919
7.29k
  declare_integer("PT_TLS");
920
7.29k
  declare_integer("PT_GNU_EH_FRAME");
921
7.29k
  declare_integer("PT_GNU_STACK");
922
923
7.29k
  declare_integer("DT_NULL");
924
7.29k
  declare_integer("DT_NEEDED");
925
7.29k
  declare_integer("DT_PLTRELSZ");
926
7.29k
  declare_integer("DT_PLTGOT");
927
7.29k
  declare_integer("DT_HASH");
928
7.29k
  declare_integer("DT_STRTAB");
929
7.29k
  declare_integer("DT_SYMTAB");
930
7.29k
  declare_integer("DT_RELA");
931
7.29k
  declare_integer("DT_RELASZ");
932
7.29k
  declare_integer("DT_RELAENT");
933
7.29k
  declare_integer("DT_STRSZ");
934
7.29k
  declare_integer("DT_SYMENT");
935
7.29k
  declare_integer("DT_INIT");
936
7.29k
  declare_integer("DT_FINI");
937
7.29k
  declare_integer("DT_SONAME");
938
7.29k
  declare_integer("DT_RPATH");
939
7.29k
  declare_integer("DT_SYMBOLIC");
940
7.29k
  declare_integer("DT_REL");
941
7.29k
  declare_integer("DT_RELSZ");
942
7.29k
  declare_integer("DT_RELENT");
943
7.29k
  declare_integer("DT_PLTREL");
944
7.29k
  declare_integer("DT_DEBUG");
945
7.29k
  declare_integer("DT_TEXTREL");
946
7.29k
  declare_integer("DT_JMPREL");
947
7.29k
  declare_integer("DT_BIND_NOW");
948
7.29k
  declare_integer("DT_INIT_ARRAY");
949
7.29k
  declare_integer("DT_FINI_ARRAY");
950
7.29k
  declare_integer("DT_INIT_ARRAYSZ");
951
7.29k
  declare_integer("DT_FINI_ARRAYSZ");
952
7.29k
  declare_integer("DT_RUNPATH");
953
7.29k
  declare_integer("DT_FLAGS");
954
7.29k
  declare_integer("DT_ENCODING");
955
956
7.29k
  declare_integer("STT_NOTYPE");
957
7.29k
  declare_integer("STT_OBJECT");
958
7.29k
  declare_integer("STT_FUNC");
959
7.29k
  declare_integer("STT_SECTION");
960
7.29k
  declare_integer("STT_FILE");
961
7.29k
  declare_integer("STT_COMMON");
962
7.29k
  declare_integer("STT_TLS");
963
964
7.29k
  declare_integer("STB_LOCAL");
965
7.29k
  declare_integer("STB_GLOBAL");
966
7.29k
  declare_integer("STB_WEAK");
967
968
7.29k
  declare_integer("PF_X");
969
7.29k
  declare_integer("PF_W");
970
7.29k
  declare_integer("PF_R");
971
972
21.8k
  begin_struct_array("segments")
973
7.29k
    declare_integer("type");
974
7.29k
    declare_integer("flags");
975
7.29k
    declare_integer("offset");
976
7.29k
    declare_integer("virtual_address");
977
7.29k
    declare_integer("physical_address");
978
7.29k
    declare_integer("file_size");
979
7.29k
    declare_integer("memory_size");
980
7.29k
    declare_integer("alignment");
981
14.5k
  end_struct_array("segments")
982
983
7.29k
  declare_integer("dynamic_section_entries");
984
21.8k
  begin_struct_array("dynamic")
985
7.29k
    declare_integer("type");
986
7.29k
    declare_integer("val");
987
14.5k
  end_struct_array("dynamic")
988
989
7.29k
  declare_integer("symtab_entries");
990
21.8k
  begin_struct_array("symtab")
991
7.29k
    declare_string("name");
992
7.29k
    declare_integer("value");
993
7.29k
    declare_integer("size");
994
7.29k
    declare_integer("type");
995
7.29k
    declare_integer("bind");
996
7.29k
    declare_integer("shndx");
997
14.5k
  end_struct_array("symtab")
998
999
7.29k
  declare_integer("dynsym_entries");
1000
21.8k
  begin_struct_array("dynsym")
1001
7.29k
    declare_string("name");
1002
7.29k
    declare_integer("value");
1003
7.29k
    declare_integer("size");
1004
7.29k
    declare_integer("type");
1005
7.29k
    declare_integer("bind");
1006
7.29k
    declare_integer("shndx");
1007
14.5k
  end_struct_array("dynsym")
1008
1009
7.29k
  declare_function("telfhash", "", "s", telfhash);
1010
1011
#if defined(HAVE_LIBCRYPTO) || defined(HAVE_WINCRYPT_H) || \
1012
    defined(HAVE_COMMONCRYPTO_COMMONCRYPTO_H)
1013
  declare_function("import_md5", "", "s", import_md5);
1014
#endif  // defined(HAVE_LIBCRYPTO) || defined(HAVE_WINCRYPT_H)
1015
1016
7.29k
end_declarations
1017
1018
int module_initialize(YR_MODULE* module)
1019
2
{
1020
2
  return ERROR_SUCCESS;
1021
2
}
1022
1023
int module_finalize(YR_MODULE* module)
1024
0
{
1025
0
  return ERROR_SUCCESS;
1026
0
}
1027
1028
int module_load(
1029
    YR_SCAN_CONTEXT* context,
1030
    YR_OBJECT* module_object,
1031
    void* module_data,
1032
    size_t module_data_size)
1033
7.28k
{
1034
7.28k
  YR_MEMORY_BLOCK* block;
1035
7.28k
  YR_MEMORY_BLOCK_ITERATOR* iterator = context->iterator;
1036
1037
7.28k
  elf32_header_t* elf_header32;
1038
7.28k
  elf64_header_t* elf_header64;
1039
1040
7.28k
  yr_set_integer(ELF_ET_NONE, module_object, "ET_NONE");
1041
7.28k
  yr_set_integer(ELF_ET_REL, module_object, "ET_REL");
1042
7.28k
  yr_set_integer(ELF_ET_EXEC, module_object, "ET_EXEC");
1043
7.28k
  yr_set_integer(ELF_ET_DYN, module_object, "ET_DYN");
1044
7.28k
  yr_set_integer(ELF_ET_CORE, module_object, "ET_CORE");
1045
1046
7.28k
  yr_set_integer(ELF_EM_NONE, module_object, "EM_NONE");
1047
7.28k
  yr_set_integer(ELF_EM_M32, module_object, "EM_M32");
1048
7.28k
  yr_set_integer(ELF_EM_SPARC, module_object, "EM_SPARC");
1049
7.28k
  yr_set_integer(ELF_EM_386, module_object, "EM_386");
1050
7.28k
  yr_set_integer(ELF_EM_68K, module_object, "EM_68K");
1051
7.28k
  yr_set_integer(ELF_EM_88K, module_object, "EM_88K");
1052
7.28k
  yr_set_integer(ELF_EM_860, module_object, "EM_860");
1053
7.28k
  yr_set_integer(ELF_EM_MIPS, module_object, "EM_MIPS");
1054
7.28k
  yr_set_integer(ELF_EM_MIPS_RS3_LE, module_object, "EM_MIPS_RS3_LE");
1055
7.28k
  yr_set_integer(ELF_EM_PARISC, module_object, "EM_PARISC");
1056
7.28k
  yr_set_integer(ELF_EM_SPARC32PLUS, module_object, "EM_SPARC32PLUS");
1057
7.28k
  yr_set_integer(ELF_EM_PPC, module_object, "EM_PPC");
1058
7.28k
  yr_set_integer(ELF_EM_PPC64, module_object, "EM_PPC64");
1059
7.28k
  yr_set_integer(ELF_EM_S390, module_object, "EM_S390");
1060
7.28k
  yr_set_integer(ELF_EM_MCORE, module_object, "EM_MCORE");
1061
  // EM_RCE is the name 0x27 was originally registered under and is still what
1062
  // some toolchains emit. Same value, exposed so either name works in a rule.
1063
7.28k
  yr_set_integer(ELF_EM_MCORE, module_object, "EM_RCE");
1064
7.28k
  yr_set_integer(ELF_EM_ARM, module_object, "EM_ARM");
1065
7.28k
  yr_set_integer(ELF_EM_SH, module_object, "EM_SH");
1066
7.28k
  yr_set_integer(ELF_EM_SPARCV9, module_object, "EM_SPARCV9");
1067
7.28k
  yr_set_integer(ELF_EM_X86_64, module_object, "EM_X86_64");
1068
7.28k
  yr_set_integer(ELF_EM_ARC_COMPACT, module_object, "EM_ARC_COMPACT");
1069
7.28k
  yr_set_integer(ELF_EM_AARCH64, module_object, "EM_AARCH64");
1070
7.28k
  yr_set_integer(ELF_EM_BPF, module_object, "EM_BPF");
1071
7.28k
  yr_set_integer(ELF_EM_LOONGARCH, module_object, "EM_LOONGARCH");
1072
1073
7.28k
  yr_set_integer(ELF_SHT_NULL, module_object, "SHT_NULL");
1074
7.28k
  yr_set_integer(ELF_SHT_PROGBITS, module_object, "SHT_PROGBITS");
1075
7.28k
  yr_set_integer(ELF_SHT_SYMTAB, module_object, "SHT_SYMTAB");
1076
7.28k
  yr_set_integer(ELF_SHT_STRTAB, module_object, "SHT_STRTAB");
1077
7.28k
  yr_set_integer(ELF_SHT_RELA, module_object, "SHT_RELA");
1078
7.28k
  yr_set_integer(ELF_SHT_HASH, module_object, "SHT_HASH");
1079
7.28k
  yr_set_integer(ELF_SHT_DYNAMIC, module_object, "SHT_DYNAMIC");
1080
7.28k
  yr_set_integer(ELF_SHT_NOTE, module_object, "SHT_NOTE");
1081
7.28k
  yr_set_integer(ELF_SHT_NOBITS, module_object, "SHT_NOBITS");
1082
7.28k
  yr_set_integer(ELF_SHT_REL, module_object, "SHT_REL");
1083
7.28k
  yr_set_integer(ELF_SHT_SHLIB, module_object, "SHT_SHLIB");
1084
7.28k
  yr_set_integer(ELF_SHT_DYNSYM, module_object, "SHT_DYNSYM");
1085
1086
7.28k
  yr_set_integer(ELF_SHF_WRITE, module_object, "SHF_WRITE");
1087
7.28k
  yr_set_integer(ELF_SHF_ALLOC, module_object, "SHF_ALLOC");
1088
7.28k
  yr_set_integer(ELF_SHF_EXECINSTR, module_object, "SHF_EXECINSTR");
1089
1090
7.28k
  yr_set_integer(ELF_PT_NULL, module_object, "PT_NULL");
1091
7.28k
  yr_set_integer(ELF_PT_LOAD, module_object, "PT_LOAD");
1092
7.28k
  yr_set_integer(ELF_PT_DYNAMIC, module_object, "PT_DYNAMIC");
1093
7.28k
  yr_set_integer(ELF_PT_INTERP, module_object, "PT_INTERP");
1094
7.28k
  yr_set_integer(ELF_PT_NOTE, module_object, "PT_NOTE");
1095
7.28k
  yr_set_integer(ELF_PT_SHLIB, module_object, "PT_SHLIB");
1096
7.28k
  yr_set_integer(ELF_PT_PHDR, module_object, "PT_PHDR");
1097
7.28k
  yr_set_integer(ELF_PT_TLS, module_object, "PT_TLS");
1098
7.28k
  yr_set_integer(ELF_PT_GNU_EH_FRAME, module_object, "PT_GNU_EH_FRAME");
1099
7.28k
  yr_set_integer(ELF_PT_GNU_STACK, module_object, "PT_GNU_STACK");
1100
1101
7.28k
  yr_set_integer(ELF_DT_NULL, module_object, "DT_NULL");
1102
7.28k
  yr_set_integer(ELF_DT_NEEDED, module_object, "DT_NEEDED");
1103
7.28k
  yr_set_integer(ELF_DT_PLTRELSZ, module_object, "DT_PLTRELSZ");
1104
7.28k
  yr_set_integer(ELF_DT_PLTGOT, module_object, "DT_PLTGOT");
1105
7.28k
  yr_set_integer(ELF_DT_HASH, module_object, "DT_HASH");
1106
7.28k
  yr_set_integer(ELF_DT_STRTAB, module_object, "DT_STRTAB");
1107
7.28k
  yr_set_integer(ELF_DT_SYMTAB, module_object, "DT_SYMTAB");
1108
7.28k
  yr_set_integer(ELF_DT_RELA, module_object, "DT_RELA");
1109
7.28k
  yr_set_integer(ELF_DT_RELASZ, module_object, "DT_RELASZ");
1110
7.28k
  yr_set_integer(ELF_DT_RELAENT, module_object, "DT_RELAENT");
1111
7.28k
  yr_set_integer(ELF_DT_STRSZ, module_object, "DT_STRSZ");
1112
7.28k
  yr_set_integer(ELF_DT_SYMENT, module_object, "DT_SYMENT");
1113
7.28k
  yr_set_integer(ELF_DT_INIT, module_object, "DT_INIT");
1114
7.28k
  yr_set_integer(ELF_DT_FINI, module_object, "DT_FINI");
1115
7.28k
  yr_set_integer(ELF_DT_SONAME, module_object, "DT_SONAME");
1116
7.28k
  yr_set_integer(ELF_DT_RPATH, module_object, "DT_RPATH");
1117
7.28k
  yr_set_integer(ELF_DT_SYMBOLIC, module_object, "DT_SYMBOLIC");
1118
7.28k
  yr_set_integer(ELF_DT_REL, module_object, "DT_REL");
1119
7.28k
  yr_set_integer(ELF_DT_RELSZ, module_object, "DT_RELSZ");
1120
7.28k
  yr_set_integer(ELF_DT_RELENT, module_object, "DT_RELENT");
1121
7.28k
  yr_set_integer(ELF_DT_PLTREL, module_object, "DT_PLTREL");
1122
7.28k
  yr_set_integer(ELF_DT_DEBUG, module_object, "DT_DEBUG");
1123
7.28k
  yr_set_integer(ELF_DT_TEXTREL, module_object, "DT_TEXTREL");
1124
7.28k
  yr_set_integer(ELF_DT_JMPREL, module_object, "DT_JMPREL");
1125
7.28k
  yr_set_integer(ELF_DT_BIND_NOW, module_object, "DT_BIND_NOW");
1126
7.28k
  yr_set_integer(ELF_DT_INIT_ARRAY, module_object, "DT_INIT_ARRAY");
1127
7.28k
  yr_set_integer(ELF_DT_FINI_ARRAY, module_object, "DT_FINI_ARRAY");
1128
7.28k
  yr_set_integer(ELF_DT_INIT_ARRAYSZ, module_object, "DT_INIT_ARRAYSZ");
1129
7.28k
  yr_set_integer(ELF_DT_FINI_ARRAYSZ, module_object, "DT_FINI_ARRAYSZ");
1130
7.28k
  yr_set_integer(ELF_DT_RUNPATH, module_object, "DT_RUNPATH");
1131
7.28k
  yr_set_integer(ELF_DT_FLAGS, module_object, "DT_FLAGS");
1132
7.28k
  yr_set_integer(ELF_DT_ENCODING, module_object, "DT_ENCODING");
1133
1134
7.28k
  yr_set_integer(ELF_STT_NOTYPE, module_object, "STT_NOTYPE");
1135
7.28k
  yr_set_integer(ELF_STT_OBJECT, module_object, "STT_OBJECT");
1136
7.28k
  yr_set_integer(ELF_STT_FUNC, module_object, "STT_FUNC");
1137
7.28k
  yr_set_integer(ELF_STT_SECTION, module_object, "STT_SECTION");
1138
7.28k
  yr_set_integer(ELF_STT_FILE, module_object, "STT_FILE");
1139
7.28k
  yr_set_integer(ELF_STT_COMMON, module_object, "STT_COMMON");
1140
7.28k
  yr_set_integer(ELF_STT_TLS, module_object, "STT_TLS");
1141
1142
7.28k
  yr_set_integer(ELF_STB_LOCAL, module_object, "STB_LOCAL");
1143
7.28k
  yr_set_integer(ELF_STB_GLOBAL, module_object, "STB_GLOBAL");
1144
7.28k
  yr_set_integer(ELF_STB_WEAK, module_object, "STB_WEAK");
1145
1146
7.28k
  yr_set_integer(ELF_PF_X, module_object, "PF_X");
1147
7.28k
  yr_set_integer(ELF_PF_W, module_object, "PF_W");
1148
7.28k
  yr_set_integer(ELF_PF_R, module_object, "PF_R");
1149
1150
7.28k
  uint64_t parse_result = ERROR_SUCCESS;
1151
1152
7.28k
  foreach_memory_block(iterator, block)
1153
7.28k
  {
1154
7.28k
    const uint8_t* block_data = yr_fetch_block_data(block);
1155
1156
7.28k
    if (block_data == NULL)
1157
0
      continue;
1158
1159
7.28k
    ELF* elf = (ELF*) module_object->data;
1160
7.28k
    if (elf == NULL)
1161
7.28k
    {
1162
7.28k
      elf = (ELF*) yr_calloc(1, sizeof(ELF));
1163
7.28k
      if (elf == NULL)
1164
0
        return ERROR_INSUFFICIENT_MEMORY;
1165
1166
7.28k
      module_object->data = elf;
1167
7.28k
    }
1168
7.28k
    int class_data = get_elf_class_data(block_data, block->size);
1169
1170
7.28k
    if (class_data == CLASS_DATA(ELF_CLASS_32, ELF_DATA_2LSB) &&
1171
1.42k
        block->size > sizeof(elf32_header_t))
1172
1.23k
    {
1173
1.23k
      elf_header32 = (elf32_header_t*) block_data;
1174
1175
1.23k
      if (!(context->flags & SCAN_FLAGS_PROCESS_MEMORY) ||
1176
0
          yr_le16toh(elf_header32->type) == ELF_ET_EXEC)
1177
1.23k
      {
1178
1.23k
        parse_result = parse_elf_header_32_le(
1179
1.23k
            elf,
1180
1.23k
            elf_header32,
1181
1.23k
            block->base,
1182
1.23k
            block->size,
1183
1.23k
            context->flags,
1184
1.23k
            module_object);
1185
1.23k
        break;
1186
1.23k
      }
1187
6.05k
    } else if (
1188
6.05k
        class_data == CLASS_DATA(ELF_CLASS_32, ELF_DATA_2MSB) &&
1189
1.16k
        block->size > sizeof(elf32_header_t))
1190
1.09k
    {
1191
1.09k
      elf_header32 = (elf32_header_t*) block_data;
1192
1193
1.09k
      if (!(context->flags & SCAN_FLAGS_PROCESS_MEMORY) ||
1194
0
          yr_be16toh(elf_header32->type) == ELF_ET_EXEC)
1195
1.09k
      {
1196
1.09k
        parse_result = parse_elf_header_32_be(
1197
1.09k
            elf,
1198
1.09k
            elf_header32,
1199
1.09k
            block->base,
1200
1.09k
            block->size,
1201
1.09k
            context->flags,
1202
1.09k
            module_object);
1203
1.09k
        break;
1204
1.09k
      }
1205
4.95k
    } else if (
1206
4.95k
        class_data == CLASS_DATA(ELF_CLASS_64, ELF_DATA_2LSB) &&
1207
2.24k
        block->size > sizeof(elf64_header_t))
1208
1.92k
    {
1209
1.92k
      elf_header64 = (elf64_header_t*) block_data;
1210
1211
1.92k
      if (!(context->flags & SCAN_FLAGS_PROCESS_MEMORY) ||
1212
0
          yr_le16toh(elf_header64->type) == ELF_ET_EXEC)
1213
1.92k
      {
1214
1.92k
        parse_result = parse_elf_header_64_le(
1215
1.92k
            elf,
1216
1.92k
            elf_header64,
1217
1.92k
            block->base,
1218
1.92k
            block->size,
1219
1.92k
            context->flags,
1220
1.92k
            module_object);
1221
1.92k
        break;
1222
1.92k
      }
1223
3.03k
    } else if (
1224
3.03k
        class_data == CLASS_DATA(ELF_CLASS_64, ELF_DATA_2MSB) &&
1225
1.98k
        block->size > sizeof(elf64_header_t))
1226
1.88k
    {
1227
1.88k
      elf_header64 = (elf64_header_t*) block_data;
1228
1229
1.88k
      if (!(context->flags & SCAN_FLAGS_PROCESS_MEMORY) ||
1230
0
          yr_be16toh(elf_header64->type) == ELF_ET_EXEC)
1231
1.88k
      {
1232
1.88k
        parse_result = parse_elf_header_64_be(
1233
1.88k
            elf,
1234
1.88k
            elf_header64,
1235
1.88k
            block->base,
1236
1.88k
            block->size,
1237
1.88k
            context->flags,
1238
1.88k
            module_object);
1239
1.88k
        break;
1240
1.88k
      }
1241
1.88k
    }
1242
7.28k
  }
1243
1244
7.28k
  return parse_result;
1245
7.28k
}
1246
1247
int module_unload(YR_OBJECT* module_object)
1248
7.28k
{
1249
7.28k
  ELF* elf = (ELF*) module_object->data;
1250
7.28k
  if (elf == NULL)
1251
0
    return ERROR_SUCCESS;
1252
1253
7.28k
  if (elf->symtab != NULL)
1254
609
  {
1255
609
    ELF_SYMBOL *act = NULL, *next = NULL;
1256
292k
    for (act = elf->symtab->symbols; act != NULL; act = next)
1257
291k
    {
1258
291k
      next = act->next;
1259
291k
      if (act->name != NULL)
1260
70.6k
        yr_free(act->name);
1261
291k
      yr_free(act);
1262
291k
    }
1263
609
    yr_free(elf->symtab);
1264
609
  }
1265
1266
7.28k
  if (elf->dynsym != NULL)
1267
582
  {
1268
582
    ELF_SYMBOL *act = NULL, *next = NULL;
1269
190k
    for (act = elf->dynsym->symbols; act != NULL; act = next)
1270
190k
    {
1271
190k
      next = act->next;
1272
190k
      if (act->name != NULL)
1273
52.4k
        yr_free(act->name);
1274
190k
      yr_free(act);
1275
190k
    }
1276
582
    yr_free(elf->dynsym);
1277
582
  }
1278
1279
7.28k
  yr_free(elf->telfhash);
1280
7.28k
  yr_free(elf->import_hash);
1281
7.28k
  yr_free(elf);
1282
1283
7.28k
  module_object->data = NULL;
1284
1285
7.28k
  return ERROR_SUCCESS;
1286
7.28k
}