Skip to content

Commit 620a53d

Browse files
Sven Schnellehdeller
authored andcommitted
parisc: add parisc code patching
Instead of re-mapping the whole kernel text with RWX rights add a patch_text() which can be used to replace instructions in the kernel .text section. Based on the ARM implementation. Signed-off-by: Sven Schnelle <svens@stackframe.org> Signed-off-by: Helge Deller <deller@gmx.de>
1 parent ccfbc68 commit 620a53d

3 files changed

Lines changed: 91 additions & 1 deletion

File tree

‎arch/parisc/include/asm/patch.h‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
#ifndef _PARISC_KERNEL_PATCH_H
3+
#define _PARISC_KERNEL_PATCH_H
4+
5+
/* stop machine and patch kernel text */
6+
void patch_text(void *addr, unsigned int insn);
7+
8+
/* patch kernel text with machine already stopped (e.g. in kgdb) */
9+
void __patch_text(void *addr, unsigned int insn);
10+
11+
#endif

‎arch/parisc/kernel/Makefile‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ obj-y := cache.o pacache.o setup.o pdt.o traps.o time.o irq.o \
99
pa7300lc.o syscall.o entry.o sys_parisc.o firmware.o \
1010
ptrace.o hardware.o inventory.o drivers.o alternative.o \
1111
signal.o hpmc.o real2.o parisc_ksyms.o unaligned.o \
12-
process.o processor.o pdc_cons.o pdc_chassis.o unwind.o
12+
process.o processor.o pdc_cons.o pdc_chassis.o unwind.o \
13+
patch.o
1314

1415
ifdef CONFIG_FUNCTION_TRACER
1516
# Do not profile debug and lowlevel utilities

‎arch/parisc/kernel/patch.c‎

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/*
3+
* functions to patch RO kernel text during runtime
4+
*
5+
* Copyright (c) 2019 Sven Schnelle <svens@stackframe.org>
6+
*/
7+
8+
#include <linux/kernel.h>
9+
#include <linux/spinlock.h>
10+
#include <linux/kprobes.h>
11+
#include <linux/mm.h>
12+
#include <linux/stop_machine.h>
13+
14+
#include <asm/cacheflush.h>
15+
#include <asm/fixmap.h>
16+
#include <asm/patch.h>
17+
18+
struct patch {
19+
void *addr;
20+
unsigned int insn;
21+
};
22+
23+
static void __kprobes *patch_map(void *addr, int fixmap, unsigned long *flags)
24+
{
25+
unsigned long uintaddr = (uintptr_t) addr;
26+
bool module = !core_kernel_text(uintaddr);
27+
struct page *page;
28+
29+
if (module && IS_ENABLED(CONFIG_STRICT_MODULE_RWX))
30+
page = vmalloc_to_page(addr);
31+
else if (!module && IS_ENABLED(CONFIG_STRICT_KERNEL_RWX))
32+
page = virt_to_page(addr);
33+
else
34+
return addr;
35+
36+
set_fixmap(fixmap, page_to_phys(page));
37+
38+
return (void *) (__fix_to_virt(fixmap) + (uintaddr & ~PAGE_MASK));
39+
}
40+
41+
static void __kprobes patch_unmap(int fixmap, unsigned long *flags)
42+
{
43+
clear_fixmap(fixmap);
44+
}
45+
46+
void __kprobes __patch_text(void *addr, unsigned int insn)
47+
{
48+
unsigned long flags;
49+
void *waddr = addr;
50+
int size;
51+
52+
waddr = patch_map(addr, FIX_TEXT_POKE0, &flags);
53+
*(u32 *)waddr = insn;
54+
size = sizeof(u32);
55+
flush_kernel_vmap_range(waddr, size);
56+
patch_unmap(FIX_TEXT_POKE0, &flags);
57+
flush_icache_range((uintptr_t)(addr),
58+
(uintptr_t)(addr) + size);
59+
}
60+
61+
static int __kprobes patch_text_stop_machine(void *data)
62+
{
63+
struct patch *patch = data;
64+
65+
__patch_text(patch->addr, patch->insn);
66+
67+
return 0;
68+
}
69+
70+
void __kprobes patch_text(void *addr, unsigned int insn)
71+
{
72+
struct patch patch = {
73+
.addr = addr,
74+
.insn = insn,
75+
};
76+
77+
stop_machine_cpuslocked(patch_text_stop_machine, &patch, NULL);
78+
}

0 commit comments

Comments
 (0)