1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Module strict rwx
4 *
5 * Copyright (C) 2015 Rusty Russell
6 */
7
8 #include <linux/module.h>
9 #include <linux/mm.h>
10 #include <linux/vmalloc.h>
11 #include <linux/set_memory.h>
12 #include <linux/execmem.h>
13 #include "internal.h"
14
module_set_memory(const struct module * mod,enum mod_mem_type type,int (* set_memory)(unsigned long start,int num_pages))15 static int module_set_memory(const struct module *mod, enum mod_mem_type type,
16 int (*set_memory)(unsigned long start, int num_pages))
17 {
18 const struct module_memory *mod_mem = &mod->mem[type];
19
20 if (!mod_mem->base)
21 return 0;
22
23 set_vm_flush_reset_perms(mod_mem->base);
24 return set_memory((unsigned long)mod_mem->base, mod_mem->size >> PAGE_SHIFT);
25 }
26
27 /*
28 * Since some arches are moving towards PAGE_KERNEL module allocations instead
29 * of PAGE_KERNEL_EXEC, keep module_enable_x() independent of
30 * CONFIG_STRICT_MODULE_RWX because they are needed regardless of whether we
31 * are strict.
32 */
module_enable_text_rox(const struct module * mod)33 int module_enable_text_rox(const struct module *mod)
34 {
35 for_class_mod_mem_type(type, text) {
36 const struct module_memory *mem = &mod->mem[type];
37 int ret;
38
39 if (mem->is_rox)
40 ret = execmem_restore_rox(mem->base, mem->size);
41 else if (IS_ENABLED(CONFIG_STRICT_MODULE_RWX))
42 ret = module_set_memory(mod, type, set_memory_rox);
43 else
44 ret = module_set_memory(mod, type, set_memory_x);
45 if (ret)
46 return ret;
47 }
48 return 0;
49 }
50
module_enable_rodata_ro(const struct module * mod)51 int module_enable_rodata_ro(const struct module *mod)
52 {
53 int ret;
54
55 if (!IS_ENABLED(CONFIG_STRICT_MODULE_RWX) || !rodata_enabled)
56 return 0;
57
58 ret = module_set_memory(mod, MOD_RODATA, set_memory_ro);
59 if (ret)
60 return ret;
61 ret = module_set_memory(mod, MOD_INIT_RODATA, set_memory_ro);
62 if (ret)
63 return ret;
64
65 return 0;
66 }
67
module_enable_rodata_ro_after_init(const struct module * mod)68 int module_enable_rodata_ro_after_init(const struct module *mod)
69 {
70 if (!IS_ENABLED(CONFIG_STRICT_MODULE_RWX) || !rodata_enabled)
71 return 0;
72
73 return module_set_memory(mod, MOD_RO_AFTER_INIT, set_memory_ro);
74 }
75
module_enable_data_nx(const struct module * mod)76 int module_enable_data_nx(const struct module *mod)
77 {
78 if (!IS_ENABLED(CONFIG_STRICT_MODULE_RWX))
79 return 0;
80
81 for_class_mod_mem_type(type, data) {
82 int ret = module_set_memory(mod, type, set_memory_nx);
83
84 if (ret)
85 return ret;
86 }
87 return 0;
88 }
89
module_enforce_rwx_sections(Elf_Ehdr * hdr,Elf_Shdr * sechdrs,char * secstrings,struct module * mod)90 int module_enforce_rwx_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
91 char *secstrings, struct module *mod)
92 {
93 const unsigned long shf_wx = SHF_WRITE | SHF_EXECINSTR;
94 int i;
95
96 if (!IS_ENABLED(CONFIG_STRICT_MODULE_RWX))
97 return 0;
98
99 for (i = 0; i < hdr->e_shnum; i++) {
100 if ((sechdrs[i].sh_flags & shf_wx) == shf_wx) {
101 pr_err("%s: section %s (index %d) has invalid WRITE|EXEC flags\n",
102 mod->name, secstrings + sechdrs[i].sh_name, i);
103 return -ENOEXEC;
104 }
105 }
106
107 return 0;
108 }
109