Skip to contentSkip to conversation

powerpc/64/module: REL32 relocation range check

RFC PATCH Applied Nicholas Piggin 2 participants 2 messages Aug 29, 2018 – Oct 22, 2018 PowerPC

lore
Share & tools
lore

Nicholas Piggin

author reviewer
#0 lore raw

RFC PATCH 1/1 powerpc/64/module: REL32 relocation range check

The recent module relocation overflow crash demonstrated that we have no range checking on REL32 relative relocations. This patch implements a basic check, the same kernel that previously oopsed and rebooted now continues with some of these errors when loading the module:

    module_64: x_tables: REL32 527703503449812 out of range!

Question is whether other relocations (ADDR32, REL16, TOC16, etc.) should also have overflow checks.

---
 arch/powerpc/kernel/module_64.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
arch/powerpc/kernel/module_64.c +8 -1
index a2636c250b7b..2a2fb656d23b 100644
@@ -678,7 +678,14 @@ int apply_relocate_add(Elf64_Shdr *sechdrs,
 
 		case R_PPC64_REL32:
 			/* 32 bits relative (used by relative exception tables) */
-			*(u32 *)location = value - (unsigned long)location;
+			/* Convert value to relative */
+			value -= (unsigned long)location;
+			if (value + 0x80000000 > 0xffffffff) {
+				pr_err("%s: REL32 %li out of range!\n",
+				       me->name, (long int)value);
+				return -ENOEXEC;
+			}
+			*(u32 *)location = value;
 			break;
 
 		case R_PPC64_TOCSAVE:
Signature · 1 line
2.18.0
#1 lore raw
Quoted 24 lines · Nicholas Piggin

The recent module relocation overflow crash demonstrated that we have no range checking on REL32 relative relocations. This patch implements a basic check, the same kernel that previously oopsed and rebooted now continues with some of these errors when loading the module:

    module_64: x_tables: REL32 527703503449812 out of range!

Question is whether other relocations (ADDR32, REL16, TOC16, etc.) should also have overflow checks.

---
 arch/powerpc/kernel/module_64.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
arch/powerpc/kernel/module_64.c +1 -1
index a2636c250b7b..2a2fb656d23b 100644
@@ -678,7 +678,14 @@ int apply_relocate_add(Elf64_Shdr *sechdrs,
 
 		case R_PPC64_REL32:
 			/* 32 bits relative (used by relative exception tables) */
-			*(u32 *)location = value - (unsigned long)location;
+			/* Convert value to relative */

arch/powerpc/kernel/module_64.c int apply_relocate_add(Elf64_Shdr *sechdrs, · line 691

Quoted 10 lines
arch/powerpc/kernel/module_64.c +7 -0
+			value -= (unsigned long)location;
+			if (value + 0x80000000 > 0xffffffff) {
+				pr_err("%s: REL32 %li out of range!\n",
+				       me->name, (long int)value);
+				return -ENOEXEC;
+			}
+			*(u32 *)location = value;
 			break;
 
 		case R_PPC64_TOCSAVE:

Applied to powerpc next, thanks.

https://git.kernel.org/powerpc/c/b851ba02a6f3075f0f99c60c4bc30a

cheers