Skip to contentSkip to conversation

asm-generic: {get,put}_user ptr argument evaluate only 1 time

PATCH Superseded Yoshinori Sato 4 participants 12 messages Jul 16, 2015 – Jul 23, 2015 Generic include/asm header files

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 asm-generic: {get,put}_user ptr argument evaluate only 1 time

Current implemantation ptr argument evaluate 2 times. It'll be an unexpected result.

---
 include/asm-generic/uaccess.h | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
include/asm-generic/uaccess.h +6 -4
index 72d8803..1b813fb 100644
@@ -163,9 +163,10 @@ static inline __must_check long __copy_to_user(void __user *to,
 
 #define put_user(x, ptr)					\
 ({								\
+	__typeof__((ptr)) __p = (ptr);				\
 	might_fault();						\
-	access_ok(VERIFY_WRITE, ptr, sizeof(*ptr)) ?		\
-		__put_user(x, ptr) :				\
+	access_ok(VERIFY_WRITE, __p, sizeof(*__p)) ?		\
+		__put_user(x, __p) :				\
 		-EFAULT;					\
 })
 
@@ -225,9 +226,10 @@ extern int __put_user_bad(void) __attribute__((noreturn));
 
 #define get_user(x, ptr)					\
 ({								\
+	__typeof__((ptr)) __p = (ptr);				\
 	might_fault();						\
-	access_ok(VERIFY_READ, ptr, sizeof(*ptr)) ?		\
-		__get_user(x, ptr) :				\
+	access_ok(VERIFY_READ, __p, sizeof(*__p)) ?		\
+		__get_user(x, __p) :				\
 		-EFAULT;					\
 })
 
Signature · 1 line
2.1.4
Acked-by on v1 1/1
#1 lore raw

Hi Sato-san,

Quoted 4 lines · Yoshinori Sato

Current implemantation ptr argument evaluate 2 times. It'll be an unexpected result.

Quoted 9 lines
---
 include/asm-generic/uaccess.h | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
include/asm-generic/uaccess.h +0 -0
index 72d8803..1b813fb 100644

include/asm-generic/uaccess.h static inline __must_check long __copy_to_user(void __user *to, · line 169

Quoted 9 lines
include/asm-generic/uaccess.h +3 -2
@@ -163,9 +163,10 @@ static inline __must_check long __copy_to_user(void __user *to,
 
 #define put_user(x, ptr)                                       \
 ({                                                             \
+       __typeof__((ptr)) __p = (ptr);                          \
        might_fault();                                          \
-       access_ok(VERIFY_WRITE, ptr, sizeof(*ptr)) ?            \
-               __put_user(x, ptr) :                            \
+       access_ok(VERIFY_WRITE, __p, sizeof(*__p)) ?            \
+               __put_user(x, __p) :                            \

For safety, you may want to change "x" to "(x") while at it.

include/asm-generic/uaccess.h extern int __put_user_bad(void) __attribute__((noreturn)); · line 232

Quoted 13 lines
include/asm-generic/uaccess.h +3 -2
                -EFAULT;                                        \
 })
 
@@ -225,9 +226,10 @@ extern int __put_user_bad(void) __attribute__((noreturn));
 
 #define get_user(x, ptr)                                       \
 ({                                                             \
+       __typeof__((ptr)) __p = (ptr);                          \
        might_fault();                                          \
-       access_ok(VERIFY_READ, ptr, sizeof(*ptr)) ?             \
-               __get_user(x, ptr) :                            \
+       access_ok(VERIFY_READ, __p, sizeof(*__p)) ?             \
+               __get_user(x, __p) :                            \

Likewise.

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds

#2 lore raw
Quoted 22 lines · Geert Uytterhoeven

Hi Sato-san,

Quoted 4 lines · Yoshinori Sato

Current implemantation ptr argument evaluate 2 times. It'll be an unexpected result.

Quoted 10 lines
---
 include/asm-generic/uaccess.h | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
include/asm-generic/uaccess.h +0 -0
index 72d8803..1b813fb 100644
@@ -163,9 +163,10 @@ static inline __must_check long __copy_to_user(void __user *to,
 
Quoted 10 lines

include/asm-generic/uaccess.h static inline __must_check long __copy_to_user(void __user *to, · line 169

Quoted 8 lines
include/asm-generic/uaccess.h +3 -2
 #define put_user(x, ptr)                                       \
 ({                                                             \
+       __typeof__((ptr)) __p = (ptr);                          \
        might_fault();                                          \
-       access_ok(VERIFY_WRITE, ptr, sizeof(*ptr)) ?            \
-               __put_user(x, ptr) :                            \
+       access_ok(VERIFY_WRITE, __p, sizeof(*__p)) ?            \
+               __put_user(x, __p) :                            \

For safety, you may want to change "x" to "(x") while at it.

That's right.
I'll sent v2.

Quoted 26 lines

include/asm-generic/uaccess.h extern int __put_user_bad(void) __attribute__((noreturn)); · line 232

Quoted 13 lines
include/asm-generic/uaccess.h +3 -2
                -EFAULT;                                        \
 })
 
@@ -225,9 +226,10 @@ extern int __put_user_bad(void) __attribute__((noreturn));
 
 #define get_user(x, ptr)                                       \
 ({                                                             \
+       __typeof__((ptr)) __p = (ptr);                          \
        might_fault();                                          \
-       access_ok(VERIFY_READ, ptr, sizeof(*ptr)) ?             \
-               __get_user(x, ptr) :                            \
+       access_ok(VERIFY_READ, __p, sizeof(*__p)) ?             \
+               __get_user(x, __p) :                            \

Likewise.

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds

Signature · 2 lines
Yoshinori Sato
<[email protected]>
maintainer Acked-by on v1 1/1
#3 lore raw
In reply to Yoshinori Sato
Quoted 4 lines · On Thursday 16 July 2015 14:15:22 Yoshinori Sato wrote:

Current implemantation ptr argument evaluate 2 times. It'll be an unexpected result.

Do you want to include this into a pull request of your own? I'm currently on leave and not planning to merge asm-generic patches for the next merge window.

Arnd

#5 lore raw

PATCH v2 1/1 asm-generic: {get,put}_user ptr argument evaluate only 1 time

In reply to Yoshinori Sato

Current implemantation ptr argument evaluate 2 times. It'll be an unexpected result.

Changes v2:
Add parenthesis to argument "x".

---
 include/asm-generic/uaccess.h | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
include/asm-generic/uaccess.h +6 -4
index 72d8803..ba6ab80 100644
@@ -163,9 +163,10 @@ static inline __must_check long __copy_to_user(void __user *to,
 
 #define put_user(x, ptr)					\
 ({								\
+	__typeof__((ptr)) __p = (ptr);				\
 	might_fault();						\
-	access_ok(VERIFY_WRITE, ptr, sizeof(*ptr)) ?		\
-		__put_user(x, ptr) :				\
+	access_ok(VERIFY_WRITE, __p, sizeof(*__p)) ?		\
+		__put_user((x), __p) :				\
 		-EFAULT;					\
 })
 
@@ -225,9 +226,10 @@ extern int __put_user_bad(void) __attribute__((noreturn));
 
 #define get_user(x, ptr)					\
 ({								\
+	__typeof__((ptr)) __p = (ptr);				\
 	might_fault();						\
-	access_ok(VERIFY_READ, ptr, sizeof(*ptr)) ?		\
-		__get_user(x, ptr) :				\
+	access_ok(VERIFY_READ, __p, sizeof(*__p)) ?		\
+		__get_user((x), __p) :				\
 		-EFAULT;					\
 })
 
Signature · 1 line
2.1.4
#4 lore raw
In reply to Arnd Bergmann
Quoted 7 lines · Arnd Bergmann
Quoted 4 lines · On Thursday 16 July 2015 14:15:22 Yoshinori Sato wrote:

Current implemantation ptr argument evaluate 2 times. It'll be an unexpected result.

Quoted 7 lines

Do you want to include this into a pull request of your own? I'm currently on leave and not planning to merge asm-generic patches for the next merge window.

Arnd

OK.
I'll sent later.
Thanks.

Signature · 2 lines
Yoshinori Sato
<[email protected]>
#6 lore raw

PATCH v3 1/1 asm-generic: {get,put}_user ptr argument evaluate only 1 time

In reply to Yoshinori Sato

Current implemantation ptr argument evaluate 2 times. It'll be an unexpected result.

Changes v3:
Some build error fix.
Changes v2:
Argument x protect.

---
 include/asm-generic/uaccess.h | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
include/asm-generic/uaccess.h +6 -4
index 72d8803..d800a3f 100644
@@ -163,9 +163,10 @@ static inline __must_check long __copy_to_user(void __user *to,
 
 #define put_user(x, ptr)					\
 ({								\
+	uintptr_t __uip = (uintptr_t)(ptr);			\
 	might_fault();						\
-	access_ok(VERIFY_WRITE, ptr, sizeof(*ptr)) ?		\
-		__put_user(x, ptr) :				\
+	access_ok(VERIFY_WRITE, __uip, sizeof(*ptr)) ?		\
+		__put_user((x), ((__typeof__(*ptr) *)__uip)) :	\
 		-EFAULT;					\
 })
 
@@ -225,9 +226,10 @@ extern int __put_user_bad(void) __attribute__((noreturn));
 
 #define get_user(x, ptr)					\
 ({								\
+	uintptr_t __uip = (uintptr_t)(ptr);			\
 	might_fault();						\
-	access_ok(VERIFY_READ, ptr, sizeof(*ptr)) ?		\
-		__get_user(x, ptr) :				\
+	access_ok(VERIFY_READ, __uip, sizeof(*ptr)) ?		\
+		__get_user((x), (__typeof__(*ptr) *)__uip) :	\
 		-EFAULT;					\
 })
 
Signature · 1 line
2.1.4
#7 lore raw

Review

Quoted 3 lines · Yoshinori Sato

#define get_user(x, ptr) \
({ \

(quoted diff) +1 -0
+	uintptr_t __uip = (uintptr_t)(ptr);			\

const?

include/asm-generic/uaccess.h extern int __put_user_bad(void) __attribute__((noreturn)); · line 234

Quoted 7 lines
include/asm-generic/uaccess.h +2 -2
 	might_fault();						\
-	access_ok(VERIFY_READ, ptr, sizeof(*ptr)) ?		\
-		__get_user(x, ptr) :				\
+	access_ok(VERIFY_READ, __uip, sizeof(*ptr)) ?		\
+		__get_user((x), (__typeof__(*ptr) *)__uip) :	\
 		-EFAULT;					\
 })

Would it be better to use void* instead of uintptr_t?

David

#8 lore raw
Quoted 8 lines · David Howells

Review

Quoted 3 lines · Yoshinori Sato

#define get_user(x, ptr) \
({ \

(quoted diff) +1 -0
+	uintptr_t __uip = (uintptr_t)(ptr);			\

const?

OK.

Quoted 9 lines

include/asm-generic/uaccess.h extern int __put_user_bad(void) __attribute__((noreturn)); · line 234

Quoted 7 lines
include/asm-generic/uaccess.h +2 -2
 	might_fault();						\
-	access_ok(VERIFY_READ, ptr, sizeof(*ptr)) ?		\
-		__get_user(x, ptr) :				\
+	access_ok(VERIFY_READ, __uip, sizeof(*ptr)) ?		\
+		__get_user((x), (__typeof__(*ptr) *)__uip) :	\
 		-EFAULT;					\
 })

Would it be better to use void* instead of uintptr_t?

No reason.
I'll changed void*

Thanks.

Quoted 1 line

David

Signature · 2 lines
Yoshinori Sato
<[email protected]>
#9 lore raw

PATCH v4 1/1 asm-generic: {get,put}_user ptr argument evaluate only 1 time

In reply to Yoshinori Sato

Current implemantation ptr argument evaluate 2 times. It'll be an unexpected result.

Changes v4:
Temporary pointer type change to const void*
Changes v3:
Some build error fix.
Changes v2:
Argument x protect.

---
 include/asm-generic/uaccess.h | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
include/asm-generic/uaccess.h +6 -4
index 72d8803..f02e696 100644
@@ -163,9 +163,10 @@ static inline __must_check long __copy_to_user(void __user *to,
 
 #define put_user(x, ptr)					\
 ({								\
+	const void *__p = (ptr);				\
 	might_fault();						\
-	access_ok(VERIFY_WRITE, ptr, sizeof(*ptr)) ?		\
-		__put_user(x, ptr) :				\
+	access_ok(VERIFY_WRITE, __p, sizeof(*ptr)) ?		\
+		__put_user((x), ((__typeof__(*(ptr)) *)__p)) :	\
 		-EFAULT;					\
 })
 
@@ -225,9 +226,10 @@ extern int __put_user_bad(void) __attribute__((noreturn));
 
 #define get_user(x, ptr)					\
 ({								\
+	const void *__p = (ptr);				\
 	might_fault();						\
-	access_ok(VERIFY_READ, ptr, sizeof(*ptr)) ?		\
-		__get_user(x, ptr) :				\
+	access_ok(VERIFY_READ, __p, sizeof(*ptr)) ?		\
+		__get_user((x), (__typeof__(*(ptr)) *)__p) :	\
 		-EFAULT;					\
 })
 
Signature · 1 line
2.1.4
#10 lore raw

Review

Quoted 3 lines · Yoshinori Sato

#define put_user(x, ptr) \
({ \

(quoted diff) +1 -0
+	const void *__p = (ptr);				\

Not const here, though...

David

#11 lore raw

PATCH v5 1/1 asm-generic: {get,put}_user ptr argument evaluate only 1 time

In reply to Yoshinori Sato

Current implemantation ptr argument evaluate 2 times. It'll be an unexpected result.

Changes v5:
Remove unnecessary const.
Changes v4:
Temporary pointer type change to const void*
Changes v3:
Some build error fix.
Changes v2:
Argument x protect.

---
 include/asm-generic/uaccess.h | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
include/asm-generic/uaccess.h +6 -4
index 72d8803..1bfa602 100644
@@ -163,9 +163,10 @@ static inline __must_check long __copy_to_user(void __user *to,
 
 #define put_user(x, ptr)					\
 ({								\
+	void *__p = (ptr);					\
 	might_fault();						\
-	access_ok(VERIFY_WRITE, ptr, sizeof(*ptr)) ?		\
-		__put_user(x, ptr) :				\
+	access_ok(VERIFY_WRITE, __p, sizeof(*ptr)) ?		\
+		__put_user((x), ((__typeof__(*(ptr)) *)__p)) :	\
 		-EFAULT;					\
 })
 
@@ -225,9 +226,10 @@ extern int __put_user_bad(void) __attribute__((noreturn));
 
 #define get_user(x, ptr)					\
 ({								\
+	const void *__p = (ptr);				\
 	might_fault();						\
-	access_ok(VERIFY_READ, ptr, sizeof(*ptr)) ?		\
-		__get_user(x, ptr) :				\
+	access_ok(VERIFY_READ, __p, sizeof(*ptr)) ?		\
+		__get_user((x), (__typeof__(*(ptr)) *)__p) :	\
 		-EFAULT;					\
 })
 
Signature · 1 line
2.1.4