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

Showing the changes to kernel/module (1 of 3 files). Show all

Unified · Split

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,

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