Repository navigation
Expand file tree
/
Copy pathkallsyms.py
More file actions
1556 lines (1232 loc) · 54.8 KB
/
Copy pathkallsyms.py
File metadata and controls
1556 lines (1232 loc) · 54.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
# -*- encoding: Utf-8 -*-
import logging
import math
from enum import Enum
from re import match, search, findall
from struct import pack, unpack_from
from io import StringIO
from typing import Optional
from vmlinux_to_elf.core.architecture_detecter import (
ArchitectureDetectionResult,
ArchitectureDetector,
ArchitectureGuessError,
ArchitectureName,
)
from vmlinux_to_elf.kernel_db.database import (
KernelVersion,
KernelRelevantFile,
DebianRelease,
KernelVersionDependency,
EMachineValue,
KnownArchitecture,
)
"""
This class will take a raw kernel image (.IMG), and return the file
offsets for all kernel symbols, as well as the kallsyms_* structs and
base addresses, etc.
It is used as an import by "vmlinuz_extractor.py" and "elf_symbolizer.py".
The kallsyms table's current layout was introduced in August 2004 (since
kernel 2.6.10), its 2004+ implementation can be found here for parsing:
https://github.com/torvalds/linux/blob/v2.6.32/kernel/kallsyms.c
And here for generation:
https://github.com/torvalds/linux/blob/v2.6.32/scripts/kallsyms.c
^ This format is fully supported.
A major evolution is that since v4.6 (2016), by default on all architectures
except IA64, a new label called "kallsyms_relative_base" was added and
addresses are now offsets relative to this base, rather than relative
addresses. Also, these offsets are stored as the GNU As ".long" type, which
is 32-bits on x86_64.
https://github.com/torvalds/linux/commit/2213e9a66bb87d8344a1256b4ef568220d9587fb
^ This format should be supported.
In v4.20 (2018), more fields were shrunk down independently.
https://github.com/torvalds/linux/commit/80ffbaa5b1bd98e80e3239a3b8cfda2da433009a
^ This format should be supported.
Its 2002-2004 (versions 2.5.54-2.6.9) implementation code with basic "stem
compression" can be found here for parsing:
https://github.com/sarnobat/knoppix/blob/master/kernel/kallsyms.c
Here for generation:
https://github.com/sarnobat/knoppix/blob/master/scripts/kallsyms.c
(patch https://lwn.net/Articles/18837/)
In 2002 (versions 2.5.49-2.5.53) it shortly had a version of this code
without compression:
https://kernel.googlesource.com/pub/scm/linux/kernel/git/ralf/linux/+/refs/tags/linux-2.5.49/kernel/kallsyms.c
https://kernel.googlesource.com/pub/scm/linux/kernel/git/ralf/linux/+/refs/tags/linux-2.5.49/scripts/kallsyms
In earlier implementations (2000-2002), it was part of the modutils package
and was more primitive (no real compression). Its implementation code can be found
here for parsing:
https://github.com/carlobar/uclinux_leon3_UD/blob/master/user/modutils-2.4.26/example/kallsyms.c
Here for generation:
https://github.com/carlobar/uclinux_leon3_UD/blob/master/user/modutils-2.4.26/obj/obj_kallsyms.c
Kernels 2.5.39-2.5.48 (2002) also had a transitory parser at "kernel/kallsyms.c", but the generation
was still in modutils:
https://kernel.googlesource.com/pub/scm/linux/kernel/git/ralf/linux/+/refs/tags/linux-2.5.39/kernel/kallsyms.c
(patch https://lwn.net/Articles/10796/)
"""
# Symbol types are the same as exposed by "man nm"
class KallsymsSymbolType(Enum):
# Seen in actual kernels
ABSOLUTE = 'A'
BSS = 'B'
DATA = 'D'
RODATA = 'R'
TEXT = 'T'
WEAK_OBJECT_WITH_DEFAULT = 'V'
WEAK_SYMBOL_WITH_DEFAULT = 'W'
# Seen on nm's manpage
SMALL_DATA = 'G'
INDIRECT_FUNCTION = 'I'
DEBUGGING = 'N'
STACK_UNWIND = 'P'
COMMON = 'C'
SMALL_BSS = 'S'
UNDEFINED = 'U'
UNIQUE_GLOBAL = 'u'
WEAK_OBJECT = 'v'
WEAK_SYMBOL = 'w'
STABS_DEBUG = '-'
UNKNOWN = '?'
class KallsymsSymbol:
name: str = None
file_offset: int = None
virtual_address: int = None
symbol_type: KallsymsSymbolType = None
is_global: bool = False
class KallsymsNotFoundException(ValueError):
pass
class KallsymsFinder:
# Structure offsets to find
kallsyms_addresses_or_offsets__offset: int = None
kallsyms_num_syms__offset: int = None
kallsyms_names__offset: int = None
kallsyms_markers__offset: int = None
kallsyms_token_table__offset: int = None
kallsyms_token_index__offset: int = None
kallsyms_token_index_end__offset: int = None
elf64_rela: list[tuple[int, int, int]] = None
elf64_rela_start: int = None
elf64_rela_end_excl: int = None
kernel_text_candidate: int = None
# Inferred information
architecture: ArchitectureName = None
elf_machine: int = None
is_64_bits: int = None # Can be set manually
is_big_endian: bool = None
offset_table_element_size: int = None
# Parsed information
num_symbols: int = None
symbol_names: list = None
symbol_addresses: list = None
has_relative_base: bool = None
has_absolute_percpu: bool = None
relative_base_address: int = None
kernel_addresses: list[int] = None
symbols: list[KallsymsSymbol] = None
name_to_symbol: dict[str, KallsymsSymbol] = None
"""
Symbols are output in this order:
$ curl -sL https://github.com/torvalds/linux/raw/v2.6.32/scripts/kallsyms.c | grep output_label
output_label("kallsyms_addresses");
output_label("kallsyms_num_syms");
output_label("kallsyms_names");
output_label("kallsyms_markers");
output_label("kallsyms_token_table");
output_label("kallsyms_token_index");
We'll find kallsyms_token_table and infer the rest
"""
def __init__(
self,
kernel_img: bytes,
bit_size: int = None,
override_relative_base: bool = False,
base_address: Optional[int] = None,
# extra_info: bool = False,
):
self.override_relative_base = override_relative_base
self.kernel_img = kernel_img
# -
self.find_linux_kernel_version()
if bit_size:
if bit_size not in (64, 32):
raise ArchitectureGuessError(
'Please specify a register bit size of either 32 or 64 bits'
)
else:
self.is_64_bits = bit_size == 64
self.guess_architecture()
self.extract_db_information()
if self.is_64_bits:
self.find_elf64_rela(base_address)
self.apply_elf64_rela()
# -
try:
self.find_kallsyms_token_table()
self.find_kallsyms_token_index()
self.uncompressed_kallsyms = False
except KallsymsNotFoundException as first_error: # Maybe an OpenWRT kernel with an uncompressed kallsyms
try:
self.find_kallsyms_names_uncompressed()
self.find_kallsyms_markers_uncompressed()
self.uncompressed_kallsyms = True
except KallsymsNotFoundException:
raise first_error
else:
self.find_kallsyms_markers()
self.find_kallsyms_names()
self.find_kallsyms_num_syms()
self.find_kallsyms_addresses_or_symbols()
# -
self.parse_symbol_table()
def find_linux_kernel_version(self):
regex_match = search(
rb'Linux version (\d+\.[\d.]*\d)[ -~]+', self.kernel_img
)
if not regex_match:
raise ValueError('No version string found in this kernel')
self.version_string = regex_match.group(0).decode('ascii')
self.version_number = regex_match.group(1).decode('ascii')
logging.info('[+] Version string: {0:s}'.format(self.version_string))
logging.info(
'[+] Other related strings containing the version number: %s'
% findall(
b'[ -~]*%s[ -~]*' % regex_match.group(1), self.kernel_img
)
)
arch_string = search(b'mod_unload[ -~]+', self.kernel_img)
if (
arch_string
and len(arch_string.group(0).decode('utf-8').strip().split()) > 2
):
logging.info(
'[+] Architecture string: %s'
% arch_string.group(0).decode('utf-8')
)
def guess_architecture(self):
try:
result: ArchitectureDetectionResult = ArchitectureDetector.guess(
self.kernel_img
)
except ArchitectureGuessError:
if self.is_64_bits is None:
raise
else:
self.architecture: ArchitectureName = result.architecture_name
self.elf_machine = int(result.elf_machine)
if self.is_64_bits is None:
self.is_64_bits = result.is_64_bit
self.is_big_endian = result.is_big_endian
def extract_db_information(self, extra_info=True):
kernel_major = int(self.version_number.split('.')[0])
kernel_minor = int(self.version_number.split('.')[1])
version_partial = 'v%d.%d' % (kernel_major, kernel_minor)
if self.version_number.count('.') > 1:
kernel_sublevel = int(self.version_number.split('.')[2])
version_full = '%s.%d' % (version_partial, kernel_sublevel)
else:
kernel_sublevel = None
version_full = version_partial
kernel = next(
iter(
KernelVersion.select().where(
KernelVersion.version_string == version_full
)
),
None,
) or next(
iter(
KernelVersion.select().where(
KernelVersion.version_string == version_partial
)
),
None,
)
if kernel:
logging.info('[+] Kernel found in database')
logging.info('[+] Read kernel source: ' + kernel.browse_url)
logging.info('[+] Download kernel: ' + kernel.download_url)
logging.info(
'[+] Kernel release date: '
+ kernel.release_date.strftime('%Y-%m-%d')
)
if self.elf_machine:
# if not extra_info:
# logging.info(
# '[+] Use --extra-info or -e to print more interesting details'
# )
if extra_info:
logging.info('[+] Interesting files:')
for file in kernel.relevant_files.select().where(
KernelRelevantFile.architecture_code == None
):
if file.vcs_browser_url:
logging.info(
'[~] - %s: %s'
% (file.file_name, file.vcs_browser_url)
)
else:
file.file_name
logging.info('[~] - ' + file.file_name)
e_machine = next(
iter(
EMachineValue.select().where(
EMachineValue.elf_machine_int == self.elf_machine
)
),
None,
)
if e_machine:
machine_name = e_machine.elf_machine_str
for link in e_machine.arch_code_links:
arch: KnownArchitecture = link.architecture
arch_code: str = arch.architecture_code
capacities = []
if arch.has_32bit_class:
capacities.append('32-bit')
if arch.has_64bit_class:
capacities.append('64-bit')
if arch.has_msb_class:
capacities.append('big-endian')
if arch.has_lsb_class:
capacities.append('little-endian')
logging.info(
'[+] Architecture %s (%s) supports %s'
% (arch_code, machine_name, ', '.join(capacities))
)
if extra_info:
for file in kernel.relevant_files.select().where(
KernelRelevantFile.architecture_code
== arch_code
):
if file.vcs_browser_url:
logging.info(
'[~] - %s: %s'
% (
file.file_name,
file.vcs_browser_url,
)
)
else:
file.file_name
logging.info('[~] - ' + file.file_name)
if extra_info:
debian_version = next(
iter(
DebianRelease.select()
.where(
DebianRelease.debian_release_date
<= kernel.release_date
)
.order_by(DebianRelease.debian_release_date.desc())
),
None,
)
if debian_version:
logging.info(
'[+] Suggested build environment: docker run -it %s (Debian %s "%s" released %s)'
% (
debian_version.docker_archive_name,
debian_version.debian_version_number,
debian_version.debian_version_name,
debian_version.debian_release_date.strftime(
'%Y-%m-%d'
),
)
)
def find_elf64_rela(self, base_address: int = None) -> bool:
"""
Find relocations table, return True if success, False
otherwise
"""
# FIX: architecture is not set when guess_architecture() wasn't called
if (
not hasattr(self, 'architecture')
or ArchitectureName.aarch64 != self.architecture
):
# I've tested this only for ARM64
return False
rela64_size = 24
self.elf64_rela_start = len(self.kernel_img)
self.elf64_rela_start -= (
self.elf64_rela_start & 3
) # align to pointer size
R_AARCH64_RELATIVE = 0x403
elf64_rela = []
minimal_heuristic_count = 1000
minimal_kernel_va = 0xFFFFC00080000000
maximal_kernel_va = 0xFFFFFFFFFFFFFFFF
addend_candidate = None
# Relocations table located at 'init' part of kernel image
# Thus reverse-search is more efficient
while self.elf64_rela_start >= rela64_size:
rela = unpack_from(
'<QQQ', self.kernel_img, self.elf64_rela_start - rela64_size
)
r_offset, r_info, r_addend = rela
if (0 == r_offset) and (0 == r_info) and (0 == r_addend):
# possible empty entry ?
if elf64_rela:
# just skip empty entries inside relocation table
self.elf64_rela_start -= (
rela64_size # move to one rela64 struct backward
)
continue
if R_AARCH64_RELATIVE != r_info:
# Relocations must be the same type
# BUG: this is not true in practice, R_AARCH64_GLOB_DAT and maybe some other are between first few R_AARCH64_RELATIVE, which results in missing ~30 relocations
if len(elf64_rela) >= minimal_heuristic_count:
break
# reset current state
elf64_rela = []
kernel_text_candidate = maximal_kernel_va
# move to the next candidate
possible_offset = self.elf64_rela_start - 1
while (
possible_offset % 8 != 0
): # Find a pointer-aligned r_info entry
possible_offset = self.kernel_img.rfind(
R_AARCH64_RELATIVE.to_bytes(8, 'little'),
8,
possible_offset - rela64_size + 1,
)
if possible_offset == -1:
self.elf64_rela_start = 0
break
if possible_offset != -1:
self.elf64_rela_start = possible_offset - 8
continue
elf64_rela.append(rela)
if (0 == (r_addend & 0xFFF)) and (minimal_kernel_va <= r_addend):
if addend_candidate is None or r_addend < addend_candidate:
addend_candidate = r_addend
self.elf64_rela_start -= (
rela64_size # move to one rela64 struct backward
)
count = len(elf64_rela)
if count < minimal_heuristic_count:
return False
self.elf64_rela = elf64_rela
self.elf64_rela_end_excl = self.elf64_rela_start + count * rela64_size
logging.info(
'[+] Found relocations table at file offset 0x%04x (count=%d)'
% (self.elf64_rela_start, count)
)
# Infer a sane base range from relocation offsets so that every
# relocation offset maps somewhere inside the image.
min_offset = min(r[0] for r in elf64_rela)
max_offset = max(r[0] for r in elf64_rela)
img_len = len(self.kernel_img)
base_low = max_offset - (img_len - 8)
base_high = min_offset
def fits(base: int) -> bool:
return base is not None and base_low <= base <= base_high
if base_address is not None:
self.kernel_text_candidate = base_address
logging.info(
'[+] Using supplied base address as kernel text candidate: 0x%08x'
% (self.kernel_text_candidate)
)
elif addend_candidate is not None and fits(addend_candidate):
self.kernel_text_candidate = addend_candidate
logging.info(
'[+] Found kernel text candidate from relocation addends: 0x%08x'
% (self.kernel_text_candidate)
)
elif base_low <= base_high:
# HACK: kernel might not be aligned to 0x10000?
ALIGN = 0x10000
candidate = (base_low + ALIGN - 1) & ~(ALIGN - 1)
if candidate > base_high:
candidate = base_high & ~(ALIGN - 1)
self.kernel_text_candidate = candidate
logging.info(
'[+] Guessed kernel base from relocation offsets range 0x%08x-0x%08x -> 0x%08x'
% (base_low, base_high, self.kernel_text_candidate)
)
else:
self.kernel_text_candidate = (
addend_candidate
if addend_candidate is not None
else base_address
)
logging.info(
'[!] Could not derive a consistent base from relocations, keeping candidate 0x%08x'
% (self.kernel_text_candidate)
)
return True
def apply_elf64_rela(self) -> bool:
"""
Apply relocations table, return True if success, False
otherwise
TODO: Use the __relocate_kernel symbol and parse instructions
instead of making this wild guess?
TODO: Move to a dedicated file?
"""
if self.elf64_rela is None or self.kernel_text_candidate is None:
return False
img = bytearray(self.kernel_img)
offset_max = len(img) - 8 # size of ptr
kernel_base = self.kernel_text_candidate
# There is no guarantee that relocation addresses are monotonous
count = 0
for rela in self.elf64_rela:
r_offset, r_info, r_addend = rela
offset = r_offset - kernel_base
if offset < 0 or offset >= offset_max:
logging.warning('WARNING! bad rela offset %08x' % (r_offset))
self.kernel_text_candidate = None
self.elf64_rela = None
return False # Don't try more to apply relocations
(value,) = unpack_from('<Q', self.kernel_img, offset)
if value == r_addend:
# don't know why, but some relocations already initialized
continue
# BUG: Sometimes 'r_addend' has pretty small value, and applied to 0.
# BUG: Result much smaller that valid kernel address.
# BUG: Probably 'r_addend' can represent offset from kernel_base. Need further investigation.
value += r_addend
value &= (1 << 64) - 1
img[offset : offset + 8] = pack('<Q', value)
count += 1
self.kernel_img = bytes(img)
logging.info('[+] Successfully applied %d relocations.' % count)
return True
def find_kallsyms_token_table(self):
"""
kallsyms_token_table is an array of 256 variable length null-
terminated string fragments. Positions which correspond to
an ASCII character which is used in at least one symbol
contain the corresponing character (1), other position contain
a string fragment chosen by the compression algorithm (2).
Hence, characters [0-9] and [a-z] are always present at their
respective positions, but ":" (which comes after "9") never does.
(1) See "insert_real_symbols_in_table" of "scripts/kallsyms.c"
(2) See "optimize_result" of "scripts/kallsyms.c"
"""
position = 0
candidates_offsets = [] # offsets at which sequence_to_find was found
candidates_offsets_followed_with_ascii = [] # variant with an higher certainty
sequence_to_find = b''.join(
b'%c\0' % i for i in range(ord('0'), ord('9') + 1)
)
sequences_to_avoid = [b':\0', b'\0\0', b'\0\1', b'\0\2', b'ASCII\0']
while True:
position = self.kernel_img.find(sequence_to_find, position + 1)
if position == -1:
break
for seq in sequences_to_avoid:
pos = position + len(sequence_to_find)
if self.kernel_img[pos : pos + len(seq)] == seq:
break
else:
candidates_offsets.append(position)
if self.kernel_img[pos : pos + 1].isalnum():
candidates_offsets_followed_with_ascii.append(position)
if len(candidates_offsets) != 1:
if len(candidates_offsets_followed_with_ascii) == 1:
candidates_offsets = candidates_offsets_followed_with_ascii
elif len(candidates_offsets) == 0:
raise KallsymsNotFoundException(
'%d candidates for kallsyms_token_table in kernel image'
% len(candidates_offsets)
)
else:
raise ValueError(
'%d candidates for kallsyms_token_table in kernel image'
% len(candidates_offsets)
)
position = candidates_offsets[0]
# Get back to the beginning of the table
current_index_in_array = ord('0')
position -= 1
assert position >= 0 and self.kernel_img[position] == 0
for tokens_backwards in range(current_index_in_array):
for chars_in_token_backwards in range(50):
position -= 1
assert position >= 0
# (caveat: we may overlap on "kallsyms_markers" for the
# last entry, so also check for high-range characters)
if self.kernel_img[position] == 0 or self.kernel_img[
position
] > ord('z'):
break
if chars_in_token_backwards >= 50 - 1:
raise ValueError(
'This structure is not a kallsyms_token_table'
)
position += 1
position += -position % 4
self.kallsyms_token_table__offset = position
logging.info(
'[+] Found kallsyms_token_table at file offset 0x%08x'
% self.kallsyms_token_table__offset
)
def find_kallsyms_token_index(self):
# Get to the end of the kallsyms_token_table
position = self.kallsyms_token_table__offset
all_token_offsets = []
position -= 1
for tokens_forward in range(256):
position += 1
all_token_offsets.append(
position - self.kallsyms_token_table__offset
)
for chars_in_token_forward in range(50):
position += 1
if self.kernel_img[position] == 0:
break
if chars_in_token_forward >= 50 - 1:
raise ValueError(
'This structure is not a kallsyms_token_table'
)
# Find kallsyms_token_index through the offset through searching
# the reconstructed structure, also use this to guess endianness
MAX_ALIGNMENT = 256
KALLSYMS_TOKEN_INDEX__SIZE = 256 * 2
memory_to_search = bytes(
self.kernel_img[
position : position
+ KALLSYMS_TOKEN_INDEX__SIZE
+ MAX_ALIGNMENT
]
)
little_endian_offsets = pack(
'<%dH' % len(all_token_offsets), *all_token_offsets
)
big_endian_offsets = pack(
'>%dH' % len(all_token_offsets), *all_token_offsets
)
found_position_for_le_value = memory_to_search.find(
little_endian_offsets
)
found_position_for_be_value = memory_to_search.find(big_endian_offsets)
if found_position_for_le_value == found_position_for_be_value == -1:
raise ValueError('The value of kallsyms_token_index was not found')
elif found_position_for_le_value > found_position_for_be_value:
self.is_big_endian = False
self.kallsyms_token_index__offset = (
position + found_position_for_le_value
)
elif found_position_for_be_value > found_position_for_le_value:
self.is_big_endian = True
self.kallsyms_token_index__offset = (
position + found_position_for_be_value
)
self.kallsyms_token_index_end__offset = (
self.kallsyms_token_index__offset + len(little_endian_offsets)
)
logging.info(
'[+] Found kallsyms_token_index at file offset 0x%08x'
% self.kallsyms_token_index__offset
)
def find_kallsyms_names_uncompressed(self):
"""
OpenWRT versions since 2013 may have an
uncompressed kallsyms table built-in.
"""
# Find the length byte-separated symbol names
ksymtab_match = search(
rb'(?:[\x05-\x23][TWtbBrRAdD][a-z0-9_.]{4,34}){14}',
self.kernel_img,
)
if not ksymtab_match:
raise KallsymsNotFoundException(
'No embedded symbol table found in this kernel'
)
self.kallsyms_names__offset = ksymtab_match.start(0)
# Count the number of symbol names
position = self.kallsyms_names__offset
self.number_of_symbols = 0
self.symbol_names: list[str] = []
while position + 1 < len(self.kernel_img):
if (
self.kernel_img[position] < 2
or chr(self.kernel_img[position + 1]).lower()
not in 'abdrtvwginpcsu-?'
):
break
symbol_name_and_type: bytes = self.kernel_img[
position + 1 : position + 1 + self.kernel_img[position]
]
if not match(rb'^[\x21-\x7e]+$', symbol_name_and_type):
break
position += 1 + self.kernel_img[position]
self.number_of_symbols += 1
if self.number_of_symbols < 100:
raise KallsymsNotFoundException(
'No embedded symbol table found in this kernel'
)
logging.info(
'[+] Kernel symbol names found at file offset 0x%08x'
% ksymtab_match.start(0)
)
logging.info(
'[+] Found %d uncompressed kernel symbols (end at 0x%08x)'
% (self.number_of_symbols, position)
)
self.end_of_kallsyms_names_uncompressed = position
def find_kallsyms_markers_uncompressed(self):
"""
This is the OpenWRT-specific version of the
"find_kallsyms_markers" method below. It works
the same except that is tries to guess the integer
size forward rather than backard.
"""
position = self.end_of_kallsyms_names_uncompressed
position += -position % 4
max_number_of_space_between_two_nulls = 0
# Go just after the first chunk of non-null bytes
while (
position + 1 < len(self.kernel_img)
and self.kernel_img[position + 1] == 0
):
position += 1
for null_separated_bytes_chunks in range(20):
num_non_null_bytes = (
1 # we always start at a non-null byte in this loop
)
num_null_bytes = 1 # we will at least encounter one null byte before the end of this loop
while True:
position += 1
assert position >= 0
if self.kernel_img[position] == 0:
break
num_non_null_bytes += 1
while True:
position += 1
assert position >= 0
if self.kernel_img[position] != 0:
break
num_null_bytes += 1
max_number_of_space_between_two_nulls = max(
max_number_of_space_between_two_nulls,
num_non_null_bytes + num_null_bytes,
)
if (
max_number_of_space_between_two_nulls % 2 == 1
): # There may be a leap to a shorter offset in the latest processed entries
max_number_of_space_between_two_nulls -= 1
if max_number_of_space_between_two_nulls not in (2, 4, 8):
raise ValueError(
'Could not guess the architecture register '
+ 'size for kernel'
)
self.offset_table_element_size = max_number_of_space_between_two_nulls
# Once the size of a long has been guessed, use it to find
# the first offset (0)
position = self.end_of_kallsyms_names_uncompressed
position += -position % 4
# Go just at the first non-null byte
while (
position < len(self.kernel_img) and self.kernel_img[position] == 0
):
position += 1
likely_is_big_endian = position % self.offset_table_element_size > 1
if self.is_big_endian is None: # Manual architecture specification
self.is_big_endian = likely_is_big_endian
if position % self.offset_table_element_size == 0:
position += self.offset_table_element_size
else:
position += -position + self.offset_table_element_size
position -= self.offset_table_element_size
position -= self.offset_table_element_size
position -= position % self.offset_table_element_size
self.kallsyms_markers__offset = position
logging.info(
'[+] Found kallsyms_markers at file offset 0x%08x' % position
)
def find_kallsyms_markers(self):
"""
kallsyms_markers contains one offset in kallsyms_names for each
1 in 256 entries of it. Offsets are stored as either ".long"
(a Gnu AS type that corresponds for example to 4 bytes in
x86_64) since kernel v4.20, either as the maximum register
byte of the system (the C "long" type) on older kernels.
Remember about the size of this field for later.
The first index is always 0, it is sorted, and it is aligned.
"""
# Try possible sizes for the table element (long type)
for table_element_size in (8, 4, 2):
position = self.kallsyms_token_table__offset
endianness_marker = '>' if self.is_big_endian else '<'
long_size_marker = {2: 'H', 4: 'I', 8: 'Q'}[table_element_size]
# Search for start of kallsyms_markers given first element is 0 and it is sorted
for _ in range(32):
position = self.kernel_img.rfind(
b'\x00' * table_element_size, 0, position
)
position -= position % table_element_size
entries = unpack_from(
endianness_marker + '4' + long_size_marker,
self.kernel_img,
position,
)
if entries[0] != 0:
continue
for i in range(1, len(entries)):
# kallsyms_names entries are at least 2 bytes and at most 0x3FFF bytes long
if (
entries[i - 1] + 0x200 >= entries[i]
or entries[i - 1] + 0x40000 < entries[i]
):
break
else:
logging.info(
'[+] Found kallsyms_markers at file offset 0x%08x'
% position
)
self.kallsyms_markers__offset = position
self.offset_table_element_size = table_element_size
return
raise ValueError('Could not find kallsyms_markers')
def find_kallsyms_names(self):
position = self.kallsyms_markers__offset
# Approximate the position of kallsyms_names based on the
# last entry of "kallsyms_markers" - we'll determine the
# precise position in the next method
endianness_marker = '>' if self.is_big_endian else '<'
long_size_marker = {2: 'H', 4: 'I', 8: 'Q'}[
self.offset_table_element_size
]
# Estimate kallsyms_markers length. Limit to 3000 for kernels with kallsyms_seqs_of_names
num_of_kallsyms_markers_entries = (
self.kallsyms_token_table__offset - self.kallsyms_markers__offset
) // self.offset_table_element_size
kallsyms_markers_entries = unpack_from(
endianness_marker
+ str(min(3000, num_of_kallsyms_markers_entries))
+ long_size_marker,