Skip to contentSkip to conversation

linux-user: Correct definition of stack_t

PATCH Superseded LemonBoy 2 participants 4 messages Nov 5, 2020

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

LemonBoy

author
#0 lore raw

PATCH 1/1 linux-user: Correct definition of stack_t

From 914c30863b45d6fcd3e2fc83929bcac546be1555 Mon Sep 17 00:00:00 2001
From: LemonBoy <[email protected]>
Date: Thu, 5 Nov 2020 15:48:24 +0100
Subject: [PATCH] linux-user: Correct definition of stack_t

Some platforms used the wrong definition of stack_t where the flags and size fields were swapped or where the flags field had type ulong instead of int.

Due to the presence of padding space in the structure and the prevalence of little-endian machines this problem went unnoticed for a long time.

The type definitions have been cross-checked with the ones defined in the Linux kernel v5.9, plus some older versions for a few architecture that have been removed and Xilinx's kernel fork for NiosII [1].

The microblaze header and the bsd-user ones remain unchanged as I don't know if they are wrong or not.

[1] https://github.com/Xilinx/linux-xlnx/blob/master/arch/nios2/include/uapi/asm/signal.h

---
 linux-user/alpha/target_signal.h  | 3 +--
 linux-user/arm/target_signal.h    | 6 +++---
 linux-user/cris/target_signal.h   | 6 +++---
 linux-user/hppa/target_signal.h   | 2 +-
 linux-user/i386/target_signal.h   | 6 +++---
 linux-user/m68k/target_signal.h   | 6 +++---
 linux-user/mips/target_signal.h   | 6 +++---
 linux-user/mips64/target_signal.h | 7 +++----
 linux-user/nios2/target_signal.h  | 5 +++--
 linux-user/ppc/target_signal.h    | 6 +++---
 linux-user/s390x/target_signal.h  | 2 +-
 linux-user/sh4/target_signal.h    | 6 +++---
 linux-user/sparc/target_signal.h  | 6 +++---
 linux-user/x86_64/target_signal.h | 6 +++---
 14 files changed, 36 insertions(+), 37 deletions(-)
linux-user/alpha/target_signal.h +1 -2
index cd63d59fde..b83797281c 100644
@@ -42,8 +42,7 @@
 
 typedef struct target_sigaltstack {
     abi_ulong ss_sp;
-    int32_t ss_flags;
-    int32_t dummy;
+    abi_int ss_flags;
     abi_ulong ss_size;
 } target_stack_t;
 
linux-user/arm/target_signal.h +3 -3
index ea123c40f3..0998dd6dfa 100644
@@ -4,9 +4,9 @@
 /* this struct defines a stack used during syscall handling */
 
 typedef struct target_sigaltstack {
-	abi_ulong ss_sp;
-	abi_long ss_flags;
-	abi_ulong ss_size;
+    abi_ulong ss_sp;
+    abi_int ss_flags;
+    abi_ulong ss_size;
 } target_stack_t;
 
 
linux-user/cris/target_signal.h +3 -3
index 1cb5548f85..495a142896 100644
@@ -4,9 +4,9 @@
 /* this struct defines a stack used during syscall handling */
 
 typedef struct target_sigaltstack {
-	abi_ulong ss_sp;
-	abi_ulong ss_size;
-	abi_long ss_flags;
+    abi_ulong ss_sp;
+    abi_int ss_flags;
+    abi_ulong ss_size;
 } target_stack_t;
 
 
linux-user/hppa/target_signal.h +1 -1
index c2a0102ed7..c52a3ea579 100644
@@ -44,7 +44,7 @@
 
 typedef struct target_sigaltstack {
     abi_ulong ss_sp;
-    int32_t ss_flags;
+    abi_int ss_flags;
     abi_ulong ss_size;
 } target_stack_t;
 
linux-user/i386/target_signal.h +3 -3
index f55e78fd33..50361af874 100644
@@ -4,9 +4,9 @@
 /* this struct defines a stack used during syscall handling */
 
 typedef struct target_sigaltstack {
-	abi_ulong ss_sp;
-	abi_long ss_flags;
-	abi_ulong ss_size;
+    abi_ulong ss_sp;
+    abi_int ss_flags;
+    abi_ulong ss_size;
 } target_stack_t;
 
 
linux-user/m68k/target_signal.h +3 -3
index 314e808844..d096544ef8 100644
@@ -4,9 +4,9 @@
 /* this struct defines a stack used during syscall handling */
 
 typedef struct target_sigaltstack {
-	abi_ulong ss_sp;
-	abi_long ss_flags;
-	abi_ulong ss_size;
+    abi_ulong ss_sp;
+    abi_int ss_flags;
+    abi_ulong ss_size;
 } target_stack_t;
 
 
linux-user/mips/target_signal.h +3 -3
index 66e1ad44a6..fa4084a99d 100644
@@ -45,9 +45,9 @@
 /* this struct defines a stack used during syscall handling */
 
 typedef struct target_sigaltstack {
-	abi_long ss_sp;
-	abi_ulong ss_size;
-	abi_long ss_flags;
+    abi_ulong ss_sp;
+    abi_ulong ss_size;
+    abi_int ss_flags;
 } target_stack_t;
 
 
linux-user/mips64/target_signal.h +3 -4
index 753e91fbd6..799f7a668c 100644
@@ -45,12 +45,11 @@
 /* this struct defines a stack used during syscall handling */
 
 typedef struct target_sigaltstack {
-	abi_long ss_sp;
-	abi_ulong ss_size;
-	abi_int ss_flags;
+    abi_ulong ss_sp;
+    abi_ulong ss_size;
+    abi_int ss_flags;
 } target_stack_t;
 
-
 /*
  * sigaltstack controls
  */
linux-user/nios2/target_signal.h +3 -2
index fe48721b3d..aebf749f12 100644
@@ -4,11 +4,12 @@
 /* this struct defines a stack used during syscall handling */
 
 typedef struct target_sigaltstack {
-    abi_long ss_sp;
+    abi_ulong ss_sp;
+    abi_int ss_flags;
     abi_ulong ss_size;
-    abi_long ss_flags;
 } target_stack_t;
 
+
 /* sigaltstack controls  */
 #define TARGET_SS_ONSTACK     1
 #define TARGET_SS_DISABLE     2
linux-user/ppc/target_signal.h +3 -3
index 4453e2e7ef..72fcdd9bfa 100644
@@ -4,9 +4,9 @@
 /* this struct defines a stack used during syscall handling */
 
 typedef struct target_sigaltstack {
-	abi_ulong ss_sp;
-	int ss_flags;
-	abi_ulong ss_size;
+    abi_ulong ss_sp;
+    abi_int ss_flags;
+    abi_ulong ss_size;
 } target_stack_t;
 
 
linux-user/s390x/target_signal.h +1 -1
index b58bc7c20f..bbfc464d44 100644
@@ -3,7 +3,7 @@
 
 typedef struct target_sigaltstack {
     abi_ulong ss_sp;
-    int ss_flags;
+    abi_int ss_flags;
     abi_ulong ss_size;
 } target_stack_t;
 
linux-user/sh4/target_signal.h +3 -3
index 434970a990..d7309b7136 100644
@@ -4,9 +4,9 @@
 /* this struct defines a stack used during syscall handling */
 
 typedef struct target_sigaltstack {
-	abi_ulong ss_sp;
-	abi_long ss_flags;
-	abi_ulong ss_size;
+    abi_ulong ss_sp;
+    abi_int ss_flags;
+    abi_ulong ss_size;
 } target_stack_t;
 
 
linux-user/sparc/target_signal.h +3 -3
index 5cc40327d2..1b10d1490f 100644
@@ -42,9 +42,9 @@
 /* this struct defines a stack used during syscall handling */
 
 typedef struct target_sigaltstack {
-	abi_ulong ss_sp;
-	abi_long ss_flags;
-	abi_ulong ss_size;
+    abi_ulong ss_sp;
+    abi_int ss_flags;
+    abi_ulong ss_size;
 } target_stack_t;
 
 
linux-user/x86_64/target_signal.h +3 -3
index 4c4380f7b9..4ea74f20dd 100644
@@ -4,9 +4,9 @@
 /* this struct defines a stack used during syscall handling */
 
 typedef struct target_sigaltstack {
-	abi_ulong ss_sp;
-	abi_long ss_flags;
-	abi_ulong ss_size;
+    abi_ulong ss_sp;
+    abi_int ss_flags;
+    abi_ulong ss_size;
 } target_stack_t;
 
 
Signature · 1 line
2.27.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 9 lines · LemonBoy

From 914c30863b45d6fcd3e2fc83929bcac546be1555 Mon Sep 17 00:00:00 2001
From: LemonBoy <[email protected]>
Date: Thu, 5 Nov 2020 15:48:24 +0100
Subject: [PATCH] linux-user: Correct definition of stack_t

Some platforms used the wrong definition of stack_t where the flags and size fields were swapped or where the flags field had type ulong instead of int.

Coincidentally, I just noticed this yesterday (am in the middle of trying to debug why qemu-sparc64 crashes trying to start bash...)

Quoted 9 lines

Due to the presence of padding space in the structure and the prevalence of little-endian machines this problem went unnoticed for a long time.

The type definitions have been cross-checked with the ones defined in the Linux kernel v5.9, plus some older versions for a few architecture that have been removed and Xilinx's kernel fork for NiosII [1].

The microblaze header and the bsd-user ones remain unchanged as I don't know if they are wrong or not.

As far as I can tell, Microblaze doesn't define its own stack_t, so it must be using the asm-generic one, so I think it also needs changing.

Quoted 3 lines

[1] https://github.com/Xilinx/linux-xlnx/blob/master/arch/nios2/include/uapi/asm/signal.h

Other than also changing microblaze,

thanks
-- PMM

author
#2 lore raw
Quoted 7 lines · Peter Maydell
Quoted 6 lines · LemonBoy

From 914c30863b45d6fcd3e2fc83929bcac546be1555 Mon Sep 17 00:00:00 2001
From: LemonBoy <[email protected]>
Date: Thu, 5 Nov 2020 15:48:24 +0100
Subject: [PATCH] linux-user: Correct definition of stack_t

Quoted 7 lines
Quoted 3 lines

Some platforms used the wrong definition of stack_t where the flags and size fields were swapped or where the flags field had type ulong instead of int.

Coincidentally, I just noticed this yesterday (am in the middle of trying to debug why qemu-sparc64 crashes trying to start bash...)

What an interesting coincidence, I also stumbled across this bug while debugging a crash with some sparc64 binary.

Quoted 7 lines
Quoted 7 lines

Due to the presence of padding space in the structure and the prevalence of little-endian machines this problem went unnoticed for a long time.

The type definitions have been cross-checked with the ones defined in the Linux kernel v5.9, plus some older versions for a few architecture that have been removed and Xilinx's kernel fork for NiosII [1].

Quoted 7 lines
Quoted 2 lines

The microblaze header and the bsd-user ones remain unchanged as I don't know if they are wrong or not.

As far as I can tell, Microblaze doesn't define its own stack_t, so it must be using the asm-generic one, so I think it also needs changing.

I'll change it and send an updated patch ASAP.

Quoted 10 lines
Quoted 3 lines

[1] https://github.com/Xilinx/linux-xlnx/blob/master/arch/nios2/include/uapi/asm/signal.h

Other than also changing microblaze,

thanks
-- PMM

#3 lore raw
Quoted 7 lines · LemonBoy
Quoted 3 lines · Peter Maydell

Coincidentally, I just noticed this yesterday (am in the middle of trying to debug why qemu-sparc64 crashes trying to start bash...)

What an interesting coincidence, I also stumbled across this bug while debugging a crash with some sparc64 binary.

Don't suppose it involves sigsetjmp/siglongjmp? So far I have discovered that the linux-user/sparc target_ucontext and friends have host pointers where they should have abi_ulong and also that target_mc_fpu needs explicit 16-alignment (patch written, will post it sometime this week), and once past that have hit another bug that seems to be down to the setcontext/getcontext traps not saving and restoring all the right registers.

thanks
-- PMM