Skip to content

Commit 2b835e2

Browse files
xairywilldeacon
authored andcommitted
arm64: untag user pointers in access_ok and __uaccess_mask_ptr
This patch is a part of a series that extends kernel ABI to allow to pass tagged user pointers (with the top byte set to something else other than 0x00) as syscall arguments. copy_from_user (and a few other similar functions) are used to copy data from user memory into the kernel memory or vice versa. Since a user can provided a tagged pointer to one of the syscalls that use copy_from_user, we need to correctly handle such pointers. Do this by untagging user pointers in access_ok and in __uaccess_mask_ptr, before performing access validity checks. Note, that this patch only temporarily untags the pointers to perform the checks, but then passes them as is into the kernel internals. Reviewed-by: Vincenzo Frascino <[email protected]> Reviewed-by: Kees Cook <[email protected]> Reviewed-by: Catalin Marinas <[email protected]> Signed-off-by: Andrey Konovalov <[email protected]> [will: Add __force to casting in untagged_addr() to kill sparse warning] Signed-off-by: Will Deacon <[email protected]>
1 parent e21a712 commit 2b835e2

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

arch/arm64/include/asm/memory.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ extern u64 vabits_user;
201201
* pass on to access_ok(), for instance.
202202
*/
203203
#define untagged_addr(addr) \
204-
((__typeof__(addr))sign_extend64((u64)(addr), 55))
204+
((__typeof__(addr))sign_extend64((__force u64)(addr), 55))
205205

206206
#ifdef CONFIG_KASAN_SW_TAGS
207207
#define __tag_shifted(tag) ((u64)(tag) << 56)

arch/arm64/include/asm/uaccess.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ static inline unsigned long __range_ok(const void __user *addr, unsigned long si
6262
{
6363
unsigned long ret, limit = current_thread_info()->addr_limit;
6464

65+
addr = untagged_addr(addr);
66+
6567
__chk_user_ptr(addr);
6668
asm volatile(
6769
// A + B <= C + 1 for all A,B,C, in four easy steps:
@@ -215,18 +217,20 @@ static inline void uaccess_enable_not_uao(void)
215217

216218
/*
217219
* Sanitise a uaccess pointer such that it becomes NULL if above the
218-
* current addr_limit.
220+
* current addr_limit. In case the pointer is tagged (has the top byte set),
221+
* untag the pointer before checking.
219222
*/
220223
#define uaccess_mask_ptr(ptr) (__typeof__(ptr))__uaccess_mask_ptr(ptr)
221224
static inline void __user *__uaccess_mask_ptr(const void __user *ptr)
222225
{
223226
void __user *safe_ptr;
224227

225228
asm volatile(
226-
" bics xzr, %1, %2\n"
229+
" bics xzr, %3, %2\n"
227230
" csel %0, %1, xzr, eq\n"
228231
: "=&r" (safe_ptr)
229-
: "r" (ptr), "r" (current_thread_info()->addr_limit)
232+
: "r" (ptr), "r" (current_thread_info()->addr_limit),
233+
"r" (untagged_addr(ptr))
230234
: "cc");
231235

232236
csdb();

0 commit comments

Comments
 (0)