Skip to content

Commit

Permalink
Split kasld.h header into separate header files for each architecture
Browse files Browse the repository at this point in the history
  • Loading branch information
bcoles committed Apr 11, 2024
1 parent bc35f87 commit 5ae25b8
Show file tree
Hide file tree
Showing 47 changed files with 636 additions and 547 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
a.out
*.gch
build
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pre-build :
@echo "Building $(OBJ_DIR) ..."
mkdir -p "$(OBJ_DIR)"
# check for pre-compile errors before building
$(CC) $(ALL_CFLAGS) $(ALL_LDFLAGS) $(SRC_DIR)/kasld.h
$(CC) $(ALL_CFLAGS) $(ALL_LDFLAGS) $(SRC_DIR)/include/kasld.h


.PHONY: build
Expand Down
2 changes: 1 addition & 1 deletion src/bcm_msg_head_struct.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// ---
// <[email protected]>

#include "kasld.h"
#include "include/kasld.h"
#include <fcntl.h>
#include <linux/can.h>
#include <linux/can/bcm.h>
Expand Down
2 changes: 1 addition & 1 deletion src/boot-config.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
// ---
// <[email protected]>

#include "kasld.h"
#include "include/kasld.h"
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
Expand Down
2 changes: 1 addition & 1 deletion src/default.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "kasld.h"
#include "include/kasld.h"

unsigned long get_kernel_addr_default() {
return (unsigned long)KERNEL_TEXT_DEFAULT;
Expand Down
2 changes: 1 addition & 1 deletion src/dmesg_android_ion_snapshot.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
// <[email protected]>

#define _GNU_SOURCE
#include "kasld.h"
#include "include/kasld.h"
#include "include/syslog.h"
#include <stdint.h>
#include <stdio.h>
Expand Down
2 changes: 1 addition & 1 deletion src/dmesg_backtrace.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// <[email protected]>

#define _GNU_SOURCE
#include "kasld.h"
#include "include/kasld.h"
#include "include/syslog.h"
#include <stdint.h>
#include <stdio.h>
Expand Down
2 changes: 1 addition & 1 deletion src/dmesg_check_for_initrd.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
// <[email protected]>

#define _GNU_SOURCE
#include "kasld.h"
#include "include/kasld.h"
#include "include/syslog.h"
#include <stdint.h>
#include <stdio.h>
Expand Down
2 changes: 1 addition & 1 deletion src/dmesg_driver_component_ops.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
// <[email protected]>

#define _GNU_SOURCE
#include "kasld.h"
#include "include/kasld.h"
#include "include/syslog.h"
#include <stdint.h>
#include <stdio.h>
Expand Down
2 changes: 1 addition & 1 deletion src/dmesg_early_init_dt_add_memory_arch.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
// <[email protected]>

#define _GNU_SOURCE
#include "kasld.h"
#include "include/kasld.h"
#include "include/syslog.h"
#include <stdint.h>
#include <stdio.h>
Expand Down
2 changes: 1 addition & 1 deletion src/dmesg_ex_handler_msr.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
// <[email protected]>

#define _GNU_SOURCE
#include "kasld.h"
#include "include/kasld.h"
#include "include/syslog.h"
#include <stdint.h>
#include <stdio.h>
Expand Down
2 changes: 1 addition & 1 deletion src/dmesg_fake_numa_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
// <[email protected]>

#define _GNU_SOURCE
#include "kasld.h"
#include "include/kasld.h"
#include "include/syslog.h"
#include <stdint.h>
#include <stdio.h>
Expand Down
2 changes: 1 addition & 1 deletion src/dmesg_free_area_init_node.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
// <[email protected]>

#define _GNU_SOURCE
#include "kasld.h"
#include "include/kasld.h"
#include "include/syslog.h"
#include <stdint.h>
#include <stdio.h>
Expand Down
2 changes: 1 addition & 1 deletion src/dmesg_free_reserved_area.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
// <[email protected]>

#define _GNU_SOURCE
#include "kasld.h"
#include "include/kasld.h"
#include "include/syslog.h"
#include <stdint.h>
#include <stdio.h>
Expand Down
5 changes: 4 additions & 1 deletion src/dmesg_kaslr-disabled.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
//
// Search kernel log for messages stating KASLR is disabled.
//
// x86/x86_64:
// KASLR disabled: 'kaslr' not on cmdline (hibernation selected).
//
// ARM64:
// KASLR disabled due to lack of seed
// KASLR disabled due to FDT remapping failure
Expand All @@ -28,7 +31,7 @@

#define _GNU_SOURCE
#include "include/syslog.h"
#include "kasld.h"
#include "include/kasld.h"
#include <errno.h>
#include <stdbool.h>
#include <string.h>
Expand Down
2 changes: 1 addition & 1 deletion src/dmesg_mem_init_kernel_layout.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
// <[email protected]>

#define _GNU_SOURCE
#include "kasld.h"
#include "include/kasld.h"
#include "include/syslog.h"
#include <stdint.h>
#include <stdio.h>
Expand Down
2 changes: 1 addition & 1 deletion src/dmesg_mmu_idmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
// <[email protected]>

#define _GNU_SOURCE
#include "kasld.h"
#include "include/kasld.h"
#include "include/syslog.h"
#include <stdint.h>
#include <stdio.h>
Expand Down
2 changes: 1 addition & 1 deletion src/dmesg_riscv_relocation.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@

#define _GNU_SOURCE
#include "include/syslog.h"
#include "kasld.h"
#include "include/kasld.h"
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
Expand Down
2 changes: 1 addition & 1 deletion src/entrybleed.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
#endif

#define _GNU_SOURCE
#include "kasld.h"
#include "include/kasld.h"
#include <errno.h>
#include <stdbool.h>
#include <stdint.h>
Expand Down
95 changes: 95 additions & 0 deletions src/include/kasld.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// This file is part of KASLD - https://github.com/bcoles/kasld
//
// Expected kernel address space values for supported architectures.
//
// - KERNEL_VAS_START: Expected virtual address for start of the kernel
// virtual address space (VAS).
// (eg. 0xc0000000 for 32-bit systems with 3GB vmsplit)
//
// - KERNEL_VAS_END: Expected end of kernel virtual address space.
// (including modules, I/O, guard regions, ...)
//
// - KERNEL_BASE_MIN: Expected minimum possible kernel base virtual address.
//
// - KERNEL_BASE_MAX: Expected maximum possible kernel base virtual address.
//
// - MODULES_START: Expected start virtual address for kernel modules.
//
// - MODULES_END: Expected end virtual address for kernel modules.
//
// - KERNEL_ALIGN: Expected kernel address alignment.
// (usually 2MiB on modern systems)
//
// - KERNEL_TEXT_DEFAULT: Default kernel base virtual address when KASLR is
// disabled (including text offset). This value is
// calculated automatically based on above values.
//
// The default values should work on most systems, but may need
// to be tweaked for the target system - especially old kernels,
// embedded devices (ie, armv7), or systems with a non-default
// memory layout.
// ---
// <[email protected]>

#define MB 0x100000ul
#define GB 0x40000000ul

#if defined(__x86_64__) || defined(__amd64__)
#include "kasld/x86_64.h"
#elif defined(__i386__)
#include "kasld/x86_32.h"
#elif defined(__aarch64__)
#include "kasld/arm64.h"
#elif defined(__arm__) || defined(__ARM_ARCH_6__) || \
defined(__ARM_ARCH_6J__) || defined(__ARM_ARCH_6K__) || \
defined(__ARM_ARCH_6Z__) || defined(__ARM_ARCH_6ZK__) || \
defined(__ARM_ARCH_6T2__) || defined(__ARM_ARCH_7__) || \
defined(__ARM_ARCH_7A__) || defined(__ARM_ARCH_7R__) || \
defined(__ARM_ARCH_7M__) || defined(__ARM_ARCH_7S__)
#include "kasld/arm32.h"
#elif defined(__mips64) || defined(__mips64__)
#include "kasld/mips64.h"
#elif defined(__mips__)
#include "kasld/mips32.h"
#elif defined(__powerpc64__) || defined(__POWERPC64__) || \
defined(__ppc64__) || defined(__PPC64__)
#include "kasld/ppc64.h"
#elif defined(__powerpc__) || defined(__POWERPC__) || defined(__ppc__) || \
defined(__PPC__)
#include "kasld/ppc32.h"
#elif (defined(__riscv) || defined(__riscv__)) && __riscv_xlen == 64
#include "kasld/riscv64.h"
#elif (defined(__riscv) || defined(__riscv__)) && __riscv_xlen == 32
#include "kasld/riscv32.h"
#elif defined(__s390__) || defined(__s390x__) || defined(__370__) || \
defined(__zarch__)
#include "kasld/s390.h"
#elif defined(__sparc__)
#include "kasld/sparc.h"
#else
#error "Unrecognised architecture!"
#endif

/* -----------------------------------------------------------------------------
* Sanity check configured values
* -----------------------------------------------------------------------------
*/
#if KERNEL_VAS_START > KERNEL_VAS_END
#error "Defined KERNEL_VAS_START is larger than KERNEL_VAS_END"
#endif

#if KERNEL_VAS_START > KERNEL_BASE_MIN
#error "Defined KERNEL_VAS_START is larger than KERNEL_BASE_MIN"
#endif

#if KERNEL_BASE_MAX > KERNEL_VAS_END
#error "Defined KERNEL_BASE_MAX is larger than KERNEL_VAS_END"
#endif

#if KERNEL_TEXT_DEFAULT > KERNEL_BASE_MAX
#error "Generated KERNEL_TEXT_DEFAULT is larger than KERNEL_BASE_MAX"
#endif

#if KERNEL_TEXT_DEFAULT < KERNEL_BASE_MIN
#error "Generated KERNEL_TEXT_DEFAULT is smaller than KERNEL_BASE_MIN"
#endif
46 changes: 46 additions & 0 deletions src/include/kasld/arm32.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// This file is part of KASLD - https://github.com/bcoles/kasld
//
// Definitions for ARM 32-bit (arm6l / arm7l / armhf)
//
// KASLR support added in commit 588ab3f9afdfa1a6b1e5761c858b2c4ab6098285 in
// kernel v4.6-rc1~110 on 2016-03-17.
//
// References:
// https://github.com/torvalds/linux/commit/588ab3f9afdfa1a6b1e5761c858b2c4ab6098285
// https://people.kernel.org/linusw/how-the-arm32-linux-kernel-decompresses
// https://people.kernel.org/linusw/how-the-arm32-kernel-starts
// https://www.kernel.org/doc/Documentation/arm/Porting
// https://elixir.bootlin.com/linux/v6.1.1/source/arch/arm/kernel/head.S
// https://elixir.bootlin.com/linux/v6.1.1/source/arch/arm/include/asm/memory.h#L30
// ---
// <[email protected]>

// 3GB vmsplit (0xc0000000) is common; but an unsafe assumption,
// especially for embedded systems
// https://elixir.bootlin.com/linux/v6.1.1/source/arch/arm/Kconfig#L1116
#define PAGE_OFFSET 0xc0000000ul

// https://elixir.bootlin.com/linux/v6.1.1/source/arch/arm/Kconfig#L276
#define PHYS_OFFSET 0ul

// https://elixir.bootlin.com/linux/v6.1.1/source/arch/arm/include/asm/memory.h#L286
#define phys_to_virt(x) ((unsigned long)((x) - PHYS_OFFSET + PAGE_OFFSET))

#define KERNEL_VAS_START PAGE_OFFSET
#define KERNEL_VAS_END 0xfffffffful

// https://elixir.bootlin.com/linux/v6.1.1/source/arch/arm/include/asm/memory.h#L26
#define KERNEL_BASE_MIN PAGE_OFFSET
#define KERNEL_BASE_MAX 0xf0000000ul

// Modules are located below kernel: PAGE_OFFSET - 16MiB (0x01000000)
// https://elixir.bootlin.com/linux/v6.1.1/source/arch/arm/include/asm/memory.h#L51
#define MODULES_START PAGE_OFFSET - 0x01000000 // 0xbf000000ul
#define MODULES_END PAGE_OFFSET

#define KERNEL_ALIGN 2 * MB

// https://elixir.bootlin.com/linux/v6.1.1/source/arch/arm/Makefile#L145
#define TEXT_OFFSET 0x8000

#define KERNEL_TEXT_DEFAULT (KERNEL_BASE_MIN + TEXT_OFFSET)
60 changes: 60 additions & 0 deletions src/include/kasld/arm64.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// This file is part of KASLD - https://github.com/bcoles/kasld
//
// Definitions for ARM 64-bit (aarch64 / arm64)
//
// KASLR support added in commit 588ab3f9afdfa1a6b1e5761c858b2c4ab6098285 in
// kernel v4.6-rc1~110 on 2016-03-17.
//
// References:
// https://github.com/torvalds/linux/commit/588ab3f9afdfa1a6b1e5761c858b2c4ab6098285
// https://lwn.net/Articles/673598/
// https://www.kernel.org/doc/Documentation/arm64/memory.txt
// https://github.com/torvalds/linux/blob/master/Documentation/arm64/booting.rst
// https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/arm64/memory.rst
// https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/arch/arm64/kernel/head.S
// ---
// <[email protected]>

// 52 va bits (CONFIG_ARM64_VA_BITS_48_52) is largest.
// 48 va bits (CONFIG_ARM64_VA_BITS_48) is more common.
// page_offset = (0xffffffffffffffffUL) << (va_bits - 1)
// https://elixir.bootlin.com/linux/v6.1.1/source/arch/arm64/include/asm/memory.h#L45
// We assume 52 va bits:
#define PAGE_OFFSET 0xfff8000000000000ul
#define PHYS_OFFSET 0ul

// https://elixir.bootlin.com/linux/v6.1.1/source/arch/arm64/include/asm/memory.h#L295
#define phys_to_virt(x) ((unsigned long)((x) - PHYS_OFFSET) | PAGE_OFFSET)

#define KERNEL_VAS_START PAGE_OFFSET
#define KERNEL_VAS_END 0xfffffffffffffffful

// 48 va bits (CONFIG_ARM64_VA_BITS_48) is a common configuration;
// but an unsafe assumption since introduction of CONFIG_ARM64_VA_BITS_48_52.
// older kernels may use 0xffff000008000000ul
#define KERNEL_BASE_MIN 0xffff800008000000ul
#define KERNEL_BASE_MAX 0xffffffffff000000ul

#define MODULES_START 0xffff800000000000ul
#define MODULES_END 0xffff800007fffffful

// MIN_KIMG_ALIGN is 2MiB (used without KASLR).
// https://elixir.bootlin.com/linux/v6.2-rc2/source/arch/arm64/include/asm/boot.h#L18
// EFI_KIMG_ALIGN is the larger of THREAD_ALIGN or SEGMENT_ALIGN:
// https://elixir.bootlin.com/linux/v6.2-rc2/source/arch/arm64/include/asm/efi.h#L102
// https://elixir.bootlin.com/linux/v6.2-rc2/source/arch/arm64/include/asm/efi.h#L72
// SEGMENT_ALIGN is hard-coded as 64KiB:
// https://elixir.bootlin.com/linux/v6.2-rc2/source/arch/arm64/include/asm/memory.h#L131
// The largest possible THREAD_ALIGN is also 64KiB.
// THREAD_ALIGN = THREAD_SIZE = (1 << THREAD_SHIFT)
// default CONFIG_ARM64_PAGE_SHIFT is 12. largest is 16.
// https://elixir.bootlin.com/linux/v6.2-rc2/source/arch/arm64/Kconfig#L262
// Use 64KiB (0x10000) by default
#define KERNEL_ALIGN 0x10000ul

// TEXT_OFFSET was changed from 0x80000 to zero in 2020 from kernel v5.8 onwards
// https://elixir.bootlin.com/linux/v5.8/source/arch/arm64/Makefile
// https://lore.kernel.org/all/20200428134119.GI6791@willie-the-truck/T/
#define TEXT_OFFSET 0

#define KERNEL_TEXT_DEFAULT (KERNEL_BASE_MIN + TEXT_OFFSET)
Loading

0 comments on commit 5ae25b8

Please sign in to comment.