Skip to content

Commit 3a58275

Browse files
Merge patch series "riscv: modules: Fix module loading error handling"
Charlie Jenkins <[email protected]> says: When modules are loaded while there is not ample allocatable memory, there was previously not proper error handling. This series fixes a use-after-free error and a different issue that caused a non graceful exit after memory was not properly allocated. * b4-shazam-merge: riscv: Fix relocation_hashtable size riscv: Correctly free relocation hashtable on error riscv: Fix module loading free order Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Palmer Dabbelt <[email protected]>
2 parents 17f2c30 + a35551c commit 3a58275

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

arch/riscv/kernel/module.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -723,8 +723,8 @@ static int add_relocation_to_accumulate(struct module *me, int type,
723723

724724
if (!bucket) {
725725
kfree(entry);
726-
kfree(rel_head);
727726
kfree(rel_head->rel_entry);
727+
kfree(rel_head);
728728
return -ENOMEM;
729729
}
730730

@@ -747,6 +747,10 @@ initialize_relocation_hashtable(unsigned int num_relocations,
747747
{
748748
/* Can safely assume that bits is not greater than sizeof(long) */
749749
unsigned long hashtable_size = roundup_pow_of_two(num_relocations);
750+
/*
751+
* When hashtable_size == 1, hashtable_bits == 0.
752+
* This is valid because the hashing algorithm returns 0 in this case.
753+
*/
750754
unsigned int hashtable_bits = ilog2(hashtable_size);
751755

752756
/*
@@ -760,10 +764,10 @@ initialize_relocation_hashtable(unsigned int num_relocations,
760764
hashtable_size <<= should_double_size;
761765

762766
*relocation_hashtable = kmalloc_array(hashtable_size,
763-
sizeof(*relocation_hashtable),
767+
sizeof(**relocation_hashtable),
764768
GFP_KERNEL);
765769
if (!*relocation_hashtable)
766-
return -ENOMEM;
770+
return 0;
767771

768772
__hash_init(*relocation_hashtable, hashtable_size);
769773

@@ -789,8 +793,8 @@ int apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab,
789793
hashtable_bits = initialize_relocation_hashtable(num_relocations,
790794
&relocation_hashtable);
791795

792-
if (hashtable_bits < 0)
793-
return hashtable_bits;
796+
if (!relocation_hashtable)
797+
return -ENOMEM;
794798

795799
INIT_LIST_HEAD(&used_buckets_list);
796800

0 commit comments

Comments
 (0)