Giter VIP home page Giter VIP logo

kernelpatch's Introduction

KernelPatch

Patching and hooking the Linux kernel with only stripped Linux kernel image.

 _  __                    _ ____       _       _     
| |/ /___ _ __ _ __   ___| |  _ \ __ _| |_ ___| |__  
| ' // _ \ '__| '_ \ / _ \ | |_) / _` | __/ __| '_ \ 
| . \  __/ |  | | | |  __/ |  __/ (_| | || (__| | | |
|_|\_\___|_|  |_| |_|\___|_|_|   \__,_|\__\___|_| |_|
  • Obtain all symbol information without source code and symbol information.
  • Inject arbitrary code into the kernel. (Static patching the kernel image or Runtime dynamic loading).
  • Kernel function inline hook and syscall table hook are provided.
  • Additional SU for Android.

If you are using Android, APatch would be a better choice.

Requirement

CONFIG_KALLSYMS=y

Supported Versions

Currently only supports arm64 architecture.

Linux 3.18 - 6.2 (theoretically)
Linux 6.3+ (not yet adapted)

Get Involved

More Information

Documentation

Credits

  • vmlinux-to-elf: Some ideas for parsing kernel symbols.
  • android-inline-hook: Some code for fixing arm64 inline hook instructions.
  • tlsf: Memory allocator used for KPM. (Need another to allocate ROX memory.)

License

KernelPatch is licensed under the GNU General Public License (GPL) 2.0 (https://www.gnu.org/licenses/old-licenses/gpl-2.0.html).

kernelpatch's People

Contributors

admirepowered avatar affggh avatar bmax121 avatar forenche avatar pomelohan avatar qwerty472123 avatar sekaiacg avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

kernelpatch's Issues

Patch Failed (Device Name ) (Perangkat Kerenl Versi) ( redmi note 7 /lavender) (4.4.205)

The following two information are what I need to fix the problem

  1. Your boot.img or kernel image

Upload to here or file download path

  1. The real symbol informations corresponding to your boot.img or kernel
    It can be obtained through the following two commands under root.
echo 1 > /proc/sys/kernel/kptr_restrict
cat /proc/kallsyms

Upload to here or file download path

Patch Failed nothing phone 2 custom kernel 5.10

The following two information are what I need to fix the problem

  1. Your boot.img or kernel image

Upload to here or file download path

  1. The real symbol informations corresponding to your boot.img or kernel
    It can be obtained through the following two commands under root.
echo 1 > /proc/sys/kernel/kptr_restrict
cat /proc/kallsyms

Upload to here or file download path

Uploading with image file with extra .zip extension added, the file is an image though.
arter97-kernel-r7-boot.img.zip

Patch Failed (Redmi Note 9 / Merlinx) (4.14.332)

First, confirm whether your kernel has CONFIG_KALLSYMS_ALL=y enabled. If not or cannot be sure, please wait for support.

The following two information are what I need to fix the problem

  1. Your boot.img or kernel image

Upload to here or file download path
CrDroid.zip

  1. The real symbol informations corresponding to your boot.img or kernel
    It can be obtained through the following two commands under root.
echo 1 > /proc/sys/kernel/kptr_restrict
cat /proc/kallsyms

Upload to here or file download path
n/a

Patch Failed (Device Name) (Device Kerenl Version)

The following two information are what I need to fix the problem

  1. Your boot.img or kernel image

Upload to here or file download path

  1. The real symbol informations corresponding to your boot.img or kernel
    It can be obtained through the following two commands under root.
echo 1 > /proc/sys/kernel/kptr_restrict
cat /proc/kallsyms

Upload to here or file download path

Patch Failed (Poco M3) (Custom Kernel 4.19 )

The following two information are what I need to fix the problem

  1. Your kernel image. boot.img is too big, you can use 'magiskboot unpack boot.img' to get kernel image (which named 'kernel' after unpack)

boot.zip

  1. The real symbol informations corresponding to your boot.img or kernel
    It can be obtained through the following two commands under root.
echo 1 > /proc/sys/kernel/kptr_restrict
cat /proc/kallsyms

cat kallsyms.txt

Screenshot_20240203-093456_APatch

APatch kernel 5.15.94 bootlooping in Xiaomi 13.

Device: Xiaomi 13
ROM: HyperOS 1.0.6.0 UMCCNXM
Explanation of the problem: I've tried many ways to make the patched kernel work on my phone. First I tried the advice from the telegram group on bootloop_fix, it didn't help. After that, I tried to patch the kernel with magisk first, and then patch APatch, which also did not give results. Then I took the kernel from KernelSU and also tried to patch it, also no point either. I can't provide logs, because even my recovery doesn't boot and the phone stuck on the phone logo.
If you need to provide the images that I flashed, I can send an archive with the assembly of all boot.img

compile kpm using ndk and solve errors/kpm使用ndk编译的方法和错误解决

Example: syscallhook

info

error1:unsupported RELA relocation: 311
Solution:Add cflag: -fno-PIC

1

error2:overflow in relocation type R_AARCH64_PREL32 val ffffffecb1c00000"
Solution:Add cflag: -fno-asynchronous-unwind-tables

2

@bmax121

case R_AARCH64_PREL32:
ovf = reloc_data(RELOC_OP_PREL, loc, val, 32);
break;


Makefile:

ifndef KP_DIR
    KP_DIR = ../..
endif

CFLAGS = -O3 -fno-PIC -fno-asynchronous-unwind-tables -fno-stack-protector
CC = ${ANDROID_NDK}/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android31-clang
LD = ${ANDROID_NDK}/toolchains/llvm/prebuilt/linux-x86_64/bin/ld.lld
STRIP = ${ANDROID_NDK}/toolchains/llvm/prebuilt/linux-x86_64/bin/llvm-strip

INCLUDE_DIRS := . include patch/include linux/include linux/arch/arm64/include linux/tools/arch/arm64/include

INCLUDE_FLAGS := $(foreach dir,$(INCLUDE_DIRS),-I$(KP_DIR)/kernel/$(dir))

objs := syscallhook.o

all: syscallhook.kpm

syscallhook.kpm: ${objs}
	${CC} $(CFLAGS) $(INCLUDE_FLAGS) -r -o $@ $^
	${STRIP} -g $@

%.o: %.c
	${CC} $(CFLAGS) $(INCLUDE_FLAGS) -c -o $@ $<

.PHONY: clean
clean:
	rm -rf *.kpm
	find . -name "*.o" | xargs rm -f

Configuration cannot be initialized

There is no post-fs-data-init in the following log.

[    0.000000] KP Kernel pa: 80080000
[    0.000000] KP Kernel va: ffffff8008080000
[    0.000000] KP Kernel Version: 40499
[    0.000000] KP Kernel Patch Version: a00
[    0.000000] KP Kernel Patch Config: 2
[    0.000000] KP Kernel Patch Compile Time: 06:23:21 Feb 23 2024
......
[    3.752198] KP exec /init first stage
[    3.752203] KP write kpatch to /dev/kpatch
[    3.752418] KP after kernel_init ...
[    3.826939] KP exec /init second stage 1
[    3.870921] KP redirect rc file: 10
[    3.870933] KP restore rc file: 1b
[    3.870938] [+] KP V Wrap func pointer remove: ffffff80090021c0, ffffff8105f929e0, ffffff8105f92734
[    3.870945] [+] KP V Unwrap func pointer: ffffff80090021c0, ffffff8105f929e0, ffffff8105f92734
[    6.388467] [+] KP I commit_su: pid: 626, tgid: 0, to_uid: 0, sctx: (null), via_hook: 1
[    6.389158] [+] KP I commit_su: pid: 627, tgid: 0, to_uid: 0, sctx: (null), via_hook: 1
[    6.394095] [+] KP I hello1158
[    6.394118] [+] KP I commit_su: pid: 627, tgid: 0, to_uid: 0, sctx: u:r:magisk:s0, via_hook: 1
[    6.781948] [+] KP I user log: 627 wait /data/adb/apd post-fs-data  status: 0x0
[    6.808291] [+] KP I user log: 627 wait dmesg status: 0x0
[    7.862346] [+] KP I commit_su: pid: 992, tgid: 0, to_uid: 0, sctx: (null), via_hook: 1
[    7.868713] [+] KP I hello1158
[    7.868748] [+] KP I commit_su: pid: 992, tgid: 0, to_uid: 0, sctx: u:r:magisk:s0, via_hook: 1
[    7.895479] [+] KP I user log: 992 wait /data/adb/apd services  status: 0x0
[    8.026224] [+] KP I user log: 992 wait dmesg status: 0x0
[   22.869895] [+] KP I commit_su: pid: 3149, tgid: 0, to_uid: 0, sctx: (null), via_hook: 1
[   22.884905] [+] KP I hello1158
[   22.884933] [+] KP I commit_su: pid: 3149, tgid: 0, to_uid: 0, sctx: u:r:magisk:s0, via_hook: 1
[   22.894944] [+] KP I user log: 3149 wait /data/adb/apd boot-completed  status: 0x0
[   22.965873] [+] KP I user log: 3149 wait dmesg status: 0x0
[   35.154332] KP exec app_process, /data prepared, second_stage: 1

The /dev/kpatch file is successfully extracted, but does not exist.

[    3.752203] KP write kpatch to /dev/kpatch

sagit:/data/adb/ap # ls -al /dev/kpatch
ls: /dev/kpatch: No such file or directory

log file:
kpatch_0.log

[    6.388122] init: starting service 'exec 6 (/system/bin/truncate a12345678 /dev/kpatch a12345678 android_user post-fs-data-init -k)'...
[    6.388467] [+] KP I commit_su: pid: 626, tgid: 0, to_uid: 0, sctx: (null), via_hook: 1
[    6.388505] init: SVC_EXEC pid 626 (uid 0 gid 0+0 context default) started; waiting...
[    6.388508] init: cannot execve('/system/bin/truncate'): No such file or directory
[    6.388772] init: Service 'exec 6 (/system/bin/truncate a12345678 /dev/kpatch a12345678 android_user post-fs-data-init -k)' (pid 626) exited with status 127 waiting took 0.000000 seconds
[    6.388888] init: starting service 'exec 7 (/system/bin/truncate a12345678 /data/adb/kpatch a12345678 android_user post-fs-data -k)'...
[    6.389158] [+] KP I commit_su: pid: 627, tgid: 0, to_uid: 0, sctx: (null), via_hook: 1
[    6.389201] init: SVC_EXEC pid 627 (uid 0 gid 0+0 context default) started; waiting...
[    6.394095] [+] KP I hello1158

There is sth wrong with boot-complete trigger

I tried to study code, and change kernel/patch/android/kpuserd.c,

diff --git a/kernel/patch/android/kpuserd.c b/kernel/patch/android/kpuserd.c
index aba3099..359dc50 100644
--- a/kernel/patch/android/kpuserd.c
+++ b/kernel/patch/android/kpuserd.c
@@ -1,5 +1,5 @@
 /* SPDX-License-Identifier: GPL-2.0-or-later */
-/* 
+/*
  * Copyright (C) 2023 bmax121. All Rights Reserved.
  */
 
@@ -36,7 +36,8 @@ const char replace_rc_file[] = "/dev/.atrace.rc";
 
 static const char patch_rc[] = ""
                                "on late-init\n"
-                               "    rm %s \n"
+                               "    ls %s \n"
+                               "    touch /sdcard/Download/touch1.txt\n"
                                "on post-fs-data\n"
                                "    start logd\n"
                                "    exec -- " KPATCH_SHADOW_PATH " %s android_user init -k\n"
@@ -46,7 +47,7 @@ static const char patch_rc[] = ""
                                "on property:vold.decrypt=trigger_restart_framework\n"
                                "    exec -- " KPATCH_SHADOW_PATH " %s android_user services -k'\n"
                                "on property:sys.boot_completed=1\n"
-                               "    exec -- " KPATCH_SHADOW_PATH " %s android_user boot-completed -k'\n"
+                               "    exec -- /data/adb/boot-completed.d/hello\n"
                                "\n"
                                "";
 
@@ -276,4 +277,4 @@ int kpuserd_init()
 
 out:
     return rc;
-}
\ No newline at end of file
+

I want to take over boot-complete event directly, but it does not work. Why ? I want to know, please help.

hello handler is:

cat /data/adb/boot-completed.d/hello
am start -p com.miui.notes
date > /sdcard/Download/magic.txt

BTW, If I revert the above patch, boot complete trigger can launch miui notes APP with help of apd shipped by Apatch, but it's not 100% successful upon each reboot.

Patch Failed (tabs7 wifi) (4.19.113-27114284) 1# fri oct 6 2023

First, confirm whether your kernel has CONFIG_KALLSYMS_ALL=y enabled. If not or cannot be sure, please wait for support.

The following two information are what I need to fix the problem

  1. Your boot.img or kernel image

Upload to here or file download path
boot.zip

  1. The real symbol informations corresponding to your boot.img or kernel
    It can be obtained through the following two commands under root.
echo 1 > /proc/sys/kernel/kptr_restrict
cat /proc/kallsyms

Upload to here or file download path
log.txt

There is a risk of leakage of superkey.

When reporting problems with various systems/third-party apps, users will be asked to upload log files. If there is a superkey in the log, there is a risk of leakage.

example:
[ 6.388122] init: starting service 'exec 6 (/system/bin/truncate a12345678 /dev/kpatch a12345678 android_user post-fs-data-init -k)'...

kernelpatch加载ko驱动问题

作者你好,目前发现kpatch似乎存在部分问题。
1.使用abd shell命令,insmod xxx.ko显示成功,但是无法通过sh使用驱动读取数据。
2.在使用termux安装ko驱动后,使用sh脚本读取游戏数据的过程中,似乎su权限不是持续的或者ko驱动在安装一段时间后会无效,导致sh脚本在十多分钟后会被关闭😂请问会有机会修复这个问题吗。

Patch Failed (Mi Mix 2s/Polaris) (4.9.248)

The following two information are what I need to fix the problem

  1. Your boot.img or kernel image

Upload to here or file download path

  1. The real symbol informations corresponding to your boot.img or kernel
    It can be obtained through the following two commands under root.
echo 1 > /proc/sys/kernel/kptr_restrict
cat /proc/kallsyms

Upload to here or file download path
boot.img.zip

A bin for patch the boot dron recovery

Hello, I want you to provide a bin which has the utility of patching the boot from the recovery since I am a recovery builder and until now I can't get it together because I don't fully understand your tool

Patch Failed (Device Name) (Device Kerenl Version)

The following two information are what I need to fix the problem

  1. Your kernel image. boot.img is too big, you can use 'magiskboot unpack boot.img' to get kernel image (which named 'kernel' after unpack)

Upload to here or file download path

  1. The real symbol informations corresponding to your boot.img or kernel
    It can be obtained through the following two commands under root.
echo 1 > /proc/sys/kernel/kptr_restrict
cat /proc/kallsyms

Upload to here or file download path

Patch Failed (Redmi Note 10 Pro 'chopin') (4.14.186)

The following two information are what I need to fix the problem

  1. Your kernel image. boot.img is too big, you can use 'magiskboot unpack boot.img' to get kernel image (which named 'kernel' after unpack)

!!!This is a 7zip file and you should rename it to kernel.7z and open it!!!

kernel.7z.zip

  1. The real symbol informations corresponding to your boot.img or kernel
    It can be obtained through the following two commands under root.
echo 1 > /proc/sys/kernel/kptr_restrict
cat /proc/kallsyms

!!!This is a 7zip file and you should rename it to kallsyms.7z and open it!!!

kallsyms.7z.zip

Redmi Note 10 Pro named as 'chopin'. A MTK cpu device which has version 4.14.186 kernel.
kernel config is here:

CONFIG_KALLSYMS=y
# CONFIG_KALLSYMS_ALL is not set
# CONFIG_KALLSYMS_ABSOLUTE_PERCPU is not set
# CONFIG_KALLSYMS_BASE_RELATIVE is not set

Screenshot_2024-02-15-00-20-55-071_me bmax apatch

Help: how to run kpatch without APP Apatch

please forgive me to use "Feature request" type to issue this.

Is it possible to run kpatch in android directly, like in adb shell ?

I try to download kpatch to /data/bootchart, then assign exec permission by 'chmod +x kpatch', but failed.

venus:/data/bootchart $ ls -l
total 28
-rwxrwx--x 1 shell shell 27352 2024-01-20 20:45 kpatch
venus:/data/bootchart $ ./kpatch
/system/bin/sh: ./kpatch: can't execute: Permission denied

Patch Failed (one plus 9 pro) (Device Kerenl Version5.4.242 Aospa.Vislal@aserv)

The following two information are what I need to fix the problem

  1. Your boot.img or kernel image

Upload to here or file download path

  1. The real symbol informations corresponding to your boot.img or kernel
    It can be obtained through the following two commands under root.
echo 1 > /proc/sys/kernel/kptr_restrict
cat /proc/kallsyms

Upload to here or file download path
Kallsyms.txt
https://drive.google.com/drive/folders/1J0GKGqAR8rwLGC5gT7xzsv7XfsbCnWjt

一加9pro。color14.0.0.500,内核修补失败

The following two information are what I need to fix the problem

  1. Your kernel image. boot.img is too big, you can use 'magiskboot unpack boot.img' to get kernel image (which named 'kernel' after unpack)

boot下载链接:https://wwp.lanzoui.com/isObk1qz4h6f

Upload to here or file download path

  1. The real symbol informations corresponding to your boot.img or kernel
    It can be obtained through the following two commands under root.
echo 1 > /proc/sys/kernel/kptr_restrict
cat /proc/kallsyms

1.txt

Upload to here or file download path
1
2

Error de parche (one plus 9pro) (Dispositivo Kerenl 5.4.242.Aospa.(jake@aserv)

untitled.txt
First, confirm whether your kernel has CONFIG_KALLSYMS_ALL=y enabled. If not or cannot be sure, please wait for support.

The following two information are what I need to fix the problem

  1. Your boot.img or kernel image

Upload to here or file download path

  1. The real symbol informations corresponding to your boot.img or kernel
    It can be obtained through the following two commands under root.
echo 1 > /proc/sys/kernel/kptr_restrict
cat /proc/kallsyms

untitled.txt
https://drive.google.com/drive/folders/1J0GKGqAR8rwLGC5gT7xzsv7XfsbCnWjt

Upload to here or file download path

Patch Failed (one plus 9pro) (Device Kerenl 5.4.242.Aospa.Vishal@aserv)

The following two information are what I need to fix the problem

  1. Your boot.img or kernel image

Upload to here or file download path

  1. The real symbol informations corresponding to your boot.img or kernel
    It can be obtained through the following two commands under root.
echo 1 > /proc/sys/kernel/kptr_restrict
cat /proc/kallsyms

Upload to here or file download path
untitled.txt
https://drive.google.com/drive/folders/1J0GKGqAR8rwLGC5gT7xzsv7XfsbCnWjt

Patch Failed (moto g51 5g) (5.4.233)

The following two information are what I need to fix the problem
Screenshot_20240317-175447
Screenshot_20240317-175453

  1. Your kernel image. boot.img is too big, you can use 'magiskboot unpack boot.img' to get kernel image (which named 'kernel' after unpack)

Upload to here or file download path
Image.zip
kallsyms.txt

  1. The real symbol informations corresponding to your boot.img or kernel
    It can be obtained through the following two commands under root.
echo 1 > /proc/sys/kernel/kptr_restrict
cat /proc/kallsyms

Upload to here or file download path
config.gz

Hook via module

I see that KernelPatch can hook kernel symbols in before_rest_init:

void before_rest_init(hook_fdata0_t *fdata, void *udata)

Is it also possible to hook symbols after the kernel has loaded, e.g. via a module that is loaded with --load_kpm (once that is implemented)?
Or does all hooking/patching need to be done before the kernel started? If so why?

Thank you for this project, it looks very promising and useful!

未正确处理旧版高通平台kernel文件头(”UNCOMPRESSED_IMG“)

参考一些说法和实现:

可知该文件头由 UNCOMPRESSED_IMG + 4字节kernel文件大小(不包含 UNCOMPRESSED_IMG和这4字节共20字节)。

虽然在 kallsym.c image.c 中有一部分针对该文件头的处理,但另有一些部分(patch_update_img 的大部分内容)并没有遵照这个来。

举例而言 4字节kernel文件大小 没有得到修改、setup_offset 是含开头20字节的(导致 setup 结束 br x16 实际上并没有跳到 开头B 指令上去,而是跳到了这个位置-20字节)。

造成的结果(我使用的设备是xiaomi 9)是,aboot 根据 4字节 kernel 文件大小算出来的 dtb_offset 错误,后续对dtb的检查失败,因此没有启动系统,直接回到了 fastboot 页面。

经测试,手动去掉前20直接进行patch,然后写上正确的前20字节(含新kernel文件大小),能够正确启动。

因此建议不要patch的每个部分代码都考虑这玩意(容易错漏),开头检测到有就去掉,结尾补上就行。

[Feature request] dkms support

When my kernel upgrade, my kernel doesn't auto patch.

When my upgrade kernel, my Linux will make vmlinux and initramfs, but I need repatch it kernel,

or we can write hook, when kernel upgrade auto use kernelpatch patch.

I want to patch Linux kernel, because the will can use AndroidPatch on Waydroid.

Patch Failed (Device Name) (Device Kerenl Version)

First, confirm whether your kernel has CONFIG_KALLSYMS_ALL=y enabled. If not or cannot be sure, please wait for support.

The following two information are what I need to fix the problem

  1. Your boot.img or kernel image

https://t.me/APatch_CN_Group/60093

Upload to here or file download path

  1. The real symbol informations corresponding to your boot.img or kernel
    It can be obtained through the following two commands under root.
echo 1 > /proc/sys/kernel/kptr_restrict
cat /proc/kallsyms

Upload to here or file download path

1.txt

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.