2015-07-16 05:15:41

by Yoshinori Sato

[permalink] [raw]
Subject: [PATCH] asm-generic: {get,put}_user ptr argument evaluate only 1 time

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

Signed-off-by: Yoshinori Sato <[email protected]>
---
include/asm-generic/uaccess.h | 10 ++++++----
1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/include/asm-generic/uaccess.h b/include/asm-generic/uaccess.h
index 72d8803..1b813fb 100644
--- a/include/asm-generic/uaccess.h
+++ b/include/asm-generic/uaccess.h
@@ -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; \
})

--
2.1.4


2015-07-16 06:40:35

by Geert Uytterhoeven

[permalink] [raw]
Subject: Re: [PATCH] asm-generic: {get,put}_user ptr argument evaluate only 1 time

Hi Sato-san,

On Thu, Jul 16, 2015 at 7:15 AM, Yoshinori Sato
<[email protected]> wrote:
> Current implemantation ptr argument evaluate 2 times.
> It'll be an unexpected result.
>
> Signed-off-by: Yoshinori Sato <[email protected]>

Acked-by: Geert Uytterhoeven <[email protected]>

> ---
> include/asm-generic/uaccess.h | 10 ++++++----
> 1 file changed, 6 insertions(+), 4 deletions(-)
>
> diff --git a/include/asm-generic/uaccess.h b/include/asm-generic/uaccess.h
> index 72d8803..1b813fb 100644
> --- a/include/asm-generic/uaccess.h
> +++ b/include/asm-generic/uaccess.h
> @@ -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.

> -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

2015-07-16 13:33:28

by Yoshinori Sato

[permalink] [raw]
Subject: Re: [PATCH] asm-generic: {get,put}_user ptr argument evaluate only 1 time

On Thu, 16 Jul 2015 15:40:31 +0900,
Geert Uytterhoeven wrote:
>
> Hi Sato-san,
>
> On Thu, Jul 16, 2015 at 7:15 AM, Yoshinori Sato
> <[email protected]> wrote:
> > Current implemantation ptr argument evaluate 2 times.
> > It'll be an unexpected result.
> >
> > Signed-off-by: Yoshinori Sato <[email protected]>
>
> Acked-by: Geert Uytterhoeven <[email protected]>
>
> > ---
> > include/asm-generic/uaccess.h | 10 ++++++----
> > 1 file changed, 6 insertions(+), 4 deletions(-)
> >
> > diff --git a/include/asm-generic/uaccess.h b/include/asm-generic/uaccess.h
> > index 72d8803..1b813fb 100644
> > --- a/include/asm-generic/uaccess.h
> > +++ b/include/asm-generic/uaccess.h
> > @@ -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.

That's right.
I'll sent v2.

> > -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

--
Yoshinori Sato
<[email protected]>

2015-07-16 14:15:41

by Arnd Bergmann

[permalink] [raw]
Subject: Re: [PATCH] asm-generic: {get,put}_user ptr argument evaluate only 1 time

On Thursday 16 July 2015 14:15:22 Yoshinori Sato wrote:
> Current implemantation ptr argument evaluate 2 times.
> It'll be an unexpected result.
>
> Signed-off-by: Yoshinori Sato <[email protected]>

Acked-by: Arnd Bergmann <[email protected]>

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

2015-07-16 14:16:53

by Yoshinori Sato

[permalink] [raw]
Subject: [PATCH v2] asm-generic: {get,put}_user ptr argument evaluate only 1 time

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

Changes v2:
Add parenthesis to argument "x".

Signed-off-by: Yoshinori Sato <[email protected]>
---
include/asm-generic/uaccess.h | 10 ++++++----
1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/include/asm-generic/uaccess.h b/include/asm-generic/uaccess.h
index 72d8803..ba6ab80 100644
--- a/include/asm-generic/uaccess.h
+++ b/include/asm-generic/uaccess.h
@@ -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; \
})

--
2.1.4

2015-07-17 03:27:24

by Yoshinori Sato

[permalink] [raw]
Subject: Re: [PATCH] asm-generic: {get,put}_user ptr argument evaluate only 1 time

On Thu, 16 Jul 2015 23:15:21 +0900,
Arnd Bergmann wrote:
>
> On Thursday 16 July 2015 14:15:22 Yoshinori Sato wrote:
> > Current implemantation ptr argument evaluate 2 times.
> > It'll be an unexpected result.
> >
> > Signed-off-by: Yoshinori Sato <[email protected]>
>
> Acked-by: Arnd Bergmann <[email protected]>
>
> 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.

--
Yoshinori Sato
<[email protected]>

2015-07-21 06:15:01

by Yoshinori Sato

[permalink] [raw]
Subject: [PATCH v3] asm-generic: {get,put}_user ptr argument evaluate only 1 time

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

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

Signed-off-by: Yoshinori Sato <[email protected]>
---
include/asm-generic/uaccess.h | 10 ++++++----
1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/include/asm-generic/uaccess.h b/include/asm-generic/uaccess.h
index 72d8803..d800a3f 100644
--- a/include/asm-generic/uaccess.h
+++ b/include/asm-generic/uaccess.h
@@ -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; \
})

--
2.1.4

2015-07-21 14:06:19

by David Howells

[permalink] [raw]
Subject: Re: [PATCH v3] asm-generic: {get,put}_user ptr argument evaluate only 1 time

Yoshinori Sato <[email protected]> wrote:

> #define get_user(x, ptr) \
> ({ \
> + uintptr_t __uip = (uintptr_t)(ptr); \

const?

> 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

2015-07-22 06:09:07

by Yoshinori Sato

[permalink] [raw]
Subject: Re: [PATCH v3] asm-generic: {get,put}_user ptr argument evaluate only 1 time

On Tue, 21 Jul 2015 23:06:13 +0900,
David Howells wrote:
>
> Yoshinori Sato <[email protected]> wrote:
>
> > #define get_user(x, ptr) \
> > ({ \
> > + uintptr_t __uip = (uintptr_t)(ptr); \
>
> const?

OK.

> > 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.

> David

--
Yoshinori Sato
<[email protected]>

2015-07-22 14:52:46

by Yoshinori Sato

[permalink] [raw]
Subject: [PATCH v4] asm-generic: {get,put}_user ptr argument evaluate only 1 time

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.

Signed-off-by: Yoshinori Sato <[email protected]>
---
include/asm-generic/uaccess.h | 10 ++++++----
1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/include/asm-generic/uaccess.h b/include/asm-generic/uaccess.h
index 72d8803..f02e696 100644
--- a/include/asm-generic/uaccess.h
+++ b/include/asm-generic/uaccess.h
@@ -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; \
})

--
2.1.4

2015-07-22 22:24:35

by David Howells

[permalink] [raw]
Subject: Re: [PATCH v4] asm-generic: {get,put}_user ptr argument evaluate only 1 time

Yoshinori Sato <[email protected]> wrote:

> #define put_user(x, ptr) \
> ({ \
> + const void *__p = (ptr); \

Not const here, though...

David

2015-07-23 17:55:27

by Yoshinori Sato

[permalink] [raw]
Subject: [PATCH v5] asm-generic: {get,put}_user ptr argument evaluate only 1 time

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.

Signed-off-by: Yoshinori Sato <[email protected]>
---
include/asm-generic/uaccess.h | 10 ++++++----
1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/include/asm-generic/uaccess.h b/include/asm-generic/uaccess.h
index 72d8803..1bfa602 100644
--- a/include/asm-generic/uaccess.h
+++ b/include/asm-generic/uaccess.h
@@ -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; \
})

--
2.1.4