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 | class MMUShell(MMUShellDefault):
def __init__(self, completekey="tab", stdin=None, stdout=None, machine={}):
super(MMUShell, self).__init__(completekey, stdin, stdout, machine)
if not self.data:
self.data = Data(
is_mem_parsed=False,
is_radix_found=False,
page_tables={
"global": [
{} for i in range(self.machine.mmu.radix_levels["global"])
]
},
data_pages=[],
empty_tables=[],
reverse_map_tables=[
defaultdict(set)
for i in range(self.machine.mmu.radix_levels["global"])
],
reverse_map_pages=[
defaultdict(set)
for i in range(self.machine.mmu.radix_levels["global"])
],
satps={},
)
def do_parse_memory(self, args):
"""Parse memory to find tables"""
if self.data.is_mem_parsed:
logger.warning("Memory already parsed")
return
self.parse_memory()
self.data.is_mem_parsed = True
def do_show_table(self, args):
"""Show MMU table. Usage: show_table ADDRESS [level]"""
args = args.split()
if len(args) < 1:
logger.warning("Missing table address")
return
try:
addr = self.parse_int(args[0])
except ValueError:
logger.warning("Invalid table address")
return
if addr not in self.machine.memory:
logger.warning("Table not in RAM range")
return
lvl = -1
if len(args) > 1:
try:
lvl = self.parse_int(args[1])
if lvl > self.machine.mmu.radix_levels["global"] - 1:
raise ValueError
except ValueError:
logger.warning(
"Level must be an integer between 0 and {}".format(
str(self.machine.mmu.radix_levels["global"] - 1)
)
)
return
if lvl == -1:
table_size = self.machine.mmu.PAGE_SIZE
else:
table_size = self.machine.mmu.map_level_to_table_size["global"][lvl]
table_buff = self.machine.memory.get_data(addr, self.machine.mmu.PAGE_SIZE)
invalids, pt_classes, table_obj = self.machine.mmu.parse_frame(
table_buff, addr, table_size, lvl
)
print(table_obj)
print(f"Invalid entries: {invalids} Table levels: {pt_classes}")
def parse_memory(self):
logger.info("Look for paging tables...")
parallel_results = self.machine.apply_parallel(
self.machine.mmu.PAGE_SIZE, self.machine.mmu.parse_parallel_frame
)
logger.info("Reaggregate threads data...")
for result in parallel_results:
page_tables, data_pages, empty_tables = result.get()
for level in range(self.machine.mmu.radix_levels["global"]):
self.data.page_tables["global"][level].update(page_tables[level])
self.data.data_pages.extend(data_pages)
self.data.empty_tables.extend(empty_tables)
self.data.data_pages = set(self.data.data_pages)
self.data.empty_tables = set(self.data.empty_tables)
logger.info("Reduce false positives...")
# Remove all tables which point to inexistent table of lower level
for lvl in range(self.machine.mmu.radix_levels["global"] - 1):
ptr_class = self.machine.mmu.map_ptr_entries_to_levels["global"][lvl]
referenced_nxt = []
for table_addr in list(self.data.page_tables["global"][lvl].keys()):
for entry_obj in (
self.data.page_tables["global"][lvl][table_addr]
.entries[ptr_class]
.values()
):
if (
entry_obj.address
not in self.data.page_tables["global"][lvl + 1]
and entry_obj.address not in self.data.empty_tables
):
# Remove the table
self.data.page_tables["global"][lvl].pop(table_addr)
break
else:
referenced_nxt.append(entry_obj.address)
# Remove table not referenced by upper levels
referenced_nxt = set(referenced_nxt)
for table_addr in set(
self.data.page_tables["global"][lvl + 1].keys()
).difference(referenced_nxt):
self.data.page_tables["global"][lvl + 1].pop(table_addr)
logger.info("Fill reverse maps...")
for lvl in range(0, self.machine.mmu.radix_levels["global"]):
ptr_class = self.machine.mmu.map_ptr_entries_to_levels["global"][lvl]
page_class = self.machine.mmu.map_datapages_entries_to_levels["global"][
lvl
][
0
] # Trick: it has only one dataclass per level
for table_addr, table_obj in self.data.page_tables["global"][lvl].items():
for entry_obj in table_obj.entries[ptr_class].values():
self.data.reverse_map_tables[lvl][entry_obj.address].add(
table_obj.address
)
for entry_obj in table_obj.entries[page_class].values():
self.data.reverse_map_pages[lvl][entry_obj.address].add(
table_obj.address
)
def do_find_radix_trees(self, args):
"""Reconstruct radix trees"""
if not self.data.is_mem_parsed:
logging.info("Please, parse the memory first!")
return
# Some table level was not found...
if not len(self.data.page_tables["global"][0]):
logger.warning("OOPS... no tables in first level... Wrong MMU mode?")
return
# Go back from PTLn up to top level, the particular form of PTLn permits to find PTL0
logging.info("Go up the paging trees starting from data pages...")
candidates = []
already_explored = set()
for page_addr in tqdm(self.data.data_pages):
derived_addresses = self.machine.mmu.derive_page_address(page_addr)
for derived_address in derived_addresses:
if derived_address in already_explored:
continue
lvl, addr = derived_address
candidates.extend(
self.radix_roots_from_data_page(
lvl,
addr,
self.data.reverse_map_pages,
self.data.reverse_map_tables,
)
)
already_explored.add(derived_address)
candidates = list(
set(candidates).intersection(self.data.page_tables["global"][0].keys())
)
candidates.sort()
logger.info("Filter candidates...")
satps = {}
for candidate in tqdm(candidates):
# Obtain radix tree infos
consistency, pas = self.physpace(
candidate, self.data.page_tables["global"], self.data.empty_tables
)
# Only consistent trees are valid
if not consistency:
continue
# Esclude empty trees
if pas.get_kernel_size() == pas.get_user_size() == 0:
continue
vas = self.virtspace(candidate, 0, self.machine.mmu.top_prefix)
satps[candidate] = RadixTree(candidate, 0, pas, vas)
self.data.satps = satps
self.data.is_radix_found = True
def do_show_radix_trees(self, args):
"""Show found radix trees"""
if not self.data.is_radix_found:
logging.info("Please, find them first!")
return
labels = [
"Radix address",
"First level",
"Kernel size (Bytes)",
"User size (Bytes)",
]
table = PrettyTable()
table.field_names = labels
for satp in self.data.satps.values():
table.add_row(satp.entry_resume_stringified())
table.sortby = "Radix address"
print(table)
|