powerpc/uaccess: Implement user_read_access_begin and user_write_access_begin
authorChristophe Leroy <christophe.leroy@c-s.fr>
Fri, 3 Apr 2020 07:20:53 +0000 (07:20 +0000)
committerMichael Ellerman <mpe@ellerman.id.au>
Fri, 1 May 2020 02:37:15 +0000 (12:37 +1000)
Add support for selective read or write user access with
user_read_access_begin/end and user_write_access_begin/end.

Signed-off-by: Christophe Leroy <christophe.leroy@c-s.fr>
Reviewed-by: Kees Cook <keescook@chromium.org>
Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
Link: https://lore.kernel.org/r/6c83af0f0809ef2a955c39ac622767f6cbede035.1585898438.git.christophe.leroy@c-s.fr
arch/powerpc/include/asm/book3s/32/kup.h
arch/powerpc/include/asm/kup.h
arch/powerpc/include/asm/uaccess.h

index 3c0ba22..1617e73 100644 (file)
@@ -108,7 +108,7 @@ static __always_inline void allow_user_access(void __user *to, const void __user
        u32 addr, end;
 
        BUILD_BUG_ON(!__builtin_constant_p(dir));
-       BUILD_BUG_ON(dir == KUAP_CURRENT);
+       BUILD_BUG_ON(dir & ~KUAP_READ_WRITE);
 
        if (!(dir & KUAP_WRITE))
                return;
@@ -131,7 +131,7 @@ static __always_inline void prevent_user_access(void __user *to, const void __us
 
        BUILD_BUG_ON(!__builtin_constant_p(dir));
 
-       if (dir == KUAP_CURRENT) {
+       if (dir & KUAP_CURRENT_WRITE) {
                u32 kuap = current->thread.kuap;
 
                if (unlikely(!kuap))
index 92bcd1a..c745ee4 100644 (file)
@@ -10,7 +10,9 @@
  * Use the current saved situation instead of the to/from/size params.
  * Used on book3s/32
  */
-#define KUAP_CURRENT   4
+#define KUAP_CURRENT_READ      4
+#define KUAP_CURRENT_WRITE     8
+#define KUAP_CURRENT           (KUAP_CURRENT_READ | KUAP_CURRENT_WRITE)
 
 #ifdef CONFIG_PPC64
 #include <asm/book3s/64/kup-radix.h>
@@ -101,6 +103,16 @@ static inline void prevent_current_access_user(void)
        prevent_user_access(NULL, NULL, ~0UL, KUAP_CURRENT);
 }
 
+static inline void prevent_current_read_from_user(void)
+{
+       prevent_user_access(NULL, NULL, ~0UL, KUAP_CURRENT_READ);
+}
+
+static inline void prevent_current_write_to_user(void)
+{
+       prevent_user_access(NULL, NULL, ~0UL, KUAP_CURRENT_WRITE);
+}
+
 #endif /* !__ASSEMBLY__ */
 
 #endif /* _ASM_POWERPC_KUAP_H_ */
index 42b6c44..62cc8d7 100644 (file)
@@ -532,6 +532,28 @@ static __must_check inline bool user_access_begin(const void __user *ptr, size_t
 #define user_access_save       prevent_user_access_return
 #define user_access_restore    restore_user_access
 
+static __must_check inline bool
+user_read_access_begin(const void __user *ptr, size_t len)
+{
+       if (unlikely(!access_ok(ptr, len)))
+               return false;
+       allow_read_from_user(ptr, len);
+       return true;
+}
+#define user_read_access_begin user_read_access_begin
+#define user_read_access_end           prevent_current_read_from_user
+
+static __must_check inline bool
+user_write_access_begin(const void __user *ptr, size_t len)
+{
+       if (unlikely(!access_ok(ptr, len)))
+               return false;
+       allow_write_to_user((void __user *)ptr, len);
+       return true;
+}
+#define user_write_access_begin        user_write_access_begin
+#define user_write_access_end          prevent_current_write_to_user
+
 #define unsafe_op_wrap(op, err) do { if (unlikely(op)) goto err; } while (0)
 #define unsafe_get_user(x, p, e) unsafe_op_wrap(__get_user_allowed(x, p), e)
 #define unsafe_put_user(x, p, e) __put_user_goto(x, p, e)