Commit b56b36ee authored by Josh Poimboeuf's avatar Josh Poimboeuf Committed by Jiri Kosina

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: default avatarMiroslav Benes <mbenes@suse.cz>
Signed-off-by: default avatarJosh Poimboeuf <jpoimboe@redhat.com>
Reviewed-by: default avatarPetr Mladek <pmladek@suse.com>
Signed-off-by: default avatarJiri Kosina <jkosina@suse.cz>
parent fc284d63
...@@ -20,8 +20,6 @@ ...@@ -20,8 +20,6 @@
#include <linux/module.h> #include <linux/module.h>
#include <linux/uaccess.h> #include <linux/uaccess.h>
#include <asm/cacheflush.h>
#include <asm/page_types.h>
#include <asm/elf.h> #include <asm/elf.h>
#include <asm/livepatch.h> #include <asm/livepatch.h>
...@@ -38,8 +36,7 @@ ...@@ -38,8 +36,7 @@
int klp_write_module_reloc(struct module *mod, unsigned long type, int klp_write_module_reloc(struct module *mod, unsigned long type,
unsigned long loc, unsigned long value) unsigned long loc, unsigned long value)
{ {
int ret, numpages, size = 4; size_t size = 4;
bool readonly;
unsigned long val; unsigned long val;
unsigned long core = (unsigned long)mod->core_layout.base; unsigned long core = (unsigned long)mod->core_layout.base;
unsigned long core_size = mod->core_layout.size; unsigned long core_size = mod->core_layout.size;
...@@ -69,23 +66,5 @@ int klp_write_module_reloc(struct module *mod, unsigned long type, ...@@ -69,23 +66,5 @@ int klp_write_module_reloc(struct module *mod, unsigned long type,
/* loc does not point to any symbol inside the module */ /* loc does not point to any symbol inside the module */
return -EINVAL; return -EINVAL;
readonly = false; return probe_kernel_write((void *)loc, &val, size);
#ifdef CONFIG_DEBUG_SET_MODULE_RONX
if (loc < core + mod->core_layout.ro_size)
readonly = true;
#endif
/* determine if the relocation spans a page boundary */
numpages = ((loc & PAGE_MASK) == ((loc + size) & PAGE_MASK)) ? 1 : 2;
if (readonly)
set_memory_rw(loc & PAGE_MASK, numpages);
ret = probe_kernel_write((void *)loc, &val, size);
if (readonly)
set_memory_ro(loc & PAGE_MASK, numpages);
return ret;
} }
...@@ -28,6 +28,7 @@ ...@@ -28,6 +28,7 @@
#include <linux/list.h> #include <linux/list.h>
#include <linux/kallsyms.h> #include <linux/kallsyms.h>
#include <linux/livepatch.h> #include <linux/livepatch.h>
#include <asm/cacheflush.h>
/** /**
* struct klp_ops - structure for tracking registered ftrace ops structs * 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, ...@@ -232,7 +233,7 @@ static int klp_find_external_symbol(struct module *pmod, const char *name,
static int klp_write_object_relocations(struct module *pmod, static int klp_write_object_relocations(struct module *pmod,
struct klp_object *obj) struct klp_object *obj)
{ {
int ret; int ret = 0;
unsigned long val; unsigned long val;
struct klp_reloc *reloc; struct klp_reloc *reloc;
...@@ -242,13 +243,16 @@ static int klp_write_object_relocations(struct module *pmod, ...@@ -242,13 +243,16 @@ static int klp_write_object_relocations(struct module *pmod,
if (WARN_ON(!obj->relocs)) if (WARN_ON(!obj->relocs))
return -EINVAL; return -EINVAL;
module_disable_ro(pmod);
for (reloc = obj->relocs; reloc->name; reloc++) { for (reloc = obj->relocs; reloc->name; reloc++) {
/* discover the address of the referenced symbol */ /* discover the address of the referenced symbol */
if (reloc->external) { if (reloc->external) {
if (reloc->sympos > 0) { if (reloc->sympos > 0) {
pr_err("non-zero sympos for external reloc symbol '%s' is not supported\n", pr_err("non-zero sympos for external reloc symbol '%s' is not supported\n",
reloc->name); reloc->name);
return -EINVAL; ret = -EINVAL;
goto out;
} }
ret = klp_find_external_symbol(pmod, reloc->name, &val); ret = klp_find_external_symbol(pmod, reloc->name, &val);
} else } else
...@@ -257,18 +261,20 @@ static int klp_write_object_relocations(struct module *pmod, ...@@ -257,18 +261,20 @@ static int klp_write_object_relocations(struct module *pmod,
reloc->sympos, reloc->sympos,
&val); &val);
if (ret) if (ret)
return ret; goto out;
ret = klp_write_module_reloc(pmod, reloc->type, reloc->loc, ret = klp_write_module_reloc(pmod, reloc->type, reloc->loc,
val + reloc->addend); val + reloc->addend);
if (ret) { if (ret) {
pr_err("relocation failed for symbol '%s' at 0x%016lx (%d)\n", pr_err("relocation failed for symbol '%s' at 0x%016lx (%d)\n",
reloc->name, val, ret); reloc->name, val, ret);
return ret; goto out;
} }
} }
return 0; out:
module_enable_ro(pmod);
return ret;
} }
static void notrace klp_ftrace_handler(unsigned long ip, static void notrace klp_ftrace_handler(unsigned long ip,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment