Skip to contentSkip to conversation

x86/uaccess: Don't leak the AC flag into __put_user() value evaluation

PATCH Superseded Maxim Zhukov 2 participants 2 messages Mar 3, 2019

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/uaccess: Don't leak the AC flag into __put_user() value evaluation

From: Andy Lutomirski <[email protected]>

When calling __put_user(foo(), ptr), the __put_user() macro would call foo() in between __uaccess_begin() and __uaccess_end(). If that code were buggy, then those bugs would be run without SMAP protection.

Fortunately, there seem to be few instances of the problem in the kernel. Nevertheless, __put_user() should be fixed to avoid doing this. Therefore, evaluate __put_user()'s argument before setting AC.

This issue was noticed when an objtool hack by Peter Zijlstra complained about genregs_get() and I compared the assembly output to the C source.

[ bp: Massage commit message and fixed up whitespace. ]

Fixes 11f1a4b9755f ("x86: reorganize SMAP handling in user space accesses")
Cc Peter Zijlstra <[email protected]>
Cc Brian Gerst <[email protected]>
Cc Josh Poimboeuf <[email protected]>
Cc Denys Vlasenko <[email protected]>
---
 arch/x86/include/asm/uaccess.h | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
arch/x86/include/asm/uaccess.h +4 -2
index a8d85a687cf4..39bbc225558c 100644
@@ -292,7 +292,7 @@ do {									\
 		__put_user_asm(x, ptr, retval, "l", "k", "ir", errret);	\
 		break;							\
 	case 8:								\
-		__put_user_asm_u64((__typeof__(*ptr))(x), ptr, retval,	\
+		__put_user_asm_u64(x, ptr, retval,	\
 				   errret);				\
 		break;							\
 	default:							\
@@ -427,8 +427,10 @@ do {									\
 #define __put_user_nocheck(x, ptr, size)			\
 ({								\
 	int __pu_err;						\
+	__typeof__(*(ptr)) __pu_val;				\
 	__uaccess_begin();					\
-	__put_user_size((x), (ptr), (size), __pu_err, -EFAULT);	\
+	__pu_val = x;						\
+	__put_user_size(__pu_val, (ptr), (size), __pu_err, -EFAULT);	\
 	__uaccess_end();					\
 	__builtin_expect(__pu_err, 0);				\
 })
Signature · 1 line
2.21.0
#1 lore raw
Quoted 43 lines · Maxim Zhukov

From: Andy Lutomirski <[email protected]>

When calling __put_user(foo(), ptr), the __put_user() macro would call foo() in between __uaccess_begin() and __uaccess_end(). If that code were buggy, then those bugs would be run without SMAP protection.

Fortunately, there seem to be few instances of the problem in the kernel. Nevertheless, __put_user() should be fixed to avoid doing this. Therefore, evaluate __put_user()'s argument before setting AC.

This issue was noticed when an objtool hack by Peter Zijlstra complained about genregs_get() and I compared the assembly output to the C source.

[ bp: Massage commit message and fixed up whitespace. ]

Fixes 11f1a4b9755f ("x86: reorganize SMAP handling in user space accesses")
Cc Peter Zijlstra <[email protected]>
Cc Brian Gerst <[email protected]>
Cc Josh Poimboeuf <[email protected]>
Cc Denys Vlasenko <[email protected]>
---
 arch/x86/include/asm/uaccess.h | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
arch/x86/include/asm/uaccess.h +1 -1
index a8d85a687cf4..39bbc225558c 100644
@@ -292,7 +292,7 @@ do {									\
 		__put_user_asm(x, ptr, retval, "l", "k", "ir", errret);	\
 		break;							\
 	case 8:								\
-		__put_user_asm_u64((__typeof__(*ptr))(x), ptr, retval,	\
+		__put_user_asm_u64(x, ptr, retval,	\
 				   errret);				\
 		break;							\
 	default:							\

arch/x86/include/asm/uaccess.h do { \ · line 432

Quoted 7 lines
arch/x86/include/asm/uaccess.h +2 -1
@@ -427,8 +427,10 @@ do {									\
 #define __put_user_nocheck(x, ptr, size)			\
 ({								\
 	int __pu_err;						\
+	__typeof__(*(ptr)) __pu_val;				\
 	__uaccess_begin();					\
-	__put_user_size((x), (ptr), (size), __pu_err, -EFAULT);	\
+	__pu_val = x;						\

This is incorrect, __pu_val's assignment should be before __uaccess_begin like in the original patch (which is the point of the patch in the first place).

Cheers,
Nathan

Quoted 7 lines
arch/x86/include/asm/uaccess.h +1 -0
+	__put_user_size(__pu_val, (ptr), (size), __pu_err, -EFAULT);	\
 	__uaccess_end();					\
 	__builtin_expect(__pu_err, 0);				\
 })
Signature · 1 line
2.21.0