Skip to contentSkip to conversation

console-low: Fix off-by-one stack corruption in entInt

PATCH In review Matt Turner 2 participants 5 messages Sep 24 – Oct 21

lore
Share & tools
lore
#1 lore raw

PATCH 2/3 console-low: Fix $8/$19 stack slot collision in entInt

In reply to Matt Turner

$8 and $19 were both stored to 9*8($sp), so $19 overwrote $8's saved value and the interrupted context got $19's value back in $8.

Store $8 at 8*8($sp) instead, matching its .cfi_rel_offset.

---
 console-low.S | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
console-low.S +2 -2
index 1f61cbc..83adb2a 100644
@@ -46,7 +46,7 @@ entInt:
 	stq	$5, 5*8($sp)
 	stq	$6, 6*8($sp)
 	stq	$7, 7*8($sp)
-	stq	$8, 9*8($sp)
+	stq	$8, 8*8($sp)
 	stq	$19, 9*8($sp)
 	stq	$20, 10*8($sp)
 	stq	$21, 11*8($sp)
@@ -87,7 +87,7 @@ entInt:
 	ldq	$5, 5*8($sp)
 	ldq	$6, 6*8($sp)
 	ldq	$7, 7*8($sp)
-	ldq	$8, 9*8($sp)
+	ldq	$8, 8*8($sp)
 	ldq	$19, 9*8($sp)
 	ldq	$20, 10*8($sp)
 	ldq	$21, 11*8($sp)
Signature · 1 line
2.54.0
#0 lore raw

PATCH 1/3 console-low: Fix off-by-one stack corruption in entInt

From: Iain Nicol <[email protected]>

entInt stores nineteen registers, not eighteen.

The corruption would sometimes manifest as a memory fault. The STACK_FRAME macro saves the internal PS register to 0($sp). The same location would later be overwritten by entInt storing $at. Later, the wrong PS register value would be restored. This would sometimes cause a switch from kernel to user mode, which can cause memory permission checks to fail.

---
 console-low.S | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
console-low.S +1 -1
index a03f4ed..1f61cbc 100644
@@ -23,7 +23,7 @@
 	.text
 	.cfi_sections .debug_frame
 
-#define SAVE_ALL_SIZE	(18*8)
+#define SAVE_ALL_SIZE	((18+1)*8)
 
 	.globl	entInt
 	.type	entInt, @function
Signature · 1 line
2.54.0
#3 lore raw

PATCH 3/3 console-low: Keep $sp 16-byte aligned in entInt

The PAL stack frame is 48 bytes, and entInt then allocates 19*8 = 152 bytes to save registers, leaving $sp 8 mod 16 at the call to do_entInt. The Alpha calling convention requires 16-byte stack alignment at calls.

Pad the save area to 20*8 bytes.

---
 console-low.S | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
console-low.S +2 -1
index 83adb2a..157a094 100644
@@ -23,7 +23,8 @@
 	.text
 	.cfi_sections .debug_frame
 
-#define SAVE_ALL_SIZE	((18+1)*8)
+/* 19 saved registers plus one pad slot to keep $sp 16-byte aligned. */
+#define SAVE_ALL_SIZE	(20*8)
 
 	.globl	entInt
 	.type	entInt, @function
Signature · 1 line
2.54.0
Reviewed-by on v1 2/3
#2 lore raw

Re: [PATCH qemu-palcode 2/3] console-low: Fix $8/$19 stack slot collision in entInt

In reply to Matt Turner
Quoted 9 lines · Matt Turner

$8 and $19 were both stored to 9*8($sp), so $19 overwrote $8's saved value and the interrupted context got $19's value back in $8.

Store $8 at 8*8($sp) instead, matching its .cfi_rel_offset.

---
  console-low.S | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)