History

lib: prevent module unloading if memory is not freed

Skip freeing module's data section if there are non-zero allocation tags because otherwise, once these allocations are freed, the access to their code tag would cause UAF.

Suren Baghdasaryan committed 2 years ago

Commit 47a92df1 parent a4735733 files · +30 −14Browse files

Unified · Split

3 files
  1. include/linux/codetag.h +3 −3
  2. kernel/module/main.c +19 −8
  3. lib/codetag.c +8 −3
include/linux/codetag.h+3−3
@@ -35,7 +35,7 @@ struct codetag_type_desc { · 34 unmodified lines
3535 size_t tag_size;
3636 void (*module_load)(struct codetag_type *cttype,
3737 struct codetag_module *cmod);
38− void (*module_unload)(struct codetag_type *cttype,
38+ bool (*module_unload)(struct codetag_type *cttype,
3939 struct codetag_module *cmod);
4040 };
4141
@@ -71,10 +71,10 @@ codetag_register_type(const struct codetag_type_desc *desc); · 29 unmodified lines
7171
7272 #if defined(CONFIG_CODE_TAGGING) && defined(CONFIG_MODULES)
7373 void codetag_load_module(struct module *mod);
74−void codetag_unload_module(struct module *mod);
74+bool codetag_unload_module(struct module *mod);
7575 #else
7676 static inline void codetag_load_module(struct module *mod) {}
77−static inline void codetag_unload_module(struct module *mod) {}
77+static inline bool codetag_unload_module(struct module *mod) { return true; }
7878 #endif
7979
8080 #endif /* _LINUX_CODETAG_H */
kernel/module/main.c+19−8
@@ -1211,15 +1211,19 @@ static void *module_memory_alloc(unsigned int size, enum mod_mem_type type) · 1210 unmodified lines
12111211 return module_alloc(size);
12121212 }
12131213
1214−static void module_memory_free(void *ptr, enum mod_mem_type type)
1214+static void module_memory_free(void *ptr, enum mod_mem_type type,
1215+ bool unload_codetags)
12151216 {
1217+ if (!unload_codetags && mod_mem_type_is_core_data(type))
1218+ return;
1219+
12161220 if (mod_mem_use_vmalloc(type))
12171221 vfree(ptr);
12181222 else
12191223 module_memfree(ptr);
12201224 }
12211225
1222−static void free_mod_mem(struct module *mod)
1226+static void free_mod_mem(struct module *mod, bool unload_codetags)
12231227 {
12241228 for_each_mod_mem_type(type) {
12251229 struct module_memory *mod_mem = &mod->mem[type];
@@ -1230,20 +1234,27 @@ static void free_mod_mem(struct module *mod) · 4 unmodified lines
12301234 /* Free lock-classes; relies on the preceding sync_rcu(). */
12311235 lockdep_free_key_range(mod_mem->base, mod_mem->size);
12321236 if (mod_mem->size)
1233− module_memory_free(mod_mem->base, type);
1237+ module_memory_free(mod_mem->base, type,
1238+ unload_codetags);
12341239 }
12351240
12361241 /* MOD_DATA hosts mod, so free it at last */
12371242 lockdep_free_key_range(mod->mem[MOD_DATA].base, mod->mem[MOD_DATA].size);
1238− module_memory_free(mod->mem[MOD_DATA].base, MOD_DATA);
1243+ module_memory_free(mod->mem[MOD_DATA].base, MOD_DATA, unload_codetags);
12391244 }
12401245
12411246 /* Free a module, remove from lists, etc. */
12421247 static void free_module(struct module *mod)
12431248 {
1249+ bool unload_codetags;
1250+
12441251 trace_module_free(mod);
12451252
1246− codetag_unload_module(mod);
1253+ unload_codetags = codetag_unload_module(mod);
1254+ if (!unload_codetags)
1255+ pr_warn("%s: memory allocation(s) from the module still alive, cannot unload cleanly\n",
1256+ mod->name);
1257+
12471258 mod_sysfs_teardown(mod);
12481259
12491260 /*
@@ -1285,7 +1296,7 @@ static void free_module(struct module *mod) · 35 unmodified lines
12851296 kfree(mod->args);
12861297 percpu_modfree(mod);
12871298
1288− free_mod_mem(mod);
1299+ free_mod_mem(mod, unload_codetags);
12891300 }
12901301
12911302 void *__symbol_get(const char *symbol)
@@ -2298,7 +2309,7 @@ static int move_module(struct module *mod, struct load_info *info) · 1006 unmodified lines
22982309 return 0;
22992310 out_enomem:
23002311 for (t--; t >= 0; t--)
2301− module_memory_free(mod->mem[t].base, t);
2312+ module_memory_free(mod->mem[t].base, t, true);
23022313 return ret;
23032314 }
23042315
@@ -2428,7 +2439,7 @@ static void module_deallocate(struct module *mod, struct load_info *info) · 123 unmodified lines
24282439 percpu_modfree(mod);
24292440 module_arch_freeing_init(mod);
24302441
2431− free_mod_mem(mod);
2442+ free_mod_mem(mod, true);
24322443 }
24332444
24342445 int __weak module_finalize(const Elf_Ehdr *hdr,
lib/codetag.c+8−3
@@ -5,6 +5,7 @@ · 4 unmodified lines
55 #include <linux/module.h>
66 #include <linux/seq_buf.h>
77 #include <linux/slab.h>
8+#include <linux/vmalloc.h>
89
910 struct codetag_type {
1011 struct list_head link;
@@ -206,12 +207,13 @@ void codetag_load_module(struct module *mod) · 195 unmodified lines
206207 mutex_unlock(&codetag_lock);
207208 }
208209
209−void codetag_unload_module(struct module *mod)
210+bool codetag_unload_module(struct module *mod)
210211 {
211212 struct codetag_type *cttype;
213+ bool unload_ok = true;
212214
213215 if (!mod)
214− return;
216+ return true;
215217
216218 mutex_lock(&codetag_lock);
217219 list_for_each_entry(cttype, &codetag_types, link) {
@@ -228,7 +230,8 @@ void codetag_unload_module(struct module *mod) · 10 unmodified lines
228230 }
229231 if (found) {
230232 if (cttype->desc.module_unload)
231− cttype->desc.module_unload(cttype, cmod);
233+ if (!cttype->desc.module_unload(cttype, cmod))
234+ unload_ok = false;
232235
233236 cttype->count -= range_size(cttype, &cmod->range);
234237 idr_remove(&cttype->mod_idr, mod_id);
@@ -237,6 +240,8 @@ void codetag_unload_module(struct module *mod) · 2 unmodified lines
237240 up_write(&cttype->mod_lock);
238241 }
239242 mutex_unlock(&codetag_lock);
243+
244+ return unload_ok;
240245 }
241246
242247 #else /* CONFIG_MODULES */

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t