Skip to contentSkip to conversation

riscv: modules: Fix module loading error handling

PATCH Superseded Charlie Jenkins 2 participants 7 messages Jan 3, 2024 – Jan 4, 2024 RISC-V

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover riscv: modules: Fix module loading error handling

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.

---
Changes in v2:
- Split changes across multiple patches
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Charlie Jenkins (4):
riscv: Fix module loading free order
riscv: Correctly free relocation hashtable on error
riscv: Fix relocation_hashtable size
riscv: Convert relocation iterator to do-while

 arch/riscv/kernel/module.c | 40 ++++++++++++++++++++++++++++++----------
 1 file changed, 30 insertions(+), 10 deletions(-)

---
base-commit: a39b6ac3781d46ba18193c9dbb2110f31e9bffe9
change-id: 20231213-module_loading_fix-3ac6d4ea8129

Signature · 1 line
- Charlie
#1 lore raw

PATCH v2 1/4 riscv: Fix module loading free order

Reverse order of kfree calls to resolve use-after-free error.

---
 arch/riscv/kernel/module.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/riscv/kernel/module.c +1 -1
index aac019ed63b1..21c7a773a8ef 100644
@@ -723,8 +723,8 @@ static int add_relocation_to_accumulate(struct module *me, int type,
 
 			if (!bucket) {
 				kfree(entry);
-				kfree(rel_head);
 				kfree(rel_head->rel_entry);
+				kfree(rel_head);
 				return -ENOMEM;
 			}
 
Signature · 1 line
2.43.0
#2 lore raw

PATCH v2 2/4 riscv: Correctly free relocation hashtable on error

In reply to Charlie Jenkins

When there is not enough allocatable memory for the relocation hashtable, module loading should exit gracefully. Previously, this was attempted to be accomplished by checking if an unsigned number is less than zero which does not work. Instead have the caller check if the hashtable was correctly allocated and add a comment explaining that hashtable_bits that is 0 is valid.

---
 arch/riscv/kernel/module.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
arch/riscv/kernel/module.c +7 -3
index 21c7a773a8ef..32743180e8ef 100644
@@ -747,6 +747,10 @@ initialize_relocation_hashtable(unsigned int num_relocations,
 {
 	/* Can safely assume that bits is not greater than sizeof(long) */
 	unsigned long hashtable_size = roundup_pow_of_two(num_relocations);
+	/*
+	 * When hashtable_size == 1, hashtable_bits == 0.
+	 * This is valid because the hashing algorithm returns 0 in this case.
+	 */
 	unsigned int hashtable_bits = ilog2(hashtable_size);
 
 	/*
@@ -763,7 +767,7 @@ initialize_relocation_hashtable(unsigned int num_relocations,
 					      sizeof(*relocation_hashtable),
 					      GFP_KERNEL);
 	if (!*relocation_hashtable)
-		return -ENOMEM;
+		return 0;
 
 	__hash_init(*relocation_hashtable, hashtable_size);
 
@@ -789,8 +793,8 @@ int apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab,
 	hashtable_bits = initialize_relocation_hashtable(num_relocations,
 							 &relocation_hashtable);
 
-	if (hashtable_bits < 0)
-		return hashtable_bits;
+	if (!relocation_hashtable)
+		return -ENOMEM;
 
 	INIT_LIST_HEAD(&used_buckets_list);
 
Signature · 1 line
2.43.0
#3 lore raw

PATCH v2 3/4 riscv: Fix relocation_hashtable size

In reply to Charlie Jenkins

A second dereference is needed to get the accurate size of the relocation_hashtable.

Fixes d8792a5734b0 ("riscv: Safely remove entries from relocation list")
---
 arch/riscv/kernel/module.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/riscv/kernel/module.c +1 -1
index 32743180e8ef..ceb0adb38715 100644
@@ -764,7 +764,7 @@ initialize_relocation_hashtable(unsigned int num_relocations,
 	hashtable_size <<= should_double_size;
 
 	*relocation_hashtable = kmalloc_array(hashtable_size,
-					      sizeof(*relocation_hashtable),
+					      sizeof(**relocation_hashtable),
 					      GFP_KERNEL);
 	if (!*relocation_hashtable)
 		return 0;
Signature · 1 line
2.43.0
#4 lore raw

PATCH v2 4/4 riscv: Convert relocation iterator to do-while

In reply to Charlie Jenkins

Use a do-while loop to iterate through relocation entries to prevent curr_type from being marked as uninitialized.

Fixes d8792a5734b0 ("riscv: Safely remove entries from relocation list")
---
 arch/riscv/kernel/module.c | 26 +++++++++++++++++++++-----
 1 file changed, 21 insertions(+), 5 deletions(-)
arch/riscv/kernel/module.c +21 -5
index ceb0adb38715..581e425686ab 100644
@@ -633,15 +633,31 @@ process_accumulated_relocations(struct module *me,
 					  bucket_iter->bucket, node) {
 			buffer = 0;
 			location = rel_head_iter->location;
-			list_for_each_entry_safe(rel_entry_iter,
-						 rel_entry_iter_tmp,
-						 rel_head_iter->rel_entry,
-						 head) {
+			rel_entry_iter =
+				list_first_entry(rel_head_iter->rel_entry,
+						 typeof(*rel_entry_iter), head);
+			rel_entry_iter_tmp =
+				list_next_entry(rel_entry_iter, head);
+
+			/*
+			 * Iterate through all relocation entries that share
+			 * this location. This uses a do-while loop instead of
+			 * list_for_each_entry_safe since it is known that there
+			 * is at least one entry and curr_type needs to be the
+			 * value of the last entry when the loop exits.
+			 */
+			do {
 				curr_type = rel_entry_iter->type;
 				reloc_handlers[curr_type].reloc_handler(
 					me, &buffer, rel_entry_iter->value);
 				kfree(rel_entry_iter);
-			}
+
+				rel_entry_iter = rel_entry_iter_tmp;
+				rel_entry_iter_tmp = list_next_entry(rel_entry_iter_tmp, head);
+			} while (!list_entry_is_head(rel_entry_iter,
+						     rel_head_iter->rel_entry,
+						     head));
+
 			reloc_handlers[curr_type].accumulate_handler(
 				me, location, buffer);
 			kfree(rel_head_iter);
Signature · 1 line
2.43.0
#5 lore raw

Re: [PATCH v2 4/4] riscv: Convert relocation iterator to do-while

Quoted 27 lines · Charlie Jenkins

Use a do-while loop to iterate through relocation entries to prevent curr_type from being marked as uninitialized.

Fixes d8792a5734b0 ("riscv: Safely remove entries from relocation list")
---
 arch/riscv/kernel/module.c | 26 +++++++++++++++++++++-----
 1 file changed, 21 insertions(+), 5 deletions(-)
arch/riscv/kernel/module.c +3 -4
index ceb0adb38715..581e425686ab 100644
@@ -633,15 +633,31 @@ process_accumulated_relocations(struct module *me,
 					  bucket_iter->bucket, node) {
 			buffer = 0;
 			location = rel_head_iter->location;
-			list_for_each_entry_safe(rel_entry_iter,
-						 rel_entry_iter_tmp,
-						 rel_head_iter->rel_entry,
-						 head) {
+			rel_entry_iter =
+				list_first_entry(rel_head_iter->rel_entry,
+						 typeof(*rel_entry_iter), head);

arch/riscv/kernel/module.c process_accumulated_relocations(struct module *me, · line 648

Quoted 10 lines
arch/riscv/kernel/module.c +10 -0
+			rel_entry_iter_tmp =
+				list_next_entry(rel_entry_iter, head);
+
+			/*
+			 * Iterate through all relocation entries that share
+			 * this location. This uses a do-while loop instead of
+			 * list_for_each_entry_safe since it is known that there
+			 * is at least one entry and curr_type needs to be the
+			 * value of the last entry when the loop exits.
+			 */

I know that I reported this static checker and all, but actually after reading this comment, I think we should stay with original code. So long as we know the list has "least one entry" which we do then the original code worked fine.

To be honest, I probably would not have even reported this static checker warning except that I saw there were some other issues and thought "Eh, why not throw this warning in as well, in case the list can be empty."

The other three patches look good.

regards,
dan carpenter

#6 lore raw

Re: [PATCH v2 4/4] riscv: Convert relocation iterator to do-while

Quoted 44 lines · Dan Carpenter
Quoted 27 lines · Charlie Jenkins

Use a do-while loop to iterate through relocation entries to prevent curr_type from being marked as uninitialized.

Fixes d8792a5734b0 ("riscv: Safely remove entries from relocation list")
---
 arch/riscv/kernel/module.c | 26 +++++++++++++++++++++-----
 1 file changed, 21 insertions(+), 5 deletions(-)
arch/riscv/kernel/module.c +3 -4
index ceb0adb38715..581e425686ab 100644
@@ -633,15 +633,31 @@ process_accumulated_relocations(struct module *me,
 					  bucket_iter->bucket, node) {
 			buffer = 0;
 			location = rel_head_iter->location;
-			list_for_each_entry_safe(rel_entry_iter,
-						 rel_entry_iter_tmp,
-						 rel_head_iter->rel_entry,
-						 head) {
+			rel_entry_iter =
+				list_first_entry(rel_head_iter->rel_entry,
+						 typeof(*rel_entry_iter), head);

arch/riscv/kernel/module.c process_accumulated_relocations(struct module *me, · line 648

Quoted 10 lines
arch/riscv/kernel/module.c +10 -0
+			rel_entry_iter_tmp =
+				list_next_entry(rel_entry_iter, head);
+
+			/*
+			 * Iterate through all relocation entries that share
+			 * this location. This uses a do-while loop instead of
+			 * list_for_each_entry_safe since it is known that there
+			 * is at least one entry and curr_type needs to be the
+			 * value of the last entry when the loop exits.
+			 */

I know that I reported this static checker and all, but actually after reading this comment, I think we should stay with original code. So long as we know the list has "least one entry" which we do then the original code worked fine.

Quoted 4 lines

To be honest, I probably would not have even reported this static checker warning except that I saw there were some other issues and thought "Eh, why not throw this warning in as well, in case the list can be empty."

That makes sense, I will drop that patch.

- Charlie

Quoted 6 lines

The other three patches look good.

regards,
dan carpenter