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

Simplify the linking process for runtime, moving more to patching time. #2

Open
bahorn opened this issue Sep 16, 2024 · 2 comments
Open

Comments

@bahorn
Copy link
Owner

bahorn commented Sep 16, 2024

src/runtime uses a very adhoc aproach for building the payload:

  • Scripts in src/scripts which process kalllsyms and the kernel binary to find symbols and space.
  • my custom badlink format for pointing to entrypoints, and various symbols that need to be filled in at patch time.
  • stage2 + kshelf_loader is embedded in stage1-uefi-bootservices-hook, which means we have to search and find the bios patch inside that. (origin for src/runtime/fill_in.py)

I think I need to replace badlink with something better, and just compile each stage to ELFs or something and do another step to merge them together in a way that they can be modified at patching time.

The end goal is to be able to merge everything into a single python script / executable.

What we are using scripts for right now:

In src/runtime/stage1-uefi-bootservices-hook/Makefile:

		-D _initcall_offset=`python3 ../../scripts/find_initcall.py $(SYMBOLS)` \
		-D _startup_64=`python3 ../../scripts/find_symbol.py $(SYMBOLS) startup_64` \
		-DLOAD_OFFSET=$(LOAD_OFFSET) \

These are only used in the direct patching approach (avoiding the runtime hook). Unsure if I want to remove direct patching as I like the option in newer kernels to apply it.

In src/runtime/stage2/Makefile:

		-D __efi_call=`python3 ../../scripts/find_symbol.py $(SYMBOLS) __efi_call` \
		-D _stage1_offset=$(LOAD_OFFSET) \
		-D _startup_64_offset=`python3 ../../scripts/find_symbol.py $(SYMBOLS) startup_64` \
		-D runtime_hook_len=`wc -c < ../kshelf-loader/runtime_hook.bin` \
		-D _initcall_offset=`python3 ../../scripts/find_initcall.py $(SYMBOLS)` \
		-D __preempt_count=`python3 ../../scripts/find_preempt_count.py $(SYMBOLS)` \

In src/runtime/kshelf-loader/Makefile:

		-D KALLSYMS_OFFSET=`python3 ../../scripts/find_symbol.py $(SYMBOLS) kallsyms_lookup_name` \

Used to work out the relative offset from .text, which stage2 passes to it via an argument.

In src/skp.sh:

# compile the payload
PAYLOAD=$REAL_PAYLOAD \
    SYMBOLS=$INTERMEDIATE/kallsyms \
    UEFI_DIRECT_PATCH=true \
    LOAD_OFFSET=`python3 ./src/scripts/find_space.py $INTERMEDIATE/curr.elf` \
    make -C ./src/runtime

To use a offset to place the payload.

What is badlink being used for

Badlink kinda sucks and only has 6 character symbol names...

  • uefi_e, our new uefi entrypoint
  • bios_e, our new bios entrypoint
  • uefi_o, our original uefi entrypoint
  • code32, offset to our code32 hook
  • o_dest, offset to the destination where the code32 hook sho
  • o_bios, offset to our bios_entry?
  • o_tocp, offset to copy?
  • o_ptch, offset to patch?
@bahorn
Copy link
Owner Author

bahorn commented Sep 18, 2024

I merged some partial work where i got stage2 and stage1-uefi-bootservices-hook merged together as two object files that get linked together, and removed the one symbol we needed to pass to the kshelf-loader during build.

The kshelf-loader can't generally be linked in as another .o, but not really a big deal.

Main problem I have now is I want to define all the symbols stage2 and stage1-uefi-bootservices-hook and just search+replace them into the binaries, but that doesn't seem simple to do. Parsing relocations and manually resolving them? Really want to generate a R_X86_64_COPY or something.

You can't just define them in a seperate .o and link against that, as we copy stage2 into various locations.

@bahorn
Copy link
Owner Author

bahorn commented Sep 19, 2024

This is also a blocker on making the testing script parallel, as a unique runtime needs to be built for each kernel image.

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

No branches or pull requests

1 participant