Skip to contentSkip to conversation

parisc: Fix user access miscompilation

PATCH In review Helge Deller 2 participants 3 messages Feb 9, 2022 – Feb 10, 2022 PA-RISC architecture

lore
Share & tools
lore

Helge Deller

author maintainer
#0 lore raw

PATCH 1/1 parisc: Fix user access miscompilation

After commit 4b9d2a731c3d ("parisc: Switch user access functions to signal errors in r29 instead of r8") bash suddenly started to report those warnings after login:

-bash: cannot set terminal process group (-1): Bad file descriptor -bash: no job control in this shell

It turned out, that another function call inside a put_user(), e.g.:
put_user(vt_do_kdgkbmode(console), (int __user *)arg);
clobbered the error register (r29) and thus failed.
Avoid this miscompilation by first calculate the value in __put_user() before calling __put_user_internal() to actually write the value to user space.

Additionally, prefer the "+" constraint on pu_err and gu_err registers to tell the compiler that those operands are both read and written by the assembly instruction.

Fixes 4b9d2a731c3d ("parisc: Switch user access functions to signal errors in r29 instead of r8")
arch/parisc/include/asm/uaccess.h +8 -7
index ebf8a845b017..68f5c1eaaa6f 100644
@@ -89,7 +89,7 @@ struct exception_table_entry {
 	__asm__("1: " ldx " 0(" sr "%2),%0\n"		\
 		"9:\n"					\
 		ASM_EXCEPTIONTABLE_ENTRY_EFAULT(1b, 9b)	\
-		: "=r"(__gu_val), "=r"(__gu_err)        \
+		: "=&r"(__gu_val), "+r"(__gu_err)        \
 		: "r"(ptr), "1"(__gu_err));		\
 							\
 	(val) = (__force __typeof__(*(ptr))) __gu_val;	\
@@ -123,7 +123,7 @@ struct exception_table_entry {
 		"9:\n"					\
 		ASM_EXCEPTIONTABLE_ENTRY_EFAULT(1b, 9b)	\
 		ASM_EXCEPTIONTABLE_ENTRY_EFAULT(2b, 9b)	\
-		: "=&r"(__gu_tmp.l), "=r"(__gu_err)	\
+		: "=&r"(__gu_tmp.l), "+r"(__gu_err)	\
 		: "r"(ptr), "1"(__gu_err));		\
 							\
 	(val) = __gu_tmp.t;				\
@@ -132,10 +132,9 @@ struct exception_table_entry {
 #endif /* !defined(CONFIG_64BIT) */
 
 
-#define __put_user_internal(sr, x, ptr)				\
+#define __put_user_internal(sr, __x, ptr)			\
 ({								\
 	ASM_EXCEPTIONTABLE_VAR(__pu_err);		      	\
-        __typeof__(*(ptr)) __x = (__typeof__(*(ptr)))(x);	\
 								\
 	switch (sizeof(*(ptr))) {				\
 	case 1: __put_user_asm(sr, "stb", __x, ptr); break;	\
@@ -150,7 +149,9 @@ struct exception_table_entry {
 
 #define __put_user(x, ptr)					\
 ({								\
-	__put_user_internal("%%sr3,", x, ptr);			\
+	 __typeof__(*(ptr)) __x = (__typeof__(*(ptr)))(x);	\
+								\
+	__put_user_internal("%%sr3,", __x, ptr);		\
 })
 
 #define __put_kernel_nofault(dst, src, type, err_label)		\
@@ -180,7 +181,7 @@ struct exception_table_entry {
 		"1: " stx " %2,0(" sr "%1)\n"			\
 		"9:\n"						\
 		ASM_EXCEPTIONTABLE_ENTRY_EFAULT(1b, 9b)		\
-		: "=r"(__pu_err)				\
+		: "+r"(__pu_err)				\
 		: "r"(ptr), "r"(x), "0"(__pu_err))
 
 
@@ -193,7 +194,7 @@ struct exception_table_entry {
 		"9:\n"						\
 		ASM_EXCEPTIONTABLE_ENTRY_EFAULT(1b, 9b)		\
 		ASM_EXCEPTIONTABLE_ENTRY_EFAULT(2b, 9b)		\
-		: "=r"(__pu_err)				\
+		: "+r"(__pu_err)				\
 		: "r"(ptr), "r"(__val), "0"(__pu_err));		\
 } while (0)
 
#1 lore raw
Quoted 7 lines · Helge Deller

After commit 4b9d2a731c3d ("parisc: Switch user access functions to signal errors in r29 instead of r8") bash suddenly started to report those warnings after login:

(quoted diff) +0 -2
-bash: cannot set terminal process group (-1): Bad file descriptor
-bash: no job control in this shell
Quoted 6 lines

It turned out, that another function call inside a put_user(), e.g.:

   put_user(vt_do_kdgkbmode(console), (int __user *)arg);

clobbered the error register (r29) and thus failed. Avoid this miscompilation by first calculate the value in __put_user() before calling __put_user_internal() to actually write the value to user space.

Couldn't the same issue occur with ptr argument in these macros?

Quoted 13 lines

Additionally, prefer the "+" constraint on pu_err and gu_err registers to tell the compiler that those operands are both read and written by the assembly instruction.

Fixes 4b9d2a731c3d ("parisc: Switch user access functions to signal errors in r29 instead of r8")
arch/parisc/include/asm/uaccess.h +0 -0
index ebf8a845b017..68f5c1eaaa6f 100644

arch/parisc/include/asm/uaccess.h struct exception_table_entry { · line 92

Quoted 5 lines
arch/parisc/include/asm/uaccess.h +1 -1
@@ -89,7 +89,7 @@ struct exception_table_entry {
  	__asm__("1: " ldx " 0(" sr "%2),%0\n"		\
  		"9:\n"					\
  		ASM_EXCEPTIONTABLE_ENTRY_EFAULT(1b, 9b)	\
-		: "=r"(__gu_val), "=r"(__gu_err)        \
+		: "=&r"(__gu_val), "+r"(__gu_err)        \

I don't believe the early clobber is needed on __gnu_val.

Quoted 25 lines
arch/parisc/include/asm/uaccess.h +2 -3
  		: "r"(ptr), "1"(__gu_err));		\
  							\
  	(val) = (__force __typeof__(*(ptr))) __gu_val;	\
@@ -123,7 +123,7 @@ struct exception_table_entry {
  		"9:\n"					\
  		ASM_EXCEPTIONTABLE_ENTRY_EFAULT(1b, 9b)	\
  		ASM_EXCEPTIONTABLE_ENTRY_EFAULT(2b, 9b)	\
-		: "=&r"(__gu_tmp.l), "=r"(__gu_err)	\
+		: "=&r"(__gu_tmp.l), "+r"(__gu_err)	\
  		: "r"(ptr), "1"(__gu_err));		\
  							\
  	(val) = __gu_tmp.t;				\
@@ -132,10 +132,9 @@ struct exception_table_entry {
  #endif /* !defined(CONFIG_64BIT) */
 
 
-#define __put_user_internal(sr, x, ptr)				\
+#define __put_user_internal(sr, __x, ptr)			\
  ({								\
  	ASM_EXCEPTIONTABLE_VAR(__pu_err);		      	\
-        __typeof__(*(ptr)) __x = (__typeof__(*(ptr)))(x);	\
  								\
  	switch (sizeof(*(ptr))) {				\
  	case 1: __put_user_asm(sr, "stb", __x, ptr); break;	\

arch/parisc/include/asm/uaccess.h struct exception_table_entry { · line 152

Quoted 5 lines
arch/parisc/include/asm/uaccess.h +1 -1
@@ -150,7 +149,9 @@ struct exception_table_entry {
 
  #define __put_user(x, ptr)					\
  ({								\
-	__put_user_internal("%%sr3,", x, ptr);			\
+	 __typeof__(*(ptr)) __x = (__typeof__(*(ptr)))(x);	\

Whitespace?

Quoted 15 lines
arch/parisc/include/asm/uaccess.h +3 -1
+								\
+	__put_user_internal("%%sr3,", __x, ptr);		\
  })
 
  #define __put_kernel_nofault(dst, src, type, err_label)		\
@@ -180,7 +181,7 @@ struct exception_table_entry {
  		"1: " stx " %2,0(" sr "%1)\n"			\
  		"9:\n"						\
  		ASM_EXCEPTIONTABLE_ENTRY_EFAULT(1b, 9b)		\
-		: "=r"(__pu_err)				\
+		: "+r"(__pu_err)				\
  		: "r"(ptr), "r"(x), "0"(__pu_err))
 
 

arch/parisc/include/asm/uaccess.h struct exception_table_entry { · line 200

Quoted 8 lines
arch/parisc/include/asm/uaccess.h +1 -1
@@ -193,7 +194,7 @@ struct exception_table_entry {
  		"9:\n"						\
  		ASM_EXCEPTIONTABLE_ENTRY_EFAULT(1b, 9b)		\
  		ASM_EXCEPTIONTABLE_ENTRY_EFAULT(2b, 9b)		\
-		: "=r"(__pu_err)				\
+		: "+r"(__pu_err)				\
  		: "r"(ptr), "r"(__val), "0"(__pu_err));		\
  } while (0)
 

Dave

Signature · 1 line
John David Anglin  [email protected]
author maintainer
#2 lore raw
Quoted 8 lines · John David Anglin
Quoted 7 lines · Helge Deller

After commit 4b9d2a731c3d ("parisc: Switch user access functions to signal errors in r29 instead of r8") bash suddenly started to report those warnings after login:

(quoted diff) +0 -2
-bash: cannot set terminal process group (-1): Bad file descriptor
-bash: no job control in this shell
Quoted 7 lines
Quoted 6 lines

It turned out, that another function call inside a put_user(), e.g.:
   put_user(vt_do_kdgkbmode(console), (int __user *)arg); clobbered the error register (r29) and thus failed.
Avoid this miscompilation by first calculate the value in __put_user() before calling __put_user_internal() to actually write the value to user space.

Couldn't the same issue occur with ptr argument in these macros?

Theoretically, yes. Haven't seen it though.

Quoted 7 lines
Quoted 7 lines

Additionally, prefer the "+" constraint on pu_err and gu_err registers to tell the compiler that those operands are both read and written by the assembly instruction.

Fixes 4b9d2a731c3d ("parisc: Switch user access functions to signal errors in r29 instead of r8")
Quoted 11 lines

arch/parisc/include/asm/uaccess.h struct exception_table_entry { · line 92

Quoted 10 lines
arch/parisc/include/asm/uaccess.h +1 -1
index ebf8a845b017..68f5c1eaaa6f 100644
@@ -89,7 +89,7 @@ struct exception_table_entry {
       __asm__("1: " ldx " 0(" sr "%2),%0\n"        \
           "9:\n"                    \
           ASM_EXCEPTIONTABLE_ENTRY_EFAULT(1b, 9b)    \
-        : "=r"(__gu_val), "=r"(__gu_err)        \
+        : "=&r"(__gu_val), "+r"(__gu_err)        \

I don't believe the early clobber is needed on __gnu_val.

Right.

Quoted 26 lines
Quoted 26 lines
arch/parisc/include/asm/uaccess.h +2 -3
           : "r"(ptr), "1"(__gu_err));        \
                               \
       (val) = (__force __typeof__(*(ptr))) __gu_val;    \
@@ -123,7 +123,7 @@ struct exception_table_entry {
           "9:\n"                    \
           ASM_EXCEPTIONTABLE_ENTRY_EFAULT(1b, 9b)    \
           ASM_EXCEPTIONTABLE_ENTRY_EFAULT(2b, 9b)    \
-        : "=&r"(__gu_tmp.l), "=r"(__gu_err)    \
+        : "=&r"(__gu_tmp.l), "+r"(__gu_err)    \
           : "r"(ptr), "1"(__gu_err));        \
                               \
       (val) = __gu_tmp.t;                \
@@ -132,10 +132,9 @@ struct exception_table_entry {
   #endif /* !defined(CONFIG_64BIT) */
 
 
-#define __put_user_internal(sr, x, ptr)                \
+#define __put_user_internal(sr, __x, ptr)            \
   ({                                \
       ASM_EXCEPTIONTABLE_VAR(__pu_err);                  \
-        __typeof__(*(ptr)) __x = (__typeof__(*(ptr)))(x);    \
                                   \
       switch (sizeof(*(ptr))) {                \
       case 1: __put_user_asm(sr, "stb", __x, ptr); break;    \
@@ -150,7 +149,9 @@ struct exception_table_entry {
 
Quoted 5 lines

arch/parisc/include/asm/uaccess.h struct exception_table_entry { · line 152

Quoted 4 lines
arch/parisc/include/asm/uaccess.h +1 -1
   #define __put_user(x, ptr)                    \
   ({                                \
-    __put_user_internal("%%sr3,", x, ptr);            \
+     __typeof__(*(ptr)) __x = (__typeof__(*(ptr)))(x);    \

Whitespace?

Will fix.

Quoted 14 lines
Quoted 14 lines
arch/parisc/include/asm/uaccess.h +3 -1
+                                \
+    __put_user_internal("%%sr3,", __x, ptr);        \
   })
 
   #define __put_kernel_nofault(dst, src, type, err_label)        \
@@ -180,7 +181,7 @@ struct exception_table_entry {
           "1: " stx " %2,0(" sr "%1)\n"            \
           "9:\n"                        \
           ASM_EXCEPTIONTABLE_ENTRY_EFAULT(1b, 9b)        \
-        : "=r"(__pu_err)                \
+        : "+r"(__pu_err)                \
           : "r"(ptr), "r"(x), "0"(__pu_err))
 
 
Quoted 8 lines

arch/parisc/include/asm/uaccess.h struct exception_table_entry { · line 199

Quoted 8 lines
arch/parisc/include/asm/uaccess.h +1 -1
@@ -193,7 +194,7 @@ struct exception_table_entry {
           "9:\n"                        \
           ASM_EXCEPTIONTABLE_ENTRY_EFAULT(1b, 9b)        \
           ASM_EXCEPTIONTABLE_ENTRY_EFAULT(2b, 9b)        \
-        : "=r"(__pu_err)                \
+        : "+r"(__pu_err)                \
           : "r"(ptr), "r"(__val), "0"(__pu_err));        \
   } while (0)

Overall, this current patch turned out to not cover all cases. I think it's too complicated to really try prevent the compiler to optimize it here, so I'll send out a new patch shortly which simply initializes __gu_err and __pu_err in the assembly statement itself. My other codings to avoid that would increase the code a lot, and I'm not sure if it's even then possible to avoid the failure always.

Helge