x86/uaccess: Don’t leak the AC flag into __put_user() value evaluation [Linux 5.0]

This Linux kernel change "x86/uaccess: Don’t leak the AC flag into __put_user() value evaluation" is included in the Linux 5.0 release. This change is authored by Andy Lutomirski <luto [at] kernel.org> on Fri Feb 22 17:17:04 2019 -0800. The commit for this change in Linux stable tree is 2a418cf (patch).

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

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")
Signed-off-by: Andy Lutomirski <[email protected]>
Signed-off-by: Borislav Petkov <[email protected]>
Acked-by: Linus Torvalds <[email protected]>
Cc: Peter Zijlstra <[email protected]>
Cc: Brian Gerst <[email protected]>
Cc: Josh Poimboeuf <[email protected]>
Cc: Denys Vlasenko <[email protected]>
Cc: [email protected]
Link: http://lkml.kernel.org/r/[email protected]

There are 6 lines of Linux source code added/deleted in this change. Code changes to Linux kernel are as follows.

 arch/x86/include/asm/uaccess.h | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/arch/x86/include/asm/uaccess.h b/arch/x86/include/asm/uaccess.h
index a77445d..28376aa 100644
--- a/arch/x86/include/asm/uaccess.h
+++ b/arch/x86/include/asm/uaccess.h
@@ -284,7 +284,7 @@ static inline bool __chk_range_not_ok(unsigned long addr, unsigned long size, un
        __put_user_goto(x, ptr, "l", "k", "ir", label);     \
        break;                          \
    case 8:                             \
-       __put_user_goto_u64((__typeof__(*ptr))(x), ptr, label); \
+       __put_user_goto_u64(x, ptr, label);         \
        break;                          \
    default:                            \
        __put_user_bad();                   \
@@ -431,8 +431,10 @@ static inline bool __chk_range_not_ok(unsigned long addr, unsigned long size, un
 ({                             \
    __label__ __pu_label;                   \
    int __pu_err = -EFAULT;                 \
+   __typeof__(*(ptr)) __pu_val;                \
+   __pu_val = x;                       \
    __uaccess_begin();                  \
-   __put_user_size((x), (ptr), (size), __pu_label);    \
+   __put_user_size(__pu_val, (ptr), (size), __pu_label);   \
    __pu_err = 0;                       \
 __pu_label:                            \
    __uaccess_end();                    \

Leave a Reply

Your email address will not be published. Required fields are marked *