Skip to contentSkip to conversation

x86: polish __{get,put}_user_{,no}check()

PATCH In review Jan Beulich 3 participants 7 messages May 2, 2017 – May 4, 2017

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86: polish __{get,put}_user_{,no}check()

The primary purpose is correcting a latent bug in __get_user_check() (the macro has no active user at present): The access_ok() check should be before the actual access, or else any PV guest could initiate MMIO reads with side effects.

Clean up all four macros at once:
- all arguments evaluated exactly once
- build the "check" flavor using the "nocheck" ones, instead of open coding them
- "int" is wide enough for error codes
- name local variables without using underscores as prefixes
- avoid pointless parentheses
- add blanks after commas separating parameters or arguments
- consistently use tabs for indentation

---
This corrects the code which would have resulted in an XSA on Xen 4.2 and older, if those were still security supported. For that reason I at least want to explore whether this is a change we want to take for 4.9.

xen/include/asm-x86/uaccess.h +22 -24
@@ -104,37 +104,35 @@ extern void __put_user_bad(void);
 #define __put_user(x,ptr) \
   __put_user_nocheck((__typeof__(*(ptr)))(x),(ptr),sizeof(*(ptr)))
 
-#define __put_user_nocheck(x,ptr,size)				\
-({								\
-	long __pu_err;						\
-	__put_user_size((x),(ptr),(size),__pu_err,-EFAULT);	\
-	__pu_err;						\
+#define __put_user_nocheck(x, ptr, size)				\
+({									\
+	int err_; 							\
+	__put_user_size(x, ptr, size, err_, -EFAULT);			\
+	err_;								\
 })
 
-#define __put_user_check(x,ptr,size)					\
+#define __put_user_check(x, ptr, size)					\
 ({									\
-	long __pu_err = -EFAULT;					\
-	__typeof__(*(ptr)) __user *__pu_addr = (ptr);			\
-	if (access_ok(__pu_addr,size))					\
-		__put_user_size((x),__pu_addr,(size),__pu_err,-EFAULT);	\
-	__pu_err;							\
+	__typeof__(*(ptr)) __user *ptr_ = (ptr);			\
+	__typeof__(size) size_ = (size);				\
+	access_ok(ptr_, size_) ? __put_user_nocheck(x, ptr_, size_)	\
+			       : -EFAULT;				\
 })							
 
-#define __get_user_nocheck(x,ptr,size)                          \
-({                                                              \
-	long __gu_err;                                          \
-	__get_user_size((x),(ptr),(size),__gu_err,-EFAULT);     \
-	__gu_err;                                               \
+#define __get_user_nocheck(x, ptr, size)				\
+({									\
+	int err_; 							\
+	__get_user_size(x, ptr, size, err_, -EFAULT);			\
+	err_;								\
 })
 
-#define __get_user_check(x,ptr,size)                            \
-({                                                              \
-	long __gu_err;                                          \
-	__typeof__(*(ptr)) __user *__gu_addr = (ptr);           \
-	__get_user_size((x),__gu_addr,(size),__gu_err,-EFAULT); \
-	if (!access_ok(__gu_addr,size)) __gu_err = -EFAULT;     \
-	__gu_err;                                               \
-})							
+#define __get_user_check(x, ptr, size)					\
+({									\
+	__typeof__(*(ptr)) __user *ptr_ = (ptr);			\
+	__typeof__(size) size_ = (size);				\
+	access_ok(ptr_, size_) ? __get_user_nocheck(x, ptr_, size_)	\
+			       : -EFAULT;				\
+})
 
 struct __large_struct { unsigned long buf[100]; };
 #define __m(x) (*(const struct __large_struct *)(x))

x86: polish __{get,put}_user_{,no}check()

The primary purpose is correcting a latent bug in __get_user_check() (the macro has no active user at present): The access_ok() check should be before the actual access, or else any PV guest could initiate MMIO reads with side effects.

Clean up all four macros at once:
- all arguments evaluated exactly once
- build the "check" flavor using the "nocheck" ones, instead of open coding them
- "int" is wide enough for error codes
- name local variables without using underscores as prefixes
- avoid pointless parentheses
- add blanks after commas separating parameters or arguments
- consistently use tabs for indentation

---
This corrects the code which would have resulted in an XSA on Xen 4.2 and older, if those were still security supported. For that reason I at least want to explore whether this is a change we want to take for 4.9.

xen/include/asm-x86/uaccess.h +22 -24

59 lines not on this page · raw message

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#1 lore raw
Quoted 5 lines · Jan Beulich

The primary purpose is correcting a latent bug in __get_user_check() (the macro has no active user at present): The access_ok() check should be before the actual access, or else any PV guest could initiate MMIO reads with side effects.

Quoted 9 lines

Clean up all four macros at once:
- all arguments evaluated exactly once
- build the "check" flavor using the "nocheck" ones, instead of open coding them
- "int" is wide enough for error codes
- name local variables without using underscores as prefixes
- avoid pointless parentheses
- add blanks after commas separating parameters or arguments
- consistently use tabs for indentation

Could we use spaces? This file is already half and half style, and these bits of code are a long way removed from their Linux heritage.

Quoted 28 lines

---
This corrects the code which would have resulted in an XSA on Xen 4.2 and older, if those were still security supported. For that reason I at least want to explore whether this is a change we want to take for 4.9.

xen/include/asm-x86/uaccess.h +6 -6
@@ -104,37 +104,35 @@ extern void __put_user_bad(void);
 #define __put_user(x,ptr) \
   __put_user_nocheck((__typeof__(*(ptr)))(x),(ptr),sizeof(*(ptr)))
 
-#define __put_user_nocheck(x,ptr,size)				\
-({								\
-	long __pu_err;						\
-	__put_user_size((x),(ptr),(size),__pu_err,-EFAULT);	\
-	__pu_err;						\
+#define __put_user_nocheck(x, ptr, size)				\
+({									\
+	int err_; 							\
+	__put_user_size(x, ptr, size, err_, -EFAULT);			\
+	err_;								\
 })
 
-#define __put_user_check(x,ptr,size)					\
+#define __put_user_check(x, ptr, size)					\
 ({									\

xen/include/asm-x86/uaccess.h extern void __put_user_bad(void); · line 120

Quoted 10 lines
xen/include/asm-x86/uaccess.h +4 -5
-	long __pu_err = -EFAULT;					\
-	__typeof__(*(ptr)) __user *__pu_addr = (ptr);			\
-	if (access_ok(__pu_addr,size))					\
-		__put_user_size((x),__pu_addr,(size),__pu_err,-EFAULT);	\
-	__pu_err;							\
+	__typeof__(*(ptr)) __user *ptr_ = (ptr);			\
+	__typeof__(size) size_ = (size);				\
+	access_ok(ptr_, size_) ? __put_user_nocheck(x, ptr_, size_)	\
+			       : -EFAULT;				\
 })							

Can you clobber the trailing whitespace on this line, like you did with __get_user_check() ?

Otherwise, Reviewed-by: Andrew Cooper <[email protected]>

Quoted 24 lines
xen/include/asm-x86/uaccess.h +6 -13
-#define __get_user_nocheck(x,ptr,size)                          \
-({                                                              \
-	long __gu_err;                                          \
-	__get_user_size((x),(ptr),(size),__gu_err,-EFAULT);     \
-	__gu_err;                                               \
+#define __get_user_nocheck(x, ptr, size)				\
+({									\
+	int err_; 							\
+	__get_user_size(x, ptr, size, err_, -EFAULT);			\
+	err_;								\
 })
 
-#define __get_user_check(x,ptr,size)                            \
-({                                                              \
-	long __gu_err;                                          \
-	__typeof__(*(ptr)) __user *__gu_addr = (ptr);           \
-	__get_user_size((x),__gu_addr,(size),__gu_err,-EFAULT); \
-	if (!access_ok(__gu_addr,size)) __gu_err = -EFAULT;     \
-	__gu_err;                                               \
-})							
+#define __get_user_check(x, ptr, size)					\

xen/include/asm-x86/uaccess.h extern void __put_user_bad(void);

Quoted 10 lines
xen/include/asm-x86/uaccess.h +6 -0
+({									\
+	__typeof__(*(ptr)) __user *ptr_ = (ptr);			\
+	__typeof__(size) size_ = (size);				\
+	access_ok(ptr_, size_) ? __get_user_nocheck(x, ptr_, size_)	\
+			       : -EFAULT;				\
+})
 
 struct __large_struct { unsigned long buf[100]; };
 #define __m(x) (*(const struct __large_struct *)(x))
 

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#2 lore raw
Quoted 17 lines
Quoted 1 line
Quoted 1 line · On 02.05.17 at 16:28, <[email protected]> wrote:
Quoted 5 lines · Jan Beulich

The primary purpose is correcting a latent bug in __get_user_check() (the macro has no active user at present): The access_ok() check should be before the actual access, or else any PV guest could initiate MMIO reads with side effects.

Quoted 9 lines

Clean up all four macros at once:
- all arguments evaluated exactly once
- build the "check" flavor using the "nocheck" ones, instead of open coding them
- "int" is wide enough for error codes
- name local variables without using underscores as prefixes
- avoid pointless parentheses
- add blanks after commas separating parameters or arguments
- consistently use tabs for indentation

Quoted 2 lines

Could we use spaces? This file is already half and half style, and these bits of code are a long way removed from their Linux heritage.

Well, if you're convinced this is better. I did consider doing so, but didn't because it's a relatively small portion of code which does use spaces at present.

Quoted 18 lines
Quoted 18 lines
xen/include/asm-x86/uaccess.h +5 -5
@@ -104,37 +104,35 @@ extern void __put_user_bad(void);
 #define __put_user(x,ptr) \
   __put_user_nocheck((__typeof__(*(ptr)))(x),(ptr),sizeof(*(ptr)))
 
-#define __put_user_nocheck(x,ptr,size)				\
-({								\
-	long __pu_err;						\
-	__put_user_size((x),(ptr),(size),__pu_err,-EFAULT);	\
-	__pu_err;						\
+#define __put_user_nocheck(x, ptr, size)				\
+({									\
+	int err_; 							\
+	__put_user_size(x, ptr, size, err_, -EFAULT);			\
+	err_;								\
 })
 
Quoted 16 lines

xen/include/asm-x86/uaccess.h extern void __put_user_bad(void); · line 120

Quoted 13 lines
xen/include/asm-x86/uaccess.h +5 -6
-#define __put_user_check(x,ptr,size)					\
+#define __put_user_check(x, ptr, size)					\
 ({									\
-	long __pu_err = -EFAULT;					\
-	__typeof__(*(ptr)) __user *__pu_addr = (ptr);			\
-	if (access_ok(__pu_addr,size))					\
-		__put_user_size((x),__pu_addr,(size),__pu_err,-EFAULT);	\
-	__pu_err;							\
+	__typeof__(*(ptr)) __user *ptr_ = (ptr);			\
+	__typeof__(size) size_ = (size);				\
+	access_ok(ptr_, size_) ? __put_user_nocheck(x, ptr_, size_)	\
+			       : -EFAULT;				\
 })							

Can you clobber the trailing whitespace on this line, like you did with __get_user_check() ?

Oh, sure. I didn't notice there was a similar issue here.

Quoted 1 line

Otherwise, Reviewed-by: Andrew Cooper <[email protected]>

Thanks, but please let me know whether you feel strongly about using spaces instead of tabs.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#3 lore raw
Quoted 55 lines · Jan Beulich
Quoted 16 lines
Quoted 1 line
Quoted 1 line · On 02.05.17 at 16:28, <[email protected]> wrote:
Quoted 5 lines · Jan Beulich

The primary purpose is correcting a latent bug in __get_user_check() (the macro has no active user at present): The access_ok() check should be before the actual access, or else any PV guest could initiate MMIO reads with side effects.

Quoted 9 lines

Clean up all four macros at once:
- all arguments evaluated exactly once
- build the "check" flavor using the "nocheck" ones, instead of open coding them
- "int" is wide enough for error codes
- name local variables without using underscores as prefixes
- avoid pointless parentheses
- add blanks after commas separating parameters or arguments
- consistently use tabs for indentation

Quoted 2 lines

Could we use spaces? This file is already half and half style, and these bits of code are a long way removed from their Linux heritage.

Well, if you're convinced this is better. I did consider doing so, but didn't because it's a relatively small portion of code which does use spaces at present.

Quoted 18 lines
Quoted 18 lines
xen/include/asm-x86/uaccess.h +5 -5
@@ -104,37 +104,35 @@ extern void __put_user_bad(void);
 #define __put_user(x,ptr) \
   __put_user_nocheck((__typeof__(*(ptr)))(x),(ptr),sizeof(*(ptr)))
 
-#define __put_user_nocheck(x,ptr,size)				\
-({								\
-	long __pu_err;						\
-	__put_user_size((x),(ptr),(size),__pu_err,-EFAULT);	\
-	__pu_err;						\
+#define __put_user_nocheck(x, ptr, size)				\
+({									\
+	int err_; 							\
+	__put_user_size(x, ptr, size, err_, -EFAULT);			\
+	err_;								\
 })
 
Quoted 15 lines

xen/include/asm-x86/uaccess.h extern void __put_user_bad(void); · line 120

Quoted 13 lines
xen/include/asm-x86/uaccess.h +5 -6
-#define __put_user_check(x,ptr,size)					\
+#define __put_user_check(x, ptr, size)					\
 ({									\
-	long __pu_err = -EFAULT;					\
-	__typeof__(*(ptr)) __user *__pu_addr = (ptr);			\
-	if (access_ok(__pu_addr,size))					\
-		__put_user_size((x),__pu_addr,(size),__pu_err,-EFAULT);	\
-	__pu_err;							\
+	__typeof__(*(ptr)) __user *ptr_ = (ptr);			\
+	__typeof__(size) size_ = (size);				\
+	access_ok(ptr_, size_) ? __put_user_nocheck(x, ptr_, size_)	\
+			       : -EFAULT;				\
 })							

Can you clobber the trailing whitespace on this line, like you did with __get_user_check() ?

Quoted 5 lines

Oh, sure. I didn't notice there was a similar issue here.

Quoted 1 line

Otherwise, Reviewed-by: Andrew Cooper <[email protected]>

Thanks, but please let me know whether you feel strongly about using spaces instead of tabs.

I'd prefer spaces (for overall consistency in the file), but my R-by isn't conditional on it (as the file is already very mixed).

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#4 lore raw
In reply to Jan Beulich

Hi,

Quoted 15 lines · Jan Beulich

The primary purpose is correcting a latent bug in __get_user_check() (the macro has no active user at present): The access_ok() check should be before the actual access, or else any PV guest could initiate MMIO reads with side effects.

Clean up all four macros at once:
- all arguments evaluated exactly once
- build the "check" flavor using the "nocheck" ones, instead of open coding them
- "int" is wide enough for error codes
- name local variables without using underscores as prefixes
- avoid pointless parentheses
- add blanks after commas separating parameters or arguments
- consistently use tabs for indentation

Quoted 5 lines

---
This corrects the code which would have resulted in an XSA on Xen 4.2 and older, if those were still security supported. For that reason I at least want to explore whether this is a change we want to take for 4.9.

Andrew, do you have any opinion to get this patch in Xen 4.9?

Cheers,

Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#5 lore raw
Quoted 18 lines · Julien Grall

Hi,

Quoted 15 lines · Jan Beulich

The primary purpose is correcting a latent bug in __get_user_check() (the macro has no active user at present): The access_ok() check should be before the actual access, or else any PV guest could initiate MMIO reads with side effects.

Clean up all four macros at once:
- all arguments evaluated exactly once
- build the "check" flavor using the "nocheck" ones, instead of open coding them
- "int" is wide enough for error codes
- name local variables without using underscores as prefixes
- avoid pointless parentheses
- add blanks after commas separating parameters or arguments
- consistently use tabs for indentation

Quoted 7 lines
Quoted 5 lines

---
This corrects the code which would have resulted in an XSA on Xen 4.2 and older, if those were still security supported. For that reason I at least want to explore whether this is a change we want to take for 4.9.

Andrew, do you have any opinion to get this patch in Xen 4.9?

It should go in.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#6 lore raw

Hi Andrew,

Quoted 25 lines · Andrew Cooper
Quoted 24 lines · Julien Grall

Hi,

Quoted 20 lines · Jan Beulich

The primary purpose is correcting a latent bug in __get_user_check() (the macro has no active user at present): The access_ok() check should be before the actual access, or else any PV guest could initiate MMIO reads with side effects.

Clean up all four macros at once:
- all arguments evaluated exactly once
- build the "check" flavor using the "nocheck" ones, instead of open coding them
- "int" is wide enough for error codes
- name local variables without using underscores as prefixes
- avoid pointless parentheses
- add blanks after commas separating parameters or arguments
- consistently use tabs for indentation

---
This corrects the code which would have resulted in an XSA on Xen 4.2 and older, if those were still security supported. For that reason I at least want to explore whether this is a change we want to take for 4.9.

Quoted 3 lines
Quoted 1 line

Andrew, do you have any opinion to get this patch in Xen 4.9?

It should go in.

Cheers,

Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel