Hi Luis
On Feb 6, 2023, at 4:28 PM, Song Liu [off-list ref] wrote:
+static void free_mod_mem(struct module *mod)
+{
+ /* free the memory in the right order to avoid use-after-free */
+ static enum mod_mem_type mod_mem_free_order[MOD_MEM_NUM_TYPES] = {
+ /* first free init sections */
+ MOD_INIT_TEXT,
+ MOD_INIT_DATA,
+ MOD_INIT_RODATA,
+
+ /* then core sections, except rw data */
+ MOD_TEXT,
+ MOD_RODATA,
+ MOD_RO_AFTER_INIT,
+
+ /* last, rw data */
+ MOD_DATA,
+ };
+ int i;
+
+ BUILD_BUG_ON(ARRAY_SIZE(mod_mem_free_order) != MOD_MEM_NUM_TYPES);
+
+ for (i = 0; i < MOD_MEM_NUM_TYPES; i++) {
+ enum mod_mem_type type = mod_mem_free_order[i];
+ struct module_memory *mod_mem = &mod->mem[type];
+
+ /* Free lock-classes; relies on the preceding sync_rcu(). */
+ lockdep_free_key_range(mod_mem->base, mod_mem->size);
+ if (mod_mem->size)
+ module_memory_free(mod_mem->base, type);
+ }
+}
If there are no other comments, would you mind make the following
change when applying the patch?
Thanks,
Song
diff --git i/kernel/module/main.c w/kernel/module/main.c
index b9617d7e8db2..c598f11e7016 100644
--- i/kernel/module/main.c
+++ w/kernel/module/main.c
@@ -1211,7 +1211,7 @@ static void free_mod_mem(struct module *mod)
MOD_RODATA,
MOD_RO_AFTER_INIT,
- /* last, rw data */
+ /* rw data need to be freed last, as it hosts mod */
MOD_DATA,
};
int i;