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

plat/kvm_x86: ASLR for Unikraft #1317

Open
wants to merge 5 commits into
base: staging
Choose a base branch
from
Open

Conversation

SerbanSo
Copy link

@SerbanSo SerbanSo commented Feb 8, 2024

Prerequisite checklist

  • Read the contribution guidelines regarding submitting new changes to the project;
  • Tested your changes against relevant architectures and platforms;
  • Ran the checkpatch.uk on your commit series before opening this PR;
  • Updated relevant documentation.

Base target

  • Architecture(s): x86_64
  • Platform(s): kvm
  • Application: code base and python3/3.10

Additional configuration

  • CONFIG_LINK_ASLR=y
  • CONFIG_RUNTIME_ASLR=y
    (the last requires CONFIG_LIBUKSWRAND)
  • CONFIG_RANDOMIZE_BASE_ADDRESS

Description of changes

Add python scripts for library randomization at build-time
Add randomized base address placement for stack and heap
Continued the work from this PR: #492

The ASLR.py and Analyzer.py scripts are used for the build-time randomization.
Analyzer.py parses the 'link64.lds' file to make a symbol table that is then randomized in ASLR.py, resulting in the 'link64_ASLR.lds' used in the linking process.
The build-time ASLR includes:
- PHDRS order randomization
- .text library randomization and padding between each adjacent libraries
- .text base address randomization
- segment order randomization
- section inside segments order randomization (except ”bss”, ”uk inittab”, ”uk ctortab”, and ”intrstack”)

stackmemory_aslr_palloc function is used to loop through the free memory regions and select at random a region where the stack will be put. Usually, the stack will be placed somewhere inside the region, resulting in unused space before and after the stack. This spaces will create two new memory regions that will be readded in the memory pool.
The heap is allocated later in the heap_init function. There are separate cases depending on the CONFIG_LIBUKBOOT_HEAP_BASE flag. If the flag is enabled, the heap address is generated between the last virtual address used and 2 ** 46. If the flag is disabled, the heap address is generated somewhere inside the free memory regions, similar to how the stack base address is generated.

@github-actions github-actions bot added area/include Part of include/uk area/lib Internal Unikraft Microlibrary area/plat Unikraft Patform area/support Support scripts, tools, services. lang/c Issues or PRs to do with C/C++ lang/python Issues or PRs to do with Python lib/ukboot plat/common Common to all platforms plat/kvm Unikraft for KVM labels Feb 8, 2024
@razvand razvand added this to the v0.17.0 (Calypso) milestone Feb 8, 2024
@github-actions github-actions bot added area/kconfig Part of the Unikraft KConfig option system area/makefile Part of the Unikraft Makefile build system lib/ukswrand labels Apr 22, 2024
@github-actions github-actions bot added arch/arm arch/arm64 area/arch Unikraft Architecture area/docs Documentation area/workflows Issue or PR relates to GitHub action workflows lib/devfs devfs file system lib/isrlib lib/nolibc Only neccessary subset of libc functionality lib/posix-process Information about system parameters lib/posix-sysinfo labels Jun 16, 2024
@SerbanSo SerbanSo requested a review from a team as a code owner June 17, 2024 11:56
'uk_early_initcall()' gets called too late to be used for the
stach and heap randomization.
'ASLR_early_uk_random_init()' is created, which is explicitly called
before the stack initialization (only when run-time ASLR is enabled)

Signed-off-by: Serban Sorohan <serban.sorohan@gmail.com>
Introduced 'stackmemory_aslr_palloc' function which loops through
all available memory regions and randomly selectes where the stack
is put.
Usually, the stack will be placed in the middle of a free
regions. The spaces before and after the stack will be readded
in the free memory pool.

Signed-off-by: Serban Sorohan <serban.sorohan@gmail.com>
When LIBUKBOOT_HEAP_BASE and RUNTIME_ASLR are set, a random
virtual address for the heap is created, which overrides the
value from LIBUKBOOT_HEAPBASE. The addrerss is generated between
the last virtual base from the last memory region and 2 ** 46.
The upper limit is set to 2 ** 46 due to the canonical addresses
used in x86-64 architecture.

When LIBUKBOOT_HEAP_BASE is disabled, the logic is similar with
the stack randomization. A random base address is generated inside
any of the remaining memory regions. The rest of them are added
to the allocator.

Signed-off-by: Serban Sorohan <serban.sorohan@gmail.com>
We have three new scripts used for the compile-time randomization.
'base_address.py' is used to randomize the base address of the
'.text' section.
'Analyzer.py' is used to parse the linker file 'link64.lds' and
extract the sections which will be randomized
'ASLR.py' is used to randomize the sections extracted by 'Analyzer.py',
randomize the order of PHDRS, randomize the order of libraries, and
add padding between any two adjacent libs and write the new
'link64_ASLR.lds' file.

Signed-off-by: Serban Sorohan <serban.sorohan@gmail.com>
@SerbanSo SerbanSo changed the title plat/kvm_x86 : ASLR for Unikraft plat/kvm_x86: ASLR for Unikraft Jun 17, 2024
@SerbanSo SerbanSo removed area/lib Internal Unikraft Microlibrary plat/xen Unikraft for Xen area/kconfig Part of the Unikraft KConfig option system lib/vfscore VFS Core Interface lib/devfs devfs file system lib/nolibc Only neccessary subset of libc functionality lib/posix-process Information about system parameters lib/posix-sysinfo lib/uknetdev lib/uksched lib/uksglist lib/ukswrand arch/arm arch/arm64 lib/ukargparse ukargparse: Simple argument parser lib/ukring lib/isrlib area/workflows Issue or PR relates to GitHub action workflows labels Jun 17, 2024
Added the "Memory security" menu for plat/kvm, which has
two options: "LINK_ASLR" for compile-time ASLR, and "RUNTIME_ASLR"
for run-time ASLR
Added the "RANDOMIZE_BASE_ADDRESS" flag. The generated address is
exported in the env variable "BASE_ADDRESS" and can be used later
in the build process
Changed the Linker from plat/kvm in order to accomodate the use of
the new 'link64_ASLR.lds' file

Signed-off-by: Serban Sorohan <serban.sorohan@gmail.com>
@github-actions github-actions bot added area/kconfig Part of the Unikraft KConfig option system area/lib Internal Unikraft Microlibrary labels Jun 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/arch Unikraft Architecture area/docs Documentation area/include Part of include/uk area/kconfig Part of the Unikraft KConfig option system area/lib Internal Unikraft Microlibrary area/makefile Part of the Unikraft Makefile build system area/plat Unikraft Patform area/support Support scripts, tools, services. lang/c Issues or PRs to do with C/C++ lang/python Issues or PRs to do with Python lib/ukboot lib/ukrandom plat/common Common to all platforms plat/kvm Unikraft for KVM
Projects
Status: No status
Status: 🧊 Icebox
Development

Successfully merging this pull request may close these issues.

None yet

3 participants