├── .clang-format ├── .gitignore ├── LICENSE ├── README.md ├── banner ├── build.sh ├── doc ├── .gitignore ├── en │ ├── build.md │ ├── guide.md │ ├── inline-hook.md │ ├── module.md │ ├── super-command.md │ ├── super-syscall.md │ └── syscall-hook.md └── zh-CN │ └── module.md ├── doxyfile ├── kernel ├── .gitignore ├── Makefile ├── base │ ├── baselib.c │ ├── cache.S │ ├── fphook.c │ ├── hmem.c │ ├── hmem.h │ ├── hook.c │ ├── hotpatch.c │ ├── log.c │ ├── map.c │ ├── map1.S │ ├── predata.c │ ├── setup.c │ ├── setup.h │ ├── setup1.S │ ├── sha256.c │ ├── start.c │ ├── start.h │ ├── symbol.c │ └── tlsf.c ├── include │ ├── barrier.h │ ├── baselib.h │ ├── cache.h │ ├── common.h │ ├── compiler.h │ ├── ctype.h │ ├── hook.h │ ├── hotpatch.h │ ├── io.h │ ├── kallsyms.h │ ├── kpmalloc.h │ ├── kpmodule.h │ ├── ktypes.h │ ├── log.h │ ├── pgtable.h │ ├── predata.h │ ├── preset.h │ ├── sha256.h │ ├── stdarg.h │ ├── stdbool.h │ ├── stddef.h │ ├── stdint.h │ ├── symbol.h │ └── tlsf.h ├── kernel-source │ ├── android_vendor.h │ ├── errseq.h │ ├── mm_types2.h │ ├── prio_tree.h │ ├── rbtree.h │ └── time64.h ├── kpimg.lds ├── linux │ ├── arch │ │ └── arm64 │ │ │ └── include │ │ │ ├── asm │ │ │ ├── atomic.h │ │ │ ├── cacheflush.h │ │ │ ├── cmpxchg.h │ │ │ ├── current.h │ │ │ ├── elf.h │ │ │ ├── hwcap.h │ │ │ ├── io.h │ │ │ ├── processor.h │ │ │ ├── ptrace.h │ │ │ ├── thread_info.h │ │ │ ├── uaccess.h │ │ │ ├── unistd.h │ │ │ └── unistd32.h │ │ │ └── uapi │ │ │ └── asm │ │ │ ├── hwcap.h │ │ │ ├── ptrace.h │ │ │ └── sysinfo.h │ ├── include │ │ ├── asm-generic │ │ │ ├── bitops │ │ │ │ └── fls_ffs.h │ │ │ ├── bug.h │ │ │ ├── compat.h │ │ │ ├── module.h │ │ │ └── rwonce.h │ │ ├── asm │ │ │ ├── stat.h │ │ │ └── statfs.h │ │ ├── linux │ │ │ ├── bitops.h │ │ │ ├── bottom_half.h │ │ │ ├── build_bug.h │ │ │ ├── capability.h │ │ │ ├── compiler.h │ │ │ ├── container_of.h │ │ │ ├── cpumask.h │ │ │ ├── cred.h │ │ │ ├── dcache.h │ │ │ ├── elf-em.h │ │ │ ├── elf.h │ │ │ ├── err.h │ │ │ ├── errno.h │ │ │ ├── fs.h │ │ │ ├── gfp.h │ │ │ ├── include │ │ │ │ └── linux │ │ │ │ │ ├── export.h │ │ │ │ │ └── smp.h │ │ │ ├── init_task.h │ │ │ ├── kallsyms.h │ │ │ ├── kern_levels.h │ │ │ ├── kernel.h │ │ │ ├── list.h │ │ │ ├── llist.h │ │ │ ├── lockdep.h │ │ │ ├── mm.h │ │ │ ├── mm_types.h │ │ │ ├── panic.h │ │ │ ├── pid.h │ │ │ ├── poison.h │ │ │ ├── preempt.h │ │ │ ├── printk.h │ │ │ ├── ptrace.h │ │ │ ├── random.h │ │ │ ├── rculist.h │ │ │ ├── rcupdate.h │ │ │ ├── rwlock.h │ │ │ ├── sched.h │ │ │ ├── sched │ │ │ │ ├── mm.h │ │ │ │ └── task.h │ │ │ ├── seccomp.h │ │ │ ├── security.h │ │ │ ├── seq_buf.h │ │ │ ├── slab.h │ │ │ ├── socket.h │ │ │ ├── spinlock.h │ │ │ ├── stacktrace.h │ │ │ ├── stop_machine.h │ │ │ ├── string.h │ │ │ ├── syscall.h │ │ │ ├── thread_info.h │ │ │ ├── trace_seq.h │ │ │ ├── uaccess.h │ │ │ ├── umh.h │ │ │ └── vmalloc.h │ │ ├── net │ │ │ └── netlabel.h │ │ ├── uapi │ │ │ ├── asm-generic │ │ │ │ ├── errno-base.h │ │ │ │ ├── errno.h │ │ │ │ ├── fcntl.h │ │ │ │ └── unistd.h │ │ │ └── linux │ │ │ │ ├── capability.h │ │ │ │ ├── elf.h │ │ │ │ ├── fcntl.h │ │ │ │ ├── fs.h │ │ │ │ ├── limits.h │ │ │ │ ├── magic.h │ │ │ │ ├── prctl.h │ │ │ │ ├── seccomp.h │ │ │ │ └── stat.h │ │ └── vdso │ │ │ └── limits.h │ ├── security │ │ └── selinux │ │ │ └── include │ │ │ ├── avc.h │ │ │ ├── avc_ss.h │ │ │ ├── classmap.h │ │ │ └── security.h │ └── tools │ │ └── arch │ │ └── arm64 │ │ └── include │ │ └── asm │ │ └── barrier.h └── patch │ ├── android │ ├── gen │ │ └── user_init.c │ ├── sepolicy_flags.c │ └── userd.c │ ├── common │ ├── accctl.c │ ├── kstorage.c │ ├── pmem.c │ ├── secpass.c │ ├── syscall.c │ ├── sysname.c │ ├── taskob.c │ ├── test.c │ ├── user_event.c │ └── utils.c │ ├── include │ ├── accctl.h │ ├── hotpatch.h │ ├── kconfig.h │ ├── kputils.h │ ├── kstorage.h │ ├── ksyms.h │ ├── module.h │ ├── pidmem.h │ ├── sepolicy_flags.h │ ├── sucompat.h │ ├── syscall.h │ ├── taskext.h │ ├── taskob.h │ ├── uapi │ │ └── scdefs.h │ └── user_event.h │ ├── ksyms │ ├── execv.c │ ├── libs.c │ ├── misc.c │ └── task_cred.c │ ├── module │ ├── insn.c │ ├── insn.h │ ├── module.c │ ├── relo.c │ └── relo.h │ ├── patch.c │ └── sukisu │ └── sukisu.c ├── kpms ├── .gitignore ├── demo-hello │ ├── .gitignore │ ├── Makefile │ ├── hello.c │ └── hello.lds ├── demo-inlinehook │ ├── .gitignore │ ├── Makefile │ └── inlinehook.c └── demo-syscallhook │ ├── .gitignore │ ├── Makefile │ └── syscallhook.c ├── make.bat ├── patch ├── .gitignore ├── CMakeLists.txt ├── Makefile ├── build.sh ├── encrypt ├── encrypt.cpp ├── include │ ├── header.h │ ├── kpimg_enc.h │ ├── kptools_android.h │ └── kptools_linux.h ├── main.cpp └── res │ ├── kpimg │ ├── kpimg.enc │ ├── kptools-android │ ├── kptools-linux │ ├── kptools.exe │ └── start.sh ├── tools ├── .gitignore ├── CMakeLists.txt ├── Makefile ├── common.c ├── common.h ├── elf │ ├── elf-em.h │ └── elf.h ├── fls_ffs.h ├── image.c ├── image.h ├── insn.c ├── insn.h ├── kallsym.c ├── kallsym.h ├── kpm.c ├── kpm.h ├── kptools.c ├── order.c ├── order.h ├── patch.c ├── patch.h ├── preset.h ├── ptrace.h ├── sha256.c ├── sha256.h ├── symbol.c └── symbol.h └── version /.gitignore: -------------------------------------------------------------------------------- 1 | .vscode/* 2 | .history/ 3 | .cache/ 4 | 5 | **/.DS_Store 6 | 7 | test 8 | script -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SukiSU's KernelPatch patch 2 | 3 | ** This is only a simple fork from bmax121's KernelPatch. ** 4 | 5 | 这是一个fork自bmax121的KernelPatch,经过剪裁后用于给SukiSU Ultra提供KPM支持 6 | 7 | ## Thanks 8 | 9 | [APatch](https://github.com/bmax121/APatch) 10 | [KernelPatch](https://github.com/bmax121/KernelPatch) 11 | 12 | 13 | # KernelPatch 14 | 15 | **Patching and hooking the Linux kernel with only stripped Linux kernel image.** 16 | 17 | ``` shell 18 | _ __ _ ____ _ _ 19 | | |/ /___ _ __ _ __ ___| | _ \ __ _| |_ ___| |__ 20 | | ' // _ \ '__| '_ \ / _ \ | |_) / _` | __/ __| '_ \ 21 | | . \ __/ | | | | | __/ | __/ (_| | || (__| | | | 22 | |_|\_\___|_| |_| |_|\___|_|_| \__,_|\__\___|_| |_| 23 | 24 | ``` 25 | 26 | - Obtain all symbol information without source code and symbol information. 27 | - Inject arbitrary code into the kernel. (Static patching the kernel image or Runtime dynamic loading). 28 | - Kernel function inline hook and syscall table hook are provided. 29 | - Additional SU for Android. 30 | 31 | If you are using Android, [APatch](https://github.com/bmax121/APatch) would be a better choice. 32 | 33 | ## Requirement 34 | 35 | CONFIG_KALLSYMS=y 36 | 37 | ## Supported Versions 38 | 39 | Currently only supports arm64 architecture. 40 | 41 | Linux 3.18 - 6.6 (theoretically) 42 | 43 | ## Get Involved 44 | 45 | ## More Information 46 | 47 | [Documentation](./doc/) 48 | 49 | ## Credits 50 | 51 | - [vmlinux-to-elf](https://github.com/marin-m/vmlinux-to-elf): Some ideas for parsing kernel symbols. 52 | - [android-inline-hook](https://github.com/bytedance/android-inline-hook): Some code for fixing arm64 inline hook instructions. 53 | - [tlsf](https://github.com/mattconte/tlsf): Memory allocator used for KPM. (Need another to allocate ROX memory.) 54 | 55 | ## License 56 | 57 | KernelPatch is licensed under the **GNU General Public License (GPL) 2.0** (). 58 | -------------------------------------------------------------------------------- /banner: -------------------------------------------------------------------------------- 1 | #define KERNEL_PATCH_BANNER \ 2 | " _ __ _ ____ _ _ \n" \ 3 | "| |/ /___ _ __ _ __ ___| | _ \\ __ _| |_ ___| |__ \n" \ 4 | "| ' // _ \\ '__| '_ \\ / _ \\ | |_) / _` | __/ __| '_ \\ \n" \ 5 | "| . \\ __/ | | | | | __/ | __/ (_| | || (__| | | |\n" \ 6 | "|_|\\_\\___|_| |_| |_|\\___|_|_| \\__,_|\\__\\___|_| |_|\n" 7 | -------------------------------------------------------------------------------- /build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | set -e 3 | 4 | HOME=/root/other 5 | TOOLS=/root/other/tools 6 | PATCH=/root/other/patch 7 | KERNEL=/root/other/kernel 8 | # 构建 Android 版本 9 | cd $TOOLS 10 | rm -rf build/android 11 | mkdir -p build/android 12 | cd build/android 13 | # 请替换以下变量为实际 NDK 路径或通过环境变量传递 14 | cmake \ 15 | -DCMAKE_TOOLCHAIN_FILE="$NDK_HOME/build/cmake/android.toolchain.cmake" \ 16 | -DCMAKE_BUILD_TYPE=Release \ 17 | -DANDROID_PLATFORM=android-33 \ 18 | -DANDROID_ABI=arm64-v8a ../.. 19 | cmake --build . 20 | mv kptools kptools-android 21 | 22 | # 构建 Linux 版本 23 | cd $TOOLS 24 | cd build 25 | cmake .. 26 | make 27 | mv kptools kptools-linux 28 | 29 | cd $KERNEL 30 | 31 | make clean 32 | make 33 | 34 | cd $HOME 35 | 36 | export ANDROID_NDK=/root/other/ndk/android-ndk-r28 37 | 38 | cp -r $TOOLS/build/android/kptools-android $PATCH/res 39 | cp -r $TOOLS/build/kptools-linux $PATCH/res 40 | cp -r $KERNEL/kpimg $PATCH/res 41 | 42 | cd $PATCH 43 | 44 | xxd -i res/kpimg.enc > include/kpimg_enc.h 45 | xxd -i res/kptools-linux > include/kptools_linux.h 46 | xxd -i res/kptools-android > include/kptools_android.h 47 | 48 | # 创建构建目录 49 | rm -rf build-android 50 | mkdir -p build-android 51 | cd build-android 52 | 53 | # 生成编译配置 54 | cmake .. \ 55 | -DCMAKE_TOOLCHAIN_FILE=${ANDROID_NDK}/build/cmake/android.toolchain.cmake \ 56 | -DCMAKE_BUILD_TYPE=Release \ 57 | -DANDROID_ABI=arm64-v8a \ 58 | -DANDROID_PLATFORM=android-33 \ 59 | -DANDROID_STL=c++_static 60 | 61 | cmake --build . 62 | cp -r patch_android $HOME 63 | 64 | cd $PATCH 65 | rm -rf build-linux 66 | mkdir -p build-linux 67 | cd build-linux 68 | cmake .. && make 69 | mv patch patch_linux 70 | cp -r patch_linux $HOME -------------------------------------------------------------------------------- /doc/.gitignore: -------------------------------------------------------------------------------- 1 | api -------------------------------------------------------------------------------- /doc/en/build.md: -------------------------------------------------------------------------------- 1 | # How to Build 2 | 3 | ## Build kpimg 4 | 5 | Require a bare-metal cross compiler 6 | [Download here](https://developer.arm.com/downloads/-/arm-gnu-toolchain-downloads) 7 | 8 | ```shell 9 | export TARGET_COMPILE=aarch64-none-elf- 10 | cd kernel 11 | export ANDROID=1 # Android version, including support for the 'su' command 12 | make 13 | ``` 14 | 15 | ## Build kptools 16 | 17 | kptools can run anywhere, just compile it. 18 | 19 | - Using Makefile 20 | 21 | ```shell 22 | export ANDROID=1 23 | cd tools 24 | make 25 | ``` 26 | 27 | - Using CMake 28 | 29 | ```shell 30 | cd tools 31 | mkdir build 32 | cd build 33 | cmake .. 34 | make 35 | ``` 36 | 37 | ## Building kpatch 38 | 39 | kpatch runs in the user space of the target system, so you can build it as usual. 40 | If you are using it for Android, you can use AndroidKernelPatch. 41 | 42 | - Using Makefile 43 | 44 | ```shell 45 | cd user 46 | make 47 | ``` 48 | 49 | - Using CMake 50 | 51 | ```shell 52 | cd user 53 | mkdir build 54 | cd build 55 | cmake .. 56 | make 57 | ``` 58 | 59 | - Compile for Android 60 | 61 | ```shell 62 | export ANDROID_NDK=/path/to/ndk 63 | export ANDROID=1 64 | cd user 65 | mkdir -p build/android && cd build/android 66 | cmake -DCMAKE_TOOLCHAIN_FILE=$ANDROID_NDK/build/cmake/android.toolchain.cmake \ 67 | -DCMAKE_BUILD_TYPE=Release \ 68 | -DANDROID_PLATFORM=android-33 \ 69 | -DANDROID_ABI=arm64-v8a ../.. 70 | cmake --build . 71 | ``` 72 | 73 | ## Build KernelPatch Module 74 | 75 | example: 76 | 77 | ```shell 78 | export TARGET_COMPILE=aarch64-none-elf- 79 | cd kpm-demo/hello 80 | make 81 | ``` 82 | -------------------------------------------------------------------------------- /doc/en/guide.md: -------------------------------------------------------------------------------- 1 | # Guide 2 | 3 | ## How KernelPatch Works 4 | 5 | KernelPatch consists of three components: kptools, kpimg, and kpatch. 6 | 7 | ### [kptools](/tools/) 8 | 9 | kptools serves the following purposes: 10 | 11 | - It can parse kernel images without source code or symbol information and retrieve the offset addresses of arbitrary kernel symbols. 12 | - It patches the kernel image by appending kpimg to the end of the image and writing necessary information to the predetermined locations in kpimg. Finally, it replaces the kernel's startup location with the starting address of kpimg. 13 | 14 | ### [kpimg](/kernel/) 15 | 16 | - kpimg is a specially designed ELF. 17 | - kpimg takes over the kernel boot process, performs all kernel dynamic patching, and exports functionality for user use via system calls. 18 | - If you don't need extensive functionalities or want customization, you can separately utilize the code in [kernel/base](/kernel/base). 19 | 20 | - [SuperCall](./super-syscall.md) 21 | 22 | - [Kernel Inline Hook](./inline-hook.md) 23 | 24 | - [Kernel Patch Module](./module.md) 25 | 26 | ### [kpuser](/user/) 27 | 28 | kpuser is the user space header file and library for KernelPatch. You can directly embed kpuser into your program. 29 | -------------------------------------------------------------------------------- /doc/en/inline-hook.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SukiSU-Ultra/SukiSU_KernelPatch_patch/2d9c2a655700b85ddc0c50ce22a7e7bb03b88233/doc/en/inline-hook.md -------------------------------------------------------------------------------- /doc/en/module.md: -------------------------------------------------------------------------------- 1 | # KernelPatch Module 2 | -------------------------------------------------------------------------------- /doc/en/super-command.md: -------------------------------------------------------------------------------- 1 | # Super Command 2 | 3 | truncate SUPERKEY help 4 | -------------------------------------------------------------------------------- /doc/en/super-syscall.md: -------------------------------------------------------------------------------- 1 | # Super System Call 2 | -------------------------------------------------------------------------------- /doc/en/syscall-hook.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SukiSU-Ultra/SukiSU_KernelPatch_patch/2d9c2a655700b85ddc0c50ce22a7e7bb03b88233/doc/en/syscall-hook.md -------------------------------------------------------------------------------- /doc/zh-CN/module.md: -------------------------------------------------------------------------------- 1 | # KernelPatch Module 2 | 3 | ## 什么是 KernelPatch Module (KPM) 4 | 5 | **KPM is an ELF file that can be loaded and run within the kernel space by KernelPatch.** 6 | 7 | ## How to write a KPM 8 | 9 | Here are a few examples that you can use to quickly understand. 10 | 11 | 1. A simple hello world KPM: [hello-world](/kpm-demo/hello) 12 | 2. How to do kernel function inline-hook via KPM: [inline-hook](/kpm-demo/inlinehook) 13 | 3. How to hook system call via KPM: [syscallhook](/kpm-demo/syscallhook) 14 | 15 | ### Working without Kernel source tree 16 | 17 | ### Working with Kernel soruce tree 18 | -------------------------------------------------------------------------------- /doxyfile: -------------------------------------------------------------------------------- 1 | PROJECT_NAME = "KernelPatch Document" 2 | OUTPUT_DIRECTORY = ./doc/api 3 | 4 | INPUT = \ 5 | ./user/supercall.h \ 6 | ./kernel/include/hook.h \ 7 | ./kernel/patch/include/accctl.h \ 8 | ./kernel/patch/include/taskext.h \ 9 | ./kernel/patch/include/uapi/scdefs.h \ 10 | 11 | FILE_PATTERNS = *.h *.md 12 | 13 | RECURSIVE = YES 14 | GENERATE_LATEX = NO 15 | SOURCE_BROWSER = YES 16 | EXTRACT_ALL = YES 17 | EXTRACT_PRIVATE = YES 18 | EXTRACT_STATIC = YES 19 | -------------------------------------------------------------------------------- /kernel/.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Object files 5 | *.o 6 | *.ko 7 | *.obj 8 | *.elf 9 | 10 | # Libraries 11 | *.lib 12 | *.a 13 | *.la 14 | *.lo 15 | 16 | *.bin 17 | *.elf 18 | 19 | # kernel patch module 20 | *.kpm 21 | 22 | kpimg -------------------------------------------------------------------------------- /kernel/Makefile: -------------------------------------------------------------------------------- 1 | ifndef TARGET_COMPILE 2 | TARGET_COMPILE=/root/other/ndk/android-ndk-r28/toolchains/llvm/prebuilt/linux-x86_64/bin 3 | endif 4 | 5 | TARGET=kpimg 6 | 7 | CC = $(TARGET_COMPILE)/aarch64-linux-android35-clang 8 | LD = $(TARGET_COMPILE)/ld.lld 9 | AS = $(TARGET_COMPILE)/llvm-as 10 | OBJCOPY = $(TARGET_COMPILE)/llvm-objcopy 11 | 12 | CFLAGS += -Wall -fno-builtin -std=gnu11 -nostdinc 13 | CFLAGS += -g 14 | CFLAGS += -Wno-unused-function -Wno-return-type -Wno-c23-extensions -Wno-uninitialized 15 | CFLAGS += -fno-PIC -fno-asynchronous-unwind-tables -fno-stack-protector -fno-unwind-tables -fno-semantic-interposition -U_FORTIFY_SOURCE -fno-common 16 | 17 | ifdef DEBUG 18 | CFLAGS += -DDEBUG -DMAP_DEBUG -g 19 | endif 20 | 21 | # ifdef ANDROID 22 | CFLAGS += -DANDROID 23 | # endif 24 | 25 | INCLUDE := -I. -Iinclude -Ipatch/include -Ilinux -Ilinux/include -Ilinux/arch/arm64/include -Ilinux/tools/arch/arm64/include -Ikernel-source -I$(TARGET_COMPILE)/../sysroot/usr/include -I$(TARGET_COMPILE)/../sysroot/usr/include/aarch64-linux-android 26 | 27 | BASE_SRCS += base/setup.c 28 | BASE_SRCS += base/setup1.S 29 | BASE_SRCS += base/cache.S 30 | BASE_SRCS += base/tlsf.c 31 | BASE_SRCS += base/start.c 32 | BASE_SRCS += base/map.c 33 | BASE_SRCS += base/map1.S 34 | BASE_SRCS += base/hook.c 35 | BASE_SRCS += base/fphook.c 36 | BASE_SRCS += base/hmem.c 37 | BASE_SRCS += base/predata.c 38 | BASE_SRCS += base/symbol.c 39 | BASE_SRCS += base/baselib.c 40 | BASE_SRCS += base/sha256.c 41 | 42 | BASE_SRCS += $(wildcard patch/*.c) 43 | BASE_SRCS += $(wildcard patch/common/*.c) 44 | BASE_SRCS += $(wildcard patch/module/*.c) 45 | BASE_SRCS += $(wildcard patch/ksyms/*.c) 46 | 47 | BASE_SRCS += patch/android/sepolicy_flags.c 48 | 49 | BASE_SRCS += $(wildcard patch/sukisu/*.c) 50 | 51 | 52 | SRCS += $(BASE_SRCS) 53 | SRCS += $(LINUX_SRCS) 54 | 55 | OBJS := $(SRCS:.c=.o) 56 | OBJS := $(OBJS:.S=.o) 57 | 58 | 59 | LDFLAGS += -Tkpimg.lds --no-relax 60 | 61 | all: ${TARGET} 62 | 63 | ${TARGET}: ${TARGET}.elf 64 | ${OBJCOPY} -O binary -S $^ $@ 65 | 66 | ${TARGET}.elf: ${OBJS} 67 | ${LD} -nostdlib -static -no-pie -Tkpimg.lds -e start -o $@ $^ 68 | 69 | %.o: %.c 70 | ${CC} $(CFLAGS) $(INCLUDE) -c -O2 -o $@ $< 71 | 72 | %.o: %.S 73 | ${CC} $(CFLAGS) $(INCLUDE) -c -o $@ $< 74 | 75 | .PHONY: clean 76 | clean: 77 | rm -rf *.elf 78 | rm -rf kpimg 79 | find . -name *.o | xargs rm -f 80 | -------------------------------------------------------------------------------- /kernel/base/hmem.c: -------------------------------------------------------------------------------- 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 | /* 3 | * Copyright (C) 2023 bmax121. All Rights Reserved. 4 | */ 5 | 6 | #include "hook.h" 7 | 8 | #include 9 | 10 | static uint64_t mem_region_start = 0; 11 | static uint64_t mem_region_end = 0; 12 | 13 | typedef struct 14 | { 15 | int using; 16 | enum hook_type type; 17 | uintptr_t addr; 18 | // must align 8 19 | union 20 | { 21 | hook_t inl; 22 | hook_chain_t inl_chain; 23 | fp_hook_chain_t fp_chain; 24 | } chain __attribute__((aligned(8))); 25 | } hook_mem_warp_t __attribute__((aligned(16))); 26 | 27 | int hook_mem_add(uint64_t start, int32_t size) 28 | { 29 | for (uint64_t i = start; i < start + size; i += 8) { 30 | *(uint64_t *)i = 0; 31 | } 32 | mem_region_start = start; 33 | mem_region_end = start + size; 34 | return 0; 35 | } 36 | 37 | void *hook_mem_zalloc(uintptr_t origin_addr, enum hook_type type) 38 | { 39 | uint64_t start = mem_region_start; 40 | for (uint64_t addr = start; addr < mem_region_end; addr += sizeof(hook_mem_warp_t)) { 41 | hook_mem_warp_t *wrap = (hook_mem_warp_t *)addr; 42 | if (wrap->using) continue; 43 | 44 | wrap->using = 1; 45 | wrap->addr = origin_addr; 46 | wrap->type = type; 47 | 48 | for (uintptr_t i = (uintptr_t)&wrap->chain; i < (uintptr_t)&wrap->chain + sizeof(wrap->chain); i += 8) { 49 | *(uint64_t *)i = 0; 50 | } 51 | 52 | // todo: assert 53 | if (((uintptr_t)&wrap->chain) & 0b111) { 54 | return 0; 55 | } 56 | return &wrap->chain; 57 | } 58 | return 0; 59 | } 60 | 61 | void hook_mem_free(void *hook_mem) 62 | { 63 | hook_mem_warp_t *warp = local_container_of(hook_mem, hook_mem_warp_t, chain); 64 | warp->using = 0; 65 | } 66 | 67 | void *hook_get_mem_from_origin(uint64_t origin_addr) 68 | { 69 | uint64_t start = mem_region_start; 70 | 71 | for (uint64_t addr = start; addr < mem_region_end; addr += sizeof(hook_mem_warp_t)) { 72 | hook_mem_warp_t *wrap = (hook_mem_warp_t *)addr; 73 | if (wrap->using && wrap->addr == origin_addr) { 74 | return &wrap->chain; 75 | } 76 | } 77 | return 0; 78 | } 79 | -------------------------------------------------------------------------------- /kernel/base/hmem.h: -------------------------------------------------------------------------------- 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 | /* 3 | * Copyright (C) 2023 bmax121. All Rights Reserved. 4 | */ 5 | 6 | #ifndef _KP_HMEM_H_ 7 | #define _KP_HMEM_H_ 8 | 9 | #include 10 | 11 | int hook_mem_add(uint64_t start, int32_t size); 12 | void *hook_mem_zalloc(uintptr_t origin_addr, enum hook_type type); 13 | void hook_mem_free(void *hook_mem); 14 | void *hook_get_mem_from_origin(uint64_t origin_addr); 15 | 16 | #endif -------------------------------------------------------------------------------- /kernel/base/hotpatch.c: -------------------------------------------------------------------------------- 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 | /* 3 | * Copyright (C) 2023 bmax121. All Rights Reserved. 4 | */ 5 | 6 | #include 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | #define MAX_STACK_TRACE_DEPTH 64 16 | // static unsigned long stack_entries[MAX_STACK_TRACE_DEPTH]; 17 | // static struct stack_trace trace = { 0 }; 18 | 19 | // static int backtrace_address_verify(unsigned long address, bool replace) 20 | // { 21 | // return 0; 22 | // } 23 | 24 | /* 25 | * https://github.com/dynup/kpatch/blob/922cd458091915b0dad8c1892d7a609addd4afd7/kmod/core/core.c#L274C20-L274C20 26 | * Verify activeness safety, i.e. that none of the to-be-patched functions are on the stack of any task. 27 | */ 28 | int patch_verify_safety() 29 | { 30 | // if (task_struct_offset.tasks_offset < 0) { 31 | // return -1; 32 | // } 33 | int ret = 0; 34 | 35 | // trace.max_entries = sizeof(stack_entries) / sizeof(stack_entries[0]); 36 | // trace.entries = &stack_entries[0]; 37 | 38 | // struct task_struct *g, *t; 39 | 40 | // do_each_thread(p) 41 | // { 42 | // trace.nr_entries = 0; 43 | // save_stack_trace_tsk(t, &trace); 44 | 45 | // if (trace.nr_entries >= trace.max_entries) { 46 | // logke("more than %u trace entries!\n", trace.max_entries); 47 | // ret = -EBUSY; 48 | // goto out; 49 | // } 50 | 51 | // for (int i = 0; i < trace.nr_entries; i++) { 52 | // if (trace.entries[i] == ULONG_MAX) 53 | // break; 54 | // ret = backtrace_address_verify(trace.entries[i], 0); 55 | // if (ret) 56 | // goto out; 57 | // } 58 | // } 59 | 60 | // out: 61 | // if (ret) { 62 | // // pid_t pid = 63 | // logke("Comm: %.20s\n", get_task_comm(t)); 64 | // for (int i = 0; i < trace.nr_entries; i++) { 65 | // if (trace.entries[i] == ULONG_MAX) 66 | // break; 67 | // logke(" [<%pK>] %pB\n", (void *)trace.entries[i], (void *)trace.entries[i]); 68 | // } 69 | // } 70 | 71 | return ret; 72 | } 73 | 74 | // static int check_all_task(void *data) 75 | // { 76 | // hook_t *hook = (hook_t *)data; 77 | // logkd("check_all_task data: %llx\n", hook); 78 | // return 0; 79 | // } 80 | 81 | int hot_patch_text() 82 | { 83 | // int rc = stop_machine(check_all_task, &data, 0); 84 | // logkd("stop_machine rc: %d\n", rc); 85 | return 0; 86 | } 87 | 88 | int kp_insn_patch_text(void *addrs[], uint32_t insn[], int cnt) 89 | { 90 | } -------------------------------------------------------------------------------- /kernel/base/log.c: -------------------------------------------------------------------------------- 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 | /* 3 | * Copyright (C) 2023 bmax121. All Rights Reserved. 4 | */ 5 | 6 | #include 7 | 8 | #define BOOT_LOG_SIZE 1024 9 | 10 | static char boot_log[BOOT_LOG_SIZE] = { 0 }; 11 | static int boot_log_len = 0; 12 | static int boot_log_fin = 0; 13 | -------------------------------------------------------------------------------- /kernel/base/map1.S: -------------------------------------------------------------------------------- 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 | /* 3 | * Copyright (C) 2023 bmax121. All Rights Reserved. 4 | */ 5 | 6 | // todo: -------------------------------------------------------------------------------- /kernel/base/setup.c: -------------------------------------------------------------------------------- 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 | /* 3 | * Copyright (C) 2023 bmax121. All Rights Reserved. 4 | */ 5 | 6 | #include "setup.h" 7 | #include "../../version" 8 | 9 | setup_header_t header __section(.setup.header) = { .magic = KP_MAGIC, 10 | .kp_version.major = MAJOR, 11 | .kp_version.minor = MINOR, 12 | .kp_version.patch = PATCH, 13 | .config_flags = 0 14 | #ifdef ANDROID 15 | | CONFIG_ANDROID 16 | #endif 17 | #ifdef DEBUG 18 | | CONFIG_DEBUG 19 | #endif 20 | , 21 | .compile_time = __TIME__ " " __DATE__ }; 22 | 23 | setup_preset_t setup_preset __section(.setup.preset) = { 0 }; 24 | 25 | struct 26 | { 27 | uint8_t fp[STACK_SIZE]; 28 | uint8_t sp[0]; 29 | } stack __section(.setup.data) __aligned(16); -------------------------------------------------------------------------------- /kernel/base/setup.h: -------------------------------------------------------------------------------- 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 | /* 3 | * Copyright (C) 2023 bmax121. All Rights Reserved. 4 | */ 5 | 6 | #ifndef _KP_SETUP_H_ 7 | #define _KP_SETUP_H_ 8 | 9 | #include "./preset.h" 10 | 11 | #define STACK_SIZE 0x800 12 | 13 | #ifndef __ASSEMBLY__ 14 | 15 | #define offsetof(TYPE, MEMBER) ((size_t) & ((TYPE *)0)->MEMBER) 16 | 17 | #define __section(s) __attribute__((section(#s))) 18 | #define __noinline __attribute__((__noinline__)) 19 | #define __aligned(x) __attribute__((aligned(x))) 20 | #define __bti_c __attribute__((target("branch-protection=bti"))) 21 | 22 | #endif 23 | 24 | #ifndef __ASSEMBLY__ 25 | typedef struct 26 | { 27 | // preset 28 | uint32_t paging_init_backup; 29 | uint32_t __; 30 | int64_t map_offset; 31 | int64_t start_offset; 32 | int64_t start_size; 33 | int64_t start_img_size; 34 | int64_t extra_size; 35 | int64_t alloc_size; 36 | uint64_t kernel_pa; 37 | uint64_t paging_init_relo; 38 | map_symbol_t map_symbol; 39 | #ifdef MAP_DEBUG 40 | uint64_t printk_relo; 41 | uint64_t tmp0_offset; 42 | uint64_t tmp1_offset; 43 | char str_fmt_px[24]; 44 | #endif 45 | // local 46 | int64_t va1_bits; 47 | int64_t page_shift; 48 | uint64_t kimage_voffset; 49 | uint64_t linear_voffset; 50 | } map_data_t; 51 | #else 52 | #define map_paging_init_backup_offset 0 53 | #define map_map_offset_offset (map_paging_init_backup_offset + 8) 54 | #define map_start_offset_offset (map_map_offset_offset + 8) 55 | #define map_start_size_offset (map_start_offset_offset + 8) 56 | #define map_start_img_size_offset (map_start_size_offset + 8) 57 | #define map_extra_size_offset (map_start_img_size_offset + 8) 58 | #define map_alloc_size_offset (map_extra_size_offset + 8) 59 | #define map_kernel_pa_offset (map_alloc_size_offset + 8) 60 | #define map_paging_init_relo_offset (map_kernel_pa_offset + 8) 61 | #define map_map_symbol_offset (map_paging_init_relo_offset + 8) 62 | #ifdef MAP_DEBUG 63 | #define map_printk_relo_offset (map_map_symbol_offset + MAP_SYMBOL_SIZE) 64 | #define map_tmp0_offset (map_printk_relo_offset + 8) 65 | #define map_tmp1_offset (map_tmp0_offset + 8) 66 | #define map_str_fmt_px_offset (map_tmp1_offset + 8) 67 | #endif // MAP_DEBUG 68 | #endif 69 | 70 | #ifndef __ASSEMBLY__ 71 | typedef int (*start_f)(uint64_t kimage_voffset, uint64_t linear_voffset); 72 | extern void _start_kernel(); 73 | extern void _paging_init(); 74 | extern void _link_base(); 75 | extern void _link_end(); 76 | extern void _setup_start(); 77 | extern void _setup_end(); 78 | extern void _map_start(); 79 | extern void _map_text(); 80 | extern void _map_data(); 81 | extern void _map_end(); 82 | extern void _kp_end(); 83 | #endif // __ASSEMBLY__ 84 | 85 | #endif // _KP_SETUP_H_ -------------------------------------------------------------------------------- /kernel/base/start.h: -------------------------------------------------------------------------------- 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 | /* 3 | * Copyright (C) 2023 bmax121. All Rights Reserved. 4 | */ 5 | 6 | #ifndef _KP_START_H_ 7 | #define _KP_START_H_ 8 | 9 | #include 10 | 11 | #ifndef __ASSEMBLY__ 12 | typedef struct 13 | { 14 | setup_header_t header; 15 | version_t kernel_version; 16 | uint32_t _; 17 | int64_t kallsyms_lookup_name_offset; 18 | int64_t kernel_size; 19 | int64_t start_offset; 20 | int64_t extra_size; 21 | uint64_t kernel_pa; 22 | int64_t map_offset; 23 | int64_t map_backup_len; 24 | uint8_t map_backup[MAP_MAX_SIZE]; 25 | uint8_t superkey[SUPER_KEY_LEN]; 26 | uint8_t root_superkey[ROOT_SUPER_KEY_HASH_LEN]; 27 | patch_config_t patch_config; 28 | } start_preset_t; 29 | #else 30 | #define start_header_offset 0 31 | #define start_kernel_version_offset (start_header_offset + KP_HEADER_SIZE) 32 | #define start_kallsyms_lookup_name_offset_offset (start_kernel_version_offset + 8) 33 | #define start_kernel_size_offset (start_kallsyms_lookup_name_offset_offset + 8) 34 | #define start_start_offset_offset (start_kernel_size_offset + 8) 35 | #define start_extra_size_offset (start_start_offset_offset + 8) 36 | #define start_kernel_pa_offset (start_extra_size_offset + 8) 37 | #define start_map_offset_offset (start_kernel_pa_offset + 8) 38 | #define start_map_backup_len_offset (start_map_offset_offset + 8) 39 | #define start_map_backup_offset (start_map_backup_len_offset + 8) 40 | #define start_superkey_offset (start_map_backup_offset + MAP_MAX_SIZE) 41 | #define start_root_superkey_offset (start_superkey_offset + SUPER_KEY_LEN) 42 | #define start_patch_config_offset (start_root_superkey_offset + ROOT_SUPER_KEY_HASH_LEN) 43 | #define start_patch_extra_offset_offset (start_patch_config_offset + PATCH_CONFIG_LEN) 44 | #define start_patch_extra_size_offset (start_patch_extra_offset_offset + 8) 45 | #define start_end (start_patch_extra_size_offset + 8) 46 | #endif 47 | 48 | #endif // _KP_START_H_ -------------------------------------------------------------------------------- /kernel/base/symbol.c: -------------------------------------------------------------------------------- 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 | /* 3 | * Copyright (C) 2023 bmax121. All Rights Reserved. 4 | */ 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #include "start.h" 12 | #include "setup.h" 13 | 14 | extern void _kp_symbol_start(); 15 | extern void _kp_symbol_end(); 16 | static uint64_t symbol_start = 0; 17 | static uint64_t symbol_end = 0; 18 | 19 | // DJB2 20 | static unsigned long sym_hash(const char *str) 21 | { 22 | unsigned long hash = 5381; 23 | int c; 24 | while ((c = *str++)) { 25 | hash = ((hash << 5) + hash) + c; 26 | } 27 | return hash; 28 | } 29 | 30 | int local_strcmp(const char *s1, const char *s2) 31 | { 32 | const unsigned char *c1 = (const unsigned char *)s1; 33 | const unsigned char *c2 = (const unsigned char *)s2; 34 | unsigned char ch; 35 | int d = 0; 36 | while (1) { 37 | d = (int)(ch = *c1++) - (int)*c2++; 38 | if (d || !ch) break; 39 | } 40 | return d; 41 | } 42 | 43 | unsigned long (*sukisu_compact_find_symbol)(const char* name); 44 | 45 | unsigned long symbol_lookup_name(const char *name) 46 | { 47 | unsigned long hash = sym_hash(name); 48 | for (uint64_t addr = symbol_start; addr < symbol_end; addr += sizeof(kp_symbol_t)) { 49 | kp_symbol_t *symbol = (kp_symbol_t *)addr; 50 | if (hash == symbol->hash && !local_strcmp(name, symbol->name)) { 51 | return symbol->addr; 52 | } 53 | } 54 | 55 | if(sukisu_compact_find_symbol) { 56 | return sukisu_compact_find_symbol(name); 57 | } 58 | 59 | return 0; 60 | } 61 | 62 | void symbol_init() 63 | { 64 | symbol_start = (uint64_t)_kp_symbol_start; 65 | symbol_end = (uint64_t)_kp_symbol_end; 66 | log_boot("Symbol: %llx, %llx\n", symbol_start, symbol_end); 67 | for (uint64_t addr = symbol_start; addr < symbol_end; addr += sizeof(kp_symbol_t)) { 68 | kp_symbol_t *symbol = (kp_symbol_t *)addr; 69 | symbol->addr = symbol->addr - link_base_addr + runtime_base_addr; 70 | symbol->hash = sym_hash(symbol->name); 71 | } 72 | 73 | sukisu_compact_find_symbol = (typeof(sukisu_compact_find_symbol)) kallsyms_lookup_name("sukisu_compact_find_symbol"); 74 | } -------------------------------------------------------------------------------- /kernel/include/baselib.h: -------------------------------------------------------------------------------- 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 | /* 3 | * Copyright (C) 2023 bmax121. All Rights Reserved. 4 | */ 5 | 6 | #ifndef _KP_BASELIB_H_ 7 | #define _KP_BASELIB_H_ 8 | 9 | #include 10 | #include 11 | 12 | void *lib_memccpy(void *dst, const void *src, int c, size_t n); 13 | void *lib_memchr(const void *s, int c, size_t n); 14 | int lib_memcmp(const void *s1, const void *s2, size_t n); 15 | void *lib_memcpy(void *dst, const void *src, size_t n); 16 | void *lib_memmove(void *dst, const void *src, size_t n); 17 | void *lib_memrchr(const void *s, int c, size_t n); 18 | void *lib_memset(void *dst, int c, size_t n); 19 | void lib_memswap(void *m1, void *m2, size_t n); 20 | int lib_memcmp(const void *s1, const void *s2, size_t n); 21 | void *lib_memmem(const void *haystack, size_t n, const void *needle, size_t m); 22 | 23 | int lib_strcasecmp(const char *s1, const char *s2); 24 | char *lib_strchr(const char *s, int c); 25 | int lib_strcmp(const char *s1, const char *s2); 26 | char *lib_strcpy(char *dst, const char *src); 27 | size_t lib_strlcpy(char *dst, const char *src, size_t size); 28 | size_t lib_strlen(const char *s); 29 | int lib_strncasecmp(const char *s1, const char *s2, size_t n); 30 | char *lib_strncat(char *dst, const char *src, size_t n); 31 | char *lib_strcat(char *dst, const char *src); 32 | int lib_strncmp(const char *s1, const char *s2, size_t n); 33 | char *lib_strncpy(char *dst, const char *src, size_t n); 34 | size_t lib_strnlen(const char *s, size_t maxlen); 35 | char *lib_strpbrk(const char *s1, const char *s2); 36 | char *lib_strrchr(const char *s, int c); 37 | char *lib_strsep(char **stringp, const char *delim); 38 | size_t lib_strspn(const char *s1, const char *s2); 39 | 40 | #endif -------------------------------------------------------------------------------- /kernel/include/common.h: -------------------------------------------------------------------------------- 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 | /* 3 | * Copyright (C) 2023 bmax121. All Rights Reserved. 4 | */ 5 | 6 | #ifndef _KP_COMMON_H_ 7 | #define _KP_COMMON_H_ 8 | 9 | #include 10 | #include 11 | 12 | #define VERSION(major, minor, patch) (((major) << 16) + ((minor) << 8) + (patch)) 13 | 14 | typedef enum 15 | { 16 | little = 0, 17 | big = 1 18 | } endian_t; 19 | 20 | extern uint32_t kver; 21 | extern uint32_t kpver; 22 | extern endian_t endian; 23 | 24 | extern const char kernel_patch_logo[]; 25 | 26 | extern void _kp_start(); 27 | extern void _kp_text_start(); 28 | extern void _kp_text_end(); 29 | extern void _kp_data_start(); 30 | extern void _kp_data_end(); 31 | extern void _kp_end(); 32 | 33 | extern uint64_t _kp_hook_start; 34 | extern uint64_t _kp_hook_end; 35 | 36 | extern uint64_t _kp_extra_start; 37 | extern uint64_t _kp_extra_end; 38 | 39 | extern uint64_t _kp_rox_start; 40 | extern uint64_t _kp_rox_end; 41 | extern uint64_t _kp_rw_start; 42 | extern uint64_t _kp_rw_end; 43 | 44 | extern uint64_t _kp_region_start; 45 | extern uint64_t _kp_region_end; 46 | 47 | static inline bool is_kp_text_area(unsigned long addr) 48 | { 49 | return addr >= (unsigned long)_kp_text_start && addr < (unsigned long)_kp_text_end; 50 | } 51 | 52 | static inline bool is_kp_hook_area(unsigned long addr) 53 | { 54 | return addr >= (unsigned long)_kp_hook_start && addr < (unsigned long)_kp_hook_end; 55 | } 56 | 57 | static inline bool is_kpm_rox_area(unsigned long addr) 58 | { 59 | return addr >= (unsigned long)_kp_rox_start && addr < (unsigned long)_kp_rox_end; 60 | } 61 | 62 | #endif -------------------------------------------------------------------------------- /kernel/include/ctype.h: -------------------------------------------------------------------------------- 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 | /* 3 | * Copyright (C) 2023 bmax121. All Rights Reserved. 4 | */ 5 | 6 | #ifndef _KP_CTYPE_H_ 7 | #define _KP_CTYPE_H_ 8 | 9 | inline int isupper(int c) 10 | { 11 | return c >= 'A' && c <= 'Z'; 12 | } 13 | 14 | inline int islower(int c) 15 | { 16 | return c >= 'a' && c <= 'z'; 17 | } 18 | 19 | inline int isalpha(int c) 20 | { 21 | return islower(c) || isupper(c); 22 | } 23 | 24 | inline int isdigit(int c) 25 | { 26 | return ((unsigned)c - '0') <= 9; 27 | } 28 | 29 | inline int isalnum(int c) 30 | { 31 | return isalpha(c) || isdigit(c); 32 | } 33 | 34 | inline int isascii(int c) 35 | { 36 | return !(c & ~0x7f); 37 | } 38 | 39 | inline int isblank(int c) 40 | { 41 | return (c == '\t') || (c == ' '); 42 | } 43 | 44 | inline int iscntrl(int c) 45 | { 46 | return c < 0x20; 47 | } 48 | 49 | inline int isspace(int c) 50 | { 51 | return c == ' ' || c == '\n' || c == '\t' || c == '\r'; 52 | } 53 | 54 | inline int isxdigit(int c) 55 | { 56 | return isdigit(c) || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'); 57 | } 58 | 59 | inline int toupper(int c) 60 | { 61 | return islower(c) ? (c & ~32) : c; 62 | } 63 | 64 | inline int tolower(int c) 65 | { 66 | return isupper(c) ? (c | 32) : c; 67 | } 68 | 69 | #endif -------------------------------------------------------------------------------- /kernel/include/hotpatch.h: -------------------------------------------------------------------------------- 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 | /* 3 | * Copyright (C) 2024 bmax121. All Rights Reserved. 4 | */ 5 | 6 | #ifndef _KP_HOTPATCH_H_ 7 | #define _KP_HOTPATCH_H_ 8 | 9 | #include 10 | 11 | int kp_insn_patch_text(void *addrs[], uint32_t insn[], int cnt); 12 | 13 | #endif -------------------------------------------------------------------------------- /kernel/include/kallsyms.h: -------------------------------------------------------------------------------- 1 | #ifndef _KP_KALLSYMS_H_ 2 | #define _KP_KALLSYMS_H_ 3 | 4 | struct module; 5 | 6 | #define KSYM_NAME_LEN 512 7 | 8 | extern int (*kallsyms_on_each_symbol)(int (*fn)(void *, const char *, struct module *, unsigned long), void *data); 9 | extern unsigned long (*kallsyms_lookup_name)(const char *name); 10 | 11 | #endif -------------------------------------------------------------------------------- /kernel/include/kpmalloc.h: -------------------------------------------------------------------------------- 1 | #ifndef _KP_KPMALLOC_H_ 2 | #define _KP_KPMALLOC_H_ 3 | 4 | #include 5 | 6 | extern tlsf_t kp_rw_mem; 7 | extern tlsf_t kp_rox_mem; 8 | 9 | static inline void *kp_malloc_exec(size_t bytes) 10 | { 11 | return tlsf_malloc(kp_rox_mem, bytes); 12 | } 13 | 14 | static inline void *kp_memalign_exec(size_t align, size_t bytes) 15 | { 16 | return tlsf_memalign(kp_rox_mem, align, bytes); 17 | } 18 | 19 | static inline void *kp_realloc_exec(void *ptr, size_t size) 20 | { 21 | return tlsf_realloc(kp_rox_mem, ptr, size); 22 | } 23 | 24 | static inline void kp_free_exec(void *ptr) 25 | { 26 | tlsf_free(kp_rox_mem, ptr); 27 | } 28 | 29 | static inline void *kp_malloc(size_t bytes) 30 | { 31 | return tlsf_malloc(kp_rw_mem, bytes); 32 | } 33 | 34 | static inline void *kp_memalign(size_t align, size_t bytes) 35 | { 36 | return tlsf_memalign(kp_rw_mem, align, bytes); 37 | } 38 | 39 | static inline void *kp_realloc(void *ptr, size_t size) 40 | { 41 | return tlsf_realloc(kp_rw_mem, ptr, size); 42 | } 43 | 44 | static inline void kp_free(void *ptr) 45 | { 46 | tlsf_free(kp_rw_mem, ptr); 47 | } 48 | 49 | #endif -------------------------------------------------------------------------------- /kernel/include/kpmodule.h: -------------------------------------------------------------------------------- 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 | /* 3 | * Copyright (C) 2023 bmax121. All Rights Reserved. 4 | */ 5 | 6 | #ifndef _KP_KPMODULE_H_ 7 | #define _KP_KPMODULE_H_ 8 | 9 | #define KPM_INFO(name, info, limit) \ 10 | _Static_assert(sizeof(info) <= limit, "Info string too long"); \ 11 | static const char __kpm_info_##name[] __attribute__((__used__)) \ 12 | __attribute__((section(".kpm.info"), unused, aligned(1))) = #name "=" info 13 | 14 | #define KPM_NAME_LEN 32 15 | #define KPM_VERSION_LEN 12 16 | #define KPM_LICENSE_LEN 32 17 | #define KPM_AUTHOR_LEN 32 18 | #define KPM_DESCRIPTION_LEN 512 19 | #define KPM_ARGS_LEN 1024 20 | 21 | #define KPM_NAME(x) KPM_INFO(name, x, KPM_NAME_LEN) 22 | #define KPM_VERSION(x) KPM_INFO(version, x, KPM_VERSION_LEN) 23 | #define KPM_LICENSE(x) KPM_INFO(license, x, KPM_LICENSE_LEN) 24 | #define KPM_AUTHOR(x) KPM_INFO(author, x, KPM_AUTHOR_LEN) 25 | #define KPM_DESCRIPTION(x) KPM_INFO(description, x, KPM_DESCRIPTION_LEN) 26 | 27 | typedef long (*mod_initcall_t)(const char *args, const char *event, void *reserved); 28 | typedef long (*mod_ctl0call_t)(const char *ctl_args, char *__user out_msg, int outlen); 29 | typedef long (*mod_ctl1call_t)(void *a1, void *a2, void *a3); 30 | typedef long (*mod_exitcall_t)(void *reserved); 31 | 32 | #define KPM_INIT(fn) \ 33 | static mod_initcall_t __kpm_initcall_##fn __attribute__((__used__)) __attribute__((__section__(".kpm.init"))) = fn 34 | 35 | #define KPM_CTL0(fn) \ 36 | static mod_ctl0call_t __kpm_ctlmodule_##fn __attribute__((__used__)) __attribute__((__section__(".kpm.ctl0"))) = fn 37 | 38 | #define KPM_CTL1(fn) \ 39 | static mod_ctl1call_t __kpm_ctlmodule_##fn __attribute__((__used__)) __attribute__((__section__(".kpm.ctl1"))) = fn 40 | 41 | #define KPM_EXIT(fn) \ 42 | static mod_exitcall_t __kpm_exitcall_##fn __attribute__((__used__)) __attribute__((__section__(".kpm.exit"))) = fn 43 | 44 | #endif -------------------------------------------------------------------------------- /kernel/include/log.h: -------------------------------------------------------------------------------- 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 | /* 3 | * Copyright (C) 2023 bmax121. All Rights Reserved. 4 | */ 5 | 6 | #ifndef _KP_LOG_H_ 7 | #define _KP_LOG_H_ 8 | 9 | #include 10 | 11 | #define PREFIX_MAX 48 12 | #define LOG_LINE_MAX (1024 - PREFIX_MAX) 13 | 14 | extern void (*printk)(const char *fmt, ...); 15 | 16 | #define logkv(fmt, ...) printk("[+] KP V " fmt, ##__VA_ARGS__) 17 | // #define logkv(fmt, ...) 18 | 19 | // #define logkfv(fmt, ...) printk("[+] KP V %s: " fmt, __func__, ##__VA_ARGS__) 20 | #define logkfv(fmt, ...) 21 | 22 | #define logkd(fmt, ...) printk("[+] KP D " fmt, ##__VA_ARGS__) 23 | #define logkfd(fmt, ...) printk("[+] KP D %s: " fmt, __func__, ##__VA_ARGS__) 24 | 25 | #define logki(fmt, ...) printk("[+] KP I " fmt, ##__VA_ARGS__) 26 | #define logkfi(fmt, ...) printk("[+] KP I %s: " fmt, __func__, ##__VA_ARGS__) 27 | 28 | #define logkw(fmt, ...) printk("[-] KP W " fmt, ##__VA_ARGS__) 29 | #define logkfw(fmt, ...) printk("[-] KP W %s: " fmt, __func__, ##__VA_ARGS__) 30 | 31 | #define logke(fmt, ...) printk("[-] KP E " fmt, ##__VA_ARGS__) 32 | #define logkfe(fmt, ...) printk("[-] KP E %s: " fmt, __func__, ##__VA_ARGS__) 33 | 34 | void log_boot(const char *fmt, ...); 35 | const char *get_boot_log(); 36 | 37 | #endif -------------------------------------------------------------------------------- /kernel/include/predata.h: -------------------------------------------------------------------------------- 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 | /* 3 | * Copyright (C) 2023 bmax121. All Rights Reserved. 4 | */ 5 | 6 | #ifndef _KP_PREDATA_H_ 7 | #define _KP_PREDATA_H_ 8 | 9 | #include 10 | #include 11 | 12 | extern struct patch_config *patch_config; 13 | extern setup_header_t *setup_header; 14 | 15 | int auth_superkey(const char *key); 16 | void reset_superkey(const char *key); 17 | void enable_auth_root_key(bool enable); 18 | const char *get_superkey(); 19 | const char *get_build_time(); 20 | uint64_t rand_next(); 21 | 22 | int on_each_extra_item(int (*callback)(const patch_extra_item_t *extra, const char *arg, const void *data, void *udata), 23 | void *udata); 24 | 25 | void predata_init(); 26 | 27 | #endif -------------------------------------------------------------------------------- /kernel/include/sha256.h: -------------------------------------------------------------------------------- 1 | /********************************************************************* 2 | * Filename: sha256.h 3 | * Author: Brad Conte (brad AT bradconte.com) 4 | * Copyright: 5 | * Disclaimer: This code is presented "as is" without any guarantees. 6 | * Details: Defines the API for the corresponding SHA1 implementation. 7 | *********************************************************************/ 8 | 9 | #ifndef SHA256_H 10 | #define SHA256_H 11 | 12 | /*************************** HEADER FILES ***************************/ 13 | #include 14 | 15 | /****************************** MACROS ******************************/ 16 | #define SHA256_BLOCK_SIZE 32 // SHA256 outputs a 32 byte digest 17 | 18 | /**************************** DATA TYPES ****************************/ 19 | typedef uint8_t BYTE; // 8-bit byte 20 | typedef uint32_t WORD; // 32-bit word, change to "long" for 16-bit machines 21 | 22 | typedef struct 23 | { 24 | BYTE data[64]; 25 | WORD datalen; 26 | unsigned long bitlen; 27 | WORD state[8]; 28 | } SHA256_CTX; 29 | 30 | /*********************** FUNCTION DECLARATIONS **********************/ 31 | void sha256_init(SHA256_CTX *ctx); 32 | void sha256_update(SHA256_CTX *ctx, const BYTE data[], size_t len); 33 | void sha256_final(SHA256_CTX *ctx, BYTE hash[]); 34 | 35 | #endif // SHA256_H -------------------------------------------------------------------------------- /kernel/include/stdarg.h: -------------------------------------------------------------------------------- 1 | #ifndef _KP_STDARG_H_ 2 | #define _KP_STDARG_H_ 3 | 4 | #ifndef _VA_LIST 5 | typedef __builtin_va_list va_list; 6 | #define _VA_LIST 7 | #endif 8 | #define va_start(ap, param) __builtin_va_start(ap, param) 9 | #define va_end(ap) __builtin_va_end(ap) 10 | #define va_arg(ap, type) __builtin_va_arg(ap, type) 11 | 12 | /* GCC always defines __va_copy, but does not define va_copy unless in c99 mode 13 | * or -ansi is not specified, since it was not part of C90. 14 | */ 15 | #define __va_copy(d, s) __builtin_va_copy(d, s) 16 | 17 | #if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || (defined(__cplusplus) && __cplusplus >= 201103L) || \ 18 | !defined(__STRICT_ANSI__) 19 | #define va_copy(dest, src) __builtin_va_copy(dest, src) 20 | #endif 21 | 22 | #ifndef __GNUC_VA_LIST 23 | #define __GNUC_VA_LIST 1 24 | typedef __builtin_va_list __gnuc_va_list; 25 | #endif 26 | 27 | #endif 28 | -------------------------------------------------------------------------------- /kernel/include/stdbool.h: -------------------------------------------------------------------------------- 1 | #ifndef _KP_STDBOOL_H_ 2 | #define _KP_STDBOOL_H_ 3 | 4 | #ifndef __bool_true_false_are_defined 5 | 6 | #define bool _Bool 7 | #define true 1 8 | #define false 0 9 | 10 | #endif 11 | 12 | #endif -------------------------------------------------------------------------------- /kernel/include/stddef.h: -------------------------------------------------------------------------------- 1 | #ifndef _KP_STDDEF_H_ 2 | #define _KP_STDDEF_H_ 3 | 4 | #ifndef NULL 5 | #define NULL 0 6 | #endif 7 | 8 | #define RET_VOID ((void)0) 9 | 10 | #define offsetof(TYPE, MEMBER) ((size_t) & ((TYPE *)0)->MEMBER) 11 | 12 | #endif -------------------------------------------------------------------------------- /kernel/include/stdint.h: -------------------------------------------------------------------------------- 1 | #ifndef _KP_STDINT_H_ 2 | #define _KP_STDINT_H_ 3 | 4 | #ifndef NULL 5 | #define NULL 0 6 | #endif 7 | 8 | typedef signed char int8_t; 9 | typedef unsigned char uint8_t; 10 | typedef signed short int16_t; 11 | typedef unsigned short uint16_t; 12 | typedef signed int int32_t; 13 | typedef unsigned int uint32_t; 14 | typedef signed long int64_t; 15 | typedef unsigned long uint64_t; 16 | 17 | typedef uint64_t u64; 18 | typedef int64_t s64; 19 | typedef uint32_t u32; 20 | typedef int32_t s32; 21 | typedef uint16_t u16; 22 | typedef int16_t s16; 23 | typedef uint8_t u8; 24 | typedef int8_t s8; 25 | 26 | typedef long ptrdiff_t; 27 | 28 | typedef unsigned long size_t; 29 | typedef long ssize_t; 30 | typedef unsigned long uintptr_t; 31 | 32 | typedef long intmax_t; 33 | typedef unsigned long uintmax_t; 34 | 35 | #endif -------------------------------------------------------------------------------- /kernel/include/symbol.h: -------------------------------------------------------------------------------- 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 | /* 3 | * Copyright (C) 2023 bmax121. All Rights Reserved. 4 | */ 5 | 6 | #ifndef _KP_SYMBOL_H_ 7 | #define _KP_SYMBOL_H_ 8 | 9 | #define KP_SYMBOL_LEN 32 10 | 11 | // todo: name len 12 | typedef struct 13 | { 14 | unsigned long addr; 15 | unsigned long hash; 16 | const char name[KP_SYMBOL_LEN]; 17 | } kp_symbol_t; 18 | 19 | #define _KP_EXPORT_SYMBOL(sym) \ 20 | static kp_symbol_t __kp_symbol_##sym __attribute__((used)) \ 21 | __attribute__((section(".kp.symbol"))) = { .name = #sym, .addr = (unsigned long)&sym, .hash = 0 } 22 | 23 | #define KP_EXPORT_SYMBOL(sym) _KP_EXPORT_SYMBOL(sym) 24 | 25 | extern unsigned long link_base_addr; 26 | extern unsigned long runtime_base_addr; 27 | 28 | unsigned long symbol_lookup_name(const char *name); 29 | 30 | static inline unsigned long link2runtime(unsigned long addr) 31 | { 32 | return addr - link_base_addr + runtime_base_addr; 33 | } 34 | 35 | #endif -------------------------------------------------------------------------------- /kernel/include/tlsf.h: -------------------------------------------------------------------------------- 1 | #ifndef INCLUDED_tlsf 2 | #define INCLUDED_tlsf 3 | 4 | /* 5 | ** Two Level Segregated Fit memory allocator, version 3.1. 6 | ** Written by Matthew Conte 7 | ** http://tlsf.baisoku.org 8 | ** 9 | ** Based on the original documentation by Miguel Masmano: 10 | ** http://www.gii.upv.es/tlsf/main/docs 11 | ** 12 | ** This implementation was written to the specification 13 | ** of the document, therefore no GPL restrictions apply. 14 | ** 15 | ** Copyright (c) 2006-2016, Matthew Conte 16 | ** All rights reserved. 17 | ** 18 | ** Redistribution and use in source and binary forms, with or without 19 | ** modification, are permitted provided that the following conditions are met: 20 | ** * Redistributions of source code must retain the above copyright 21 | ** notice, this list of conditions and the following disclaimer. 22 | ** * Redistributions in binary form must reproduce the above copyright 23 | ** notice, this list of conditions and the following disclaimer in the 24 | ** documentation and/or other materials provided with the distribution. 25 | ** * Neither the name of the copyright holder nor the 26 | ** names of its contributors may be used to endorse or promote products 27 | ** derived from this software without specific prior written permission. 28 | ** 29 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 30 | ** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 31 | ** WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 32 | ** DISCLAIMED. IN NO EVENT SHALL MATTHEW CONTE BE LIABLE FOR ANY 33 | ** DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 34 | ** (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 35 | ** LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 36 | ** ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 37 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 38 | ** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 39 | */ 40 | 41 | /* 42 | from: 43 | https://github.com/mattconte/tlsf 44 | */ 45 | 46 | #include 47 | #include 48 | 49 | /* tlsf_t: a TLSF structure. Can contain 1 to N pools. */ 50 | /* pool_t: a block of memory that TLSF can manage. */ 51 | typedef void *tlsf_t; 52 | typedef void *pool_t; 53 | 54 | /* Create/destroy a memory pool. */ 55 | tlsf_t tlsf_create(void *mem); 56 | tlsf_t tlsf_create_with_pool(void *mem, size_t bytes); 57 | void tlsf_destroy(tlsf_t tlsf); 58 | pool_t tlsf_get_pool(tlsf_t tlsf); 59 | 60 | /* Add/remove memory pools. */ 61 | pool_t tlsf_add_pool(tlsf_t tlsf, void *mem, size_t bytes); 62 | void tlsf_remove_pool(tlsf_t tlsf, pool_t pool); 63 | 64 | /* malloc/memalign/realloc/free replacements. */ 65 | void *tlsf_malloc(tlsf_t tlsf, size_t bytes); 66 | void *tlsf_memalign(tlsf_t tlsf, size_t align, size_t bytes); 67 | void *tlsf_realloc(tlsf_t tlsf, void *ptr, size_t size); 68 | void tlsf_free(tlsf_t tlsf, void *ptr); 69 | 70 | /* Returns internal block size, not original request size */ 71 | size_t tlsf_block_size(void *ptr); 72 | 73 | /* Overheads/limits of internal structures. */ 74 | size_t tlsf_size(void); 75 | size_t tlsf_align_size(void); 76 | size_t tlsf_block_size_min(void); 77 | size_t tlsf_block_size_max(void); 78 | size_t tlsf_pool_overhead(void); 79 | size_t tlsf_alloc_overhead(void); 80 | 81 | typedef void (*tlsf_walker)(void *ptr, size_t size, int used, void *user); 82 | void tlsf_walk_pool(pool_t pool, tlsf_walker walker, void *user); 83 | /* Returns nonzero if any internal consistency check fails. */ 84 | int tlsf_check(tlsf_t tlsf); 85 | int tlsf_check_pool(pool_t pool); 86 | 87 | #endif -------------------------------------------------------------------------------- /kernel/kernel-source/android_vendor.h: -------------------------------------------------------------------------------- 1 | /* SPDX-License-Identifier: GPL-2.0-only */ 2 | /* 3 | * android_vendor.h - Android vendor data 4 | * 5 | * Copyright 2020 Google LLC 6 | * 7 | * These macros are to be used to reserve space in kernel data structures 8 | * for use by vendor modules. 9 | * 10 | * These macros should be used before the kernel abi is "frozen". 11 | * Fields can be added to various kernel structures that need space 12 | * for functionality implemented in vendor modules. The use of 13 | * these fields is vendor specific. 14 | */ 15 | #ifndef _ANDROID_VENDOR_H 16 | #define _ANDROID_VENDOR_H 17 | 18 | /* 19 | * ANDROID_VENDOR_DATA 20 | * Reserve some "padding" in a structure for potential future use. 21 | * This normally placed at the end of a structure. 22 | * number: the "number" of the padding variable in the structure. Start with 23 | * 1 and go up. 24 | * 25 | * ANDROID_VENDOR_DATA_ARRAY 26 | * Same as ANDROID_VENDOR_DATA but allocates an array of u64 with 27 | * the specified size 28 | */ 29 | #ifdef CONFIG_ANDROID_VENDOR_OEM_DATA 30 | #define ANDROID_VENDOR_DATA(n) u64 android_vendor_data##n 31 | #define ANDROID_VENDOR_DATA_ARRAY(n, s) u64 android_vendor_data##n[s] 32 | 33 | #define ANDROID_OEM_DATA(n) u64 android_oem_data##n 34 | #define ANDROID_OEM_DATA_ARRAY(n, s) u64 android_oem_data##n[s] 35 | 36 | #define android_init_vendor_data(p, n) \ 37 | memset(&p->android_vendor_data##n, 0, sizeof(p->android_vendor_data##n)) 38 | #define android_init_oem_data(p, n) \ 39 | memset(&p->android_oem_data##n, 0, sizeof(p->android_oem_data##n)) 40 | #else 41 | #define ANDROID_VENDOR_DATA(n) 42 | #define ANDROID_VENDOR_DATA_ARRAY(n, s) 43 | #define ANDROID_OEM_DATA(n) 44 | #define ANDROID_OEM_DATA_ARRAY(n, s) 45 | 46 | #define android_init_vendor_data(p, n) 47 | #define android_init_oem_data(p, n) 48 | #endif 49 | 50 | #endif /* _ANDROID_VENDOR_H */ 51 | -------------------------------------------------------------------------------- /kernel/kernel-source/errseq.h: -------------------------------------------------------------------------------- 1 | /* SPDX-License-Identifier: GPL-2.0 */ 2 | /* 3 | * See Documentation/errseq.rst and lib/errseq.c 4 | */ 5 | #ifndef _LINUX_ERRSEQ_H 6 | #define _LINUX_ERRSEQ_H 7 | typedef u32 errseq_t; 8 | errseq_t errseq_set(errseq_t *eseq, int err); 9 | errseq_t errseq_sample(errseq_t *eseq); 10 | int errseq_check(errseq_t *eseq, errseq_t since); 11 | int errseq_check_and_advance(errseq_t *eseq, errseq_t *since); 12 | #endif -------------------------------------------------------------------------------- /kernel/kernel-source/prio_tree.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef _LINUX_PRIO_TREE_H 3 | #define _LINUX_PRIO_TREE_H 4 | /* 5 | * K&R 2nd ed. A8.3 somewhat obliquely hints that initial sequences of struct 6 | * fields with identical types should end up at the same location. We'll use 7 | * this until we can scrap struct raw_prio_tree_node. 8 | * 9 | * Note: all this could be done more elegantly by using unnamed union/struct 10 | * fields. However, gcc 2.95.3 and apparently also gcc 3.0.4 don't support this 11 | * language extension. 12 | */ 13 | struct raw_prio_tree_node { 14 | struct prio_tree_node *left; 15 | struct prio_tree_node *right; 16 | struct prio_tree_node *parent; 17 | }; 18 | 19 | #endif -------------------------------------------------------------------------------- /kernel/kernel-source/rbtree.h: -------------------------------------------------------------------------------- 1 | #ifndef _LINUX_RBTREE_H 2 | #define _LINUX_RBTREE_H 3 | 4 | struct rb_node 5 | { 6 | unsigned long rb_parent_color; 7 | #define RB_RED 0 8 | #define RB_BLACK 1 9 | struct rb_node *rb_right; 10 | struct rb_node *rb_left; 11 | } __attribute__((aligned(sizeof(long)))); 12 | 13 | 14 | struct rb_root { 15 | struct rb_node *rb_node; 16 | }; 17 | 18 | #endif -------------------------------------------------------------------------------- /kernel/kernel-source/time64.h: -------------------------------------------------------------------------------- 1 | #ifndef __LINUX_TIME64_H_ 2 | #define __LINUX_TIME64_H_ 3 | 4 | typedef long long time64_t; 5 | typedef unsigned long long timeu64_t; 6 | 7 | 8 | struct timespec64 { 9 | time64_t tv_sec; /* seconds */ 10 | long tv_nsec; /* nanoseconds */ 11 | }; 12 | 13 | struct itimerspec64 { 14 | struct timespec64 it_interval; 15 | struct timespec64 it_value; 16 | }; 17 | 18 | #endif -------------------------------------------------------------------------------- /kernel/kpimg.lds: -------------------------------------------------------------------------------- 1 | ENTRY(start) 2 | OUTPUT_ARCH(aarch64) 3 | 4 | _link_base = 64k - 3 * 4k; 5 | 6 | SECTIONS 7 | { 8 | /DISCARD/ : { *(.discard) *(.discard.*) } 9 | /DISCARD/ : { 10 | *(.interp .dynamic) 11 | *(.dynsym .dynstr .hash .gnu.hash) 12 | } 13 | . = _link_base; 14 | _link_base = .; 15 | 16 | .setup.data : { 17 | _setup_start = .; 18 | base/setup.o(.setup.header) 19 | . = _setup_start + 64; 20 | base/setup.o(.setup.preset) 21 | base/setup.o(.setup.data) 22 | . = _setup_start + 4K; 23 | } 24 | 25 | .setup.text : { 26 | base/setup1.o(.entry.text) 27 | base/setup1.o(.text) 28 | base/setup.o(.text) 29 | _setup_end = .; 30 | } 31 | 32 | . = ALIGN(16); 33 | .setup.map : { 34 | _map_start = .; 35 | base/map.o(.map.data) 36 | base/map.o(.map.text) 37 | base/map.o(.text) 38 | base/map1.o(.text) 39 | . = ALIGN(16); 40 | _map_end = .; 41 | } 42 | ASSERT(SIZEOF(.setup.map) < 0xa00, "Size too large of .setup.map!") 43 | 44 | . = ALIGN(64K); 45 | _kp_start = .; 46 | .kp.text : { 47 | _kp_text_start = .; 48 | 49 | base/start.o(.start.text) 50 | base/start.o(.text) 51 | 52 | base/hook.o(.transit0.text); 53 | _transit0_end = .; 54 | base/hook.o(.transit4.text); 55 | _transit4_end = .; 56 | base/hook.o(.transit8.text); 57 | _transit8_end = .; 58 | base/hook.o(.transit12.text); 59 | _transit12_end = .; 60 | 61 | base/fphook.o(.fp.transit0.text); 62 | _fp_transit0_end = .; 63 | base/fphook.o(.fp.transit4.text); 64 | _fp_transit4_end = .; 65 | base/fphook.o(.fp.transit8.text); 66 | _fp_transit8_end = .; 67 | base/fphook.o(.fp.transit12.text); 68 | _fp_transit12_end = .; 69 | 70 | base/*(.text) 71 | base/*(.rodata*) 72 | 73 | *(.text) 74 | *(.rodata*) 75 | /* *(.got) */ 76 | 77 | . = ALIGN(16); 78 | _kp_text_end = .; 79 | } 80 | 81 | . = ALIGN(64k); 82 | .kp.data : { 83 | _kp_data_start = .; 84 | base/start.o(.start.data) 85 | base/*(.data) 86 | base/*(.bss) 87 | *(.data) 88 | *(.bss) 89 | . = ALIGN(16); 90 | _kp_symbol_start = .; 91 | *(.kp.symbol) 92 | _kp_symbol_end = .; 93 | _kp_data_end = .; 94 | } 95 | 96 | .got.plt : { *(.got.plt) } 97 | ASSERT(SIZEOF(.got.plt) == 0, "Unexpected GOT/PLT entries detected!") 98 | 99 | . = ALIGN(64k); 100 | _kp_end = .; 101 | 102 | _link_end = .; 103 | 104 | .got : { 105 | *(.got) 106 | } 107 | ASSERT(SIZEOF(.got) == 0, "Unexpected GOT detected!") 108 | 109 | .plt : { 110 | *(.plt) *(.plt.*) *(.iplt) *(.igot .igot.plt) 111 | } 112 | ASSERT(SIZEOF(.plt) == 0, "Unexpected run-time procedure linkages detected!") 113 | 114 | 115 | .data.rel.ro : { 116 | *(.data.rel.ro) 117 | } 118 | ASSERT(SIZEOF(.data.rel.ro) == 0, "Unexpected RELRO detected!") 119 | 120 | .rela.dyn : { 121 | *(.rela .rela*) 122 | } 123 | ASSERT(SIZEOF(.rela.dyn) == 0, "Unexpected RELRDYN detected!") 124 | } -------------------------------------------------------------------------------- /kernel/linux/arch/arm64/include/asm/cacheflush.h: -------------------------------------------------------------------------------- 1 | // todo -------------------------------------------------------------------------------- /kernel/linux/arch/arm64/include/asm/hwcap.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2012 ARM Ltd. 3 | * 4 | * This program is free software; you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License version 2 as 6 | * published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 | * GNU General Public License for more details. 12 | * 13 | * You should have received a copy of the GNU General Public License 14 | * along with this program. If not, see . 15 | */ 16 | #ifndef __ASM_HWCAP_H 17 | #define __ASM_HWCAP_H 18 | 19 | #include 20 | 21 | #define COMPAT_HWCAP_HALF (1 << 1) 22 | #define COMPAT_HWCAP_THUMB (1 << 2) 23 | #define COMPAT_HWCAP_FAST_MULT (1 << 4) 24 | #define COMPAT_HWCAP_VFP (1 << 6) 25 | #define COMPAT_HWCAP_EDSP (1 << 7) 26 | #define COMPAT_HWCAP_NEON (1 << 12) 27 | #define COMPAT_HWCAP_VFPv3 (1 << 13) 28 | #define COMPAT_HWCAP_TLS (1 << 15) 29 | #define COMPAT_HWCAP_VFPv4 (1 << 16) 30 | #define COMPAT_HWCAP_IDIVA (1 << 17) 31 | #define COMPAT_HWCAP_IDIVT (1 << 18) 32 | #define COMPAT_HWCAP_IDIV (COMPAT_HWCAP_IDIVA | COMPAT_HWCAP_IDIVT) 33 | #define COMPAT_HWCAP_LPAE (1 << 20) 34 | #define COMPAT_HWCAP_EVTSTRM (1 << 21) 35 | 36 | #define COMPAT_HWCAP2_AES (1 << 0) 37 | #define COMPAT_HWCAP2_PMULL (1 << 1) 38 | #define COMPAT_HWCAP2_SHA1 (1 << 2) 39 | #define COMPAT_HWCAP2_SHA2 (1 << 3) 40 | #define COMPAT_HWCAP2_CRC32 (1 << 4) 41 | #define COMPAT_HWCAP2_SB (1 << 5) 42 | #define COMPAT_HWCAP2_SSBS (1 << 6) 43 | 44 | #ifndef __ASSEMBLY__ 45 | /* 46 | * This yields a mask that user programs can use to figure out what 47 | * instruction set this cpu supports. 48 | */ 49 | #define ELF_HWCAP cpu_get_elf_hwcap() 50 | #define ELF_HWCAP2 cpu_get_elf_hwcap2() 51 | 52 | #ifdef CONFIG_COMPAT 53 | #define COMPAT_ELF_HWCAP (compat_elf_hwcap) 54 | #define COMPAT_ELF_HWCAP2 (compat_elf_hwcap2) 55 | extern unsigned int compat_elf_hwcap, compat_elf_hwcap2; 56 | #endif 57 | 58 | enum 59 | { 60 | CAP_HWCAP = 1, 61 | #ifdef CONFIG_COMPAT 62 | CAP_COMPAT_HWCAP, 63 | CAP_COMPAT_HWCAP2, 64 | #endif 65 | }; 66 | 67 | #endif 68 | #endif -------------------------------------------------------------------------------- /kernel/linux/arch/arm64/include/asm/io.h: -------------------------------------------------------------------------------- 1 | #ifndef __ASM_IO_H 2 | #define __ASM_IO_H 3 | 4 | #include 5 | 6 | #endif -------------------------------------------------------------------------------- /kernel/linux/arch/arm64/include/asm/processor.h: -------------------------------------------------------------------------------- 1 | /* SPDX-License-Identifier: GPL-2.0-only */ 2 | /* 3 | * Based on arch/arm/include/asm/processor.h 4 | * 5 | * Copyright (C) 1995-1999 Russell King 6 | * Copyright (C) 2012 ARM Ltd. 7 | */ 8 | #ifndef __ASM_PROCESSOR_H 9 | #define __ASM_PROCESSOR_H 10 | 11 | #include 12 | #include 13 | 14 | #define task_stack_page(task) (get_stack(task)) 15 | 16 | // #define THREAD_SIZE 16384 17 | // #define THREAD_START_SP (THREAD_SIZE - 16) 18 | // #define task_pt_regs(p) ((struct pt_regs *)(THREAD_START_SP + task_stack_page(p)) - 1) 19 | 20 | extern int16_t pt_regs_offset; 21 | 22 | struct pt_regs *_task_pt_reg(struct task_struct *task); 23 | 24 | #define task_pt_regs(p) _task_pt_reg(p) 25 | 26 | #endif -------------------------------------------------------------------------------- /kernel/linux/arch/arm64/include/asm/thread_info.h: -------------------------------------------------------------------------------- 1 | #ifndef __ASM_THREAD_INFO_H 2 | #define __ASM_THREAD_INFO_H 3 | 4 | #include 5 | 6 | struct task_struct; 7 | typedef unsigned long mm_segment_t; 8 | 9 | #define TIF_SIGPENDING 0 /* signal pending */ 10 | #define TIF_NEED_RESCHED 1 /* rescheduling necessary */ 11 | #define TIF_NOTIFY_RESUME 2 /* callback before returning to user */ 12 | #define TIF_FOREIGN_FPSTATE 3 /* CPU's FP state is not current's */ 13 | #define TIF_UPROBE 4 /* uprobe breakpoint or singlestep */ 14 | #define TIF_MTE_ASYNC_FAULT 5 /* MTE Asynchronous Tag Check Fault */ 15 | #define TIF_NOTIFY_SIGNAL 6 /* signal notifications exist */ 16 | #define TIF_SYSCALL_TRACE 8 /* syscall trace active */ 17 | #define TIF_SYSCALL_AUDIT 9 /* syscall auditing */ 18 | #define TIF_SYSCALL_TRACEPOINT 10 /* syscall tracepoint for ftrace */ 19 | #define TIF_SECCOMP 11 /* syscall secure computing */ 20 | #define TIF_SYSCALL_EMU 12 /* syscall emulation active */ 21 | #define TIF_MEMDIE 18 /* is terminating due to OOM killer */ 22 | #define TIF_FREEZE 19 23 | #define TIF_RESTORE_SIGMASK 20 24 | #define TIF_SINGLESTEP 21 25 | #define TIF_32BIT 22 /* 32bit process */ 26 | #define TIF_SVE 23 /* Scalable Vector Extension in use */ 27 | #define TIF_SVE_VL_INHERIT 24 /* Inherit SVE vl_onexec across exec */ 28 | #define TIF_SSBD 25 /* Wants SSB mitigation */ 29 | #define TIF_TAGGED_ADDR 26 /* Allow tagged user addresses */ 30 | #define TIF_SME 27 /* SME in use */ 31 | #define TIF_SME_VL_INHERIT 28 /* Inherit SME vl_onexec across exec */ 32 | 33 | #define _TIF_SIGPENDING (1 << TIF_SIGPENDING) 34 | #define _TIF_NEED_RESCHED (1 << TIF_NEED_RESCHED) 35 | #define _TIF_NOTIFY_RESUME (1 << TIF_NOTIFY_RESUME) 36 | #define _TIF_FOREIGN_FPSTATE (1 << TIF_FOREIGN_FPSTATE) 37 | #define _TIF_SYSCALL_TRACE (1 << TIF_SYSCALL_TRACE) 38 | #define _TIF_SYSCALL_AUDIT (1 << TIF_SYSCALL_AUDIT) 39 | #define _TIF_SYSCALL_TRACEPOINT (1 << TIF_SYSCALL_TRACEPOINT) 40 | #define _TIF_SECCOMP (1 << TIF_SECCOMP) 41 | #define _TIF_SYSCALL_EMU (1 << TIF_SYSCALL_EMU) 42 | #define _TIF_UPROBE (1 << TIF_UPROBE) 43 | #define _TIF_SINGLESTEP (1 << TIF_SINGLESTEP) 44 | #define _TIF_32BIT (1 << TIF_32BIT) 45 | #define _TIF_SVE (1 << TIF_SVE) 46 | #define _TIF_MTE_ASYNC_FAULT (1 << TIF_MTE_ASYNC_FAULT) 47 | #define _TIF_NOTIFY_SIGNAL (1 << TIF_NOTIFY_SIGNAL) 48 | 49 | #define _TIF_WORK_MASK \ 50 | (_TIF_NEED_RESCHED | _TIF_SIGPENDING | _TIF_NOTIFY_RESUME | _TIF_FOREIGN_FPSTATE | _TIF_UPROBE | \ 51 | _TIF_MTE_ASYNC_FAULT | _TIF_NOTIFY_SIGNAL) 52 | 53 | #define _TIF_SYSCALL_WORK \ 54 | (_TIF_SYSCALL_TRACE | _TIF_SYSCALL_AUDIT | _TIF_SYSCALL_TRACEPOINT | _TIF_SECCOMP | _TIF_SYSCALL_EMU) 55 | 56 | struct thread_info_be490 57 | { 58 | unsigned long flags; /* low level flags */ 59 | mm_segment_t addr_limit; /* address limit */ 60 | // from 3.7 to 4.9 61 | struct task_struct *task; /* main task structure */ 62 | char _others[0]; 63 | }; 64 | 65 | /* 66 | * low level task data that entry.S needs immediate access to. 67 | */ 68 | struct thread_info 69 | { 70 | unsigned long flags; /* low level flags */ 71 | mm_segment_t addr_limit; /* address limit */ 72 | #ifdef CONFIG_ARM64_SW_TTBR0_PAN 73 | u64 ttbr0; /* saved TTBR0_EL1 */ 74 | #endif 75 | union 76 | { 77 | u64 preempt_count; /* 0 => preemptible, <0 => bug */ 78 | struct 79 | { 80 | #ifdef CONFIG_CPU_BIG_ENDIAN 81 | u32 need_resched; 82 | u32 count; 83 | #else 84 | u32 count; 85 | u32 need_resched; 86 | #endif 87 | } preempt; 88 | }; 89 | #ifdef CONFIG_SHADOW_CALL_STACK 90 | void *scs_base; 91 | void *scs_sp; 92 | #endif 93 | }; 94 | 95 | #endif -------------------------------------------------------------------------------- /kernel/linux/arch/arm64/include/asm/uaccess.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Based on arch/arm/include/asm/uaccess.h 3 | * 4 | * Copyright (C) 2012 ARM Ltd. 5 | * 6 | * This program is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License version 2 as 8 | * published by the Free Software Foundation. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see . 17 | */ 18 | #ifndef __ASM_UACCESS_H 19 | #define __ASM_UACCESS_H 20 | 21 | #include 22 | 23 | // todo: 24 | 25 | #define VERIFY_READ 0 26 | #define VERIFY_WRITE 1 27 | 28 | #define KERNEL_DS (-1UL) 29 | // #define get_ds() (KERNEL_DS) 30 | 31 | // #define USER_DS TASK_SIZE_64 32 | // #define get_fs() (current_thread_info()->addr_limit) 33 | 34 | // static inline void set_fs(mm_segment_t fs) 35 | // { 36 | // current_thread_info()->addr_limit = fs; 37 | // } 38 | 39 | // #define segment_eq(a, b) ((a) == (b)) 40 | 41 | #endif -------------------------------------------------------------------------------- /kernel/linux/arch/arm64/include/asm/unistd.h: -------------------------------------------------------------------------------- 1 | 2 | #define __ARCH_WANT_COMPAT_STAT 3 | #define __ARCH_WANT_COMPAT_STAT64 4 | #define __ARCH_WANT_SYS_GETHOSTNAME 5 | #define __ARCH_WANT_SYS_PAUSE 6 | #define __ARCH_WANT_SYS_GETPGRP 7 | #define __ARCH_WANT_SYS_NICE 8 | #define __ARCH_WANT_SYS_SIGPENDING 9 | #define __ARCH_WANT_SYS_SIGPROCMASK 10 | #define __ARCH_WANT_COMPAT_SYS_SENDFILE 11 | #define __ARCH_WANT_SYS_UTIME32 12 | #define __ARCH_WANT_SYS_FORK 13 | #define __ARCH_WANT_SYS_VFORK 14 | 15 | /* 16 | * Compat syscall numbers used by the AArch64 kernel. 17 | */ 18 | #define __NR_compat_restart_syscall 0 19 | #define __NR_compat_exit 1 20 | #define __NR_compat_read 3 21 | #define __NR_compat_write 4 22 | #define __NR_compat_gettimeofday 78 23 | #define __NR_compat_sigreturn 119 24 | #define __NR_compat_rt_sigreturn 173 25 | #define __NR_compat_clock_gettime 263 26 | #define __NR_compat_clock_getres 264 27 | #define __NR_compat_clock_gettime64 403 28 | #define __NR_compat_clock_getres_time64 406 29 | 30 | /* 31 | * The following SVCs are ARM private. 32 | */ 33 | #define __ARM_NR_COMPAT_BASE 0x0f0000 34 | #define __ARM_NR_compat_cacheflush (__ARM_NR_COMPAT_BASE + 2) 35 | #define __ARM_NR_compat_set_tls (__ARM_NR_COMPAT_BASE + 5) 36 | #define __ARM_NR_COMPAT_END (__ARM_NR_COMPAT_BASE + 0x800) 37 | -------------------------------------------------------------------------------- /kernel/linux/arch/arm64/include/uapi/asm/sysinfo.h: -------------------------------------------------------------------------------- 1 | #ifndef _LINUX_SYSINFO_H 2 | #define _LINUX_SYSINFO_H 3 | 4 | #include 5 | 6 | #define SI_LOAD_SHIFT 16 7 | struct sysinfo { 8 | __kernel_long_t uptime; /* Seconds since boot */ 9 | __kernel_ulong_t loads[3]; /* 1, 5, and 15 minute load averages */ 10 | __kernel_ulong_t totalram; /* Total usable main memory size */ 11 | __kernel_ulong_t freeram; /* Available memory size */ 12 | __kernel_ulong_t sharedram; /* Amount of shared memory */ 13 | __kernel_ulong_t bufferram; /* Memory used by buffers */ 14 | __kernel_ulong_t totalswap; /* Total swap space size */ 15 | __kernel_ulong_t freeswap; /* swap space still available */ 16 | __u16 procs; /* Number of current processes */ 17 | __u16 pad; /* Explicit padding for m68k */ 18 | __kernel_ulong_t totalhigh; /* Total high memory size */ 19 | __kernel_ulong_t freehigh; /* Available high memory size */ 20 | __u32 mem_unit; /* Memory unit size in bytes */ 21 | char _f[20-2*sizeof(__kernel_ulong_t)-sizeof(__u32)]; /* Padding: libc5 uses this.. */ 22 | }; 23 | 24 | #endif /* _LINUX_SYSINFO_H */ -------------------------------------------------------------------------------- /kernel/linux/include/asm-generic/bitops/fls_ffs.h: -------------------------------------------------------------------------------- 1 | #ifndef _ASM_GENERIC_BITOPS_FLS_FFS_H_ 2 | #define _ASM_GENERIC_BITOPS_FLS_FFS_H_ 3 | 4 | #include 5 | 6 | /** 7 | * fls - find last (most-significant) bit set 8 | * @x: the word to search 9 | * 10 | * This is defined the same way as ffs. 11 | * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32. 12 | */ 13 | static __always_inline int fls(unsigned int x) 14 | { 15 | int r = 32; 16 | 17 | if (!x) return 0; 18 | if (!(x & 0xffff0000u)) { 19 | x <<= 16; 20 | r -= 16; 21 | } 22 | if (!(x & 0xff000000u)) { 23 | x <<= 8; 24 | r -= 8; 25 | } 26 | if (!(x & 0xf0000000u)) { 27 | x <<= 4; 28 | r -= 4; 29 | } 30 | if (!(x & 0xc0000000u)) { 31 | x <<= 2; 32 | r -= 2; 33 | } 34 | if (!(x & 0x80000000u)) { 35 | x <<= 1; 36 | r -= 1; 37 | } 38 | return r; 39 | } 40 | 41 | static __always_inline unsigned long __fls(unsigned long word) 42 | { 43 | int num = BITS_PER_LONG - 1; 44 | 45 | #if BITS_PER_LONG == 64 46 | if (!(word & (~0ul << 32))) { 47 | num -= 32; 48 | word <<= 32; 49 | } 50 | #endif 51 | if (!(word & (~0ul << (BITS_PER_LONG - 16)))) { 52 | num -= 16; 53 | word <<= 16; 54 | } 55 | if (!(word & (~0ul << (BITS_PER_LONG - 8)))) { 56 | num -= 8; 57 | word <<= 8; 58 | } 59 | if (!(word & (~0ul << (BITS_PER_LONG - 4)))) { 60 | num -= 4; 61 | word <<= 4; 62 | } 63 | if (!(word & (~0ul << (BITS_PER_LONG - 2)))) { 64 | num -= 2; 65 | word <<= 2; 66 | } 67 | if (!(word & (~0ul << (BITS_PER_LONG - 1)))) num -= 1; 68 | return num; 69 | } 70 | 71 | static __always_inline int fls64(__u64 x) 72 | { 73 | if (x == 0) return 0; 74 | return __fls(x) + 1; 75 | } 76 | 77 | /** 78 | * __ffs - find first bit in word. 79 | * @word: The word to search 80 | * 81 | * Undefined if no bit exists, so code should check against 0 first. 82 | */ 83 | static __always_inline unsigned long __ffs(unsigned long word) 84 | { 85 | int num = 0; 86 | 87 | #if BITS_PER_LONG == 64 88 | if ((word & 0xffffffff) == 0) { 89 | num += 32; 90 | word >>= 32; 91 | } 92 | #endif 93 | if ((word & 0xffff) == 0) { 94 | num += 16; 95 | word >>= 16; 96 | } 97 | if ((word & 0xff) == 0) { 98 | num += 8; 99 | word >>= 8; 100 | } 101 | if ((word & 0xf) == 0) { 102 | num += 4; 103 | word >>= 4; 104 | } 105 | if ((word & 0x3) == 0) { 106 | num += 2; 107 | word >>= 2; 108 | } 109 | if ((word & 0x1) == 0) num += 1; 110 | return num; 111 | } 112 | 113 | /** 114 | * ffs - find first bit set 115 | * @x: the word to search 116 | * 117 | * This is defined the same way as 118 | * the libc and compiler builtin ffs routines, therefore 119 | * differs in spirit from the above ffz (man ffs). 120 | */ 121 | static inline int ffs(int x) 122 | { 123 | int r = 1; 124 | 125 | if (!x) return 0; 126 | if (!(x & 0xffff)) { 127 | x >>= 16; 128 | r += 16; 129 | } 130 | if (!(x & 0xff)) { 131 | x >>= 8; 132 | r += 8; 133 | } 134 | if (!(x & 0xf)) { 135 | x >>= 4; 136 | r += 4; 137 | } 138 | if (!(x & 3)) { 139 | x >>= 2; 140 | r += 2; 141 | } 142 | if (!(x & 1)) { 143 | x >>= 1; 144 | r += 1; 145 | } 146 | return r; 147 | } 148 | 149 | #endif -------------------------------------------------------------------------------- /kernel/linux/include/asm-generic/bug.h: -------------------------------------------------------------------------------- 1 | /* SPDX-License-Identifier: GPL-2.0 */ 2 | #ifndef _ASM_GENERIC_BUG_H 3 | #define _ASM_GENERIC_BUG_H 4 | 5 | #endif -------------------------------------------------------------------------------- /kernel/linux/include/asm-generic/module.h: -------------------------------------------------------------------------------- 1 | /* SPDX-License-Identifier: GPL-2.0 */ 2 | #ifndef __ASM_GENERIC_MODULE_H 3 | #define __ASM_GENERIC_MODULE_H 4 | 5 | #include 6 | 7 | /* 8 | * Many architectures just need a simple module 9 | * loader without arch specific data. 10 | */ 11 | #ifndef CONFIG_HAVE_MOD_ARCH_SPECIFIC 12 | struct mod_arch_specific 13 | { 14 | }; 15 | #endif 16 | 17 | #define CONFIG_64BIT 18 | 19 | #ifdef CONFIG_64BIT 20 | #define Elf_Shdr Elf64_Shdr 21 | #define Elf_Phdr Elf64_Phdr 22 | #define Elf_Sym Elf64_Sym 23 | #define Elf_Dyn Elf64_Dyn 24 | #define Elf_Ehdr Elf64_Ehdr 25 | #define Elf_Addr Elf64_Addr 26 | #ifdef CONFIG_MODULES_USE_ELF_REL 27 | #define Elf_Rel Elf64_Rel 28 | #endif 29 | #ifdef CONFIG_MODULES_USE_ELF_RELA 30 | #define Elf_Rela Elf64_Rela 31 | #endif 32 | #define ELF_R_TYPE(X) ELF64_R_TYPE(X) 33 | #define ELF_R_SYM(X) ELF64_R_SYM(X) 34 | 35 | #else /* CONFIG_64BIT */ 36 | 37 | #define Elf_Shdr Elf32_Shdr 38 | #define Elf_Phdr Elf32_Phdr 39 | #define Elf_Sym Elf32_Sym 40 | #define Elf_Dyn Elf32_Dyn 41 | #define Elf_Ehdr Elf32_Ehdr 42 | #define Elf_Addr Elf32_Addr 43 | #ifdef CONFIG_MODULES_USE_ELF_REL 44 | #define Elf_Rel Elf32_Rel 45 | #endif 46 | #ifdef CONFIG_MODULES_USE_ELF_RELA 47 | #define Elf_Rela Elf32_Rela 48 | #endif 49 | #define ELF_R_TYPE(X) ELF32_R_TYPE(X) 50 | #define ELF_R_SYM(X) ELF32_R_SYM(X) 51 | #endif 52 | 53 | #endif /* __ASM_GENERIC_MODULE_H */ -------------------------------------------------------------------------------- /kernel/linux/include/asm-generic/rwonce.h: -------------------------------------------------------------------------------- 1 | #ifndef __ASM_GENERIC_RWONCE_H 2 | #define __ASM_GENERIC_RWONCE_H 3 | 4 | // todo: 5 | 6 | #define READ_ONCE(x) (*(const volatile typeof(x) *)&(x)) 7 | 8 | #define WRITE_ONCE(x, val) \ 9 | do { \ 10 | *(volatile typeof(x) *)&(x) = (val); \ 11 | } while (0) 12 | 13 | #endif 14 | -------------------------------------------------------------------------------- /kernel/linux/include/asm/stat.h: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | **************************************************************************** 3 | *** 4 | *** This header was automatically generated from a Linux kernel header 5 | *** of the same name, to make information necessary for userspace to 6 | *** call into the kernel available to libc. It contains only constants, 7 | *** structures, and macros generated from the original header, and thus, 8 | *** contains no copyrightable information. 9 | *** 10 | *** To edit the content of this header, modify the corresponding 11 | *** source file (e.g. under external/kernel-headers/original/) then 12 | *** run bionic/libc/kernel/tools/update_all.py 13 | *** 14 | *** Any manual change here will be lost the next time this script will 15 | *** be run. You've been warned! 16 | *** 17 | **************************************************************************** 18 | ****************************************************************************/ 19 | #include 20 | -------------------------------------------------------------------------------- /kernel/linux/include/asm/statfs.h: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | **************************************************************************** 3 | *** 4 | *** This header was automatically generated from a Linux kernel header 5 | *** of the same name, to make information necessary for userspace to 6 | *** call into the kernel available to libc. It contains only constants, 7 | *** structures, and macros generated from the original header, and thus, 8 | *** contains no copyrightable information. 9 | *** 10 | *** To edit the content of this header, modify the corresponding 11 | *** source file (e.g. under external/kernel-headers/original/) then 12 | *** run bionic/libc/kernel/tools/update_all.py 13 | *** 14 | *** Any manual change here will be lost the next time this script will 15 | *** be run. You've been warned! 16 | *** 17 | **************************************************************************** 18 | ****************************************************************************/ 19 | #ifndef __ASM_STATFS_H 20 | #define __ASM_STATFS_H 21 | #define ARCH_PACK_COMPAT_STATFS64 __attribute__((packed, aligned(4))) 22 | #include 23 | #endif 24 | -------------------------------------------------------------------------------- /kernel/linux/include/linux/bottom_half.h: -------------------------------------------------------------------------------- 1 | /* SPDX-License-Identifier: GPL-2.0 */ 2 | #ifndef _LINUX_BH_H 3 | #define _LINUX_BH_H 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | extern void kfunc_def(__local_bh_disable_ip)(unsigned long ip, unsigned int cnt); 11 | extern void kfunc_def(__local_bh_enable_ip)(unsigned long ip, unsigned int cnt); 12 | extern void kfunc_def(_local_bh_enable)(void); 13 | extern bool kfunc_def(local_bh_blocked)(void); 14 | 15 | static inline void local_bh_disable(void) 16 | { 17 | kfunc_call(__local_bh_disable_ip, _THIS_IP_, SOFTIRQ_DISABLE_OFFSET); 18 | } 19 | 20 | static inline void local_bh_enable_ip(unsigned long ip) 21 | { 22 | kfunc_call(__local_bh_enable_ip, ip, SOFTIRQ_DISABLE_OFFSET); 23 | } 24 | 25 | static inline void local_bh_enable(void) 26 | { 27 | kfunc_call(__local_bh_enable_ip, _THIS_IP_, SOFTIRQ_DISABLE_OFFSET); 28 | } 29 | 30 | static inline bool local_bh_blocked(void) 31 | { 32 | kfunc_call(local_bh_blocked); 33 | return false; 34 | } 35 | 36 | #endif -------------------------------------------------------------------------------- /kernel/linux/include/linux/build_bug.h: -------------------------------------------------------------------------------- 1 | /* SPDX-License-Identifier: GPL-2.0 */ 2 | #ifndef _LINUX_BUILD_BUG_H 3 | #define _LINUX_BUILD_BUG_H 4 | 5 | #include 6 | 7 | #ifdef __CHECKER__ 8 | #define BUILD_BUG_ON_ZERO(e) (0) 9 | #else /* __CHECKER__ */ 10 | /* 11 | * Force a compilation error if condition is true, but also produce a 12 | * result (of value 0 and type int), so the expression can be used 13 | * e.g. in a structure initializer (or where-ever else comma expressions 14 | * aren't permitted). 15 | */ 16 | #define BUILD_BUG_ON_ZERO(e) ((int)(sizeof(struct { int : (-!!(e)); }))) 17 | #endif /* __CHECKER__ */ 18 | 19 | /* Force a compilation error if a constant expression is not a power of 2 */ 20 | #define __BUILD_BUG_ON_NOT_POWER_OF_2(n) BUILD_BUG_ON(((n) & ((n)-1)) != 0) 21 | #define BUILD_BUG_ON_NOT_POWER_OF_2(n) BUILD_BUG_ON((n) == 0 || (((n) & ((n)-1)) != 0)) 22 | 23 | /* 24 | * BUILD_BUG_ON_INVALID() permits the compiler to check the validity of the 25 | * expression but avoids the generation of any code, even if that expression 26 | * has side-effects. 27 | */ 28 | #define BUILD_BUG_ON_INVALID(e) ((void)(sizeof((__force long)(e)))) 29 | 30 | /** 31 | * BUILD_BUG_ON_MSG - break compile if a condition is true & emit supplied 32 | * error message. 33 | * @condition: the condition which the compiler should know is false. 34 | * 35 | * See BUILD_BUG_ON for description. 36 | */ 37 | #define BUILD_BUG_ON_MSG(cond, msg) compiletime_assert(!(cond), msg) 38 | 39 | /** 40 | * BUILD_BUG_ON - break compile if a condition is true. 41 | * @condition: the condition which the compiler should know is false. 42 | * 43 | * If you have some code which relies on certain constants being equal, or 44 | * some other compile-time-evaluated condition, you should use BUILD_BUG_ON to 45 | * detect if someone changes it. 46 | */ 47 | #define BUILD_BUG_ON(condition) BUILD_BUG_ON_MSG(condition, "BUILD_BUG_ON failed: " #condition) 48 | 49 | /** 50 | * BUILD_BUG - break compile if used. 51 | * 52 | * If you have some code that you expect the compiler to eliminate at 53 | * build time, you should use BUILD_BUG to detect if it is 54 | * unexpectedly used. 55 | */ 56 | #define BUILD_BUG() BUILD_BUG_ON_MSG(1, "BUILD_BUG failed") 57 | 58 | /** 59 | * static_assert - check integer constant expression at build time 60 | * 61 | * static_assert() is a wrapper for the C11 _Static_assert, with a 62 | * little macro magic to make the message optional (defaulting to the 63 | * stringification of the tested expression). 64 | * 65 | * Contrary to BUILD_BUG_ON(), static_assert() can be used at global 66 | * scope, but requires the expression to be an integer constant 67 | * expression (i.e., it is not enough that __builtin_constant_p() is 68 | * true for expr). 69 | * 70 | * Also note that BUILD_BUG_ON() fails the build if the condition is 71 | * true, while static_assert() fails the build if the expression is 72 | * false. 73 | */ 74 | #define static_assert(expr, ...) __static_assert(expr, ##__VA_ARGS__, #expr) 75 | #define __static_assert(expr, msg, ...) _Static_assert(expr, msg) 76 | 77 | #endif /* _LINUX_BUILD_BUG_H */ -------------------------------------------------------------------------------- /kernel/linux/include/linux/capability.h: -------------------------------------------------------------------------------- 1 | #ifndef _LINUX_CAPABILITY_H 2 | #define _LINUX_CAPABILITY_H 3 | 4 | #include 5 | 6 | #define _LINUX_CAPABILITY_VERSION_3 0x20080522 7 | #define _LINUX_CAPABILITY_U32S_3 2 8 | 9 | #define _KERNEL_CAPABILITY_VERSION _LINUX_CAPABILITY_VERSION_3 10 | #define _KERNEL_CAPABILITY_U32S _LINUX_CAPABILITY_U32S_3 11 | 12 | typedef struct 13 | { 14 | u64 val; 15 | } kernel_cap_t; 16 | 17 | #define CAP_FS_MASK \ 18 | (BIT_ULL(CAP_CHOWN) | BIT_ULL(CAP_MKNOD) | BIT_ULL(CAP_DAC_OVERRIDE) | BIT_ULL(CAP_DAC_READ_SEARCH) | \ 19 | BIT_ULL(CAP_FOWNER) | BIT_ULL(CAP_FSETID) | BIT_ULL(CAP_MAC_OVERRIDE)) 20 | #define CAP_VALID_MASK (BIT_ULL(CAP_LAST_CAP + 1) - 1) 21 | 22 | #define CAP_EMPTY_SET ((kernel_cap_t){ 0 }) 23 | #define CAP_FULL_SET ((kernel_cap_t){ CAP_VALID_MASK }) 24 | #define CAP_FS_SET ((kernel_cap_t){ CAP_FS_MASK | BIT_ULL(CAP_LINUX_IMMUTABLE) }) 25 | #define CAP_NFSD_SET ((kernel_cap_t){ CAP_FS_MASK | BIT_ULL(CAP_SYS_RESOURCE) }) 26 | 27 | #define cap_clear(c) \ 28 | do { \ 29 | (c).val = 0; \ 30 | } while (0) 31 | 32 | #define cap_raise(c, flag) ((c).val |= BIT_ULL(flag)) 33 | #define cap_lower(c, flag) ((c).val &= ~BIT_ULL(flag)) 34 | #define cap_raised(c, flag) (((c).val & BIT_ULL(flag)) != 0) 35 | 36 | struct user_namespace; 37 | struct task_struct; 38 | 39 | extern bool has_capability(struct task_struct *t, int cap); 40 | extern bool has_ns_capability(struct task_struct *t, struct user_namespace *ns, int cap); 41 | extern bool has_capability_noaudit(struct task_struct *t, int cap); 42 | extern bool has_ns_capability_noaudit(struct task_struct *t, struct user_namespace *ns, int cap); 43 | extern bool capable(int cap); 44 | extern bool ns_capable(struct user_namespace *ns, int cap); 45 | extern bool ns_capable_noaudit(struct user_namespace *ns, int cap); 46 | extern bool ns_capable_setid(struct user_namespace *ns, int cap); 47 | 48 | extern kernel_cap_t full_cap; 49 | 50 | #endif -------------------------------------------------------------------------------- /kernel/linux/include/linux/compiler.h: -------------------------------------------------------------------------------- 1 | #ifndef __LINUX_COMPILER_H 2 | #define __LINUX_COMPILER_H 3 | 4 | /* 5 | * Prevent the compiler from merging or refetching reads or writes. The 6 | * compiler is also forbidden from reordering successive instances of 7 | * READ_ONCE, WRITE_ONCE and ACCESS_ONCE (see below), but only when the 8 | * compiler is aware of some particular ordering. One way to make the 9 | * compiler aware of ordering is to put the two invocations of READ_ONCE, 10 | * WRITE_ONCE or ACCESS_ONCE() in different C statements. 11 | * 12 | * In contrast to ACCESS_ONCE these two macros will also work on aggregate 13 | * data types like structs or unions. If the size of the accessed data 14 | * type exceeds the word size of the machine (e.g., 32 bits or 64 bits) 15 | * READ_ONCE() and WRITE_ONCE() will fall back to memcpy and print a 16 | * compile-time warning. 17 | * 18 | * Their two major use cases are: (1) Mediating communication between 19 | * process-level code and irq/NMI handlers, all running on the same CPU, 20 | * and (2) Ensuring that the compiler does not fold, spindle, or otherwise 21 | * mutilate accesses that either do not require ordering or that interact 22 | * with an explicit memory barrier or atomic instruction that provides the 23 | * required ordering. 24 | */ 25 | 26 | #define READ_ONCE(x) \ 27 | ({ \ 28 | union \ 29 | { \ 30 | typeof(x) __val; \ 31 | char __c[1]; \ 32 | } __u; \ 33 | __read_once_size(&(x), __u.__c, sizeof(x)); \ 34 | __u.__val; \ 35 | }) 36 | 37 | #define WRITE_ONCE(x, val) \ 38 | ({ \ 39 | typeof(x) __val = (val); \ 40 | __write_once_size(&(x), &__val, sizeof(__val)); \ 41 | __val; \ 42 | }) 43 | 44 | /* 45 | * Prevent the compiler from merging or refetching accesses. The compiler 46 | * is also forbidden from reordering successive instances of ACCESS_ONCE(), 47 | * but only when the compiler is aware of some particular ordering. One way 48 | * to make the compiler aware of ordering is to put the two invocations of 49 | * ACCESS_ONCE() in different C statements. 50 | * 51 | * This macro does absolutely -nothing- to prevent the CPU from reordering, 52 | * merging, or refetching absolutely anything at any time. Its main intended 53 | * use is to mediate communication between process-level code and irq/NMI 54 | * handlers, all running on the same CPU. 55 | */ 56 | #define ACCESS_ONCE(x) (*(volatile typeof(x) *)&(x)) 57 | 58 | #endif -------------------------------------------------------------------------------- /kernel/linux/include/linux/container_of.h: -------------------------------------------------------------------------------- 1 | /* SPDX-License-Identifier: GPL-2.0 */ 2 | #ifndef _LINUX_CONTAINER_OF_H 3 | #define _LINUX_CONTAINER_OF_H 4 | 5 | #include 6 | #include 7 | 8 | #define typeof_member(T, m) typeof(((T *)0)->m) 9 | 10 | /** 11 | * container_of - cast a member of a structure out to the containing structure 12 | * @ptr: the pointer to the member. 13 | * @type: the type of the container struct this is embedded in. 14 | * @member: the name of the member within the struct. 15 | * 16 | */ 17 | #define container_of(ptr, type, member) \ 18 | ({ \ 19 | void *__mptr = (void *)(ptr); \ 20 | static_assert(__same_type(*(ptr), ((type *)0)->member) || __same_type(*(ptr), void), \ 21 | "pointer type mismatch in container_of()"); \ 22 | ((type *)(__mptr - offsetof(type, member))); \ 23 | }) 24 | 25 | /** 26 | * container_of_safe - cast a member of a structure out to the containing structure 27 | * @ptr: the pointer to the member. 28 | * @type: the type of the container struct this is embedded in. 29 | * @member: the name of the member within the struct. 30 | * 31 | * If IS_ERR_OR_NULL(ptr), ptr is returned unchanged. 32 | */ 33 | #define container_of_safe(ptr, type, member) \ 34 | ({ \ 35 | void *__mptr = (void *)(ptr); \ 36 | static_assert(__same_type(*(ptr), ((type *)0)->member) || __same_type(*(ptr), void), \ 37 | "pointer type mismatch in container_of_safe()"); \ 38 | IS_ERR_OR_NULL(__mptr) ? ERR_CAST(__mptr) : ((type *)(__mptr - offsetof(type, member))); \ 39 | }) 40 | 41 | #endif /* _LINUX_CONTAINER_OF_H */ -------------------------------------------------------------------------------- /kernel/linux/include/linux/cpumask.h: -------------------------------------------------------------------------------- 1 | #ifndef __LINUX_CPUMASK_H 2 | #define __LINUX_CPUMASK_H 3 | 4 | struct cpumask; 5 | 6 | /** 7 | * cpumask_bits - get the bits in a cpumask 8 | * @maskp: the struct cpumask * 9 | * 10 | * You should only assume nr_cpu_ids bits of this mask are valid. This is 11 | * a macro so it's const-correct. 12 | */ 13 | #define cpumask_bits(maskp) ((maskp)->bits) 14 | 15 | #define num_online_cpus() cpumask_weight(cpu_online_mask) 16 | #define num_possible_cpus() cpumask_weight(cpu_possible_mask) 17 | #define num_present_cpus() cpumask_weight(cpu_present_mask) 18 | #define num_active_cpus() cpumask_weight(cpu_active_mask) 19 | #define cpu_online(cpu) cpumask_test_cpu((cpu), cpu_online_mask) 20 | #define cpu_possible(cpu) cpumask_test_cpu((cpu), cpu_possible_mask) 21 | #define cpu_present(cpu) cpumask_test_cpu((cpu), cpu_present_mask) 22 | #define cpu_active(cpu) cpumask_test_cpu((cpu), cpu_active_mask) 23 | 24 | /** 25 | * cpumask_weight - Count of bits in *srcp 26 | * @srcp: the cpumask to count bits (< nr_cpu_ids) in. 27 | */ 28 | static inline unsigned int cpumask_weight(const struct cpumask *srcp) 29 | { 30 | return bitmap_weight(cpumask_bits(srcp), nr_cpumask_bits); 31 | } 32 | 33 | #endif -------------------------------------------------------------------------------- /kernel/linux/include/linux/dcache.h: -------------------------------------------------------------------------------- 1 | /* SPDX-License-Identifier: GPL-2.0 */ 2 | #ifndef __LINUX_DCACHE_H 3 | #define __LINUX_DCACHE_H 4 | 5 | #include 6 | #include 7 | 8 | struct path; 9 | struct vfsmount; 10 | 11 | char *kfunc_def(d_path)(const struct path *path, char *buf, int buflen); 12 | char *kfunc_def(simple_dname)(struct dentry *dentry, char *buffer, int buflen); 13 | char *kfunc_def(dynamic_dname)(struct dentry *dentry, char *buffer, int buflen, const char *fmt, ...); 14 | char *kfunc_def(dentry_path_raw)(struct dentry *dentry, char *buf, int buflen); 15 | char *kfunc_def(dentry_path)(struct dentry *dentry, char *buf, int buflen); 16 | 17 | char *d_path(const struct path *path, char *buf, int buflen); 18 | char *simple_dname(struct dentry *dentry, char *buffer, int buflen); 19 | char *dynamic_dname(struct dentry *dentry, char *buffer, int buflen, const char *fmt, ...); 20 | char *dentry_path_raw(struct dentry *dentry, char *buf, int buflen); 21 | char *dentry_path(struct dentry *dentry, char *buf, int buflen); 22 | 23 | #endif -------------------------------------------------------------------------------- /kernel/linux/include/linux/elf-em.h: -------------------------------------------------------------------------------- 1 | /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ 2 | #ifndef _LINUX_ELF_EM_H 3 | #define _LINUX_ELF_EM_H 4 | 5 | /* These constants define the various ELF target machines */ 6 | #define EM_NONE 0 7 | #define EM_M32 1 8 | #define EM_SPARC 2 9 | #define EM_386 3 10 | #define EM_68K 4 11 | #define EM_88K 5 12 | #define EM_486 6 /* Perhaps disused */ 13 | #define EM_860 7 14 | #define EM_MIPS 8 /* MIPS R3000 (officially, big-endian only) */ 15 | /* Next two are historical and binaries and 16 | modules of these types will be rejected by 17 | Linux. */ 18 | #define EM_MIPS_RS3_LE 10 /* MIPS R3000 little-endian */ 19 | #define EM_MIPS_RS4_BE 10 /* MIPS R4000 big-endian */ 20 | 21 | #define EM_PARISC 15 /* HPPA */ 22 | #define EM_SPARC32PLUS 18 /* Sun's "v8plus" */ 23 | #define EM_PPC 20 /* PowerPC */ 24 | #define EM_PPC64 21 /* PowerPC64 */ 25 | #define EM_SPU 23 /* Cell BE SPU */ 26 | #define EM_ARM 40 /* ARM 32 bit */ 27 | #define EM_SH 42 /* SuperH */ 28 | #define EM_SPARCV9 43 /* SPARC v9 64-bit */ 29 | #define EM_H8_300 46 /* Renesas H8/300 */ 30 | #define EM_IA_64 50 /* HP/Intel IA-64 */ 31 | #define EM_X86_64 62 /* AMD x86-64 */ 32 | #define EM_S390 22 /* IBM S/390 */ 33 | #define EM_CRIS 76 /* Axis Communications 32-bit embedded processor */ 34 | #define EM_M32R 88 /* Renesas M32R */ 35 | #define EM_MN10300 89 /* Panasonic/MEI MN10300, AM33 */ 36 | #define EM_OPENRISC 92 /* OpenRISC 32-bit embedded processor */ 37 | #define EM_ARCOMPACT 93 /* ARCompact processor */ 38 | #define EM_XTENSA 94 /* Tensilica Xtensa Architecture */ 39 | #define EM_BLACKFIN 106 /* ADI Blackfin Processor */ 40 | #define EM_UNICORE 110 /* UniCore-32 */ 41 | #define EM_ALTERA_NIOS2 113 /* Altera Nios II soft-core processor */ 42 | #define EM_TI_C6000 140 /* TI C6X DSPs */ 43 | #define EM_HEXAGON 164 /* QUALCOMM Hexagon */ 44 | #define EM_NDS32 \ 45 | 167 /* Andes Technology compact code size 46 | embedded RISC processor family */ 47 | #define EM_AARCH64 183 /* ARM 64 bit */ 48 | #define EM_TILEPRO 188 /* Tilera TILEPro */ 49 | #define EM_MICROBLAZE 189 /* Xilinx MicroBlaze */ 50 | #define EM_TILEGX 191 /* Tilera TILE-Gx */ 51 | #define EM_ARCV2 195 /* ARCv2 Cores */ 52 | #define EM_RISCV 243 /* RISC-V */ 53 | #define EM_BPF 247 /* Linux BPF - in-kernel virtual machine */ 54 | #define EM_CSKY 252 /* C-SKY */ 55 | #define EM_FRV 0x5441 /* Fujitsu FR-V */ 56 | 57 | /* 58 | * This is an interim value that we will use until the committee comes 59 | * up with a final number. 60 | */ 61 | #define EM_ALPHA 0x9026 62 | 63 | /* Bogus old m32r magic number, used by old tools. */ 64 | #define EM_CYGNUS_M32R 0x9041 65 | /* This is the old interim value for S/390 architecture */ 66 | #define EM_S390_OLD 0xA390 67 | /* Also Panasonic/MEI MN10300, AM33 */ 68 | #define EM_CYGNUS_MN10300 0xbeef 69 | 70 | #endif /* _LINUX_ELF_EM_H */ 71 | -------------------------------------------------------------------------------- /kernel/linux/include/linux/err.h: -------------------------------------------------------------------------------- 1 | #ifndef _LINUX_ERR_H 2 | #define _LINUX_ERR_H 3 | 4 | #include 5 | #include 6 | 7 | #define MAX_ERRNO 4095 8 | 9 | #define IS_ERR_VALUE(x) unlikely((unsigned long)(void *)(x) >= (unsigned long)-MAX_ERRNO) 10 | 11 | static inline void *__must_check ERR_PTR(long error) 12 | { 13 | return (void *)error; 14 | } 15 | 16 | static inline long __must_check PTR_ERR(__force const void *ptr) 17 | { 18 | return (long)ptr; 19 | } 20 | 21 | static inline bool __must_check IS_ERR(__force const void *ptr) 22 | { 23 | return IS_ERR_VALUE((unsigned long)ptr); 24 | } 25 | 26 | static inline bool __must_check IS_ERR_OR_NULL(__force const void *ptr) 27 | { 28 | return unlikely(!ptr) || IS_ERR_VALUE((unsigned long)ptr); 29 | } 30 | 31 | static inline int __must_check PTR_ERR_OR_ZERO(__force const void *ptr) 32 | { 33 | if (IS_ERR(ptr)) 34 | return PTR_ERR(ptr); 35 | else 36 | return 0; 37 | } 38 | 39 | #endif -------------------------------------------------------------------------------- /kernel/linux/include/linux/errno.h: -------------------------------------------------------------------------------- 1 | #ifndef _LINUX_ERRNO_H 2 | #define _LINUX_ERRNO_H 3 | 4 | #include 5 | 6 | #define ERESTARTSYS 512 7 | #define ERESTARTNOINTR 513 8 | #define ERESTARTNOHAND 514 /* restart if no handler.. */ 9 | #define ENOIOCTLCMD 515 /* No ioctl command */ 10 | #define ERESTART_RESTARTBLOCK 516 /* restart by calling sys_restart_syscall */ 11 | #define EPROBE_DEFER 517 /* Driver requests probe retry */ 12 | #define EOPENSTALE 518 /* open found a stale dentry */ 13 | #define ENOPARAM 519 /* Parameter not supported */ 14 | 15 | /* Defined for the NFSv3 protocol */ 16 | #define EBADHANDLE 521 /* Illegal NFS file handle */ 17 | #define ENOTSYNC 522 /* Update synchronization mismatch */ 18 | #define EBADCOOKIE 523 /* Cookie is stale */ 19 | #define ENOTSUPP 524 /* Operation is not supported */ 20 | #define ETOOSMALL 525 /* Buffer or request is too small */ 21 | #define ESERVERFAULT 526 /* An untranslatable error occurred */ 22 | #define EBADTYPE 527 /* Type not supported by server */ 23 | #define EJUKEBOX 528 /* Request initiated, but will not complete before timeout */ 24 | #define EIOCBQUEUED 529 /* iocb queued, will get completion event */ 25 | #define ERECALLCONFLICT 530 /* conflict with recalled state */ 26 | #define ENOGRACE 27 | 28 | #endif -------------------------------------------------------------------------------- /kernel/linux/include/linux/gfp.h: -------------------------------------------------------------------------------- 1 | #ifndef __LINUX_GFP_H 2 | #define __LINUX_GFP_H 3 | 4 | #include 5 | 6 | /* Plain integer GFP bitmasks. Do not use this directly. */ 7 | // #define __GFP_DMA 0x01u 8 | // #define __GFP_HIGHMEM 0x02u 9 | // #define __GFP_DMA32 0x04u 10 | // #define __GFP_MOVABLE 0x08u 11 | // #define __GFP_RECLAIMABLE 0x10u 12 | // #define __GFP_HIGH 0x20u 13 | // #define __GFP_IO 0x40u 14 | // #define __GFP_FS 0x80u 15 | // #define __GFP_ZERO 0x100u 16 | // #define __GFP_ATOMIC 0x200u 17 | // #define __GFP_DIRECT_RECLAIM 0x400u 18 | // #define __GFP_KSWAPD_RECLAIM 0x800u 19 | // #define __GFP_WRITE 0x1000u 20 | // #define __GFP_NOWARN 0x2000u 21 | // #define __GFP_RETRY_MAYFAIL 0x4000u 22 | // #define __GFP_NOFAIL 0x8000u 23 | // #define __GFP_NORETRY 0x10000u 24 | // #define __GFP_MEMALLOC 0x20000u 25 | // #define __GFP_COMP 0x40000u 26 | // #define __GFP_NOMEMALLOC 0x80000u 27 | // #define __GFP_HARDWALL 0x100000u 28 | // #define __GFP_THISNODE 0x200000u 29 | // #define __GFP_ACCOUNT 0x400000u 30 | // #define __GFP_NOLOCKDEP 0x800000u 31 | 32 | // static inline get_gfp_atomic() 33 | // { 34 | // if (kver >= VERSION(3, 18, 0)) return __GFP_HIGH; 35 | 36 | // } 37 | 38 | // #define __GFP_RECLAIM ((__force gfp_t)(__GFP_DIRECT_RECLAIM | __GFP_KSWAPD_RECLAIM)) 39 | 40 | // #define GFP_ATOMIC (__GFP_HIGH | __GFP_ATOMIC | __GFP_KSWAPD_RECLAIM) 41 | // #define GFP_KERNEL (__GFP_RECLAIM | __GFP_IO | __GFP_FS) 42 | 43 | // #define GFP_KERNEL_ACCOUNT (GFP_KERNEL | __GFP_ACCOUNT) 44 | // #define GFP_NOWAIT (__GFP_KSWAPD_RECLAIM) 45 | // #define GFP_NOIO (__GFP_RECLAIM) 46 | // #define GFP_NOFS (__GFP_RECLAIM | __GFP_IO) 47 | // #define GFP_USER (__GFP_RECLAIM | __GFP_IO | __GFP_FS | __GFP_HARDWALL) 48 | // #define GFP_DMA __GFP_DMA 49 | // #define GFP_DMA32 __GFP_DMA32 50 | // #define GFP_HIGHUSER (GFP_USER | __GFP_HIGHMEM) 51 | // #define GFP_HIGHUSER_MOVABLE (GFP_HIGHUSER | __GFP_MOVABLE) 52 | // #define GFP_TRANSHUGE_LIGHT ((GFP_HIGHUSER_MOVABLE | __GFP_COMP | __GFP_NOMEMALLOC | __GFP_NOWARN) & ~__GFP_RECLAIM) 53 | // #define GFP_TRANSHUGE (GFP_TRANSHUGE_LIGHT | __GFP_DIRECT_RECLAIM) 54 | 55 | /* Convert GFP flags to their corresponding migrate type */ 56 | // #define GFP_MOVABLE_MASK (__GFP_RECLAIMABLE | __GFP_MOVABLE) 57 | // #define GFP_MOVABLE_SHIFT 3 58 | 59 | #endif -------------------------------------------------------------------------------- /kernel/linux/include/linux/include/linux/smp.h: -------------------------------------------------------------------------------- 1 | #ifndef __LINUX_SMP_H 2 | #define __LINUX_SMP_H 3 | 4 | typedef void (*smp_call_func_t)(void *info); 5 | 6 | void kick_all_cpus_sync(void); 7 | void wake_up_all_idle_cpus(void); 8 | 9 | #endif -------------------------------------------------------------------------------- /kernel/linux/include/linux/init_task.h: -------------------------------------------------------------------------------- 1 | #ifndef _LINUX_INIT_TASK_H 2 | #define _LINUX_INIT_TASK_H 3 | 4 | extern struct task_struct *init_task; 5 | extern const struct cred *init_cred; 6 | 7 | #endif -------------------------------------------------------------------------------- /kernel/linux/include/linux/kallsyms.h: -------------------------------------------------------------------------------- 1 | #ifndef _LINUX_KALLSYMS_H 2 | #define _LINUX_KALLSYMS_H 3 | 4 | #include 5 | 6 | #endif -------------------------------------------------------------------------------- /kernel/linux/include/linux/kern_levels.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERN_LEVELS_H__ 2 | #define __KERN_LEVELS_H__ 3 | 4 | #define KERN_SOH "\001" /* ASCII Start Of Header */ 5 | #define KERN_SOH_ASCII '\001' 6 | 7 | #define KERN_EMERG KERN_SOH "0" /* system is unusable */ 8 | #define KERN_ALERT KERN_SOH "1" /* action must be taken immediately */ 9 | #define KERN_CRIT KERN_SOH "2" /* critical conditions */ 10 | #define KERN_ERR KERN_SOH "3" /* error conditions */ 11 | #define KERN_WARNING KERN_SOH "4" /* warning conditions */ 12 | #define KERN_NOTICE KERN_SOH "5" /* normal but significant condition */ 13 | #define KERN_INFO KERN_SOH "6" /* informational */ 14 | #define KERN_DEBUG KERN_SOH "7" /* debug-level messages */ 15 | 16 | #define KERN_DEFAULT "" /* the default kernel loglevel */ 17 | 18 | /* 19 | * Annotation for a "continued" line of log printout (only done after a 20 | * line that had no enclosing \n). Only to be used by core/arch code 21 | * during early bootup (a continued line is not SMP-safe otherwise). 22 | */ 23 | #define KERN_CONT KERN_SOH "c" 24 | 25 | /* integer equivalents of KERN_ */ 26 | #define LOGLEVEL_SCHED \ 27 | -2 /* Deferred messages from sched code 28 | * are set to this special level */ 29 | #define LOGLEVEL_DEFAULT -1 /* default (or last) loglevel */ 30 | #define LOGLEVEL_EMERG 0 /* system is unusable */ 31 | #define LOGLEVEL_ALERT 1 /* action must be taken immediately */ 32 | #define LOGLEVEL_CRIT 2 /* critical conditions */ 33 | #define LOGLEVEL_ERR 3 /* error conditions */ 34 | #define LOGLEVEL_WARNING 4 /* warning conditions */ 35 | #define LOGLEVEL_NOTICE 5 /* normal but significant condition */ 36 | #define LOGLEVEL_INFO 6 /* informational */ 37 | #define LOGLEVEL_DEBUG 7 /* debug-level messages */ 38 | 39 | #endif -------------------------------------------------------------------------------- /kernel/linux/include/linux/kernel.h: -------------------------------------------------------------------------------- 1 | #ifndef _LINUX_KERNEL_H 2 | #define _LINUX_KERNEL_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | extern int kfunc_def(sprintf)(char *buf, const char *fmt, ...); 9 | extern int kfunc_def(vsprintf)(char *buf, const char *fmt, va_list args); 10 | extern int kfunc_def(snprintf)(char *buf, size_t size, const char *fmt, ...); 11 | extern int kfunc_def(vsnprintf)(char *buf, size_t size, const char *fmt, va_list args); 12 | extern int kfunc_def(scnprintf)(char *buf, size_t size, const char *fmt, ...); 13 | extern int kfunc_def(vscnprintf)(char *buf, size_t size, const char *fmt, va_list args); 14 | extern char *kfunc_def(kasprintf)(gfp_t gfp, const char *fmt, ...); 15 | extern char *kfunc_def(kvasprintf)(gfp_t gfp, const char *fmt, va_list args); 16 | extern int kfunc_def(sscanf)(const char *buf, const char *fmt, ...); 17 | extern int kfunc_def(vsscanf)(const char *buf, const char *fmt, va_list args); 18 | 19 | #define sprintf(buf, fmt, ...) kfunc(sprintf)(buf, fmt, ##__VA_ARGS__) 20 | #define snprintf(buf, size, fmt, ...) kfunc(snprintf)(buf, size, fmt, ##__VA_ARGS__) 21 | #define scnprintf(buf, size, fmt, ...) kfunc(scnprintf)(buf, size, fmt, ##__VA_ARGS__) 22 | #define kasprintf(buf, fmt, ...) kfunc(kasprintf)(buf, fmt, ##__VA_ARGS__) 23 | #define sscanf(buf, fmt, ...) kfunc(sscanf)(buf, fmt, ##__VA_ARGS__) 24 | 25 | static inline int vsprintf(char *buf, const char *fmt, va_list args) 26 | { 27 | kfunc_direct_call(vsprintf, buf, fmt, args); 28 | } 29 | 30 | static inline int vsnprintf(char *buf, size_t size, const char *fmt, va_list args) 31 | { 32 | kfunc_direct_call(vsnprintf, buf, size, fmt, args); 33 | } 34 | 35 | static inline int vscnprintf(char *buf, size_t size, const char *fmt, va_list args) 36 | { 37 | kfunc_direct_call(vscnprintf, buf, size, fmt, args); 38 | } 39 | 40 | static inline char *kvasprintf(gfp_t gfp, const char *fmt, va_list args) 41 | { 42 | kfunc_direct_call(kvasprintf, gfp, fmt, args); 43 | } 44 | 45 | static inline int vsscanf(const char *buf, const char *fmt, va_list args) 46 | { 47 | kfunc_direct_call(vsscanf, buf, fmt, args); 48 | } 49 | 50 | #endif -------------------------------------------------------------------------------- /kernel/linux/include/linux/llist.h: -------------------------------------------------------------------------------- 1 | /* SPDX-License-Identifier: GPL-2.0-only */ 2 | #ifndef LLIST_H 3 | #define LLIST_H 4 | 5 | #include 6 | 7 | struct llist_head 8 | { 9 | struct llist_node *first; 10 | }; 11 | 12 | struct llist_node 13 | { 14 | struct llist_node *next; 15 | }; 16 | 17 | #define LLIST_HEAD_INIT(name) \ 18 | { \ 19 | NULL \ 20 | } 21 | #define LLIST_HEAD(name) struct llist_head name = LLIST_HEAD_INIT(name) 22 | 23 | static inline void init_llist_head(struct llist_head *list) 24 | { 25 | list->first = NULL; 26 | } 27 | 28 | #endif -------------------------------------------------------------------------------- /kernel/linux/include/linux/lockdep.h: -------------------------------------------------------------------------------- 1 | /* SPDX-License-Identifier: GPL-2.0 */ 2 | /* 3 | * Runtime locking correctness validator 4 | * 5 | * Copyright (C) 2006,2007 Red Hat, Inc., Ingo Molnar 6 | * Copyright (C) 2007 Red Hat, Inc., Peter Zijlstra 7 | * 8 | * see Documentation/locking/lockdep-design.rst for more details. 9 | */ 10 | #ifndef __LINUX_LOCKDEP_H 11 | #define __LINUX_LOCKDEP_H 12 | 13 | #include 14 | 15 | struct lockdep_map; 16 | 17 | /* 18 | * Acquire a lock. 19 | * 20 | * Values for "read": 21 | * 22 | * 0: exclusive (write) acquire 23 | * 1: read-acquire (no recursion allowed) 24 | * 2: read-acquire with same-instance recursion allowed 25 | * 26 | * Values for check: 27 | * 28 | * 0: simple checks (freeing, held-at-exit-time, etc.) 29 | * 1: full validation 30 | */ 31 | extern void kfunc_def(lock_acquire)(struct lockdep_map *lock, unsigned int subclass, int trylock, int read, int check, 32 | struct lockdep_map *nest_lock, unsigned long ip); 33 | extern void kfunc_def(lock_release)(struct lockdep_map *lock, unsigned long ip); 34 | extern void kfunc_def(lock_sync)(struct lockdep_map *lock, unsigned int subclass, int read, int check, 35 | struct lockdep_map *nest_lock, unsigned long ip); 36 | 37 | /* lock_is_held_type() returns */ 38 | #define LOCK_STATE_UNKNOWN -1 39 | #define LOCK_STATE_NOT_HELD 0 40 | #define LOCK_STATE_HELD 1 41 | 42 | #define lockdep_is_held(lock) lock_is_held(&(lock)->dep_map) 43 | #define lockdep_is_held_type(lock, r) lock_is_held_type(&(lock)->dep_map, (r)) 44 | 45 | #define lock_set_novalidate_class(l, n, i) lock_set_class(l, n, &__lockdep_no_validate__, 0, i) 46 | 47 | #define NIL_COOKIE \ 48 | (struct pin_cookie) \ 49 | { \ 50 | .val = 0U, \ 51 | } 52 | 53 | static inline void lock_acquire(struct lockdep_map *lock, unsigned int subclass, int trylock, int read, int check, 54 | struct lockdep_map *nest_lock, unsigned long ip) 55 | { 56 | kfunc_call_void(lock_acquire, lock, subclass, trylock, read, check, nest_lock, ip); 57 | } 58 | 59 | static inline void lock_release(struct lockdep_map *lock, unsigned long ip) 60 | { 61 | kfunc_call_void(lock_release, lock, ip); 62 | } 63 | 64 | static inline void lock_sync(struct lockdep_map *lock, unsigned int subclass, int read, int check, 65 | struct lockdep_map *nest_lock, unsigned long ip) 66 | { 67 | kfunc_call_void(lock_sync, lock, subclass, read, check, nest_lock, ip); 68 | } 69 | 70 | #endif -------------------------------------------------------------------------------- /kernel/linux/include/linux/mm.h: -------------------------------------------------------------------------------- 1 | #ifndef _LINUX_MM_H 2 | #define _LINUX_MM_H 3 | 4 | #endif -------------------------------------------------------------------------------- /kernel/linux/include/linux/panic.h: -------------------------------------------------------------------------------- 1 | /* SPDX-License-Identifier: GPL-2.0 */ 2 | #ifndef _LINUX_PANIC_H 3 | #define _LINUX_PANIC_H 4 | 5 | #include 6 | #include 7 | 8 | extern void kfunc_def(panic)(const char *fmt, ...) __noreturn __cold; 9 | 10 | #define panic(fmt, ...) kfunc(panic)(fmt, ##__VA_ARGS__) 11 | 12 | #define panic_kfunc_unexpected() panci("%s", __func__) 13 | 14 | #endif -------------------------------------------------------------------------------- /kernel/linux/include/linux/poison.h: -------------------------------------------------------------------------------- 1 | /* SPDX-License-Identifier: GPL-2.0 */ 2 | #ifndef _LINUX_POISON_H 3 | #define _LINUX_POISON_H 4 | 5 | /********** include/linux/list.h **********/ 6 | 7 | /* 8 | * Architectures might want to move the poison pointer offset 9 | * into some well-recognized area such as 0xdead000000000000, 10 | * that is also not mappable by user-space exploits: 11 | */ 12 | 13 | #define CONFIG_ILLEGAL_POINTER_VALUE 0xdead000000000000 14 | 15 | #ifdef CONFIG_ILLEGAL_POINTER_VALUE 16 | #define POISON_POINTER_DELTA _AC(CONFIG_ILLEGAL_POINTER_VALUE, UL) 17 | #else 18 | #define POISON_POINTER_DELTA 0 19 | #endif 20 | 21 | /* 22 | * These are non-NULL pointers that will result in page faults 23 | * under normal circumstances, used to verify that nobody uses 24 | * non-initialized list entries. 25 | */ 26 | #define LIST_POISON1 ((void *)0x100 + POISON_POINTER_DELTA) 27 | #define LIST_POISON2 ((void *)0x122 + POISON_POINTER_DELTA) 28 | 29 | /********** include/linux/timer.h **********/ 30 | #define TIMER_ENTRY_STATIC ((void *)0x300 + POISON_POINTER_DELTA) 31 | 32 | /********** mm/page_poison.c **********/ 33 | #define PAGE_POISON 0xaa 34 | 35 | /********** mm/page_alloc.c ************/ 36 | 37 | #define TAIL_MAPPING ((void *)0x400 + POISON_POINTER_DELTA) 38 | 39 | /********** mm/slab.c **********/ 40 | /* 41 | * Magic nums for obj red zoning. 42 | * Placed in the first word before and the first word after an obj. 43 | */ 44 | #define RED_INACTIVE 0x09F911029D74E35BULL /* when obj is inactive */ 45 | #define RED_ACTIVE 0xD84156C5635688C0ULL /* when obj is active */ 46 | 47 | #define SLUB_RED_INACTIVE 0xbb 48 | #define SLUB_RED_ACTIVE 0xcc 49 | 50 | /* ...and for poisoning */ 51 | #define POISON_INUSE 0x5a /* for use-uninitialised poisoning */ 52 | #define POISON_FREE 0x6b /* for use-after-free poisoning */ 53 | #define POISON_END 0xa5 /* end-byte of poisoning */ 54 | 55 | /********** arch/$ARCH/mm/init.c **********/ 56 | #define POISON_FREE_INITMEM 0xcc 57 | 58 | /********** arch/ia64/hp/common/sba_iommu.c **********/ 59 | /* 60 | * arch/ia64/hp/common/sba_iommu.c uses a 16-byte poison string with a 61 | * value of "SBAIOMMU POISON\0" for spill-over poisoning. 62 | */ 63 | 64 | /********** fs/jbd/journal.c **********/ 65 | #define JBD_POISON_FREE 0x5b 66 | #define JBD2_POISON_FREE 0x5c 67 | 68 | /********** drivers/base/dmapool.c **********/ 69 | #define POOL_POISON_FREED 0xa7 /* !inuse */ 70 | #define POOL_POISON_ALLOCATED 0xa9 /* !initted */ 71 | 72 | /********** drivers/atm/ **********/ 73 | #define ATM_POISON_FREE 0x12 74 | #define ATM_POISON 0xdeadbeef 75 | 76 | /********** kernel/mutexes **********/ 77 | #define MUTEX_DEBUG_INIT 0x11 78 | #define MUTEX_DEBUG_FREE 0x22 79 | #define MUTEX_POISON_WW_CTX ((void *)0x500 + POISON_POINTER_DELTA) 80 | 81 | /********** security/ **********/ 82 | #define KEY_DESTROY 0xbd 83 | 84 | /********** net/core/page_pool.c **********/ 85 | #define PP_SIGNATURE (0x40 + POISON_POINTER_DELTA) 86 | 87 | #endif 88 | -------------------------------------------------------------------------------- /kernel/linux/include/linux/preempt.h: -------------------------------------------------------------------------------- 1 | #ifndef __LINUX_PREEMPT_H 2 | #define __LINUX_PREEMPT_H 3 | 4 | #include 5 | 6 | /* 7 | * We put the hardirq and softirq counter into the preemption 8 | * counter. The bitmask has the following meaning: 9 | * 10 | * - bits 0-7 are the preemption count (max preemption depth: 256) 11 | * - bits 8-15 are the softirq count (max # of softirqs: 256) 12 | * 13 | * The hardirq count could in theory be the same as the number of 14 | * interrupts in the system, but we run all interrupt handlers with 15 | * interrupts disabled, so we cannot have nesting interrupts. Though 16 | * there are a few palaeontologic drivers which reenable interrupts in 17 | * the handler, so we need more than one bit here. 18 | * 19 | * PREEMPT_MASK: 0x000000ff 20 | * SOFTIRQ_MASK: 0x0000ff00 21 | * HARDIRQ_MASK: 0x000f0000 22 | * NMI_MASK: 0x00f00000 23 | * PREEMPT_NEED_RESCHED: 0x80000000 24 | */ 25 | #define PREEMPT_BITS 8 26 | #define SOFTIRQ_BITS 8 27 | #define HARDIRQ_BITS 4 28 | #define NMI_BITS 4 29 | 30 | #define PREEMPT_SHIFT 0 31 | #define SOFTIRQ_SHIFT (PREEMPT_SHIFT + PREEMPT_BITS) 32 | #define HARDIRQ_SHIFT (SOFTIRQ_SHIFT + SOFTIRQ_BITS) 33 | #define NMI_SHIFT (HARDIRQ_SHIFT + HARDIRQ_BITS) 34 | 35 | #define __IRQ_MASK(x) ((1UL << (x)) - 1) 36 | 37 | #define PREEMPT_MASK (__IRQ_MASK(PREEMPT_BITS) << PREEMPT_SHIFT) 38 | #define SOFTIRQ_MASK (__IRQ_MASK(SOFTIRQ_BITS) << SOFTIRQ_SHIFT) 39 | #define HARDIRQ_MASK (__IRQ_MASK(HARDIRQ_BITS) << HARDIRQ_SHIFT) 40 | #define NMI_MASK (__IRQ_MASK(NMI_BITS) << NMI_SHIFT) 41 | 42 | #define PREEMPT_OFFSET (1UL << PREEMPT_SHIFT) 43 | #define SOFTIRQ_OFFSET (1UL << SOFTIRQ_SHIFT) 44 | #define HARDIRQ_OFFSET (1UL << HARDIRQ_SHIFT) 45 | #define NMI_OFFSET (1UL << NMI_SHIFT) 46 | 47 | #define SOFTIRQ_DISABLE_OFFSET (2 * SOFTIRQ_OFFSET) 48 | 49 | #define PREEMPT_DISABLED (PREEMPT_DISABLE_OFFSET + PREEMPT_ENABLED) 50 | 51 | /* 52 | * Disable preemption until the scheduler is running -- use an unconditional 53 | * value so that it also works on !PREEMPT_COUNT kernels. 54 | * 55 | * Reset by start_kernel()->sched_init()->init_idle()->init_idle_preempt_count(). 56 | */ 57 | #define INIT_PREEMPT_COUNT PREEMPT_OFFSET 58 | 59 | /* 60 | * Initial preempt_count value; reflects the preempt_count schedule invariant 61 | * which states that during context switches: 62 | * 63 | * preempt_count() == 2*PREEMPT_DISABLE_OFFSET 64 | * 65 | * Note: PREEMPT_DISABLE_OFFSET is 0 for !PREEMPT_COUNT kernels. 66 | * Note: See finish_task_switch(). 67 | */ 68 | #define FORK_PREEMPT_COUNT (2 * PREEMPT_DISABLE_OFFSET + PREEMPT_ENABLED) 69 | 70 | #define preempt_count_add(val) __preempt_count_add(val) 71 | #define preempt_count_sub(val) __preempt_count_sub(val) 72 | #define preempt_count_dec_and_test() __preempt_count_dec_and_test() 73 | 74 | #define __preempt_count_inc() __preempt_count_add(1) 75 | #define __preempt_count_dec() __preempt_count_sub(1) 76 | 77 | #define preempt_count_inc() preempt_count_add(1) 78 | #define preempt_count_dec() preempt_count_sub(1) 79 | 80 | #define preempt_disable() \ 81 | do { \ 82 | preempt_count_inc(); \ 83 | barrier(); \ 84 | } while (0) 85 | 86 | #define sched_preempt_enable_no_resched() \ 87 | do { \ 88 | barrier(); \ 89 | preempt_count_dec(); \ 90 | } while (0) 91 | 92 | #define preempt_enable_no_resched() sched_preempt_enable_no_resched() 93 | 94 | #define preemptible() (preempt_count() == 0 && !irqs_disabled()) 95 | 96 | #endif -------------------------------------------------------------------------------- /kernel/linux/include/linux/printk.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERNEL_PRINTK__ 2 | #define __KERNEL_PRINTK__ 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | // extern int vprintk_emit(int facility, int level, const struct dev_printk_info *dev_info, const char *fmt, va_list args); 9 | // extern int vprintk(const char *fmt, va_list args); 10 | // extern int printk(const char *fmt, ...); 11 | // extern int printk_deferred(const char *fmt, ...); 12 | 13 | extern void kfunc_def(dump_stack_lvl)(const char *log_lvl) __cold; 14 | extern void kfunc_def(dump_stack)(void) __cold; 15 | 16 | extern int __printk_ratelimit(const char *func); 17 | #define printk_ratelimit() __printk_ratelimit(__func__) 18 | extern bool printk_timed_ratelimit(unsigned long *caller_jiffies, unsigned int interval_msec); 19 | 20 | extern int printk_delay_msec; 21 | extern int dmesg_restrict; 22 | 23 | struct ctl_table; 24 | 25 | extern int devkmsg_sysctl_set_loglvl(struct ctl_table *table, int write, void *buf, size_t *lenp, loff_t *ppos); 26 | 27 | extern void wake_up_klogd(void); 28 | 29 | extern void printk_safe_flush(void); 30 | extern void printk_safe_flush_on_panic(void); 31 | 32 | extern int kptr_restrict; 33 | 34 | #define pr_fmt(fmt) fmt 35 | #define pr_emerg(fmt, ...) printk(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__) 36 | #define pr_alert(fmt, ...) printk(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__) 37 | #define pr_crit(fmt, ...) printk(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__) 38 | #define pr_err(fmt, ...) printk(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__) 39 | #define pr_warn(fmt, ...) printk(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__) 40 | #define pr_notice(fmt, ...) printk(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__) 41 | #define pr_info(fmt, ...) printk(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__) 42 | #define pr_cont(fmt, ...) printk(KERN_CONT fmt, ##__VA_ARGS__) 43 | 44 | static inline void dump_stack_lvl(const char *log_lvl) 45 | { 46 | kfunc_call(dump_stack_lvl, log_lvl); 47 | kfunc_not_found(); 48 | } 49 | 50 | static inline void dump_stack(void) 51 | { 52 | kfunc_direct_call(dump_stack); 53 | } 54 | 55 | #endif -------------------------------------------------------------------------------- /kernel/linux/include/linux/ptrace.h: -------------------------------------------------------------------------------- 1 | /* SPDX-License-Identifier: GPL-2.0 */ 2 | #ifndef _LINUX_PTRACE_H 3 | #define _LINUX_PTRACE_H 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | #define current_pt_regs() task_pt_regs(current) 10 | 11 | #define current_user_stack_pointer() user_stack_pointer(current_pt_regs()) 12 | 13 | #endif -------------------------------------------------------------------------------- /kernel/linux/include/linux/random.h: -------------------------------------------------------------------------------- 1 | /* SPDX-License-Identifier: GPL-2.0 */ 2 | /* 3 | * include/linux/random.h 4 | * 5 | * Include file for the random number generator. 6 | */ 7 | #ifndef _LINUX_RANDOM_H 8 | #define _LINUX_RANDOM_H 9 | 10 | #include 11 | #include 12 | 13 | extern void kfunc_def(get_random_bytes)(void *buf, int nbytes); 14 | extern uint64_t kfunc_def(get_random_u64)(void); 15 | extern uint64_t kfunc_def(get_random_long)(void); 16 | 17 | #endif -------------------------------------------------------------------------------- /kernel/linux/include/linux/rwlock.h: -------------------------------------------------------------------------------- 1 | #ifndef __LINUX_RWLOCK_H 2 | #define __LINUX_RWLOCK_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | // todo: arch, enough size 9 | typedef struct 10 | { 11 | volatile unsigned int lock; 12 | } arch_rwlock_t; 13 | 14 | typedef struct 15 | { 16 | arch_rwlock_t raw_lock; 17 | } rwlock_t; 18 | 19 | #define __RW_LOCK_UNLOCKED() \ 20 | (rwlock_t) \ 21 | { \ 22 | .raw_lock = { 0, 0 } \ 23 | } 24 | 25 | #define DEFINE_RWLOCK(x) rwlock_t x = __RW_LOCK_UNLOCKED() 26 | 27 | #define rwlock_init(_lockp) \ 28 | do { \ 29 | *(_lockp) = __RW_LOCK_UNLOCKED() \ 30 | } while (0); 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /kernel/linux/include/linux/sched/mm.h: -------------------------------------------------------------------------------- 1 | /* SPDX-License-Identifier: GPL-2.0 */ 2 | #ifndef _LINUX_SCHED_MM_H 3 | #define _LINUX_SCHED_MM_H 4 | 5 | #include 6 | #include 7 | 8 | /* mmput gets rid of the mappings and all user-space */ 9 | extern void kfunc_def(mmput)(struct mm_struct *); 10 | 11 | /* same as above but performs the slow path from the async context. Can 12 | * be called from the atomic context as well 13 | */ 14 | extern void kfunc_def(mmput_async)(struct mm_struct *); 15 | 16 | /* Grab a reference to a task's mm, if it is not already going away */ 17 | extern struct mm_struct *kfunc_def(get_task_mm)(struct task_struct *task); 18 | 19 | static inline void mmput(struct mm_struct *mm) 20 | { 21 | kfunc_direct_call_void(mmput, mm); 22 | } 23 | 24 | static inline void mmput_async(struct mm_struct *mm) 25 | { 26 | kfunc_direct_call_void(mmput_async, mm); 27 | } 28 | 29 | static inline struct mm_struct *get_task_mm(struct task_struct *task) 30 | { 31 | kfunc_direct_call(get_task_mm, task); 32 | } 33 | 34 | #endif -------------------------------------------------------------------------------- /kernel/linux/include/linux/seccomp.h: -------------------------------------------------------------------------------- 1 | #ifndef _LINUX_SECCOMP_H 2 | #define _LINUX_SECCOMP_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | struct seccomp_filter; 9 | /** 10 | * struct seccomp - the state of a seccomp'ed process 11 | * 12 | * @mode: indicates one of the valid values above for controlled 13 | * system calls available to a process. 14 | * @filter: must always point to a valid seccomp-filter or NULL as it is 15 | * accessed without locking during system call entry. 16 | * 17 | * @filter must only be accessed from the context of current as there 18 | * is no read locking. 19 | */ 20 | struct seccomp 21 | { 22 | int mode; 23 | atomic_t filter_count; 24 | struct seccomp_filter *filter; 25 | }; 26 | 27 | // struct seccomp 28 | // { 29 | // int mode; 30 | // struct seccomp_filter *filter; 31 | // }; 32 | 33 | extern long kfunc_def(prctl_get_seccomp)(void); 34 | extern long kfunc_def(prctl_set_seccomp)(unsigned long seccomp_mode, char __user *filter); 35 | 36 | extern void kfunc_def(put_seccomp_filter)(struct task_struct *tsk); 37 | extern void kfunc_def(get_seccomp_filter)(struct task_struct *tsk); 38 | 39 | // #ifdef CONFIG_SECCOMP_FILTER 40 | extern void kfunc_def(seccomp_filter_release)(struct task_struct *tsk); 41 | extern void kfunc_def(get_seccomp_filter)(struct task_struct *tsk); 42 | // #else /* CONFIG_SECCOMP_FILTER */ 43 | 44 | static inline long prctl_get_seccomp(void) 45 | { 46 | kfunc_direct_call(prctl_get_seccomp); 47 | } 48 | 49 | static inline long prctl_set_seccomp(unsigned long seccomp_mode, char __user *filter) 50 | { 51 | kfunc_direct_call(prctl_set_seccomp, seccomp_mode, filter); 52 | } 53 | 54 | static inline void put_seccomp_filter(struct task_struct *tsk) 55 | { 56 | kfunc_direct_call(put_seccomp_filter, tsk); 57 | } 58 | 59 | static inline void get_seccomp_filter(struct task_struct *tsk) 60 | { 61 | kfunc_direct_call(get_seccomp_filter, tsk); 62 | } 63 | 64 | static inline void seccomp_filter_release(struct task_struct *tsk) 65 | { 66 | kfunc_call_void(seccomp_filter_release, tsk); 67 | } 68 | 69 | #endif -------------------------------------------------------------------------------- /kernel/linux/include/linux/seq_buf.h: -------------------------------------------------------------------------------- 1 | /* SPDX-License-Identifier: GPL-2.0 */ 2 | #ifndef _LINUX_SEQ_BUF_H 3 | #define _LINUX_SEQ_BUF_H 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | struct seq_buf 11 | { 12 | char *buffer; 13 | size_t size; 14 | size_t len; 15 | loff_t readpos; 16 | }; 17 | 18 | static inline void seq_buf_clear(struct seq_buf *s) 19 | { 20 | s->len = 0; 21 | s->readpos = 0; 22 | } 23 | 24 | extern int kfunc_def(seq_buf_printf)(struct seq_buf *s, const char *fmt, ...); 25 | extern int kfunc_def(seq_buf_to_user)(struct seq_buf *s, char __user *ubuf, int cnt); 26 | extern int kfunc_def(seq_buf_puts)(struct seq_buf *s, const char *str); 27 | extern int kfunc_def(seq_buf_putc)(struct seq_buf *s, unsigned char c); 28 | extern int kfunc_def(seq_buf_putmem)(struct seq_buf *s, const void *mem, unsigned int len); 29 | extern int kfunc_def(seq_buf_putmem_hex)(struct seq_buf *s, const void *mem, unsigned int len); 30 | extern int kfunc_def(seq_buf_bitmask)(struct seq_buf *s, const unsigned long *maskp, int nmaskbits); 31 | 32 | #endif -------------------------------------------------------------------------------- /kernel/linux/include/linux/slab.h: -------------------------------------------------------------------------------- 1 | #ifndef _LINUX_SLAB_H 2 | #define _LINUX_SLAB_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | // todo 10 | struct kmem_cache; 11 | struct list_lru; 12 | 13 | extern void *kfunc_def(__kmalloc)(size_t size, gfp_t flags); 14 | extern void *kfunc_def(kmalloc)(size_t size, gfp_t flags); 15 | extern void kfunc_def(kfree)(const void *); 16 | extern void kfunc_def(kvfree)(const void *addr); 17 | 18 | void *__must_check krealloc(const void *, size_t, gfp_t); 19 | void kfree_sensitive(const void *); 20 | void kvfree_sensitive(const void *addr, size_t len); 21 | size_t __ksize(const void *); 22 | size_t ksize(const void *); 23 | void *kmem_cache_alloc(struct kmem_cache *cachep, gfp_t flags); 24 | void *kmem_cache_alloc_lru(struct kmem_cache *s, struct list_lru *lru, gfp_t gfpflags); 25 | void kmem_cache_free(struct kmem_cache *s, void *objp); 26 | void kmem_cache_free_bulk(struct kmem_cache *s, size_t size, void **p); 27 | int kmem_cache_alloc_bulk(struct kmem_cache *s, gfp_t flags, size_t size, void **p); 28 | 29 | void *__kmalloc_node(size_t size, gfp_t flags, int node); 30 | void *kmem_cache_alloc_node(struct kmem_cache *s, gfp_t flags, int node); 31 | void *kmalloc_trace(struct kmem_cache *s, gfp_t flags, size_t size); 32 | void *kmalloc_node_trace(struct kmem_cache *s, gfp_t gfpflags, int node, size_t size); 33 | void *kmalloc_large(size_t size, gfp_t flags); 34 | void *kmalloc_large_node(size_t size, gfp_t flags, int node); 35 | 36 | // todo: kernel version specified different gfp_t 37 | static inline void *kmalloc(size_t size, gfp_t flags) 38 | { 39 | kfunc_call(kmalloc, size, flags); 40 | kfunc_direct_call(__kmalloc, size, flags); 41 | } 42 | 43 | static inline void kfree(const void *objp) 44 | { 45 | kfunc_direct_call(kfree, objp); 46 | } 47 | 48 | static inline void kvfree(const void *addr) 49 | { 50 | kfunc_direct_call(kvfree, addr); 51 | } 52 | 53 | #endif -------------------------------------------------------------------------------- /kernel/linux/include/linux/stacktrace.h: -------------------------------------------------------------------------------- 1 | #ifndef __LINUX_STACKTRACE_H 2 | #define __LINUX_STACKTRACE_H 3 | 4 | #include 5 | 6 | struct pt_regs; 7 | 8 | struct stack_trace 9 | { 10 | unsigned int nr_entries, max_entries; 11 | unsigned long *entries; 12 | int skip; /* input argument: How many entries to skip */ 13 | }; 14 | 15 | extern void kfunc_def(save_stack_trace)(struct stack_trace *trace); 16 | extern void kfunc_def(save_stack_trace_regs)(struct pt_regs *regs, struct stack_trace *trace); 17 | extern void kfunc_def(save_stack_trace_tsk)(struct task_struct *tsk, struct stack_trace *trace); 18 | extern void kfunc_def(print_stack_trace)(struct stack_trace *trace, int spaces); 19 | extern void kfunc_def(save_stack_trace_user)(struct stack_trace *trace); 20 | 21 | static inline void save_stack_trace(struct stack_trace *trace) 22 | { 23 | kfunc_call_void(save_stack_trace, trace); 24 | kfunc_not_found() 25 | } 26 | 27 | static inline void save_stack_trace_regs(struct pt_regs *regs, struct stack_trace *trace) 28 | { 29 | kfunc_call_void(save_stack_trace_regs, regs, trace); 30 | kfunc_not_found() 31 | } 32 | 33 | static inline void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace) 34 | { 35 | kfunc_call_void(save_stack_trace_tsk, tsk, trace); 36 | kfunc_not_found() 37 | } 38 | 39 | static inline void print_stack_trace(struct stack_trace *trace, int spaces) 40 | { 41 | kfunc_call_void(print_stack_trace, trace, spaces); 42 | kfunc_not_found() 43 | } 44 | 45 | static inline void save_stack_trace_user(struct stack_trace *trace) 46 | { 47 | kfunc_call_void(save_stack_trace_user, trace); 48 | kfunc_not_found() 49 | } 50 | 51 | #endif -------------------------------------------------------------------------------- /kernel/linux/include/linux/stop_machine.h: -------------------------------------------------------------------------------- 1 | #ifndef _LINUX_STOP_MACHINE 2 | #define _LINUX_STOP_MACHINE 3 | 4 | #include 5 | #include 6 | 7 | typedef int (*cpu_stop_fn_t)(void *arg); 8 | 9 | struct cpumask; 10 | 11 | extern bool kvar_def(stop_machine_initialized); 12 | extern const struct cpumask *kvar_def(cpu_online_mask); 13 | 14 | /** 15 | * stop_machine: freeze the machine on all CPUs and run this function 16 | * @fn: the function to run 17 | * @data: the data ptr for the @fn() 18 | * @cpus: the cpus to run the @fn() on (NULL = any online cpu) 19 | * 20 | * Description: This causes a thread to be scheduled on every cpu, 21 | * each of which disables interrupts. The result is that no one is 22 | * holding a spinlock or inside any other preempt-disabled region when 23 | * @fn() runs. 24 | * 25 | * This can be thought of as a very heavy write lock, equivalent to 26 | * grabbing every spinlock in the kernel. 27 | * 28 | * Protects against CPU hotplug. 29 | * 30 | */ 31 | extern int kfunc_def(stop_machine)(int (*fn)(void *), void *data, const struct cpumask *cpus); 32 | 33 | static inline int stop_machine(cpu_stop_fn_t fn, void *data, const struct cpumask *cpus) 34 | { 35 | kfunc_call(stop_machine, fn, data, cpus); 36 | // todo: 37 | // unsigned long flags; 38 | // int ret; 39 | // local_irq_save(flags); 40 | // ret = fn(data); 41 | // local_irq_restore(flags); 42 | // return ret; 43 | kfunc_not_found(); 44 | return 0; 45 | } 46 | 47 | #endif -------------------------------------------------------------------------------- /kernel/linux/include/linux/syscall.h: -------------------------------------------------------------------------------- 1 | #ifndef _LINUX_SYSCALLS_H 2 | #define _LINUX_SYSCALLS_H 3 | 4 | #include 5 | 6 | struct iocb; 7 | struct io_event; 8 | struct __kernel_timespec; 9 | struct __aio_sigset; 10 | struct io_uring_params; 11 | struct old_timespec32; 12 | struct epoll_event; 13 | struct statfs; 14 | struct statfs64; 15 | struct open_how; 16 | struct linux_dirent64; 17 | struct iovec; 18 | struct pollfd; 19 | struct stat; 20 | struct stat64; 21 | struct __kernel_itimerspec; 22 | struct old_itimerspec32; 23 | struct siginfo; 24 | struct rusage; 25 | struct robust_list_head; 26 | struct futex_waitv; 27 | struct __kernel_old_itimerval; 28 | struct kexec_segment; 29 | struct sigevent; 30 | struct sched_param; 31 | struct sigaltstack; 32 | struct sigaction; 33 | struct tms; 34 | struct new_utsname; 35 | struct rlimit; 36 | struct getcpu_cache; 37 | struct __kernel_old_timeval; 38 | struct timezone; 39 | struct __kernel_timex; 40 | struct old_timex32; 41 | struct sysinfo; 42 | struct mq_attr; 43 | struct msqid_ds; 44 | struct msgbuf; 45 | struct sembuf; 46 | struct shmid_ds; 47 | struct sockaddr; 48 | struct user_msghdr; 49 | struct clone_args; 50 | struct perf_event_attr; 51 | struct mmsghdr; 52 | struct rlimit64; 53 | struct file_handle; 54 | struct sched_attr; 55 | union bpf_attr; 56 | typedef struct rwf_t rwf_t; 57 | struct statx; 58 | struct rseq; 59 | struct mount_attr; 60 | struct landlock_ruleset_attr; 61 | enum landlock_rule_type; 62 | struct utimbuf; 63 | struct old_utimbuf32; 64 | struct old_timeval32; 65 | struct linux_dirent; 66 | struct old_sigaction; 67 | struct __old_kernel_stat; 68 | struct sel_arg_struct; 69 | struct old_linux_dirent; 70 | struct old_utsname; 71 | struct oldold_utsname; 72 | struct mmap_arg_struct; 73 | struct sembuf; 74 | struct timespec64; 75 | struct ipc_namespace; 76 | 77 | #endif -------------------------------------------------------------------------------- /kernel/linux/include/linux/thread_info.h: -------------------------------------------------------------------------------- 1 | #ifndef _LINUX_THREAD_INFO_H 2 | #define _LINUX_THREAD_INFO_H 3 | 4 | #include 5 | #include 6 | 7 | #endif -------------------------------------------------------------------------------- /kernel/linux/include/linux/trace_seq.h: -------------------------------------------------------------------------------- 1 | #ifndef _LINUX_TRACE_SEQ_H 2 | #define _LINUX_TRACE_SEQ_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | // 3.18 9 | // struct trace_seq 10 | // { 11 | // unsigned char buffer[PAGE_SIZE]; 12 | // unsigned int len; 13 | // unsigned int readpos; 14 | // int full; 15 | // }; 16 | 17 | // 4.4 18 | // struct trace_seq { 19 | // char buffer[PAGE_SIZE]; 20 | // struct seq_buf seq; 21 | // int full; 22 | // }; 23 | 24 | // static inline void trace_seq_init(struct trace_seq *s) 25 | // { 26 | // s->len = 0; 27 | // s->readpos = 0; 28 | // s->full = 0; 29 | // } 30 | 31 | struct trace_seq; 32 | 33 | extern int kfunc_def(trace_seq_printf)(struct trace_seq *s, const char *fmt, ...); 34 | extern int kfunc_def(trace_seq_to_user)(struct trace_seq *s, char __user *ubuf, int cnt); 35 | extern int kfunc_def(trace_seq_puts)(struct trace_seq *s, const char *str); 36 | extern int kfunc_def(trace_seq_putc)(struct trace_seq *s, unsigned char c); 37 | extern int kfunc_def(trace_seq_putmem)(struct trace_seq *s, const void *mem, unsigned int len); 38 | extern int kfunc_def(trace_seq_putmem_hex)(struct trace_seq *s, const void *mem, unsigned int len); 39 | extern int kfunc_def(trace_seq_bitmask)(struct trace_seq *s, const unsigned long *maskp, int nmaskbits); 40 | 41 | #endif -------------------------------------------------------------------------------- /kernel/linux/include/linux/uaccess.h: -------------------------------------------------------------------------------- 1 | #ifndef __LINUX_UACCESS_H__ 2 | #define __LINUX_UACCESS_H__ 3 | 4 | #include 5 | #include 6 | 7 | #define get_fs() (current_thread_info()->addr_limit) 8 | 9 | // todo: 10 | // probe_user_write 11 | // unsigned long __must_check copy_from_user(void *to, const void __user *from, unsigned long n); 12 | // unsigned long __must_check copy_to_user(void __user *to, const void *from, unsigned long n); 13 | // unsigned long __must_check copy_in_user(void __user *to, const void __user *from, unsigned long n); 14 | 15 | // >= 5.8, On success, returns the length of the string INCLUDING the trailing NUL. 16 | extern long kfunc_def(strncpy_from_user_nofault)(char *dst, const void __user *unsafe_addr, long count); 17 | 18 | /** 19 | * strncpy_from_unsafe_user: - Copy a NUL terminated string from unsafe user 20 | * address. 21 | * @dst: Destination address, in kernel space. This buffer must be at 22 | * least @count bytes long. 23 | * @unsafe_addr: Unsafe user address. 24 | * @count: Maximum number of bytes to copy, including the trailing NUL. 25 | * 26 | * Copies a NUL-terminated string from unsafe user address to kernel buffer. 27 | * 28 | * On success, returns the length of the string INCLUDING the trailing NUL. 29 | * 30 | * If access fails, returns -EFAULT (some data may have been copied 31 | * and the trailing NUL added). 32 | * 33 | * If @count is smaller than the length of the string, copies @count-1 bytes, 34 | * sets the last byte of @dst buffer to NUL and returns @count. 35 | */ 36 | extern long kfunc_def(strncpy_from_unsafe_user)(char *dst, const void __user *unsafe_addr, long count); 37 | 38 | /** 39 | * strncpy_from_user: - Copy a NUL terminated string from userspace. 40 | * @dst: Destination address, in kernel space. This buffer must be at 41 | * least @count bytes long. 42 | * @src: Source address, in user space. 43 | * @count: Maximum number of bytes to copy, including the trailing NUL. 44 | * 45 | * Copies a NUL-terminated string from userspace to kernel space. 46 | * 47 | * On success, returns the length of the string (not including the trailing 48 | * NUL). 49 | * 50 | * If access to userspace fails, returns -EFAULT (some data may have been 51 | * copied). 52 | * 53 | * If @count is smaller than the length of the string, copies @count bytes 54 | * and returns @count. 55 | */ 56 | extern long kfunc_def(strncpy_from_user)(char *dest, const char __user *src, long count); 57 | 58 | // Unlike strnlen_user, this can be used from IRQ handler etc. because it disables pagefaults. 59 | extern long kfunc_def(strnlen_user_nofault)(const void __user *unsafe_addr, long count); 60 | extern long kfunc_def(strnlen_unsafe_user)(const void __user *unsafe_addr, long count); 61 | extern long kfunc_def(strnlen_user)(const char __user *str, long n); 62 | 63 | #endif -------------------------------------------------------------------------------- /kernel/linux/include/linux/umh.h: -------------------------------------------------------------------------------- 1 | #ifndef __LINUX_UMH_H__ 2 | #define __LINUX_UMH_H__ 3 | 4 | #include 5 | #include 6 | 7 | #define UMH_NO_WAIT 0x00 /* don't wait at all */ 8 | #define UMH_WAIT_EXEC 0x01 /* wait for the exec, but not the process */ 9 | #define UMH_WAIT_PROC 0x02 /* wait for the process to complete */ 10 | #define UMH_KILLABLE 0x04 /* wait for EXEC/PROC killable */ 11 | #define UMH_FREEZABLE 0x08 /* wait for EXEC/PROC freezable */ 12 | 13 | extern int kfunc_def(call_usermodehelper)(const char *path, char **argv, char **envp, int wait); 14 | 15 | static inline int call_usermodehelper(const char *path, char **argv, char **envp, int wait) 16 | { 17 | kfunc_call(call_usermodehelper, path, argv, envp, wait); 18 | kfunc_not_found(); 19 | return -EFAULT; 20 | } 21 | 22 | #endif -------------------------------------------------------------------------------- /kernel/linux/include/net/netlabel.h: -------------------------------------------------------------------------------- 1 | #ifndef _NETLABEL_H 2 | #define _NETLABEL_H 3 | 4 | #endif -------------------------------------------------------------------------------- /kernel/linux/include/uapi/asm-generic/errno-base.h: -------------------------------------------------------------------------------- 1 | #ifndef _ASM_GENERIC_ERRNO_BASE_H 2 | #define _ASM_GENERIC_ERRNO_BASE_H 3 | 4 | #define EPERM 1 /* Operation not permitted */ 5 | #define ENOENT 2 /* No such file or directory */ 6 | #define ESRCH 3 /* No such process */ 7 | #define EINTR 4 /* Interrupted system call */ 8 | #define EIO 5 /* I/O error */ 9 | #define ENXIO 6 /* No such device or address */ 10 | #define E2BIG 7 /* Argument list too long */ 11 | #define ENOEXEC 8 /* Exec format error */ 12 | #define EBADF 9 /* Bad file number */ 13 | #define ECHILD 10 /* No child processes */ 14 | #define EAGAIN 11 /* Try again */ 15 | #define ENOMEM 12 /* Out of memory */ 16 | #define EACCES 13 /* Permission denied */ 17 | #define EFAULT 14 /* Bad address */ 18 | #define ENOTBLK 15 /* Block device required */ 19 | #define EBUSY 16 /* Device or resource busy */ 20 | #define EEXIST 17 /* File exists */ 21 | #define EXDEV 18 /* Cross-device link */ 22 | #define ENODEV 19 /* No such device */ 23 | #define ENOTDIR 20 /* Not a directory */ 24 | #define EISDIR 21 /* Is a directory */ 25 | #define EINVAL 22 /* Invalid argument */ 26 | #define ENFILE 23 /* File table overflow */ 27 | #define EMFILE 24 /* Too many open files */ 28 | #define ENOTTY 25 /* Not a typewriter */ 29 | #define ETXTBSY 26 /* Text file busy */ 30 | #define EFBIG 27 /* File too large */ 31 | #define ENOSPC 28 /* No space left on device */ 32 | #define ESPIPE 29 /* Illegal seek */ 33 | #define EROFS 30 /* Read-only file system */ 34 | #define EMLINK 31 /* Too many links */ 35 | #define EPIPE 32 /* Broken pipe */ 36 | #define EDOM 33 /* Math argument out of domain of func */ 37 | #define ERANGE 34 /* Math result not representable */ 38 | 39 | #endif -------------------------------------------------------------------------------- /kernel/linux/include/uapi/linux/limits.h: -------------------------------------------------------------------------------- 1 | /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ 2 | #ifndef _UAPI_LINUX_LIMITS_H 3 | #define _UAPI_LINUX_LIMITS_H 4 | 5 | #define NR_OPEN 1024 6 | 7 | #define NGROUPS_MAX 65536 /* supplemental group IDs are available */ 8 | #define ARG_MAX 131072 /* # bytes of args + environ for exec() */ 9 | #define LINK_MAX 127 /* # links a file may have */ 10 | #define MAX_CANON 255 /* size of the canonical input queue */ 11 | #define MAX_INPUT 255 /* size of the type-ahead buffer */ 12 | #define NAME_MAX 255 /* # chars in a file name */ 13 | #define PATH_MAX 4096 /* # chars in a path name including nul */ 14 | #define PIPE_BUF 4096 /* # bytes in atomic write to a pipe */ 15 | #define XATTR_NAME_MAX 255 /* # chars in an extended attribute name */ 16 | #define XATTR_SIZE_MAX 65536 /* size of an extended attribute value (64k) */ 17 | #define XATTR_LIST_MAX 65536 /* size of extended attribute namelist (64k) */ 18 | 19 | #define RTSIG_MAX 32 20 | 21 | #endif -------------------------------------------------------------------------------- /kernel/linux/include/uapi/linux/seccomp.h: -------------------------------------------------------------------------------- 1 | /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ 2 | #ifndef _UAPI_LINUX_SECCOMP_H 3 | #define _UAPI_LINUX_SECCOMP_H 4 | 5 | /* Valid values for seccomp.mode and prctl(PR_SET_SECCOMP, ) */ 6 | #define SECCOMP_MODE_DISABLED 0 /* seccomp is not in use. */ 7 | #define SECCOMP_MODE_STRICT 1 /* uses hard-coded filter. */ 8 | #define SECCOMP_MODE_FILTER 2 /* uses user-supplied filter. */ 9 | 10 | /* Valid operations for seccomp syscall. */ 11 | #define SECCOMP_SET_MODE_STRICT 0 12 | #define SECCOMP_SET_MODE_FILTER 1 13 | #define SECCOMP_GET_ACTION_AVAIL 2 14 | #define SECCOMP_GET_NOTIF_SIZES 3 15 | 16 | #endif -------------------------------------------------------------------------------- /kernel/linux/include/vdso/limits.h: -------------------------------------------------------------------------------- 1 | #ifndef __VDSO_LIMITS_H 2 | #define __VDSO_LIMITS_H 3 | 4 | #define USHRT_MAX ((unsigned short)~0U) 5 | #define SHRT_MAX ((short)(USHRT_MAX >> 1)) 6 | #define SHRT_MIN ((short)(-SHRT_MAX - 1)) 7 | #define INT_MAX ((int)(~0U >> 1)) 8 | #define INT_MIN (-INT_MAX - 1) 9 | #define UINT_MAX (~0U) 10 | #define LONG_MAX ((long)(~0UL >> 1)) 11 | #define LONG_MIN (-LONG_MAX - 1) 12 | #define ULONG_MAX (~0UL) 13 | #define LLONG_MAX ((long long)(~0ULL >> 1)) 14 | #define LLONG_MIN (-LLONG_MAX - 1) 15 | #define ULLONG_MAX (~0ULL) 16 | #define UINTPTR_MAX ULONG_MAX 17 | 18 | #endif -------------------------------------------------------------------------------- /kernel/linux/security/selinux/include/avc_ss.h: -------------------------------------------------------------------------------- 1 | /* SPDX-License-Identifier: GPL-2.0 */ 2 | /* 3 | * Access vector cache interface for the security server. 4 | * 5 | * Author : Stephen Smalley, 6 | */ 7 | #ifndef _SELINUX_AVC_SS_H_ 8 | #define _SELINUX_AVC_SS_H_ 9 | 10 | #include 11 | 12 | int avc_ss_reset(u32 seqno); 13 | 14 | /* Class/perm mapping support */ 15 | struct security_class_mapping 16 | { 17 | const char *name; 18 | const char *perms[sizeof(u32) * 8 + 1]; 19 | }; 20 | 21 | extern const struct security_class_mapping secclass_map[]; 22 | 23 | #endif /* _SELINUX_AVC_SS_H_ */ -------------------------------------------------------------------------------- /kernel/linux/tools/arch/arm64/include/asm/barrier.h: -------------------------------------------------------------------------------- 1 | /* SPDX-License-Identifier: GPL-2.0 */ 2 | #ifndef _TOOLS_LINUX_ASM_AARCH64_BARRIER_H 3 | #define _TOOLS_LINUX_ASM_AARCH64_BARRIER_H 4 | 5 | #include 6 | 7 | #endif /* _TOOLS_LINUX_ASM_AARCH64_BARRIER_H */ -------------------------------------------------------------------------------- /kernel/patch/android/gen/user_init.c: -------------------------------------------------------------------------------- 1 | static const char user_init[] = "#!/bin/sh\n\nKPMS_DIR=\"/data/adb/ap/kpms/\"\nMAGISK_POLICY_PATH=\"/data/adb/ap/bin/magiskpolicy\"\nSUPERCMD=\"truncate\"\nMAGISK_SCTX=\"u:r:magisk:s0\"\nAPD_PATH=\"/data/adb/apd\"\nDEV_LOG_DIR=\"/dev/user_init_log/\"\n\nskey=\"$1\"\nevent=\"$2\"\n\nmkdir -p \"$DEV_LOG_DIR\"\n\nLOG_FILE=\"$DEV_LOG_DIR\"\"$event\"\n\nexec >>$LOG_FILE 2>&1\n\nset -x\n\nload_modules() {\n for dir in \"$KPMS_DIR/*\"; do\n if [ ! -d \"$dir\" ]; then continue; fi\n if [ -e \"$dir/disable\" ]; then continue; fi\n main_sh=\"$dir/main.sh\"\n if [ -e \"$main_sh\" ]; then\n touch \"$dir/disable\"\n echo \"loading $dir/main.sh ...\"\n . \"$main_sh\"\n rm -f \"$dir/disable\"\n else\n echo \"Error: $main_sh not found in $dir\"\n fi\n done\n}\n\nhandle() {\n $SUPERCMD $skey event $event \"before\"\n case \"$event\" in\n \"early-init\" | \"init\" | \"late-init\") ;;\n \"post-fs-data\")\n $MAGISK_POLICY_PATH --magisk --live\n load_modules $skey $event\n $SUPERCMD $skey -Z $MAGISK_SCTX exec $APD_PATH -s $skey $event\n ;;\n \"services\")\n $SUPERCMD $skey -Z $MAGISK_SCTX exec $APD_PATH -s $skey $event\n ;;\n \"boot-completed\")\n $SUPERCMD $skey -Z $MAGISK_SCTX exec $APD_PATH -s $skey $event\n $SUPERCMD su -Z $MAGISK_SCTX exec $APD_PATH uid-listener &\n ;;\n *)\n echo \"unknown user_init event: $event\"\n ;;\n esac\n $SUPERCMD $skey event $event \"after\"\n}\n\nhandle\n"; 2 | -------------------------------------------------------------------------------- /kernel/patch/android/sepolicy_flags.c: -------------------------------------------------------------------------------- 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 | /* 3 | * Copyright (C) 2024 1f2003d5. All Rights Reserved. 4 | * Copyright (C) 2024 sekaiacg. All Rights Reserved. 5 | */ 6 | 7 | #include "sepolicy_flags.h" 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | 21 | /* 22 | * @see: https://android-review.googlesource.com/c/kernel/common/+/3009995 23 | */ 24 | 25 | static void before_policydb_write(hook_fargs2_t *args, void *udata) 26 | { 27 | struct _policy_file *fp = (struct _policy_file *)args->arg1; 28 | args->local.data0 = (uint64_t)fp->data; 29 | } 30 | 31 | static void after_policydb_write(hook_fargs2_t *args, void *udata) 32 | { 33 | struct _policydb *p = (struct _policydb *)args->arg0; 34 | char *data = (char *)args->local.data0; 35 | 36 | if (!args->ret) { 37 | __le32 *config = (__le32 *)(data + POLICYDB_CONFIG_OFFSET); 38 | __le32 before_config = *config; 39 | bool android_netlink_route_exists = before_config & POLICYDB_CONFIG_ANDROID_NETLINK_ROUTE; 40 | bool android_netlink_getneigh_exists = before_config & POLICYDB_CONFIG_ANDROID_NETLINK_GETNEIGH; 41 | if (p->android_netlink_route == 1 && !android_netlink_route_exists) { 42 | *config |= POLICYDB_CONFIG_ANDROID_NETLINK_ROUTE; 43 | } 44 | if (p->android_netlink_getneigh == 1 && !android_netlink_getneigh_exists) { 45 | *config |= POLICYDB_CONFIG_ANDROID_NETLINK_GETNEIGH; 46 | } 47 | } 48 | } 49 | 50 | int android_sepolicy_flags_fix() 51 | { 52 | unsigned long policydb_write_addr = kallsyms_lookup_name("policydb_write"); 53 | 54 | if (likely(policydb_write_addr)) { 55 | hook_err_t err = hook_wrap2((void *)policydb_write_addr, before_policydb_write, after_policydb_write, 0); 56 | 57 | if (unlikely(err != HOOK_NO_ERR)) { 58 | log_boot("hook policydb_write_addr: %llx, error: %d\n", policydb_write_addr, err); 59 | return -1; 60 | } 61 | } 62 | 63 | return 0; 64 | } 65 | -------------------------------------------------------------------------------- /kernel/patch/common/secpass.c: -------------------------------------------------------------------------------- 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 | /* 3 | * Copyright (C) 2023 bmax121. All Rights Reserved. 4 | */ 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | #include 13 | 14 | struct pt_regs; 15 | 16 | static inline bool should_cfi_pass(unsigned long target) 17 | { 18 | return is_kp_text_area(target) || is_kp_hook_area(target) || is_kpm_rox_area(target); 19 | } 20 | 21 | enum bug_trap_type 22 | { 23 | BUG_TRAP_TYPE_NONE = 0, 24 | BUG_TRAP_TYPE_WARN = 1, 25 | BUG_TRAP_TYPE_BUG = 2, 26 | }; 27 | 28 | static enum bug_trap_type (*backup_report_cfi_failure)(struct pt_regs *regs, unsigned long addr, unsigned long *target, 29 | u32 type) = 0; 30 | static enum bug_trap_type replace_report_cfi_failure(struct pt_regs *regs, unsigned long addr, unsigned long *target, 31 | u32 type) 32 | { 33 | if (should_cfi_pass(*target)) { 34 | return BUG_TRAP_TYPE_WARN; 35 | } 36 | enum bug_trap_type rc = backup_report_cfi_failure(regs, addr, target, type); 37 | return rc; 38 | } 39 | 40 | typedef void (*cfi_check_fn)(uint64_t id, void *ptr, void *diag); 41 | 42 | static void (*backup__cfi_slowpath)(uint64_t id, void *ptr, void *diag) = 0; 43 | static void replace__cfi_slowpath(uint64_t id, void *ptr, void *diag) 44 | { 45 | if (should_cfi_pass((unsigned long)ptr)) return; 46 | backup__cfi_slowpath(id, ptr, diag); 47 | } 48 | 49 | int bypass_kcfi() 50 | { 51 | int rc = 0; 52 | 53 | // 6.1.0 54 | // todo: Is there more elegant way? 55 | unsigned long report_cfi_failure_addr = patch_config->report_cfi_failure; 56 | if (report_cfi_failure_addr) { 57 | hook_err_t err = hook((void *)report_cfi_failure_addr, (void *)replace_report_cfi_failure, 58 | (void **)&backup_report_cfi_failure); 59 | if (err) { 60 | log_boot("hook report_cfi_failure: %llx, error: %d\n", report_cfi_failure_addr, err); 61 | rc = err; 62 | goto out; 63 | } 64 | } 65 | 66 | // todo: direct modify cfi_shadow, __cfi_check? 67 | unsigned long __cfi_slowpath_addr = patch_config->__cfi_slowpath_diag; 68 | if (!__cfi_slowpath_addr) { 69 | __cfi_slowpath_addr = patch_config->__cfi_slowpath; 70 | } 71 | if (__cfi_slowpath_addr) { 72 | hook_err_t err = 73 | hook((void *)__cfi_slowpath_addr, (void *)replace__cfi_slowpath, (void **)&backup__cfi_slowpath); 74 | if (err) { 75 | log_boot("hook __cfi_slowpath_diag: %llx, error: %d\n", __cfi_slowpath_addr, err); 76 | rc = err; 77 | goto out; 78 | } 79 | } 80 | 81 | if (!report_cfi_failure_addr && !__cfi_slowpath_addr) { 82 | // not error 83 | log_boot("no symbol for pass kcfi\n"); 84 | } 85 | 86 | out: 87 | return rc; 88 | } -------------------------------------------------------------------------------- /kernel/patch/common/taskob.c: -------------------------------------------------------------------------------- 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 | /* 3 | * Copyright (C) 2023 bmax121. All Rights Reserved. 4 | */ 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | 24 | static inline void prepare_init_ext(struct task_struct *task) 25 | { 26 | struct task_ext *ext = get_task_ext(task); 27 | for (uintptr_t i = (uintptr_t)ext; i < (uintptr_t)ext + sizeof(struct task_ext); i += 8) { 28 | *(uintptr_t *)i = 0; 29 | } 30 | ext->size = task_ext_size; 31 | ext->_magic = TASK_EXT_MAGIC; 32 | dsb(ish); 33 | } 34 | 35 | static void prepare_task_ext(struct task_struct *new, struct task_struct *old) 36 | { 37 | struct task_ext *old_ext = get_task_ext(old); 38 | if (unlikely(!task_ext_valid(old_ext))) { 39 | logkfe("dirty task_ext, pid(maybe dirty): %d\n", old_ext->pid); 40 | return; 41 | } 42 | struct task_ext *new_ext = get_task_ext(new); 43 | for (uintptr_t i = (uintptr_t)new_ext; i < (uintptr_t)new_ext + sizeof(struct task_ext); i += 8) { 44 | *(uintptr_t *)i = 0; 45 | } 46 | new_ext->size = task_ext_size; 47 | new_ext->_magic = TASK_EXT_MAGIC; 48 | 49 | new_ext->pid = __task_pid_nr_ns(new, PIDTYPE_PID, 0); 50 | new_ext->tgid = __task_pid_nr_ns(new, PIDTYPE_TGID, 0); 51 | new_ext->sel_allow = old_ext->sel_allow; 52 | 53 | dsb(ish); 54 | } 55 | 56 | int task_ext_size = offsetof(struct task_ext, _magic); 57 | KP_EXPORT_SYMBOL(task_ext_size); 58 | 59 | static void after_copy_process(hook_fargs8_t *args, void *udata) 60 | { 61 | struct task_struct *new = (struct task_struct *)args->ret; 62 | if (unlikely(!new || IS_ERR(new))) return; 63 | prepare_task_ext(new, current); 64 | } 65 | 66 | static void after_cgroup_post_fork(hook_fargs4_t *args, void *udata) 67 | { 68 | struct task_struct *new = (struct task_struct *)args->arg0; 69 | prepare_task_ext(new, current); 70 | } 71 | 72 | int task_observer() 73 | { 74 | int rc = 0; 75 | 76 | prepare_init_ext(init_task); 77 | 78 | unsigned long copy_process_addr = patch_config->copy_process; 79 | if (copy_process_addr) { 80 | rc |= hook_wrap8((void *)copy_process_addr, 0, after_copy_process, 0); 81 | log_boot("hook copy_process: %llx, rc: %d\n", copy_process_addr, rc); 82 | } else { 83 | unsigned long cgroup_post_fork_addr = patch_config->cgroup_post_fork; 84 | if (cgroup_post_fork_addr) { 85 | rc |= hook_wrap4((void *)cgroup_post_fork_addr, 0, after_cgroup_post_fork, 0); 86 | log_boot("hook cgroup_post_fork: %llx, rc: %d\n", cgroup_post_fork_addr, rc); 87 | } else { 88 | rc = HOOK_BAD_ADDRESS; 89 | } 90 | } 91 | 92 | return rc; 93 | } -------------------------------------------------------------------------------- /kernel/patch/common/test.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | void test() 6 | { 7 | logkd("=== start test ==="); 8 | 9 | const char *sctx = "u:r:kernel:s0"; 10 | 11 | uint32_t secid = 0; 12 | int rc = security_secctx_to_secid(sctx, strlen(sctx), &secid); 13 | 14 | logkd("secid: %d, rc: %d\n", secid, rc); 15 | 16 | logkd("=== end test ==="); 17 | } -------------------------------------------------------------------------------- /kernel/patch/common/user_event.c: -------------------------------------------------------------------------------- 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 | /* 3 | * Copyright (C) 2024 bmax121. All Rights Reserved. 4 | */ 5 | 6 | #include 7 | 8 | #include 9 | 10 | int report_user_event(const char *event, const char *args) 11 | { 12 | logki("user report event: %s, args: %s\n", event, args); 13 | return 0; 14 | } -------------------------------------------------------------------------------- /kernel/patch/include/accctl.h: -------------------------------------------------------------------------------- 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 | /* 3 | * Copyright (C) 2023 bmax121. All Rights Reserved. 4 | */ 5 | 6 | #ifndef _KP_ACCCTL_H_ 7 | #define _KP_ACCCTL_H_ 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | 18 | extern char all_allow_sctx[SUPERCALL_SCONTEXT_LEN]; 19 | extern uint32_t all_allow_sid; 20 | 21 | int set_all_allow_sctx(const char *sctx); 22 | int commit_kernel_su(); 23 | int commit_common_su(uid_t to_uid, const char *sctx); 24 | int commit_su(uid_t uid, const char *sctx); 25 | int task_su(pid_t pid, uid_t to_uid, const char *sctx); 26 | 27 | /** 28 | * @brief Whether to make the current task bypass all selinux permission checks. 29 | * 30 | * @param task 31 | * @param val 32 | */ 33 | static inline void set_priv_sel_allow(struct task_struct *task, bool val) 34 | { 35 | struct task_ext *ext = get_task_ext(task); 36 | ext->priv_sel_allow = val; 37 | dsb(ish); 38 | } 39 | 40 | #endif -------------------------------------------------------------------------------- /kernel/patch/include/hotpatch.h: -------------------------------------------------------------------------------- 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 | /* 3 | * Copyright (C) 2023 bmax121. All Rights Reserved. 4 | */ 5 | 6 | #ifndef _KP_HOTPATCH_H_ 7 | #define _KP_HOTPATCH_H_ 8 | 9 | #include 10 | 11 | #endif -------------------------------------------------------------------------------- /kernel/patch/include/kconfig.h: -------------------------------------------------------------------------------- 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 | /* 3 | * Copyright (C) 2023 bmax121. All Rights Reserved. 4 | */ 5 | 6 | #ifndef _KP_KCONFIG_H_ 7 | #define _KP_KCONFIG_H_ 8 | 9 | // todo: move config to here 10 | 11 | extern bool has_config_compat; 12 | 13 | #endif -------------------------------------------------------------------------------- /kernel/patch/include/kputils.h: -------------------------------------------------------------------------------- 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 | /* 3 | * Copyright (C) 2023 bmax121. All Rights Reserved. 4 | */ 5 | 6 | #ifndef _KP_UTILS_H_ 7 | #define _KP_UTILS_H_ 8 | 9 | #include 10 | #include 11 | 12 | int __must_check compat_copy_to_user(void __user *to, const void *from, int n); 13 | long compat_strncpy_from_user(char *dest, const char __user *src, long count); 14 | void *__user copy_to_user_stack(const void *data, int len); 15 | uid_t current_uid(); 16 | uint64_t get_random_u64(void); 17 | 18 | void print_bootlog(); 19 | 20 | #endif -------------------------------------------------------------------------------- /kernel/patch/include/kstorage.h: -------------------------------------------------------------------------------- 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 | /* 3 | * Copyright (C) 2024 bmax121. All Rights Reserved. 4 | */ 5 | 6 | #ifndef _KP_KSTORAGE_H_ 7 | #define _KP_KSTORAGE_H_ 8 | 9 | #include 10 | #include 11 | #include 12 | 13 | struct kstorage 14 | { 15 | struct list_head list; 16 | struct rcu_head rcu; 17 | 18 | int gid; 19 | long did; 20 | int dlen; 21 | char data[0]; 22 | }; 23 | 24 | int try_alloc_kstroage_group(); 25 | 26 | int kstorage_group_size(int gid); 27 | 28 | int write_kstorage(int gid, long did, void *data, int offset, int len, bool data_is_user); 29 | 30 | /// must within rcu read lock 31 | const struct kstorage *get_kstorage(int gid, long did); 32 | 33 | typedef int (*on_kstorage_cb)(struct kstorage *kstorage, void *udata); 34 | int on_each_kstorage_elem(int gid, on_kstorage_cb cb, void *udata); 35 | 36 | int read_kstorage(int gid, long did, void *data, int offset, int len, bool data_is_user); 37 | 38 | int list_kstorage_ids(int gid, long *ids, int idslen, bool data_is_user); 39 | 40 | int remove_kstorage(int gid, long did); 41 | 42 | #endif -------------------------------------------------------------------------------- /kernel/patch/include/ksyms.h: -------------------------------------------------------------------------------- 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 | /* 3 | * Copyright (C) 2023 bmax121. All Rights Reserved. 4 | */ 5 | 6 | #ifndef _KP_KSYMS_H 7 | #define _KP_KSYMS_H 8 | 9 | #include 10 | #include 11 | 12 | #define INIT_USE_KALLSYMS_LOOKUP_NAME 13 | 14 | #define KFUNC_POISON 0xdeaddead00000000 15 | 16 | #define kvar(var) kv_##var 17 | #define kvar_def(var) (*kv_##var) 18 | #define kvlen(var) kvl_##var 19 | #define kvar_val(var) (*kvar(var)) 20 | 21 | #define kfunc(func) kf_##func 22 | #define kfunc_def(func) (*kf_##func) 23 | 24 | #define kvar_lookup_name(var) kv_##var = (typeof(kv_##var))kallsyms_lookup_name(#var) 25 | #define kfunc_lookup_name(func) kf_##func = (typeof(kf_##func))kallsyms_lookup_name(#func) 26 | 27 | #ifdef INIT_USE_KALLSYMS_LOOKUP_NAME 28 | #define kvar_match(var, name, addr) kvar_lookup_name(var) 29 | #define kfunc_match(func, name, addr) kfunc_lookup_name(func) 30 | #define kfunc_match_cfi(func, name, addr) \ 31 | kf_##func = (typeof(kf_##func))kallsyms_lookup_name(#func ".cfi_jt"); \ 32 | if (!kf_##func) kf_##func = (typeof(kf_##func))kallsyms_lookup_name(#func); 33 | #else 34 | int _ksym_local_strcmp(const char *s1, const char *s2); 35 | #define kvar_match(var, name, addr) \ 36 | if (!kv_##var && !_ksym_local_strcmp(#var, name)) kv_##var = (typeof(kv_##var))addr; 37 | #define kfunc_match(func, name, addr) \ 38 | if (!kf_##func && !_ksym_local_strcmp(#func, name)) kf_##func = (typeof(kf_##func))addr 39 | #endif 40 | 41 | #define kfunc_call(func, ...) \ 42 | if (kf_##func) return kf_##func(__VA_ARGS__); 43 | 44 | #define kfunc_direct_call(func, ...) return kf_##func(__VA_ARGS__); 45 | 46 | #define kfunc_call_void(func, ...) \ 47 | if (kf_##func) kf_##func(__VA_ARGS__); 48 | 49 | #define kfunc_direct_call_void(func, ...) kf_##func(__VA_ARGS__); 50 | 51 | // todo 52 | #define kfunc_not_found() logke("kfunc: %s not found\n", __func__); 53 | 54 | #endif 55 | -------------------------------------------------------------------------------- /kernel/patch/include/module.h: -------------------------------------------------------------------------------- 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 | /* 3 | * Copyright (C) 2023 bmax121. All Rights Reserved. 4 | */ 5 | 6 | #ifndef _KP_MODULE_H_ 7 | #define _KP_MODULE_H_ 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | struct load_info 15 | { 16 | struct 17 | { 18 | const char *base; 19 | unsigned long size; 20 | const char *name, *version, *license, *author, *description; 21 | } info; 22 | const Elf_Ehdr *hdr; 23 | unsigned long len; 24 | Elf_Shdr *sechdrs; 25 | char *secstrings, *strtab; 26 | unsigned long symoffs, stroffs; 27 | struct 28 | { 29 | unsigned int sym, str, mod, info; 30 | } index; 31 | }; 32 | 33 | struct module 34 | { 35 | struct 36 | { 37 | const char *base, *name, *version, *license, *author, *description; 38 | } info; 39 | 40 | char *args, *ctl_args; 41 | 42 | mod_initcall_t *init; 43 | mod_ctl0call_t *ctl0; 44 | mod_ctl1call_t *ctl1; 45 | mod_exitcall_t *exit; 46 | 47 | unsigned int size; 48 | unsigned int text_size; 49 | unsigned int ro_size; 50 | 51 | void *start; 52 | 53 | struct list_head list; 54 | }; 55 | 56 | long load_module(const void *data, int len, const char *args, const char *event, void *__user reserved); 57 | long load_module_path(const char *path, const char *args, void *__user reserved); 58 | long module_control0(const char *name, const char *ctl_args, char *__user out_msg, int outlen); 59 | long module_control1(const char *name, void *a1, void *a2, void *a3); 60 | long unload_module(const char *name, void *__user reserved); 61 | struct module *find_module(const char *name); 62 | 63 | int get_module_nums(); 64 | int list_modules(char *out_names, int size); 65 | int get_module_info(const char *name, char *out_info, int size); 66 | 67 | #endif -------------------------------------------------------------------------------- /kernel/patch/include/pidmem.h: -------------------------------------------------------------------------------- 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 | /* 3 | * Copyright (C) 2024 bmax121. All Rights Reserved. 4 | */ 5 | 6 | #ifndef _KP_PIDMEM_H_ 7 | #define _KP_PIDMEM_H_ 8 | 9 | #include 10 | 11 | // phys_addr_t pid_virt_to_phys(pid_t pid, uintptr_t vaddr); 12 | 13 | // void *pid_map_mem(pid_t pid, void *mem, size_t size, ) 14 | 15 | #endif 16 | -------------------------------------------------------------------------------- /kernel/patch/include/sepolicy_flags.h: -------------------------------------------------------------------------------- 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 | /* 3 | * Copyright (C) 2024 1f2003d5. All Rights Reserved. 4 | * Copyright (C) 2024 sekaiacg. All Rights Reserved. 5 | */ 6 | 7 | #ifndef _KP_SEPOLICY_FLAGS_H_ 8 | #define _KP_SEPOLICY_FLAGS_H_ 9 | 10 | #include 11 | 12 | #define SELINUX_MAGIC 0xf97cff8c 13 | #define POLICYDB_MAGIC SELINUX_MAGIC 14 | #define POLICYDB_STRING "SE Linux" 15 | 16 | #define POLICYDB_CONFIG_MLS 1 17 | #define POLICYDB_CONFIG_ANDROID_NETLINK_ROUTE (1 << 31) 18 | #define POLICYDB_CONFIG_ANDROID_NETLINK_GETNEIGH (1 << 30) 19 | 20 | /* 21 | * config offset: 22 | * __le32(POLICYDB_MAGIC) + __le32(POLICYDB_STRING_LEN) + 23 | * char[POLICYDB_STRING_LEN] + __le32(policyvers) 24 | */ 25 | #define POLICYDB_CONFIG_OFFSET (2 * sizeof(__le32) + strlen(POLICYDB_STRING) + sizeof(__le32)) 26 | 27 | struct _policy_file 28 | { 29 | char *data; 30 | size_t len; 31 | }; 32 | 33 | struct _policydb 34 | { 35 | int mls_enabled; 36 | int android_netlink_route; 37 | int android_netlink_getneigh; 38 | }; 39 | 40 | #endif -------------------------------------------------------------------------------- /kernel/patch/include/sucompat.h: -------------------------------------------------------------------------------- 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 | /* 3 | * Copyright (C) 2024 bmax121. All Rights Reserved. 4 | */ 5 | 6 | #ifndef _KP_SUCOMPAT_H_ 7 | #define _KP_SUCOMPAT_H_ 8 | 9 | #include 10 | #include 11 | #include 12 | 13 | extern const char sh_path[]; 14 | extern const char default_su_path[]; 15 | extern const char legacy_su_path[]; 16 | extern const char apd_path[]; 17 | 18 | struct allow_uid 19 | { 20 | uid_t uid; 21 | struct su_profile profile; 22 | struct list_head list; 23 | struct rcu_head rcu; 24 | }; 25 | 26 | int is_su_allow_uid(uid_t uid); 27 | int su_add_allow_uid(uid_t uid, uid_t to_uid, const char *scontext); 28 | int su_remove_allow_uid(uid_t uid); 29 | int su_allow_uid_nums(); 30 | int su_allow_uids(int is_user, uid_t *out_uids, int out_num); 31 | int su_allow_uid_profile(int is_user, uid_t uid, struct su_profile *profile); 32 | int su_reset_path(const char *path); 33 | const char *su_get_path(); 34 | 35 | int get_ap_mod_exclude(uid_t uid); 36 | int set_ap_mod_exclude(uid_t uid, int exclude); 37 | int list_ap_mod_exclude(uid_t *uids, int len); 38 | 39 | #endif 40 | -------------------------------------------------------------------------------- /kernel/patch/include/taskext.h: -------------------------------------------------------------------------------- 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 | /* 3 | * Copyright (C) 2023 bmax121. All Rights Reserved. 4 | */ 5 | 6 | #ifndef _KP_TASKEXT_H_ 7 | #define _KP_TASKEXT_H_ 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | #define TASK_EXT_MAGIC 0x11581158 16 | 17 | /// @brief the size of current struct task_ext, not included _magic 18 | extern int task_ext_size; 19 | 20 | /** 21 | * @brief An extension of task_struct, stored in the kernel thread stack, 22 | * can be used to store task-local(thread-local) variables. 23 | * This can be very useful if you need to pass thread-local variables across multiple hook points. 24 | * 25 | * Task-local variables can be dynamically expanded. 26 | * @see reg_task_local 27 | * @see has_task_local 28 | * @see task_local_ptr 29 | */ 30 | struct task_ext 31 | { 32 | // first 33 | int size; 34 | pid_t pid; 35 | pid_t tgid; 36 | bool root; 37 | bool sel_allow; 38 | bool priv_sel_allow; 39 | // last 40 | int _magic; 41 | }; 42 | 43 | /** 44 | * @brief Is task_ext dirty, and is it available? 45 | * 46 | * @param ext 47 | * @return int 48 | */ 49 | static inline bool task_ext_valid(struct task_ext *ext) 50 | { 51 | return !IS_ERR(ext) && (*(int *)(ext->size + (uintptr_t)ext) == TASK_EXT_MAGIC); 52 | } 53 | 54 | /** 55 | * @brief Register a new task-local varilable 56 | * 57 | * @param size The size of task-local varilable 58 | * @return The offset of of task-local varilable, 59 | * This value is needed when access this task-local variable. 60 | * 61 | * @see has_task_local 62 | * @see task_local_ptr 63 | */ 64 | static inline int reg_task_local(int size) 65 | { 66 | int offset = task_ext_size; 67 | task_ext_size += size; 68 | return offset; 69 | } 70 | 71 | /** 72 | * @brief Is there a task-local variable regiseted? 73 | * 74 | * @param ext 75 | * @param offset Return value of reg_task_local 76 | * @return true 77 | * @return false 78 | * 79 | * @see reg_task_local 80 | */ 81 | static inline bool has_task_local(struct task_ext *ext, int offset) 82 | { 83 | return offset >= ext->size; 84 | } 85 | 86 | /** 87 | * @brief Access task-local varilable, 88 | * 89 | * @param offset Return value of reg_task_local 90 | * @return void* Task-local varilable pointer 91 | * 92 | * @see reg_task_local 93 | */ 94 | static inline void *task_local_ptr(struct task_ext *ext, int offset) 95 | { 96 | return (void *)((uintptr_t)ext + offset); 97 | } 98 | 99 | #endif 100 | -------------------------------------------------------------------------------- /kernel/patch/include/taskob.h: -------------------------------------------------------------------------------- 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 | /* 3 | * Copyright (C) 2023 bmax121. All Rights Reserved. 4 | */ 5 | 6 | #ifndef _KP_TASKOB_H_ 7 | #define _KP_TASKOB_H_ 8 | 9 | #include 10 | 11 | hook_err_t add_execv_hook(hook_chain8_callback before, hook_chain8_callback after, void *udata); 12 | void remove_execv_hook(hook_chain8_callback before, hook_chain8_callback after); 13 | 14 | #endif -------------------------------------------------------------------------------- /kernel/patch/include/uapi/scdefs.h: -------------------------------------------------------------------------------- 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 | /* 3 | * Copyright (C) 2023 bmax121. All Rights Reserved. 4 | */ 5 | 6 | #ifndef _KP_UAPI_SCDEF_H_ 7 | #define _KP_UAPI_SCDEF_H_ 8 | 9 | static inline long hash_key(const char *key) 10 | { 11 | long hash = 1000000007; 12 | for (int i = 0; key[i]; i++) { 13 | hash = hash * 31 + key[i]; 14 | } 15 | return hash; 16 | } 17 | 18 | #define SUPERCALL_HELLO_ECHO "hello1158" 19 | 20 | // #define __NR_supercall __NR3264_truncate // 45 21 | #define __NR_supercall 45 22 | 23 | #define SUPERCALL_HELLO 0x1000 24 | #define SUPERCALL_KLOG 0x1004 25 | 26 | #define SUPERCALL_BUILD_TIME 0x1007 27 | #define SUPERCALL_KERNELPATCH_VER 0x1008 28 | #define SUPERCALL_KERNEL_VER 0x1009 29 | 30 | #define SUPERCALL_SKEY_GET 0x100a 31 | #define SUPERCALL_SKEY_SET 0x100b 32 | #define SUPERCALL_SKEY_ROOT_ENABLE 0x100c 33 | 34 | #define SUPERCALL_SU 0x1010 35 | #define SUPERCALL_SU_TASK 0x1011 // syscall(__NR_gettid) 36 | 37 | #define SUPERCALL_KPM_LOAD 0x1020 38 | #define SUPERCALL_KPM_UNLOAD 0x1021 39 | #define SUPERCALL_KPM_CONTROL 0x1022 40 | 41 | #define SUPERCALL_KPM_NUMS 0x1030 42 | #define SUPERCALL_KPM_LIST 0x1031 43 | #define SUPERCALL_KPM_INFO 0x1032 44 | 45 | struct kernel_storage 46 | { 47 | void *data; 48 | int len; 49 | }; 50 | 51 | #define SUPERCALL_KSTORAGE_ALLOC_GROUP 0x1040 52 | #define SUPERCALL_KSTORAGE_WRITE 0x1041 53 | #define SUPERCALL_KSTORAGE_READ 0x1042 54 | #define SUPERCALL_KSTORAGE_LIST_IDS 0x1043 55 | #define SUPERCALL_KSTORAGE_REMOVE 0x1044 56 | #define SUPERCALL_KSTORAGE_REMOVE_GROUP 0x1045 57 | 58 | #define KSTORAGE_SU_LIST_GROUP 0 59 | #define KSTORAGE_EXCLUDE_LIST_GROUP 1 60 | #define KSTORAGE_UNUSED_GROUP_2 2 61 | #define KSTORAGE_UNUSED_GROUP_3 3 62 | 63 | #define SUPERCALL_BOOTLOG 0x10fd 64 | #define SUPERCALL_PANIC 0x10fe 65 | #define SUPERCALL_TEST 0x10ff 66 | 67 | #define SUPERCALL_KEY_MAX_LEN 0x40 68 | #define SUPERCALL_SCONTEXT_LEN 0x60 69 | 70 | struct su_profile 71 | { 72 | uid_t uid; 73 | uid_t to_uid; 74 | char scontext[SUPERCALL_SCONTEXT_LEN]; 75 | }; 76 | 77 | #ifdef ANDROID 78 | #define SH_PATH "/system/bin/sh" 79 | #define SU_PATH "/system/bin/kp" 80 | #define LEGACY_SU_PATH "/system/bin/su" 81 | #define ECHO_PATH "/system/bin/echo" 82 | #define KERNELPATCH_DATA_DIR "/data/adb/kp" 83 | #define KERNELPATCH_MODULE_DATA_DIR KERNELPATCH_DATA_DIR "/modules" 84 | #define APD_PATH "/data/adb/apd" 85 | #define ALL_ALLOW_SCONTEXT "u:r:kp:s0" 86 | #define ALL_ALLOW_SCONTEXT_MAGISK "u:r:magisk:s0" 87 | #define ALL_ALLOW_SCONTEXT_KERNEL "u:r:kernel:s0" 88 | #else 89 | #define SH_PATH "/usr/bin/sh" 90 | #define ECHO_PATH "/usr/bin/echo" 91 | #define SU_PATH "/usr/bin/kp" 92 | #define ALL_ALLOW_SCONTEXT "u:r:kernel:s0" 93 | #endif 94 | 95 | #define SU_PATH_MAX_LEN 128 96 | 97 | #define SUPERCMD "/system/bin/truncate" 98 | 99 | #define SAFE_MODE_FLAG_FILE "/dev/.safemode" 100 | 101 | #define SUPERCALL_SU_GRANT_UID 0x1100 102 | #define SUPERCALL_SU_REVOKE_UID 0x1101 103 | #define SUPERCALL_SU_NUMS 0x1102 104 | #define SUPERCALL_SU_LIST 0x1103 105 | #define SUPERCALL_SU_PROFILE 0x1104 106 | #define SUPERCALL_SU_GET_ALLOW_SCTX 0x1105 107 | #define SUPERCALL_SU_SET_ALLOW_SCTX 0x1106 108 | #define SUPERCALL_SU_GET_PATH 0x1110 109 | #define SUPERCALL_SU_RESET_PATH 0x1111 110 | #define SUPERCALL_SU_GET_SAFEMODE 0x1112 111 | 112 | #define SUPERCALL_MAX 0x1200 113 | 114 | #define SUPERCALL_RES_SUCCEED 0 115 | 116 | #define SUPERCALL_HELLO_MAGIC 0x11581158 117 | 118 | #endif 119 | -------------------------------------------------------------------------------- /kernel/patch/include/user_event.h: -------------------------------------------------------------------------------- 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 | /* 3 | * Copyright (C) 2024 bmax121. All Rights Reserved. 4 | */ 5 | 6 | #ifndef _KP_USER_EVENT_H_ 7 | #define _KP_USER_EVENT_H_ 8 | 9 | int report_user_event(const char *event, const char *args); 10 | 11 | #endif -------------------------------------------------------------------------------- /kernel/patch/ksyms/execv.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | static int first_init_execed = 0; 12 | 13 | static void before_first_exec() 14 | { 15 | log_boot("event: %s\n", EXTRA_EVENT_PRE_EXEC_INIT); 16 | } 17 | 18 | // https://elixir.bootlin.com/linux/v6.1/source/fs/exec.c#L2087 19 | // SYSCALL_DEFINE3(execve, const char __user *, filename, const char __user *const __user *, argv, 20 | // const char __user *const __user *, envp) 21 | 22 | // https://elixir.bootlin.com/linux/v6.1/source/fs/exec.c#L2095 23 | // SYSCALL_DEFINE5(execveat, int, fd, const char __user *, filename, const char __user *const __user *, argv, 24 | // const char __user *const __user *, envp, int, flags) 25 | static void before_execve(hook_fargs3_t *args, void *udata) 26 | { 27 | if (first_init_execed) return; 28 | first_init_execed = 1; 29 | before_first_exec(); 30 | 31 | log_boot("kernel stack:\n"); 32 | 33 | uint64_t arg0 = syscall_argn(args, 0); 34 | uint64_t arg1 = syscall_argn(args, 1); 35 | uint64_t arg2 = syscall_argn(args, 2); 36 | uint64_t nr = (uint64_t)udata; 37 | 38 | unsigned long stack = (unsigned long)get_stack(current); 39 | uintptr_t addr = (uintptr_t)(thread_size + stack); 40 | 41 | for (uintptr_t i = addr - sizeof(struct pt_regs) - 0x40; i < addr - 32 * 8; i += 0x10) { 42 | uintptr_t val0 = *(uintptr_t *)i; 43 | uintptr_t val1 = *(uintptr_t *)(i + 0x8); 44 | uintptr_t val2 = *(uintptr_t *)(i + 0x10); 45 | 46 | if ((arg0 == val0) && (val1 == arg1) && (val2 == arg2)) { 47 | struct pt_regs *regs = (struct pt_regs *)i; 48 | if (regs->orig_x0 == arg0 && regs->syscallno == nr && regs->regs[8] == nr) { 49 | pt_regs_offset = addr - i; 50 | break; 51 | } 52 | } 53 | } 54 | log_boot(" pt_regs offset: %x\n", pt_regs_offset); 55 | } 56 | 57 | static void after_execv(hook_fargs5_t *args, void *udata) 58 | { 59 | unhook_syscalln(__NR_execve, before_execve, after_execv); 60 | unhook_syscalln(__NR_execveat, before_execve, after_execv); 61 | } 62 | #define _UNUSED(x) (void)(x) 63 | int resolve_pt_regs() 64 | { 65 | hook_err_t ret = 0; 66 | _UNUSED(ret); 67 | hook_err_t rc = HOOK_NO_ERR; 68 | 69 | rc = hook_syscalln(__NR_execve, 3, before_execve, after_execv, (void *)__NR_execve); 70 | log_boot("hook __NR_execve rc: %d\n", rc); 71 | ret |= rc; 72 | 73 | rc = hook_syscalln(__NR_execveat, 5, before_execve, after_execv, (void *)__NR_execveat); 74 | log_boot("hook __NR_execveat rc: %d\n", rc); 75 | ret |= rc; 76 | 77 | return rc; 78 | } -------------------------------------------------------------------------------- /kernel/patch/module/relo.h: -------------------------------------------------------------------------------- 1 | #ifndef _KP_RELO_H_ 2 | #define _KP_RELO_H_ 3 | 4 | #include 5 | 6 | int apply_relocate_add(Elf64_Shdr *sechdrs, const char *strtab, unsigned int symindex, unsigned int relsec, 7 | struct module *me); 8 | int apply_relocate(Elf64_Shdr *sechdrs, const char *strtab, unsigned int symindex, unsigned int relsec, 9 | struct module *me); 10 | 11 | #endif -------------------------------------------------------------------------------- /kpms/.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Object files 5 | *.o 6 | *.ko 7 | *.obj 8 | *.elf 9 | 10 | # Precompiled Headers 11 | *.gch 12 | *.pch 13 | 14 | # Libraries 15 | *.lib 16 | *.a 17 | *.la 18 | *.lo 19 | 20 | # Shared objects (inc. Windows DLLs) 21 | *.dll 22 | *.so 23 | *.so.* 24 | *.dylib 25 | 26 | build/* 27 | 28 | # 29 | *.bin 30 | *.elf 31 | 32 | *.kpm 33 | 34 | # 35 | private 36 | -------------------------------------------------------------------------------- /kpms/demo-hello/.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Object files 5 | *.o 6 | *.ko 7 | *.obj 8 | *.elf 9 | 10 | # Libraries 11 | *.lib 12 | *.a 13 | *.la 14 | *.lo 15 | 16 | *.bin 17 | *.elf 18 | 19 | *.kpm 20 | -------------------------------------------------------------------------------- /kpms/demo-hello/Makefile: -------------------------------------------------------------------------------- 1 | ifndef TARGET_COMPILE 2 | $(error TARGET_COMPILE not set) 3 | endif 4 | 5 | ifndef KP_DIR 6 | KP_DIR = ../.. 7 | endif 8 | 9 | 10 | CC = $(TARGET_COMPILE)gcc 11 | LD = $(TARGET_COMPILE)ld 12 | 13 | INCLUDE_DIRS := . include patch/include linux/include linux/arch/arm64/include linux/tools/arch/arm64/include 14 | 15 | INCLUDE_FLAGS := $(foreach dir,$(INCLUDE_DIRS),-I$(KP_DIR)/kernel/$(dir)) 16 | 17 | objs := hello.o 18 | 19 | all: hello.kpm 20 | 21 | hello.kpm: ${objs} 22 | ${CC} -r -o $@ $^ 23 | 24 | %.o: %.c 25 | ${CC} $(CFLAGS) $(INCLUDE_FLAGS) -Thello.lds -c -O2 -o $@ $< 26 | 27 | .PHONY: clean 28 | clean: 29 | rm -rf *.kpm 30 | find . -name "*.o" | xargs rm -f -------------------------------------------------------------------------------- /kpms/demo-hello/hello.c: -------------------------------------------------------------------------------- 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 | /* 3 | * Copyright (C) 2023 bmax121. All Rights Reserved. 4 | */ 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | ///< The name of the module, each KPM must has a unique name. 14 | KPM_NAME("kpm-hello-demo"); 15 | 16 | ///< The version of the module. 17 | KPM_VERSION("1.0.0"); 18 | 19 | ///< The license type. 20 | KPM_LICENSE("GPL v2"); 21 | 22 | ///< The author. 23 | KPM_AUTHOR("bmax121"); 24 | 25 | ///< The description. 26 | KPM_DESCRIPTION("KernelPatch Module Example"); 27 | 28 | /** 29 | * @brief hello world initialization 30 | * @details 31 | * 32 | * @param args 33 | * @param reserved 34 | * @return int 35 | */ 36 | static long hello_init(const char *args, const char *event, void *__user reserved) 37 | { 38 | pr_info("kpm hello init, event: %s, args: %s\n", event, args); 39 | pr_info("kernelpatch version: %x\n", kpver); 40 | return 0; 41 | } 42 | 43 | static long hello_control0(const char *args, char *__user out_msg, int outlen) 44 | { 45 | pr_info("kpm hello control0, args: %s\n", args); 46 | char echo[64] = "echo: "; 47 | strncat(echo, args, 48); 48 | compat_copy_to_user(out_msg, echo, sizeof(echo)); 49 | return 0; 50 | } 51 | 52 | static long hello_control1(void *a1, void *a2, void *a3) 53 | { 54 | pr_info("kpm hello control1, a1: %llx, a2: %llx, a3: %llx\n", a1, a2, a3); 55 | return 0; 56 | } 57 | 58 | static long hello_exit(void *__user reserved) 59 | { 60 | pr_info("kpm hello exit\n"); 61 | return 0; 62 | } 63 | 64 | KPM_INIT(hello_init); 65 | KPM_CTL0(hello_control0); 66 | KPM_CTL1(hello_control1); 67 | KPM_EXIT(hello_exit); 68 | -------------------------------------------------------------------------------- /kpms/demo-hello/hello.lds: -------------------------------------------------------------------------------- 1 | SECTIONS { 2 | .plt (NOLOAD) : { BYTE(0) } 3 | .init.plt (NOLOAD) : { BYTE(0) } 4 | .text.ftrace_trampoline (NOLOAD) : { BYTE(0) } 5 | } -------------------------------------------------------------------------------- /kpms/demo-inlinehook/.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Object files 5 | *.o 6 | *.ko 7 | *.obj 8 | *.elf 9 | 10 | # Libraries 11 | *.lib 12 | *.a 13 | *.la 14 | *.lo 15 | 16 | *.bin 17 | *.elf 18 | 19 | *.kpm 20 | -------------------------------------------------------------------------------- /kpms/demo-inlinehook/Makefile: -------------------------------------------------------------------------------- 1 | ifndef TARGET_COMPILE 2 | $(error TARGET_COMPILE not set) 3 | endif 4 | 5 | ifndef KP_DIR 6 | KP_DIR = ../.. 7 | endif 8 | 9 | 10 | CC = $(TARGET_COMPILE)gcc 11 | LD = $(TARGET_COMPILE)ld 12 | 13 | INCLUDE_DIRS := . include patch/include linux/include linux/arch/arm64/include linux/tools/arch/arm64/include 14 | 15 | INCLUDE_FLAGS := $(foreach dir,$(INCLUDE_DIRS),-I$(KP_DIR)/kernel/$(dir)) 16 | 17 | objs := inlinehook.o 18 | 19 | all: inlinehook.kpm 20 | 21 | inlinehook.kpm: ${objs} 22 | ${CC} -r -o $@ $^ 23 | 24 | %.o: %.c 25 | ${CC} $(CFLAGS) $(INCLUDE_FLAGS) -c -O2 -o $@ $< 26 | 27 | .PHONY: clean 28 | clean: 29 | rm -rf *.kpm 30 | find . -name "*.o" | xargs rm -f -------------------------------------------------------------------------------- /kpms/demo-inlinehook/inlinehook.c: -------------------------------------------------------------------------------- 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 | /* 3 | * Copyright (C) 2023 bmax121. All Rights Reserved. 4 | */ 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | KPM_NAME("kpm-inline-hook-demo"); 13 | KPM_VERSION("1.0.0"); 14 | KPM_LICENSE("GPL v2"); 15 | KPM_AUTHOR("bmax121"); 16 | KPM_DESCRIPTION("KernelPatch Module Inline Hook Example"); 17 | 18 | int __noinline add(int a, int b) 19 | { 20 | logkd("origin add called\n"); 21 | int ret = a + b; 22 | return ret; 23 | } 24 | 25 | void before_add(hook_fargs2_t *args, void *udata) 26 | { 27 | logkd("before add arg0: %d, arg1: %d\n", (int)args->arg0, (int)args->arg1); 28 | } 29 | 30 | void after_add(hook_fargs2_t *args, void *udata) 31 | { 32 | logkd("after add arg0: %d, arg1: %d, ret: %d\n", (int)args->arg0, (int)args->arg1, (int)args->ret); 33 | args->ret = 100; 34 | } 35 | 36 | static long inline_hook_demo_init(const char *args, const char *event, void *__user reserved) 37 | { 38 | logkd("kpm inline-hook-demo init\n"); 39 | 40 | int a = 20; 41 | int b = 10; 42 | 43 | int ret = add(a, b); 44 | logkd("%d + %d = %d\n", a, b, ret); 45 | 46 | hook_err_t err = hook_wrap2((void *)add, before_add, after_add, 0); 47 | logkd("hook err: %d\n", err); 48 | 49 | ret = add(a, b); 50 | logkd("%d + %d = %d\n", a, b, ret); 51 | 52 | return 0; 53 | } 54 | 55 | static long inline_hook_control0(const char *args, char *__user out_msg, int outlen) 56 | { 57 | pr_info("kpm control, args: %s\n", args); 58 | return 0; 59 | } 60 | 61 | static long inline_hook_demo_exit(void *__user reserved) 62 | { 63 | unhook((void *)add); 64 | 65 | int a = 20; 66 | int b = 10; 67 | 68 | int ret = add(a, b); 69 | logkd("%d + %d = %d\n", a, b, ret); 70 | 71 | logkd("kpm inline-hook-demo exit\n"); 72 | } 73 | 74 | KPM_INIT(inline_hook_demo_init); 75 | KPM_CTL0(inline_hook_control0); 76 | KPM_EXIT(inline_hook_demo_exit); -------------------------------------------------------------------------------- /kpms/demo-syscallhook/.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Object files 5 | *.o 6 | *.ko 7 | *.obj 8 | *.elf 9 | 10 | # Libraries 11 | *.lib 12 | *.a 13 | *.la 14 | *.lo 15 | 16 | *.bin 17 | *.elf 18 | 19 | *.kpm 20 | -------------------------------------------------------------------------------- /kpms/demo-syscallhook/Makefile: -------------------------------------------------------------------------------- 1 | ifndef TARGET_COMPILE 2 | $(error TARGET_COMPILE not set) 3 | endif 4 | 5 | ifndef KP_DIR 6 | KP_DIR = ../.. 7 | endif 8 | 9 | 10 | CC = $(TARGET_COMPILE)gcc 11 | LD = $(TARGET_COMPILE)ld 12 | 13 | INCLUDE_DIRS := . include patch/include linux/include linux/arch/arm64/include linux/tools/arch/arm64/include 14 | 15 | INCLUDE_FLAGS := $(foreach dir,$(INCLUDE_DIRS),-I$(KP_DIR)/kernel/$(dir)) 16 | 17 | objs := syscallhook.o 18 | 19 | all: syscallhook.kpm 20 | 21 | syscallhook.kpm: ${objs} 22 | ${CC} -r -o $@ $^ 23 | 24 | %.o: %.c 25 | ${CC} $(CFLAGS) $(INCLUDE_FLAGS) -c -O2 -o $@ $< 26 | 27 | .PHONY: clean 28 | clean: 29 | rm -rf *.kpm 30 | find . -name "*.o" | xargs rm -f -------------------------------------------------------------------------------- /make.bat: -------------------------------------------------------------------------------- 1 | @cd kernel 2 | @D:\ndk\prebuilt\windows-x86_64\bin\make clean 3 | @D:\ndk\prebuilt\windows-x86_64\bin\make 4 | @cd .. -------------------------------------------------------------------------------- /patch/.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Object files 5 | *.o 6 | *.ko 7 | *.obj 8 | *.elf 9 | 10 | # Libraries 11 | *.lib 12 | *.a 13 | *.la 14 | *.lo 15 | 16 | *.bin 17 | *.elf 18 | 19 | /build-android 20 | /build-linux -------------------------------------------------------------------------------- /patch/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.22) 2 | project(patch_project) 3 | 4 | set(CMAKE_CXX_STANDARD 17) 5 | set(CMAKE_CXX_STANDARD_REQUIRED ON) 6 | 7 | # 主机编译部分 8 | if(NOT ANDROID) 9 | # 创建构建目录中的必要文件夹 10 | file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/res) 11 | file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/include) 12 | 13 | # 编译encrypt工具(仅主机需要) 14 | add_executable(encrypt encrypt.cpp) 15 | target_compile_options(encrypt PRIVATE -O3) 16 | 17 | # 生成加密文件 18 | add_custom_command( 19 | OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/res/kpimg.enc 20 | COMMAND $ 21 | ${CMAKE_CURRENT_SOURCE_DIR}/res/kpimg 22 | ${CMAKE_CURRENT_BINARY_DIR}/res/kpimg.enc 23 | DEPENDS encrypt 24 | COMMENT "Generating encrypted kpimg" 25 | ) 26 | 27 | # 生成头文件的自定义命令 28 | add_custom_command( 29 | OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/include/kpimg_enc.h 30 | COMMAND xxd -i ${CMAKE_CURRENT_BINARY_DIR}/res/kpimg.enc 31 | > ${CMAKE_CURRENT_BINARY_DIR}/include/kpimg_enc.h 32 | DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/res/kpimg.enc 33 | ) 34 | 35 | add_custom_command( 36 | OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/include/kptools_linux.h 37 | COMMAND xxd -i ${CMAKE_CURRENT_SOURCE_DIR}/res/kptools-linux 38 | > ${CMAKE_CURRENT_BINARY_DIR}/include/kptools_linux.h 39 | DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/res/kptools-linux 40 | ) 41 | 42 | 43 | if(NOT ANDROID) 44 | add_custom_target(generate_headers ALL 45 | DEPENDS 46 | ${CMAKE_CURRENT_BINARY_DIR}/include/kpimg_enc.h 47 | ${CMAKE_CURRENT_BINARY_DIR}/include/kptools_linux.h 48 | ) 49 | else() 50 | add_custom_target(generate_headers ALL 51 | DEPENDS 52 | ${CMAKE_CURRENT_BINARY_DIR}/include/kpimg_enc.h 53 | ${CMAKE_CURRENT_BINARY_DIR}/include/kptools_android.h 54 | ) 55 | endif() 56 | 57 | # 编译主程序(主机版本) 58 | add_executable(patch main.cpp) 59 | target_include_directories(patch PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/include) 60 | add_dependencies(patch generate_headers) 61 | target_link_libraries(patch PRIVATE -static) 62 | endif() 63 | 64 | # Android可执行文件配置 65 | if(ANDROID) 66 | # 生成Android头文件 67 | add_custom_command( 68 | OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/include/kptools_android.h 69 | COMMAND xxd -i ${CMAKE_CURRENT_SOURCE_DIR}/res/kptools-android 70 | > ${CMAKE_CURRENT_BINARY_DIR}/include/kptools_android.h 71 | DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/res/kptools-android 72 | ) 73 | 74 | # 添加Android可执行文件 75 | add_executable(patch_android main.cpp) 76 | target_include_directories(patch_android PRIVATE 77 | ${CMAKE_CURRENT_BINARY_DIR}/include 78 | ) 79 | 80 | # 静态链接配置 81 | set_target_properties(patch_android PROPERTIES 82 | OUTPUT_NAME "patch_android" 83 | ANDROID_ABI ${ANDROID_ABI} 84 | ANDROID_PLATFORM android-24 85 | ANDROID_STL "c++_static" 86 | ) 87 | 88 | set_target_properties(patch_android PROPERTIES 89 | ANDROID_STL "c++_static" 90 | LINK_FLAGS "-static" 91 | ) 92 | 93 | target_link_libraries(patch_android PRIVATE 94 | -lc++_static 95 | ) 96 | 97 | endif() -------------------------------------------------------------------------------- /patch/Makefile: -------------------------------------------------------------------------------- 1 | CC = g++ 2 | ANDROID_CC = aarch64-linux-android-clang++ 3 | CXXFLAGS = -std=c++17 -Iinclude 4 | OPTIMIZE = -O3 5 | ANDROID_SYSROOT = $(ANDROID_NDK_HOME)/toolchains/llvm/prebuilt/linux-x86_64/sysroot 6 | ANDROID_CC = aarch64-linux-android24-clang++ 7 | ANDROID_CFLAGS = -static-libstdc++ -fPIE -pie 8 | ANDROID_LDFLAGS = -lc++_static -lc++fs -landroid_support 9 | 10 | .PHONY: all android clean 11 | 12 | all: patch 13 | 14 | encrypt: encrypt.cpp 15 | $(CC) -o $@ $< $(OPTIMIZE) $(CXXFLAGS) 16 | 17 | res/kpimg.enc: encrypt res/kpimg 18 | ./encrypt res/kpimg res/kpimg.enc 19 | 20 | include/kpimg_enc.h: res/kpimg.enc 21 | mkdir -p include 22 | xxd -i $< > $@ 23 | 24 | include/kptools_linux.h: res/kptools-linux 25 | mkdir -p include 26 | xxd -i $< > $@ 27 | 28 | include/kptools_android.h: res/kptools-android 29 | mkdir -p include 30 | xxd -i $< > $@ 31 | 32 | generate_headers: include/kpimg_enc.h include/kptools_linux.h include/kptools_android.h 33 | 34 | patch: main.cpp generate_headers 35 | $(CC) $(CXXFLAGS) main.cpp -o $@ 36 | 37 | android: generate_headers 38 | $(ANDROID_CC) $(CXXFLAGS) \ 39 | --target=aarch64-none-linux-android24 \ 40 | --sysroot=$(ANDROID_SYSROOT) \ 41 | main.cpp -o patch-android \ 42 | -pie 43 | 44 | android-static: generate_headers 45 | $(ANDROID_CC) $(CXXFLAGS) $(ANDROID_CFLAGS) \ 46 | --target=aarch64-none-linux-android24 \ 47 | --sysroot=$(ANDROID_NDK_HOME)/toolchains/llvm/prebuilt/linux-x86_64/sysroot \ 48 | main.cpp -o patch-android \ 49 | $(ANDROID_LDFLAGS) 50 | 51 | clean: 52 | rm -f encrypt patch patch-android 53 | rm -f res/kpimg.enc 54 | rm -f include/*.h -------------------------------------------------------------------------------- /patch/build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # 设置NDK路径(示例路径,请根据实际情况修改) 3 | export ANDROID_NDK=/root/other/ndk/android-ndk-r28 4 | 5 | xxd -i res/kpimg.enc > include/kpimg_enc.h 6 | xxd -i res/kptools-linux > include/kptools_linux.h 7 | xxd -i res/kptools-android > include/kptools_android.h 8 | 9 | # 创建构建目录 10 | rm -rf build-android 11 | mkdir -p build-android 12 | cd build-android 13 | 14 | # 生成编译配置 15 | cmake .. \ 16 | -DCMAKE_TOOLCHAIN_FILE=${ANDROID_NDK}/build/cmake/android.toolchain.cmake \ 17 | -DCMAKE_BUILD_TYPE=Release \ 18 | -DANDROID_ABI=arm64-v8a \ 19 | -DANDROID_PLATFORM=android-33 \ 20 | -DANDROID_STL=c++_static 21 | 22 | cmake --build . 23 | 24 | cd .. 25 | 26 | rm -rf build-linux 27 | mkdir -p build-linux 28 | cd build-linux 29 | 30 | cmake .. && make 31 | mv patch patch_linux -------------------------------------------------------------------------------- /patch/encrypt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SukiSU-Ultra/SukiSU_KernelPatch_patch/2d9c2a655700b85ddc0c50ce22a7e7bb03b88233/patch/encrypt -------------------------------------------------------------------------------- /patch/encrypt.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | void xor_encrypt(const std::string& input_path, 8 | const std::string& output_path, 9 | uint8_t xor_key = 0xAA) { 10 | std::ifstream in_file(input_path, std::ios::binary); 11 | if (!in_file) { 12 | throw std::runtime_error("无法打开输入文件: " + input_path); 13 | } 14 | 15 | std::vector buffer( 16 | (std::istreambuf_iterator(in_file)), 17 | std::istreambuf_iterator()); 18 | 19 | for (auto& byte : buffer) { 20 | byte ^= xor_key; 21 | } 22 | 23 | std::ofstream out_file(output_path, std::ios::binary); 24 | if (!out_file) { 25 | throw std::runtime_error("无法创建输出文件: " + output_path); 26 | } 27 | 28 | out_file.write(reinterpret_cast(buffer.data()), buffer.size()); 29 | } 30 | 31 | int main(int argc, char* argv[]) { 32 | try { 33 | if (argc != 3) { 34 | std::cerr << "用法: " << argv[0] << " <输入文件> <输出文件>\n"; 35 | return 1; 36 | } 37 | 38 | xor_encrypt(argv[1], argv[2]); 39 | std::cout << "加密成功: " << argv[2] << std::endl; 40 | return 0; 41 | } 42 | catch (const std::exception& e) { 43 | std::cerr << "错误: " << e.what() << std::endl; 44 | return 2; 45 | } 46 | } -------------------------------------------------------------------------------- /patch/include/header.h: -------------------------------------------------------------------------------- 1 | #ifndef HEADER_H 2 | #define HEADER_H 3 | 4 | void execute_in_memory(const std::vector& data, const std::vector& args); 5 | 6 | #endif -------------------------------------------------------------------------------- /patch/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include "include/kpimg_enc.h" 16 | #include "include/kptools_linux.h" 17 | #include "include/kptools_android.h" 18 | #include "include/header.h" 19 | 20 | #if !defined(__NR_memfd_create) 21 | #if __ANDROID_API__ < 29 22 | #define __NR_memfd_create 279 23 | #endif 24 | #endif 25 | 26 | namespace fs = std::filesystem; 27 | 28 | void decrypt_kpimg(std::vector& data) { 29 | for (auto& b : data) b ^= 0xAA; 30 | } 31 | 32 | 33 | int create_memfd(const std::vector& data) { 34 | int fd = syscall(__NR_memfd_create, "kptools_memfd", 0); 35 | if (fd == -1) return -1; 36 | 37 | if (write(fd, data.data(), data.size()) != static_cast(data.size())) { 38 | close(fd); 39 | return -1; 40 | } 41 | 42 | lseek(fd, 0, SEEK_SET); 43 | return fd; 44 | } 45 | 46 | 47 | void execute_in_memory(const std::vector& data, const std::vector& args) { 48 | 49 | int mem_fd = create_memfd(data); 50 | if (mem_fd == -1) { 51 | throw std::runtime_error("Unable to create memory file descriptor"); 52 | } 53 | 54 | 55 | std::vector argv; 56 | argv.push_back(strdup("kptools_mem")); 57 | for (const auto& arg : args) { 58 | argv.push_back(strdup(arg.c_str())); 59 | } 60 | argv.push_back(nullptr); 61 | 62 | 63 | pid_t pid = fork(); 64 | if (pid == 0) { 65 | 66 | fexecve(mem_fd, argv.data(), environ); 67 | _exit(EXIT_FAILURE); 68 | } else if (pid > 0) { 69 | 70 | int status; 71 | waitpid(pid, &status, 0); 72 | } else { 73 | throw std::runtime_error("Fork failed"); 74 | } 75 | 76 | close(mem_fd); 77 | for (auto& arg : argv) free(arg); 78 | } 79 | 80 | void check_file_exists(const std::string& file_path) { 81 | if (!fs::exists(file_path)) { 82 | std::cerr << "Error: File " << file_path << " non-existent" << std::endl; 83 | exit(EXIT_FAILURE); 84 | } 85 | } 86 | 87 | std::string fd_to_path(int fd) { 88 | return "/proc/self/fd/" + std::to_string(fd); 89 | } 90 | 91 | int main() try { 92 | check_file_exists("Image"); 93 | 94 | #ifdef __aarch64__ 95 | std::vector kptools_data(res_kptools_android, res_kptools_android + res_kptools_android_len); 96 | #else 97 | std::vector kptools_data(res_kptools_linux, res_kptools_linux + res_kptools_linux_len); 98 | #endif 99 | 100 | std::vector kpimg_data(res_kpimg_enc, res_kpimg_enc + res_kpimg_enc_len); 101 | decrypt_kpimg(kpimg_data); 102 | int kpimg_mem_fd = create_memfd(kpimg_data); 103 | std::string kpimg_path = fd_to_path(kpimg_mem_fd); 104 | 105 | 106 | std::vector args = { 107 | "-p", 108 | "-s", "123", 109 | "-i", "Image", 110 | "-k", kpimg_path, 111 | "-o", "oImage" 112 | }; 113 | 114 | execute_in_memory(kptools_data, args); 115 | 116 | close(kpimg_mem_fd); 117 | return 0; 118 | } 119 | catch (const std::exception& e) { 120 | std::cerr << "Error: " << e.what() << std::endl; 121 | return EXIT_FAILURE; 122 | } -------------------------------------------------------------------------------- /patch/res/kpimg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SukiSU-Ultra/SukiSU_KernelPatch_patch/2d9c2a655700b85ddc0c50ce22a7e7bb03b88233/patch/res/kpimg -------------------------------------------------------------------------------- /patch/res/kpimg.enc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SukiSU-Ultra/SukiSU_KernelPatch_patch/2d9c2a655700b85ddc0c50ce22a7e7bb03b88233/patch/res/kpimg.enc -------------------------------------------------------------------------------- /patch/res/kptools-android: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SukiSU-Ultra/SukiSU_KernelPatch_patch/2d9c2a655700b85ddc0c50ce22a7e7bb03b88233/patch/res/kptools-android -------------------------------------------------------------------------------- /patch/res/kptools-linux: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SukiSU-Ultra/SukiSU_KernelPatch_patch/2d9c2a655700b85ddc0c50ce22a7e7bb03b88233/patch/res/kptools-linux -------------------------------------------------------------------------------- /patch/res/kptools.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SukiSU-Ultra/SukiSU_KernelPatch_patch/2d9c2a655700b85ddc0c50ce22a7e7bb03b88233/patch/res/kptools.exe -------------------------------------------------------------------------------- /patch/res/start.sh: -------------------------------------------------------------------------------- 1 | g++ -o encrypt encrypt.cpp -O3 -std=c++17 2 | 3 | ./encrypt res/kpimg res/kpimg.enc 4 | 5 | xxd -i res/kpimg.enc > include/kpimg_enc.h 6 | xxd -i res/kptools-linux > include/kptools_linux.h 7 | xxd -i res/kptools-android > include/kptools_android.h 8 | 9 | g++ -std=c++17 main.cpp -o patch 10 | 11 | D:\.gradle\android_sdk\ndk\28.0.13004108\ndk-build NDK_PROJECT_PATH=. APP_BUILD_SCRIPT=./jni/Android.mk 12 | -------------------------------------------------------------------------------- /tools/.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Object files 5 | *.o 6 | *.ko 7 | *.obj 8 | *.elf 9 | 10 | # Precompiled Headers 11 | *.gch 12 | *.pch 13 | 14 | # Libraries 15 | *.lib 16 | *.a 17 | *.la 18 | *.lo 19 | 20 | # Shared objects (inc. Windows DLLs) 21 | *.dll 22 | *.so 23 | *.so.* 24 | *.dylib 25 | 26 | /build/* 27 | 28 | kptools 29 | 30 | # 31 | build_android.sh -------------------------------------------------------------------------------- /tools/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.5) 2 | project(kptools) 3 | 4 | include_directories(${CMAKE_CURRENT_BINARY_DIR}) 5 | 6 | set(SOURCES 7 | image.c 8 | kallsym.c 9 | kptools.c 10 | order.c 11 | insn.c 12 | patch.c 13 | symbol.c 14 | kpm.c 15 | common.c 16 | sha256.c 17 | ) 18 | 19 | add_executable(kptools ${SOURCES}) 20 | 21 | find_package(ZLIB REQUIRED) 22 | 23 | if(CMAKE_SYSTEM_NAME STREQUAL "Linux") 24 | set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static") 25 | # 显式指定静态库路径 26 | target_link_libraries(kptools PRIVATE /usr/lib/x86_64-linux-gnu/libz.a) 27 | elseif(CMAKE_SYSTEM_NAME STREQUAL "Android") 28 | target_link_libraries(kptools PRIVATE ${ZLIB_LIBRARIES}) 29 | else() 30 | target_link_libraries(kptools PRIVATE ${ZLIB_LIBRARIES}) 31 | endif() 32 | 33 | target_include_directories(kptools PRIVATE ${ZLIB_INCLUDE_DIRS}) -------------------------------------------------------------------------------- /tools/Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | 3 | CFLAGS = -std=c11 -Wall -Wextra -Wno-unused -Wno-unused-parameter 4 | LDFLAGS = -static -lz 5 | 6 | ifdef DEBUG 7 | CFLAGS += -DDEBUG -g 8 | endif 9 | 10 | objs := image.o kallsym.o kptools.o order.o insn.o patch.o symbol.o kpm.o common.o 11 | objs += sha256.o 12 | 13 | .PHONY: all 14 | all: kptools 15 | 16 | .PHONY: kptools 17 | kptools: ${objs} 18 | $(CC) -o $@ $^ $(LDFLAGS) 19 | 20 | %.o : %.c 21 | $(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@ 22 | 23 | .PHONY: clean 24 | clean: 25 | rm -rf preset.h 26 | rm -rf kptools 27 | find . -name "*.o" | xargs rm -f -------------------------------------------------------------------------------- /tools/common.c: -------------------------------------------------------------------------------- 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 | /* 3 | * Copyright (C) 2024 bmax121. All Rights Reserved. 4 | */ 5 | 6 | #include "common.h" 7 | #include "order.h" 8 | 9 | bool log_enable = false; 10 | 11 | int can_b_imm(uint64_t from, uint64_t to) 12 | { 13 | // B: 128M 14 | uint32_t imm26 = 1 << 25 << 2; 15 | return (to >= from && to - from <= imm26) || (from >= to && from - to <= imm26); 16 | } 17 | 18 | int b(uint32_t *buf, uint64_t from, uint64_t to) 19 | { 20 | if (can_b_imm(from, to)) { 21 | buf[0] = 0x14000000u | (((to - from) & 0x0FFFFFFFu) >> 2u); 22 | return 4; 23 | } 24 | return 0; 25 | } 26 | 27 | int32_t relo_branch_func(const char *img, int32_t func_offset) 28 | { 29 | uint32_t inst = *(uint32_t *)(img + func_offset); 30 | int32_t relo_offset = func_offset; 31 | if (INSN_IS_B(inst)) { 32 | uint64_t imm26 = bits32(inst, 25, 0); 33 | uint64_t imm64 = sign64_extend(imm26 << 2u, 28u); 34 | relo_offset = func_offset + (int32_t)imm64; 35 | tools_logi("relocate branch function 0x%x to 0x%x\n", func_offset, relo_offset); 36 | } 37 | return relo_offset; 38 | } 39 | 40 | void read_file_align(const char *path, char **con, int *out_len, int align) 41 | { 42 | FILE *fp = fopen(path, "rb"); 43 | if (!fp) tools_log_errno_exit("open file %s\n", path); 44 | fseek(fp, 0, SEEK_END); 45 | int len = (int)ftell(fp); 46 | fseek(fp, 0, SEEK_SET); 47 | int align_len = (int)align_ceil(len, align); 48 | char *buf = (char *)malloc(align_len); 49 | memset(buf + len, 0, align_len - len); 50 | int readlen = fread(buf, 1, len, fp); 51 | if (readlen != len) tools_log_errno_exit("read file %s\n", path); 52 | fclose(fp); 53 | *con = buf; 54 | *out_len = align_len; 55 | } 56 | 57 | void write_file(const char *path, const char *con, int len, bool append) 58 | { 59 | FILE *fout = fopen(path, append ? "ab" : "wb"); 60 | if (!fout) tools_log_errno_exit("open file %s\n", path); 61 | int writelen = fwrite(con, 1, len, fout); 62 | if (writelen != len) tools_log_errno_exit("write file %s\n", path); 63 | fclose(fout); 64 | } 65 | 66 | int64_t int_unpack(void *ptr, int32_t size, bool is_be) 67 | { 68 | bool swp = is_be ^ is_be(); 69 | int64_t res64; 70 | int32_t res32; 71 | int16_t res16; 72 | switch (size) { 73 | case 8: 74 | res64 = *(int64_t *)ptr; 75 | return swp ? i64swp(res64) : res64; 76 | case 4: 77 | res32 = *(int32_t *)ptr; 78 | return swp ? i32swp(res32) : res32; 79 | case 2: 80 | res16 = *(int16_t *)ptr; 81 | return swp ? i16swp(res16) : res16; 82 | default: 83 | return *(int8_t *)ptr; 84 | } 85 | } 86 | 87 | uint64_t uint_unpack(void *ptr, int32_t size, bool is_be) 88 | { 89 | bool swp = is_be ^ is_be(); 90 | uint64_t res64; 91 | uint32_t res32; 92 | uint16_t res16; 93 | switch (size) { 94 | case 8: 95 | res64 = *(uint64_t *)ptr; 96 | return swp ? u64swp(res64) : res64; 97 | case 4: 98 | res32 = *(uint32_t *)ptr; 99 | return swp ? u32swp(res32) : res32; 100 | case 2: 101 | res16 = *(uint16_t *)ptr; 102 | return swp ? u16swp(res16) : res16; 103 | default: 104 | return *(uint8_t *)ptr; 105 | } 106 | } 107 | -------------------------------------------------------------------------------- /tools/common.h: -------------------------------------------------------------------------------- 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 | /* 3 | * Copyright (C) 2024 bmax121. All Rights Reserved. 4 | */ 5 | 6 | #ifndef _KP_TOOL_COMMON_H_ 7 | #define _KP_TOOL_COMMON_H_ 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | #include 16 | 17 | extern bool log_enable; 18 | 19 | #define tools_logi(fmt, ...) \ 20 | if (log_enable) fprintf(stdout, "[+] " fmt, ##__VA_ARGS__); 21 | 22 | #define tools_logw(fmt, ...) \ 23 | if (log_enable) fprintf(stdout, "[?] " fmt, ##__VA_ARGS__); 24 | 25 | #define tools_loge(fmt, ...) \ 26 | if (log_enable) fprintf(stdout, "[-] %s:%d/%s(); " fmt, __FILE__, __LINE__, __func__, ##__VA_ARGS__); 27 | 28 | #define tools_loge_exit(fmt, ...) \ 29 | do { \ 30 | fprintf(stderr, "[-] %s:%d/%s(); " fmt, __FILE__, __LINE__, __func__, ##__VA_ARGS__); \ 31 | exit(EXIT_FAILURE); \ 32 | } while (0) 33 | 34 | #define tools_log_errno_exit(fmt, ...) \ 35 | do { \ 36 | fprintf(stderr, "[-] %s:%d/%s(); " fmt " - %s\n", __FILE__, __LINE__, __func__, ##__VA_ARGS__, \ 37 | strerror(errno)); \ 38 | exit(errno); \ 39 | } while (0) 40 | 41 | #define SZ_4K 0x1000 42 | 43 | #define align_floor(x, align) ((uint64_t)(x) & ~((uint64_t)(align)-1)) 44 | #define align_ceil(x, align) (((uint64_t)(x) + (uint64_t)(align)-1) & ~((uint64_t)(align)-1)) 45 | 46 | #define INSN_IS_B(inst) (((inst) & 0xFC000000) == 0x14000000) 47 | 48 | #define bits32(n, high, low) ((uint32_t)((n) << (31u - (high))) >> (31u - (high) + (low))) 49 | 50 | #define sign64_extend(n, len) \ 51 | (((uint64_t)((n) << (63u - (len - 1))) >> 63u) ? ((n) | (0xFFFFFFFFFFFFFFFF << (len))) : n) 52 | 53 | static inline void set_log_enable(bool enable) 54 | { 55 | log_enable = enable; 56 | } 57 | 58 | int can_b_imm(uint64_t from, uint64_t to); 59 | int b(uint32_t *buf, uint64_t from, uint64_t to); 60 | int32_t relo_branch_func(const char *img, int32_t func_offset); 61 | 62 | void write_file(const char *path, const char *con, int len, bool append); 63 | 64 | void read_file_align(const char *path, char **con, int *len, int align); 65 | 66 | int64_t int_unpack(void *ptr, int32_t size, bool is_be); 67 | uint64_t uint_unpack(void *ptr, int32_t size, bool is_be); 68 | 69 | static inline void read_file(const char *path, char **con, int *len) 70 | { 71 | return read_file_align(path, con, len, 1); 72 | } 73 | 74 | #endif -------------------------------------------------------------------------------- /tools/elf/elf-em.h: -------------------------------------------------------------------------------- 1 | /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ 2 | #ifndef _LINUX_ELF_EM_H 3 | #define _LINUX_ELF_EM_H 4 | 5 | /* These constants define the various ELF target machines */ 6 | #define EM_NONE 0 7 | #define EM_M32 1 8 | #define EM_SPARC 2 9 | #define EM_386 3 10 | #define EM_68K 4 11 | #define EM_88K 5 12 | #define EM_486 6 /* Perhaps disused */ 13 | #define EM_860 7 14 | #define EM_MIPS 8 /* MIPS R3000 (officially, big-endian only) */ 15 | /* Next two are historical and binaries and 16 | modules of these types will be rejected by 17 | Linux. */ 18 | #define EM_MIPS_RS3_LE 10 /* MIPS R3000 little-endian */ 19 | #define EM_MIPS_RS4_BE 10 /* MIPS R4000 big-endian */ 20 | 21 | #define EM_PARISC 15 /* HPPA */ 22 | #define EM_SPARC32PLUS 18 /* Sun's "v8plus" */ 23 | #define EM_PPC 20 /* PowerPC */ 24 | #define EM_PPC64 21 /* PowerPC64 */ 25 | #define EM_SPU 23 /* Cell BE SPU */ 26 | #define EM_ARM 40 /* ARM 32 bit */ 27 | #define EM_SH 42 /* SuperH */ 28 | #define EM_SPARCV9 43 /* SPARC v9 64-bit */ 29 | #define EM_H8_300 46 /* Renesas H8/300 */ 30 | #define EM_IA_64 50 /* HP/Intel IA-64 */ 31 | #define EM_X86_64 62 /* AMD x86-64 */ 32 | #define EM_S390 22 /* IBM S/390 */ 33 | #define EM_CRIS 76 /* Axis Communications 32-bit embedded processor */ 34 | #define EM_M32R 88 /* Renesas M32R */ 35 | #define EM_MN10300 89 /* Panasonic/MEI MN10300, AM33 */ 36 | #define EM_OPENRISC 92 /* OpenRISC 32-bit embedded processor */ 37 | #define EM_ARCOMPACT 93 /* ARCompact processor */ 38 | #define EM_XTENSA 94 /* Tensilica Xtensa Architecture */ 39 | #define EM_BLACKFIN 106 /* ADI Blackfin Processor */ 40 | #define EM_UNICORE 110 /* UniCore-32 */ 41 | #define EM_ALTERA_NIOS2 113 /* Altera Nios II soft-core processor */ 42 | #define EM_TI_C6000 140 /* TI C6X DSPs */ 43 | #define EM_HEXAGON 164 /* QUALCOMM Hexagon */ 44 | #define EM_NDS32 \ 45 | 167 /* Andes Technology compact code size 46 | embedded RISC processor family */ 47 | #define EM_AARCH64 183 /* ARM 64 bit */ 48 | #define EM_TILEPRO 188 /* Tilera TILEPro */ 49 | #define EM_MICROBLAZE 189 /* Xilinx MicroBlaze */ 50 | #define EM_TILEGX 191 /* Tilera TILE-Gx */ 51 | #define EM_ARCV2 195 /* ARCv2 Cores */ 52 | #define EM_RISCV 243 /* RISC-V */ 53 | #define EM_BPF 247 /* Linux BPF - in-kernel virtual machine */ 54 | #define EM_CSKY 252 /* C-SKY */ 55 | #define EM_FRV 0x5441 /* Fujitsu FR-V */ 56 | 57 | /* 58 | * This is an interim value that we will use until the committee comes 59 | * up with a final number. 60 | */ 61 | #define EM_ALPHA 0x9026 62 | 63 | /* Bogus old m32r magic number, used by old tools. */ 64 | #define EM_CYGNUS_M32R 0x9041 65 | /* This is the old interim value for S/390 architecture */ 66 | #define EM_S390_OLD 0xA390 67 | /* Also Panasonic/MEI MN10300, AM33 */ 68 | #define EM_CYGNUS_MN10300 0xbeef 69 | 70 | #endif /* _LINUX_ELF_EM_H */ 71 | -------------------------------------------------------------------------------- /tools/fls_ffs.h: -------------------------------------------------------------------------------- 1 | #ifndef _ASM_GENERIC_BITOPS_FLS_FFS_H_ 2 | #define _ASM_GENERIC_BITOPS_FLS_FFS_H_ 3 | 4 | #include 5 | 6 | #define BITS_PER_LONG 64 7 | 8 | /** 9 | * fls - find last (most-significant) bit set 10 | * @x: the word to search 11 | * 12 | * This is defined the same way as ffs. 13 | * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32. 14 | */ 15 | static inline int fls(uint32_t x) 16 | { 17 | int r = 32; 18 | 19 | if (!x) return 0; 20 | if (!(x & 0xffff0000u)) { 21 | x <<= 16; 22 | r -= 16; 23 | } 24 | if (!(x & 0xff000000u)) { 25 | x <<= 8; 26 | r -= 8; 27 | } 28 | if (!(x & 0xf0000000u)) { 29 | x <<= 4; 30 | r -= 4; 31 | } 32 | if (!(x & 0xc0000000u)) { 33 | x <<= 2; 34 | r -= 2; 35 | } 36 | if (!(x & 0x80000000u)) { 37 | x <<= 1; 38 | r -= 1; 39 | } 40 | return r; 41 | } 42 | 43 | static inline uint64_t __fls(uint64_t word) 44 | { 45 | int num = BITS_PER_LONG - 1; 46 | 47 | #if BITS_PER_LONG == 64 48 | if (!(word & (~0ull << 32))) { 49 | num -= 32; 50 | word <<= 32; 51 | } 52 | #endif 53 | if (!(word & (~0ull << (BITS_PER_LONG - 16)))) { 54 | num -= 16; 55 | word <<= 16; 56 | } 57 | if (!(word & (~0ull << (BITS_PER_LONG - 8)))) { 58 | num -= 8; 59 | word <<= 8; 60 | } 61 | if (!(word & (~0ull << (BITS_PER_LONG - 4)))) { 62 | num -= 4; 63 | word <<= 4; 64 | } 65 | if (!(word & (~0ull << (BITS_PER_LONG - 2)))) { 66 | num -= 2; 67 | word <<= 2; 68 | } 69 | if (!(word & (~0ull << (BITS_PER_LONG - 1)))) num -= 1; 70 | return num; 71 | } 72 | 73 | static inline int fls64(u64 x) 74 | { 75 | if (x == 0) return 0; 76 | return __fls(x) + 1; 77 | } 78 | 79 | /** 80 | * __ffs - find first bit in word. 81 | * @word: The word to search 82 | * 83 | * Undefined if no bit exists, so code should check against 0 first. 84 | */ 85 | static inline uint64_t __ffs(uint64_t word) 86 | { 87 | int num = 0; 88 | 89 | #if BITS_PER_LONG == 64 90 | if ((word & 0xffffffff) == 0) { 91 | num += 32; 92 | word >>= 32; 93 | } 94 | #endif 95 | if ((word & 0xffff) == 0) { 96 | num += 16; 97 | word >>= 16; 98 | } 99 | if ((word & 0xff) == 0) { 100 | num += 8; 101 | word >>= 8; 102 | } 103 | if ((word & 0xf) == 0) { 104 | num += 4; 105 | word >>= 4; 106 | } 107 | if ((word & 0x3) == 0) { 108 | num += 2; 109 | word >>= 2; 110 | } 111 | if ((word & 0x1) == 0) num += 1; 112 | return num; 113 | } 114 | 115 | /** 116 | * ffs - find first bit set 117 | * @x: the word to search 118 | * 119 | * This is defined the same way as 120 | * the libc and compiler builtin ffs routines, therefore 121 | * differs in spirit from the above ffz (man ffs). 122 | */ 123 | static inline int ffs(int x) 124 | { 125 | int r = 1; 126 | 127 | if (!x) return 0; 128 | if (!(x & 0xffff)) { 129 | x >>= 16; 130 | r += 16; 131 | } 132 | if (!(x & 0xff)) { 133 | x >>= 8; 134 | r += 8; 135 | } 136 | if (!(x & 0xf)) { 137 | x >>= 4; 138 | r += 4; 139 | } 140 | if (!(x & 3)) { 141 | x >>= 2; 142 | r += 2; 143 | } 144 | if (!(x & 1)) { 145 | x >>= 1; 146 | r += 1; 147 | } 148 | return r; 149 | } 150 | 151 | #endif -------------------------------------------------------------------------------- /tools/image.h: -------------------------------------------------------------------------------- 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 | /* 3 | * Copyright (C) 2023 bmax121. All Rights Reserved. 4 | */ 5 | 6 | #ifndef _KP_TOOL_IMAGE_H_ 7 | #define _KP_TOOL_IMAGE_H_ 8 | 9 | #include 10 | 11 | // /arch/arm64/kernel/head.S 12 | 13 | typedef struct 14 | { 15 | int8_t is_be; // 0: little, 1: big 16 | int8_t uefi; // 17 | int32_t load_offset; 18 | int32_t kernel_size; 19 | int32_t page_shift; 20 | int32_t b_stext_insn_offset; 21 | int32_t primary_entry_offset; 22 | } kernel_info_t; 23 | 24 | int32_t get_kernel_info(kernel_info_t *kinfo, const char *img, int32_t imglen); 25 | int32_t kernel_resize(kernel_info_t *kinfo, char *img, int32_t size); 26 | 27 | #endif 28 | -------------------------------------------------------------------------------- /tools/kallsym.h: -------------------------------------------------------------------------------- 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 | /* 3 | * Copyright (C) 2023 bmax121. All Rights Reserved. 4 | */ 5 | 6 | #ifndef _KP_TOOL_KALLSYM_H_ 7 | #define _KP_TOOL_KALLSYM_H_ 8 | 9 | #include 10 | 11 | // script/kallsym.c 12 | #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0])) 13 | 14 | #define KSYM_TOKEN_NUMS 256 15 | #define KSYM_SYMBOL_LEN 512 16 | 17 | #define KSYM_MAX_SYMS 1000000 18 | 19 | #define KSYM_MIN_NEQ_SYMS 25600 20 | #define KSYM_MIN_MARKER (KSYM_MIN_NEQ_SYMS / 256) 21 | #define KSYM_FIND_NAMES_USED_MARKER 5 22 | 23 | #define ARM64_RELO_MIN_NUM 4000 24 | 25 | enum ksym_type 26 | { 27 | // Seen in actual kernels 28 | ABSOLUTE = 'A', 29 | BSS = 'B', 30 | DATA = 'D', 31 | RODATA = 'R', 32 | TEXT = 'T', 33 | WEAK_OBJECT_WITH_DEFAULT = 'V', 34 | WEAK_SYMBOL_WITH_DEFAULT = 'W', 35 | // Seen on nm's manpage 36 | SMALL_DATA = 'G', 37 | INDIRECT_FUNCTION = 'I', 38 | DEBUGGING = 'N', 39 | STACK_UNWIND = 'P', 40 | COMMON = 'C', 41 | SMALL_BSS = 'S', 42 | UNDEFINED = 'U', 43 | UNIQUE_GLOBAL = 'u', 44 | WEAK_OBJECT = 'v', 45 | WEAK_SYMBOL = 'w', 46 | STABS_DEBUG = '-', 47 | UNKNOWN = '?', 48 | }; 49 | 50 | enum arch_type 51 | { 52 | ARM64 = 1, 53 | X86_64, 54 | ARM_BE, 55 | ARM_LE, 56 | X86 57 | }; 58 | 59 | enum current_type 60 | { 61 | SP_EL0, 62 | SP 63 | }; 64 | 65 | #define ELF64_KERNEL_MIN_VA 0xffffff8008080000 66 | #define ELF64_KERNEL_MAX_VA 0xffffffffffffffff 67 | 68 | typedef struct 69 | { 70 | enum arch_type arch; 71 | int32_t is_64; 72 | int32_t is_be; 73 | 74 | struct 75 | { 76 | uint8_t _; 77 | uint8_t patch; 78 | uint8_t minor; 79 | uint8_t major; 80 | } version; 81 | 82 | int32_t banner_num; 83 | int32_t linux_banner_offset[4]; 84 | int32_t symbol_banner_idx; 85 | 86 | char *kallsyms_token_table[KSYM_TOKEN_NUMS]; 87 | int32_t asm_long_size; 88 | int32_t asm_PTR_size; 89 | int32_t kallsyms_markers_elem_size; 90 | int32_t kallsyms_num_syms; 91 | 92 | int32_t has_relative_base; 93 | int32_t kallsyms_addresses_offset; 94 | int32_t kallsyms_offsets_offset; 95 | // int32_t kallsyms_relative_base_offset; // maybe 0 96 | int32_t kallsyms_num_syms_offset; 97 | int32_t kallsyms_names_offset; 98 | int32_t kallsyms_markers_offset; 99 | //kallsyms_seqs_of_names // todo: v6.2 100 | int32_t kallsyms_token_table_offset; 101 | int32_t kallsyms_token_index_offset; 102 | 103 | int32_t _approx_addresses_or_offsets_offset; 104 | int32_t _approx_addresses_or_offsets_end; 105 | int32_t _approx_addresses_or_offsets_num; 106 | int32_t _marker_num; 107 | 108 | int32_t try_relo; 109 | int32_t relo_applied; 110 | uint64_t kernel_base; 111 | 112 | int32_t elf64_rela_num; 113 | int32_t elf64_rela_offset; 114 | 115 | int32_t is_kallsysms_all_yes; 116 | enum current_type current_type; 117 | 118 | } kallsym_t; 119 | 120 | int analyze_kallsym_info(kallsym_t *info, char *img, int32_t imglen, enum arch_type arch, int32_t is_64); 121 | int dump_all_symbols(kallsym_t *info, char *img); 122 | int dump_all_ikconfig(char *img, int32_t imglen); 123 | int get_symbol_index_offset(kallsym_t *info, char *img, int32_t index); 124 | int get_symbol_offset_and_size(kallsym_t *info, char *img, char *symbol, int32_t *size); 125 | int get_symbol_offset(kallsym_t *info, char *img, char *symbol); 126 | int on_each_symbol(kallsym_t *info, char *img, void *userdata, 127 | int32_t (*fn)(int32_t index, char type, const char *symbol, int32_t offset, void *userdata)); 128 | 129 | #endif // _KALLSYM_H_ 130 | -------------------------------------------------------------------------------- /tools/kpm.h: -------------------------------------------------------------------------------- 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 | /* 3 | * Copyright (C) 2024 bmax121. All Rights Reserved. 4 | */ 5 | 6 | #ifndef _KP_TOOL_KPM_H_ 7 | #define _KP_TOOL_KPM_H_ 8 | 9 | #include "elf/elf.h" 10 | #include "common.h" 11 | 12 | #define Elf_Shdr Elf64_Shdr 13 | #define Elf_Phdr Elf64_Phdr 14 | #define Elf_Sym Elf64_Sym 15 | #define Elf_Dyn Elf64_Dyn 16 | #define Elf_Ehdr Elf64_Ehdr 17 | #define Elf_Addr Elf64_Addr 18 | #ifdef CONFIG_MODULES_USE_ELF_REL 19 | #define Elf_Rel Elf64_Rel 20 | #endif 21 | #ifdef CONFIG_MODULES_USE_ELF_RELA 22 | #define Elf_Rela Elf64_Rela 23 | #endif 24 | #define ELF_R_TYPE(X) ELF64_R_TYPE(X) 25 | #define ELF_R_SYM(X) ELF64_R_SYM(X) 26 | 27 | #define INFO_EXTRA_KPM_SESSION "[kpm]" 28 | 29 | struct load_info 30 | { 31 | struct 32 | { 33 | const char *base; 34 | uint64_t size; 35 | const char *name, *version, *license, *author, *description; 36 | } info; 37 | Elf_Ehdr *hdr; 38 | uint64_t len; 39 | Elf_Shdr *sechdrs; 40 | char *secstrings, *strtab; 41 | uint64_t symoffs, stroffs; 42 | struct 43 | { 44 | uint32_t sym, str, mod, info; 45 | } index; 46 | }; 47 | 48 | typedef struct 49 | { 50 | const char *name, *version, *license, *author, *description; 51 | } kpm_info_t; 52 | 53 | int get_kpm_info(const char *kpm, int len, kpm_info_t *info); 54 | 55 | void print_kpm_info(kpm_info_t *info); 56 | int print_kpm_info_path(const char *kpm_path); 57 | 58 | #endif -------------------------------------------------------------------------------- /tools/order.c: -------------------------------------------------------------------------------- 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 | /* 3 | * Copyright (C) 2023 bmax121. All Rights Reserved. 4 | */ 5 | 6 | #include "order.h" 7 | 8 | inline uint16_t u16swp(uint16_t val) 9 | { 10 | return (val << 8) | (val >> 8); 11 | } 12 | 13 | inline int16_t i16swp(int16_t val) 14 | { 15 | return (val << 8) | ((val >> 8) & 0xFF); 16 | } 17 | 18 | uint16_t u16le(uint16_t val) 19 | { 20 | return is_be() ? u16swp(val) : val; 21 | } 22 | 23 | uint16_t u16be(uint16_t val) 24 | { 25 | return is_be() ? val : u16swp(val); 26 | } 27 | 28 | int16_t i16le(int16_t val) 29 | { 30 | return is_be() ? i16swp(val) : val; 31 | } 32 | 33 | int16_t i16be(int16_t val) 34 | { 35 | return is_be() ? val : i16swp(val); 36 | } 37 | 38 | uint32_t u32swp(uint32_t val) 39 | { 40 | val = ((val << 8) & 0xFF00FF00) | ((val >> 8) & 0xFF00FF); 41 | return (val << 16) | (val >> 16); 42 | } 43 | 44 | int32_t i32swp(int32_t val) 45 | { 46 | val = ((val << 8) & 0xFF00FF00) | ((val >> 8) & 0xFF00FF); 47 | return (val << 16) | ((val >> 16) & 0xFFFF); 48 | } 49 | 50 | uint32_t u32le(uint32_t val) 51 | { 52 | return is_be() ? u32swp(val) : val; 53 | } 54 | 55 | uint32_t u32be(uint32_t val) 56 | { 57 | return is_be() ? val : u32swp(val); 58 | } 59 | 60 | int32_t i32le(int32_t val) 61 | { 62 | return is_be() ? i32swp(val) : val; 63 | } 64 | 65 | int32_t i32be(int32_t val) 66 | { 67 | return is_be() ? val : i32swp(val); 68 | } 69 | 70 | int64_t i64swp(int64_t val) 71 | { 72 | val = ((val << 8) & 0xFF00FF00FF00FF00ULL) | ((val >> 8) & 0x00FF00FF00FF00FFULL); 73 | val = ((val << 16) & 0xFFFF0000FFFF0000ULL) | ((val >> 16) & 0x0000FFFF0000FFFFULL); 74 | return (val << 32) | ((val >> 32) & 0xFFFFFFFFULL); 75 | } 76 | 77 | uint64_t u64swp(uint64_t val) 78 | { 79 | val = ((val << 8) & 0xFF00FF00FF00FF00ULL) | ((val >> 8) & 0x00FF00FF00FF00FFULL); 80 | val = ((val << 16) & 0xFFFF0000FFFF0000ULL) | ((val >> 16) & 0x0000FFFF0000FFFFULL); 81 | return (val << 32) | (val >> 32); 82 | } 83 | 84 | int64_t i64le(int64_t val) 85 | { 86 | return is_be() ? i64swp(val) : val; 87 | } 88 | 89 | int64_t i64be(int64_t val) 90 | { 91 | return is_be() ? val : i64swp(val); 92 | } 93 | 94 | uint64_t u64le(uint64_t val) 95 | { 96 | return is_be() ? u64swp(val) : val; 97 | } 98 | 99 | uint64_t u64be(uint64_t val) 100 | { 101 | return is_be() ? val : u64swp(val); 102 | } 103 | -------------------------------------------------------------------------------- /tools/order.h: -------------------------------------------------------------------------------- 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 | /* 3 | * Copyright (C) 2023 bmax121. All Rights Reserved. 4 | */ 5 | 6 | #ifndef _KP_TOOL_ORDER_H_ 7 | #define _KP_TOOL_ORDER_H_ 8 | 9 | #include 10 | 11 | #define is_be() (*(unsigned char *)&(uint16_t){ 1 } ? 0 : 1) 12 | 13 | int16_t i16swp(int16_t val); 14 | int16_t i16le(int16_t val); 15 | int16_t i16be(int16_t val); 16 | 17 | uint16_t u16swp(uint16_t val); 18 | uint16_t u16le(uint16_t val); 19 | uint16_t u16be(uint16_t val); 20 | 21 | int32_t i32swp(int32_t val); 22 | int32_t i32le(int32_t val); 23 | int32_t i32be(int32_t val); 24 | 25 | uint32_t u32swp(uint32_t val); 26 | uint32_t u32le(uint32_t val); 27 | uint32_t u32be(uint32_t val); 28 | 29 | int64_t i64swp(int64_t val); 30 | int64_t i64le(int64_t val); 31 | int64_t i64be(int64_t val); 32 | 33 | uint64_t u64swp(uint64_t val); 34 | uint64_t u64le(uint64_t val); 35 | uint64_t u64be(uint64_t val); 36 | 37 | #endif // _ORDER_H_ -------------------------------------------------------------------------------- /tools/patch.h: -------------------------------------------------------------------------------- 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 | /* 3 | * Copyright (C) 2024 bmax121. All Rights Reserved. 4 | */ 5 | 6 | #ifndef _KP_TOOL_PATCH_H_ 7 | #define _KP_TOOL_PATCH_H_ 8 | 9 | #include 10 | #include 11 | #include 12 | 13 | #include "preset.h" 14 | #include "image.h" 15 | 16 | #define INFO_KERNEL_IMG_SESSION "[kernel]" 17 | #define INFO_KP_IMG_SESSION "[kpimg]" 18 | #define INFO_ADDITIONAL_SESSION "[additional]" 19 | #define INFO_EXTRA_SESSION "[extras]" 20 | #define INFO_EXTRA_SESSION_N "[extra %d]" 21 | 22 | #define EXTRA_ITEM_MAX_NUM 32 23 | 24 | typedef struct 25 | { 26 | const char *kimg; 27 | int32_t kimg_len; 28 | int32_t ori_kimg_len; 29 | const char *banner; 30 | kernel_info_t kinfo; 31 | preset_t *preset; 32 | int32_t embed_item_num; 33 | patch_extra_item_t *embed_item[EXTRA_ITEM_MAX_NUM]; 34 | } patched_kimg_t; 35 | 36 | typedef struct 37 | { 38 | int32_t extra_type; 39 | bool is_path; 40 | union 41 | { 42 | const char *path; 43 | const char *name; 44 | }; 45 | const char *set_args; 46 | const char *set_name; 47 | const char *set_event; 48 | int32_t priority; 49 | const char *data; 50 | patch_extra_item_t *item; 51 | } extra_config_t; 52 | 53 | typedef struct 54 | { 55 | char *kfile, *kimg; 56 | int32_t kfile_len, kimg_len; 57 | bool is_uncompressed_img; 58 | } kernel_file_t; 59 | 60 | void read_kernel_file(const char *path, kernel_file_t *kernel_file); 61 | void new_kernel_file(kernel_file_t *kernel_file, kernel_file_t *old, int32_t kimg_len, bool is_different_endian); 62 | void update_kernel_file_img_len(kernel_file_t *kernel_file, int32_t kimg_len, bool is_different_endian); 63 | void write_kernel_file(kernel_file_t *kernel_file, const char *path); 64 | void free_kernel_file(kernel_file_t *kernel_file); 65 | 66 | preset_t *get_preset(const char *kimg, int kimg_len); 67 | 68 | uint32_t get_kpimg_version(const char *kpimg_path); 69 | int extra_str_type(const char *extra_str); 70 | const char *extra_type_str(extra_item_type extra_type); 71 | int patch_update_img(const char *kimg_path, const char *kpimg_path, const char *out_path, const char *superkey, 72 | bool root_skey, const char **additional, extra_config_t *extra_configs, int extra_config_num); 73 | int unpatch_img(const char *kimg_path, const char *out_path); 74 | int reset_key(const char *kimg_path, const char *out_path, const char *key); 75 | int dump_kallsym(const char *kimg_path); 76 | int dump_ikconfig(const char *kimg_path); 77 | 78 | int print_kp_image_info_path(const char *kpimg_path); 79 | int print_image_patch_info(patched_kimg_t *pimg); 80 | int print_image_patch_info_path(const char *kimg_path); 81 | 82 | #endif 83 | -------------------------------------------------------------------------------- /tools/sha256.h: -------------------------------------------------------------------------------- 1 | /********************************************************************* 2 | * Filename: sha256.h 3 | * Author: Brad Conte (brad AT bradconte.com) 4 | * Copyright: 5 | * Disclaimer: This code is presented "as is" without any guarantees. 6 | * Details: Defines the API for the corresponding SHA1 implementation. 7 | *********************************************************************/ 8 | 9 | #ifndef SHA256_H 10 | #define SHA256_H 11 | 12 | /*************************** HEADER FILES ***************************/ 13 | #include 14 | #include 15 | 16 | /****************************** MACROS ******************************/ 17 | #define SHA256_BLOCK_SIZE 32 // SHA256 outputs a 32 byte digest 18 | 19 | /**************************** DATA TYPES ****************************/ 20 | typedef unsigned char BYTE; // 8-bit byte 21 | typedef unsigned int WORD; // 32-bit word, change to "long" for 16-bit machines 22 | 23 | typedef struct 24 | { 25 | BYTE data[64]; 26 | WORD datalen; 27 | unsigned long long bitlen; 28 | WORD state[8]; 29 | } SHA256_CTX; 30 | 31 | /*********************** FUNCTION DECLARATIONS **********************/ 32 | void sha256_init(SHA256_CTX *ctx); 33 | void sha256_update(SHA256_CTX *ctx, const BYTE data[], size_t len); 34 | void sha256_final(SHA256_CTX *ctx, BYTE hash[]); 35 | 36 | #endif // SHA256_H -------------------------------------------------------------------------------- /tools/symbol.h: -------------------------------------------------------------------------------- 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 | /* 3 | * Copyright (C) 2024 bmax121. All Rights Reserved. 4 | */ 5 | 6 | #ifndef _KP_TOOL_SYBMOL_H_ 7 | #define _KP_TOOL_SYBMOL_H_ 8 | 9 | #include 10 | #include 11 | 12 | #include "image.h" 13 | #include "order.h" 14 | #include "kallsym.h" 15 | #include "preset.h" 16 | 17 | int32_t get_symbol_offset_zero(kallsym_t *info, char *img, char *symbol); 18 | int32_t get_symbol_offset_exit(kallsym_t *info, char *img, char *symbol); 19 | int32_t find_suffixed_symbol(kallsym_t *kallsym, char *img_buf, const char *symbol); 20 | void select_map_area(kallsym_t *kallsym, char *image_buf, int32_t *map_start, int32_t *max_size); 21 | int fillin_map_symbol(kallsym_t *kallsym, char *img_buf, map_symbol_t *symbol, int32_t target_is_be); 22 | int fillin_patch_config(kallsym_t *kallsym, char *img_buf, int imglen, patch_config_t *symbol, int32_t target_is_be, 23 | bool is_android); 24 | 25 | #endif -------------------------------------------------------------------------------- /version: -------------------------------------------------------------------------------- 1 | #define MAJOR 0 2 | #define MINOR 11 3 | #define PATCH 3 4 | --------------------------------------------------------------------------------