Skip to contentSkip to conversation

arm: module: handle negative R_ARM_PREL31 addends correctly

PATCH New Ard Biesheuvel 2 participants 4 messages Jan 20, 2017 – Jan 30, 2017 Arm (32-bit)

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 arm: module: handle negative R_ARM_PREL31 addends correctly

According to the spec 'ELF for the ARM Architecture' (IHI 0044E), addends for R_ARM_PREL31 relocations are 31-bit signed quantities, so we need to sign extend the value to 32 bits before it can be used as an offset in the calculation of the relocated value.

We have not been bitten by this because these relocations are usually emitted against the start of a section, which means the addends never assume negative values in practice. But it is a bug nonetheless, so fix it.

---
This is something I spotted while looking into adding support for R_ARM_REL32 relocations. Feel free to ignore if it is guaranteed in some way that these relocations can never be emitted with negative addends.

 arch/arm/kernel/module.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
arch/arm/kernel/module.c +9 -1
index 29629fe02ce5..20d8374711e2 100644
@@ -155,7 +155,15 @@ apply_relocate(Elf32_Shdr *sechdrs, const char *strtab, unsigned int symindex,
 		       break;
 
 		case R_ARM_PREL31:
-			offset = *(u32 *)loc + sym->st_value - loc;
+			offset = (*(s32 *)loc << 1) >> 1; /* sign extend */
+			offset += sym->st_value - loc;
+			if (offset >= 0x40000000 || offset < -0x40000000) {
+				pr_err("%s: section %u reloc %u sym '%s': relocation %u out of range (%#lx -> %#x)\n",
+				       module->name, relindex, i, symname,
+				       ELF32_R_TYPE(rel->r_info), loc,
+				       sym->st_value);
+				return -ENOEXEC;
+			}
 			*(u32 *)loc = offset & 0x7fffffff;
 			break;
 
Signature · 1 line
2.7.4
#1 lore raw

PATCH 1/1 arm: module: handle negative R_ARM_PREL31 addends correctly

Quoted 10 lines · Ard Biesheuvel

According to the spec 'ELF for the ARM Architecture' (IHI 0044E), addends for R_ARM_PREL31 relocations are 31-bit signed quantities, so we need to sign extend the value to 32 bits before it can be used as an offset in the calculation of the relocated value.

We have not been bitten by this because these relocations are usually emitted against the start of a section, which means the addends never assume negative values in practice. But it is a bug nonetheless, so fix it.

Quoted 5 lines

---
This is something I spotted while looking into adding support for R_ARM_REL32 relocations. Feel free to ignore if it is guaranteed in some way that these relocations can never be emitted with negative addends.

I still think this is a good thing to have, even if we should never see them - it avoids updating the relocation with an incorrect value. Any relocations we perform should be guaranteed to be correct, or we should fail.

Please put it in the patch system, thanks.

Signature · 3 lines
RMK's Patch system: http://www.armlinux.org.uk/developer/patches/
FTTC broadband for 0.8mile line: currently at 9.6Mbps down 400kbps up
according to speedtest.net.
#2 lore raw

PATCH 1/1 arm: module: handle negative R_ARM_PREL31 addends correctly

Quoted 17 lines · Russell King - ARM Linux
Quoted 10 lines · Ard Biesheuvel

According to the spec 'ELF for the ARM Architecture' (IHI 0044E), addends for R_ARM_PREL31 relocations are 31-bit signed quantities, so we need to sign extend the value to 32 bits before it can be used as an offset in the calculation of the relocated value.

We have not been bitten by this because these relocations are usually emitted against the start of a section, which means the addends never assume negative values in practice. But it is a bug nonetheless, so fix it.

Quoted 5 lines

---
This is something I spotted while looking into adding support for R_ARM_REL32 relocations. Feel free to ignore if it is guaranteed in some way that these relocations can never be emitted with negative addends.

Quoted 5 lines

I still think this is a good thing to have, even if we should never see them - it avoids updating the relocation with an incorrect value. Any relocations we perform should be guaranteed to be correct, or we should fail.

Agreed.

Quoted 2 lines

Please put it in the patch system, thanks.

Actually, I think it may still be incorrect: it seems to me that we should preserve bit 31 rather than clear it. From the EHABI

"""
Bit 31 of the relocated word does not participate in the relocation and may be used as data.
"""
(IHI0038B page 13)

and

"""
... The landing pad word contains a prel31 offset to a landing pad, with bit 31 set if the catch handler catches a reference type and bit 31 clear otherwise. ...
"""
(IHI0038B page 39)

which suggests to me that bit 31 could legally assume either truth value, and should not be touched by the relocation machinery.

#3 lore raw

PATCH 1/1 arm: module: handle negative R_ARM_PREL31 addends correctly

Quoted 2 lines · Ard Biesheuvel

Actually, I think it may still be incorrect: it seems to me that we should preserve bit 31 rather than clear it. From the EHABI

Agreed.

Signature · 3 lines
RMK's Patch system: http://www.armlinux.org.uk/developer/patches/
FTTC broadband for 0.8mile line: currently at 9.6Mbps down 400kbps up
according to speedtest.net.