[coreboot-gerrit] Change in coreboot[master]: arch/x86: Avoid accessing unaligned pointers since it's unde...

Ryan Salsamendi (Code Review) gerrit at coreboot.org
Fri Jun 9 21:16:30 CEST 2017


Ryan Salsamendi has uploaded this change for review. ( https://review.coreboot.org/20132


Change subject: arch/x86: Avoid accessing unaligned pointers since it's undefined behavior.
......................................................................

arch/x86: Avoid accessing unaligned pointers since it's undefined behavior.

Fixes report found by undefined behavior sanitizer. Switch to memcpy() in
places where there's a possibility that pointers are not aligned to the
size of access.

Change-Id: I79814bd47a14ec59d84068b11d094dc2531995d9
Signed-off-by: Ryan Salsamendi <rsalsamendi at hotmail.com>
---
M src/arch/x86/ebda.c
1 file changed, 15 insertions(+), 6 deletions(-)



  git pull ssh://review.coreboot.org:29418/coreboot refs/changes/32/20132/1

diff --git a/src/arch/x86/ebda.c b/src/arch/x86/ebda.c
index 284b5be..51af9ae 100644
--- a/src/arch/x86/ebda.c
+++ b/src/arch/x86/ebda.c
@@ -22,6 +22,10 @@
 
 void setup_ebda(u32 low_memory_size, u16 ebda_segment, u16 ebda_size)
 {
+	u16 low_memory_kb;
+	u16 ebda_kb;
+	void *ebda;
+
 	/* Skip in S3 resume path */
 	if (acpi_is_wakeup_s3())
 		return;
@@ -29,15 +33,20 @@
 	if (!low_memory_size || !ebda_segment || !ebda_size)
 		return;
 
-	/* clear BIOS DATA AREA */
-	memset((void *)X86_BDA_BASE, 0, X86_BDA_SIZE);
+	low_memory_kb = (low_memory_size >> 10);
+	ebda_kb = (ebda_size >> 10);
+	ebda = (void *)((uintptr_t)ebda_segment << 4);
 
-	write16(X86_EBDA_LOWMEM, (low_memory_size >> 10));
-	write16(X86_EBDA_SEGMENT, ebda_segment);
+	/* clear BIOS DATA AREA */
+	memset(X86_BDA_BASE, 0, X86_BDA_SIZE);
+
+	/* Avoid unaligned write16() since it's undefined behavior */
+	memcpy(X86_EBDA_LOWMEM, &low_memory_kb, 2);
+	memcpy(X86_EBDA_SEGMENT, &ebda_segment, 2);
 
 	/* Set up EBDA */
-	memset((void *)((uintptr_t)ebda_segment << 4), 0, ebda_size);
-	write16((void *)((uintptr_t)ebda_segment << 4), (ebda_size >> 10));
+	memset(ebda, 0, ebda_size);
+	memcpy(ebda, &ebda_kb, 2);
 }
 
 void setup_default_ebda(void)

-- 
To view, visit https://review.coreboot.org/20132
To unsubscribe, visit https://review.coreboot.org/settings

Gerrit-Project: coreboot
Gerrit-Branch: master
Gerrit-MessageType: newchange
Gerrit-Change-Id: I79814bd47a14ec59d84068b11d094dc2531995d9
Gerrit-Change-Number: 20132
Gerrit-PatchSet: 1
Gerrit-Owner: Ryan Salsamendi <rsalsamendi at hotmail.com>



More information about the coreboot-gerrit mailing list