Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement kernel ARM emulation #1531

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from

Conversation

McCaulay
Copy link

Checklist

Which kind of PR do you create?

  • This PR only contains minor fixes.
  • This PR contains major feature update.
  • This PR introduces a new function/api for Qiling Framework.

Coding convention?

  • The new code conforms to Qiling Framework naming convention.
  • The imports are arranged properly.
  • Essential comments are added.
  • The reference of the new code is pointed out.

Extra tests?

  • No extra tests are needed for this PR.
  • I have added enough tests for this PR.
  • Tests will be added after some discussion and review.

Changelog?

  • This PR doesn't need to update Changelog.
  • Changelog will be updated after some proper review.
  • Changelog has been updated in my PR.

Target branch?

  • The target branch is dev branch.

One last thing


These fixes add support for emulating a ARM kernel object by implementing R_ARM_CALL and R_ARM_JUMP24. Additionally, the __get_symbol function now has caching in order to speed up loading ARM kernel objects as elftools get_symbol_by_name function is slow when iterating each time.

@xwings
Copy link
Member

xwings commented Feb 16, 2025

Hi,

there is a conflict in elf.py. You might want to take a look ?

@@ -57,7 +58,7 @@ class AUXV(IntEnum):

# start area memory for API hooking
# we will reserve 0x1000 bytes for this (which contains multiple slots of 4/8 bytes, each for one api)
API_HOOK_MEM = 0x1000000
API_HOOK_MEM = 0x2000000
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason for the change?

@@ -87,6 +88,9 @@ def run(self):
stack_size = self.profile.getint('stack_size')
self.ql.mem.map(stack_address, stack_size, info='[stack]')

# Setup heap
self.ql.os.heap = QlMemoryHeap(self.ql, 0x3000000, 0x3000000 + 0x1000000)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Heap base and size should be set based on the value configured in the profile file. Using hardcoded values is not a good idea.

@@ -97,7 +101,7 @@ def run(self):

# is it a driver?
if elftype == 'ET_REL':
self.load_driver(elffile, stack_address + stack_size, loadbase=0x8000000)
self.load_driver(elffile, stack_address + stack_size, loadbase=0x1000000)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason for this change here?
Keep in mind this module serves all ELF files on all architectures, not only ARM.


# Cache
if self._symbol_name_map == None:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be simplified with an internal function decorated with @cache (available from Python 3.9) or @lru_cache (available earlier).

from functools import cache

@cache
def __get_cached_symbol(name: str) -> int:
    # access symtab symbol here and return the result
    ...

Also, I am not sure that _symbol_name_map should be assigned to self, but stay local.

@@ -25,6 +25,11 @@ def hook_kernel_api(ql: Qiling, address: int, size):
if api_func:
try:
api_func(ql, address, api_name)

# Restore PC
if ql.arch.type == QL_ARCH.ARM:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think function calls are automatically unwinded.
Have you checked this is?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants