Skip to content

Commit b56b36e

Browse files
jpoimboeJiri Kosina
authored andcommitted
livepatch: Cleanup module page permission changes
Calling set_memory_rw() and set_memory_ro() for every iteration of the loop in klp_write_object_relocations() is messy, inefficient, and error-prone. Change all the read-only pages to read-write before the loop and convert them back to read-only again afterwards. Suggested-by: Miroslav Benes <mbenes@suse.cz> Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com> Reviewed-by: Petr Mladek <pmladek@suse.com> Signed-off-by: Jiri Kosina <jkosina@suse.cz>
1 parent fc284d6 commit b56b36e

2 files changed

Lines changed: 13 additions & 28 deletions

File tree

‎arch/x86/kernel/livepatch.c‎

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020

2121
#include <linux/module.h>
2222
#include <linux/uaccess.h>
23-
#include <asm/cacheflush.h>
24-
#include <asm/page_types.h>
2523
#include <asm/elf.h>
2624
#include <asm/livepatch.h>
2725

@@ -38,8 +36,7 @@
3836
int klp_write_module_reloc(struct module *mod, unsigned long type,
3937
unsigned long loc, unsigned long value)
4038
{
41-
int ret, numpages, size = 4;
42-
bool readonly;
39+
size_t size = 4;
4340
unsigned long val;
4441
unsigned long core = (unsigned long)mod->core_layout.base;
4542
unsigned long core_size = mod->core_layout.size;
@@ -69,23 +66,5 @@ int klp_write_module_reloc(struct module *mod, unsigned long type,
6966
/* loc does not point to any symbol inside the module */
7067
return -EINVAL;
7168

72-
readonly = false;
73-
74-
#ifdef CONFIG_DEBUG_SET_MODULE_RONX
75-
if (loc < core + mod->core_layout.ro_size)
76-
readonly = true;
77-
#endif
78-
79-
/* determine if the relocation spans a page boundary */
80-
numpages = ((loc & PAGE_MASK) == ((loc + size) & PAGE_MASK)) ? 1 : 2;
81-
82-
if (readonly)
83-
set_memory_rw(loc & PAGE_MASK, numpages);
84-
85-
ret = probe_kernel_write((void *)loc, &val, size);
86-
87-
if (readonly)
88-
set_memory_ro(loc & PAGE_MASK, numpages);
89-
90-
return ret;
69+
return probe_kernel_write((void *)loc, &val, size);
9170
}

‎kernel/livepatch/core.c‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include <linux/list.h>
2929
#include <linux/kallsyms.h>
3030
#include <linux/livepatch.h>
31+
#include <asm/cacheflush.h>
3132

3233
/**
3334
* struct klp_ops - structure for tracking registered ftrace ops structs
@@ -232,7 +233,7 @@ static int klp_find_external_symbol(struct module *pmod, const char *name,
232233
static int klp_write_object_relocations(struct module *pmod,
233234
struct klp_object *obj)
234235
{
235-
int ret;
236+
int ret = 0;
236237
unsigned long val;
237238
struct klp_reloc *reloc;
238239

@@ -242,13 +243,16 @@ static int klp_write_object_relocations(struct module *pmod,
242243
if (WARN_ON(!obj->relocs))
243244
return -EINVAL;
244245

246+
module_disable_ro(pmod);
247+
245248
for (reloc = obj->relocs; reloc->name; reloc++) {
246249
/* discover the address of the referenced symbol */
247250
if (reloc->external) {
248251
if (reloc->sympos > 0) {
249252
pr_err("non-zero sympos for external reloc symbol '%s' is not supported\n",
250253
reloc->name);
251-
return -EINVAL;
254+
ret = -EINVAL;
255+
goto out;
252256
}
253257
ret = klp_find_external_symbol(pmod, reloc->name, &val);
254258
} else
@@ -257,18 +261,20 @@ static int klp_write_object_relocations(struct module *pmod,
257261
reloc->sympos,
258262
&val);
259263
if (ret)
260-
return ret;
264+
goto out;
261265

262266
ret = klp_write_module_reloc(pmod, reloc->type, reloc->loc,
263267
val + reloc->addend);
264268
if (ret) {
265269
pr_err("relocation failed for symbol '%s' at 0x%016lx (%d)\n",
266270
reloc->name, val, ret);
267-
return ret;
271+
goto out;
268272
}
269273
}
270274

271-
return 0;
275+
out:
276+
module_enable_ro(pmod);
277+
return ret;
272278
}
273279

274280
static void notrace klp_ftrace_handler(unsigned long ip,

0 commit comments

Comments
 (0)