Skip to content

Commit

Permalink
uaccess: generalize access_ok()
Browse files Browse the repository at this point in the history
There are many different ways that access_ok() is defined across
architectures, but in the end, they all just compare against the
user_addr_max() value or they accept anything.

Provide one definition that works for most architectures, checking
against TASK_SIZE_MAX for user processes or skipping the check inside
of uaccess_kernel() sections.

For architectures without CONFIG_SET_FS(), this should be the fastest
check, as it comes down to a single comparison of a pointer against a
compile-time constant, while the architecture specific versions tend to
do something more complex for historic reasons or get something wrong.

Type checking for __user annotations is handled inconsistently across
architectures, but this is easily simplified as well by using an inline
function that takes a 'const void __user *' argument. A handful of
callers need an extra __user annotation for this.

Some architectures had trick to use 33-bit or 65-bit arithmetic on the
addresses to calculate the overflow, however this simpler version uses
fewer registers, which means it can produce better object code in the
end despite needing a second (statically predicted) branch.

Reviewed-by: Christoph Hellwig <hch@lst.de>
Acked-by: Mark Rutland <mark.rutland@arm.com> [arm64, asm-generic]
Acked-by: Geert Uytterhoeven <geert@linux-m68k.org>
Acked-by: Stafford Horne <shorne@gmail.com>
Acked-by: Dinh Nguyen <dinguyen@kernel.org>
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
  • Loading branch information
arndb committed Feb 25, 2022
1 parent 23fc539 commit 12700c1
Show file tree
Hide file tree
Showing 32 changed files with 110 additions and 362 deletions.
7 changes: 7 additions & 0 deletions arch/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -898,6 +898,13 @@ config HAVE_SOFTIRQ_ON_OWN_STACK
Architecture provides a function to run __do_softirq() on a
separate stack.

config ALTERNATE_USER_ADDRESS_SPACE
bool
help
Architectures set this when the CPU uses separate address
spaces for kernel and user space pointers. In this case, the
access_ok() check on a __user pointer is skipped.

config PGTABLE_LEVELS
int
default 2
Expand Down
34 changes: 5 additions & 29 deletions arch/alpha/include/asm/uaccess.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,7 @@
#define get_fs() (current_thread_info()->addr_limit)
#define set_fs(x) (current_thread_info()->addr_limit = (x))

#define uaccess_kernel() (get_fs().seg == KERNEL_DS.seg)

/*
* Is a address valid? This does a straightforward calculation rather
* than tests.
*
* Address valid if:
* - "addr" doesn't have any high-bits set
* - AND "size" doesn't have any high-bits set
* - AND "addr+size-(size != 0)" doesn't have any high-bits set
* - OR we are in kernel mode.
*/
#define __access_ok(addr, size) ({ \
unsigned long __ao_a = (addr), __ao_b = (size); \
unsigned long __ao_end = __ao_a + __ao_b - !!__ao_b; \
(get_fs().seg & (__ao_a | __ao_b | __ao_end)) == 0; })

#define access_ok(addr, size) \
({ \
__chk_user_ptr(addr); \
__access_ok(((unsigned long)(addr)), (size)); \
})
#include <asm-generic/access_ok.h>

/*
* These are the main single-value transfer routines. They automatically
Expand Down Expand Up @@ -105,7 +84,7 @@ extern void __get_user_unknown(void);
long __gu_err = -EFAULT; \
unsigned long __gu_val = 0; \
const __typeof__(*(ptr)) __user *__gu_addr = (ptr); \
if (__access_ok((unsigned long)__gu_addr, size)) { \
if (__access_ok(__gu_addr, size)) { \
__gu_err = 0; \
switch (size) { \
case 1: __get_user_8(__gu_addr); break; \
Expand Down Expand Up @@ -200,7 +179,7 @@ extern void __put_user_unknown(void);
({ \
long __pu_err = -EFAULT; \
__typeof__(*(ptr)) __user *__pu_addr = (ptr); \
if (__access_ok((unsigned long)__pu_addr, size)) { \
if (__access_ok(__pu_addr, size)) { \
__pu_err = 0; \
switch (size) { \
case 1: __put_user_8(x, __pu_addr); break; \
Expand Down Expand Up @@ -316,17 +295,14 @@ raw_copy_to_user(void __user *to, const void *from, unsigned long len)

extern long __clear_user(void __user *to, long len);

extern inline long
static inline long
clear_user(void __user *to, long len)
{
if (__access_ok((unsigned long)to, len))
if (__access_ok(to, len))
len = __clear_user(to, len);
return len;
}

#define user_addr_max() \
(uaccess_kernel() ? ~0UL : TASK_SIZE)

extern long strncpy_from_user(char *dest, const char __user *src, long count);
extern __must_check long strnlen_user(const char __user *str, long n);

Expand Down
29 changes: 0 additions & 29 deletions arch/arc/include/asm/uaccess.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,35 +23,6 @@

#include <linux/string.h> /* for generic string functions */


#define __kernel_ok (uaccess_kernel())

/*
* Algorithmically, for __user_ok() we want do:
* (start < TASK_SIZE) && (start+len < TASK_SIZE)
* where TASK_SIZE could either be retrieved from thread_info->addr_limit or
* emitted directly in code.
*
* This can however be rewritten as follows:
* (len <= TASK_SIZE) && (start+len < TASK_SIZE)
*
* Because it essentially checks if buffer end is within limit and @len is
* non-ngeative, which implies that buffer start will be within limit too.
*
* The reason for rewriting being, for majority of cases, @len is generally
* compile time constant, causing first sub-expression to be compile time
* subsumed.
*
* The second part would generate weird large LIMMs e.g. (0x6000_0000 - 0x10),
* so we check for TASK_SIZE using get_fs() since the addr_limit load from mem
* would already have been done at this call site for __kernel_ok()
*
*/
#define __user_ok(addr, sz) (((sz) <= TASK_SIZE) && \
((addr) <= (get_fs() - (sz))))
#define __access_ok(addr, sz) (unlikely(__kernel_ok) || \
likely(__user_ok((addr), (sz))))

/*********** Single byte/hword/word copies ******************/

#define __get_user_fn(sz, u, k) \
Expand Down
20 changes: 1 addition & 19 deletions arch/arm/include/asm/uaccess.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,6 @@ extern int __put_user_bad(void);

#ifdef CONFIG_MMU

/*
* We use 33-bit arithmetic here. Success returns zero, failure returns
* addr_limit. We take advantage that addr_limit will be zero for KERNEL_DS,
* so this will always return success in that case.
*/
#define __range_ok(addr, size) ({ \
unsigned long flag, roksum; \
__chk_user_ptr(addr); \
__asm__(".syntax unified\n" \
"adds %1, %2, %3; sbcscc %1, %1, %0; movcc %0, #0" \
: "=&r" (flag), "=&r" (roksum) \
: "r" (addr), "Ir" (size), "0" (TASK_SIZE) \
: "cc"); \
flag; })

/*
* This is a type: either unsigned long, if the argument fits into
* that type, or otherwise unsigned long long.
Expand Down Expand Up @@ -241,15 +226,12 @@ extern int __put_user_8(void *, unsigned long long);

#else /* CONFIG_MMU */

#define __addr_ok(addr) ((void)(addr), 1)
#define __range_ok(addr, size) ((void)(addr), 0)

#define get_user(x, p) __get_user(x, p)
#define __put_user_check __put_user_nocheck

#endif /* CONFIG_MMU */

#define access_ok(addr, size) (__range_ok(addr, size) == 0)
#include <asm-generic/access_ok.h>

#ifdef CONFIG_CPU_SPECTRE
/*
Expand Down
11 changes: 4 additions & 7 deletions arch/arm64/include/asm/uaccess.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,7 @@
#include <asm/memory.h>
#include <asm/extable.h>

static inline int __access_ok(const void __user *ptr, unsigned long size)
{
unsigned long limit = TASK_SIZE_MAX;
unsigned long addr = (unsigned long)ptr;

return (size <= limit) && (addr <= (limit - size));
}
static inline int __access_ok(const void __user *ptr, unsigned long size);

/*
* Test whether a block of memory is a valid user space address.
Expand All @@ -54,6 +48,9 @@ static inline int access_ok(const void __user *addr, unsigned long size)

return likely(__access_ok(addr, size));
}
#define access_ok access_ok

#include <asm-generic/access_ok.h>

/*
* User access enabling/disabling.
Expand Down
8 changes: 0 additions & 8 deletions arch/csky/include/asm/uaccess.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,6 @@

#define user_addr_max() (current_thread_info()->addr_limit.seg)

static inline int __access_ok(unsigned long addr, unsigned long size)
{
unsigned long limit = user_addr_max();

return (size <= limit) && (addr <= (limit - size));
}
#define __access_ok __access_ok

/*
* __put_user_fn
*/
Expand Down
25 changes: 0 additions & 25 deletions arch/hexagon/include/asm/uaccess.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,6 @@
*/
#include <asm/sections.h>

/*
* access_ok: - Checks if a user space pointer is valid
* @addr: User space pointer to start of block to check
* @size: Size of block to check
*
* Context: User context only. This function may sleep if pagefaults are
* enabled.
*
* Checks if a pointer to a block of memory in user space is valid.
*
* Returns true (nonzero) if the memory block *may* be valid, false (zero)
* if it is definitely invalid.
*
*/
#define uaccess_kernel() (get_fs().seg == KERNEL_DS.seg)
#define user_addr_max() (uaccess_kernel() ? ~0UL : TASK_SIZE)

static inline int __access_ok(unsigned long addr, unsigned long size)
{
unsigned long limit = TASK_SIZE;

return (size <= limit) && (addr <= (limit - size));
}
#define __access_ok __access_ok

/*
* When a kernel-mode page fault is taken, the faulting instruction
* address is checked against a table of exception_table_entries.
Expand Down
5 changes: 2 additions & 3 deletions arch/ia64/include/asm/uaccess.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@
#define get_fs() (current_thread_info()->addr_limit)
#define set_fs(x) (current_thread_info()->addr_limit = (x))

#define uaccess_kernel() (get_fs().seg == KERNEL_DS.seg)

/*
* When accessing user memory, we need to make sure the entire area really is in
* user-level space. In order to do this efficiently, we make sure that the page at
Expand All @@ -65,7 +63,8 @@ static inline int __access_ok(const void __user *p, unsigned long size)
return likely(addr <= seg) &&
(seg == KERNEL_DS.seg || likely(REGION_OFFSET(addr) < RGN_MAP_LIMIT));
}
#define access_ok(addr, size) __access_ok((addr), (size))
#define __access_ok __access_ok
#include <asm-generic/access_ok.h>

/*
* These are the main single-value transfer routines. They automatically
Expand Down
1 change: 1 addition & 0 deletions arch/m68k/Kconfig.cpu
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,7 @@ config CPU_HAS_NO_UNALIGNED

config CPU_HAS_ADDRESS_SPACES
bool
select ALTERNATE_USER_ADDRESS_SPACE

config FPU
bool
Expand Down
15 changes: 1 addition & 14 deletions arch/m68k/include/asm/uaccess.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,7 @@
#include <linux/compiler.h>
#include <linux/types.h>
#include <asm/extable.h>

/* We let the MMU do all checking */
static inline int access_ok(const void __user *ptr,
unsigned long size)
{
unsigned long limit = TASK_SIZE;
unsigned long addr = (unsigned long)ptr;

if (IS_ENABLED(CONFIG_CPU_HAS_ADDRESS_SPACES) ||
!IS_ENABLED(CONFIG_MMU))
return 1;

return (size <= limit) && (addr <= (limit - size));
}
#include <asm-generic/access_ok.h>

/*
* Not all varients of the 68k family support the notion of address spaces.
Expand Down
8 changes: 1 addition & 7 deletions arch/microblaze/include/asm/uaccess.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,7 @@

# define uaccess_kernel() (get_fs().seg == KERNEL_DS.seg)

static inline int __access_ok(unsigned long addr, unsigned long size)
{
unsigned long limit = user_addr_max();

return (size <= limit) && (addr <= (limit - size));
}
#define access_ok(addr, size) __access_ok((unsigned long)addr, size)
#include <asm-generic/access_ok.h>

# define __FIXUP_SECTION ".section .fixup,\"ax\"\n"
# define __EX_TABLE_SECTION ".section __ex_table,\"a\"\n"
Expand Down
29 changes: 1 addition & 28 deletions arch/mips/include/asm/uaccess.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,34 +44,7 @@ extern u64 __ua_limit;

#endif /* CONFIG_64BIT */

/*
* access_ok: - Checks if a user space pointer is valid
* @addr: User space pointer to start of block to check
* @size: Size of block to check
*
* Context: User context only. This function may sleep if pagefaults are
* enabled.
*
* Checks if a pointer to a block of memory in user space is valid.
*
* Returns true (nonzero) if the memory block may be valid, false (zero)
* if it is definitely invalid.
*
* Note that, depending on architecture, this function probably just
* checks that the pointer is in the user space range - after calling
* this function, memory access functions may still return -EFAULT.
*/

static inline int __access_ok(const void __user *p, unsigned long size)
{
unsigned long addr = (unsigned long)p;
unsigned long limit = TASK_SIZE_MAX;

return (size <= limit) && (addr <= (limit - size));
}

#define access_ok(addr, size) \
likely(__access_ok((addr), (size)))
#include <asm-generic/access_ok.h>

/*
* put_user: - Write a simple value into user space.
Expand Down
7 changes: 2 additions & 5 deletions arch/nds32/include/asm/uaccess.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,15 @@ extern int fixup_exception(struct pt_regs *regs);

#define get_fs() (current_thread_info()->addr_limit)
#define user_addr_max get_fs
#define uaccess_kernel() (get_fs() == KERNEL_DS)

static inline void set_fs(mm_segment_t fs)
{
current_thread_info()->addr_limit = fs;
}

#define uaccess_kernel() (get_fs() == KERNEL_DS)
#include <asm-generic/access_ok.h>

#define __range_ok(addr, size) (size <= get_fs() && addr <= (get_fs() -size))

#define access_ok(addr, size) \
__range_ok((unsigned long)addr, (unsigned long)size)
/*
* Single-value transfer routines. They automatically use the right
* size if we just have the right pointer type. Note that the functions
Expand Down
11 changes: 1 addition & 10 deletions arch/nios2/include/asm/uaccess.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,10 @@
#define get_fs() (current_thread_info()->addr_limit)
#define set_fs(seg) (current_thread_info()->addr_limit = (seg))

#define uaccess_kernel() (get_fs().seg == KERNEL_DS.seg)

#define __access_ok(addr, len) \
(((signed long)(((long)get_fs().seg) & \
((long)(addr) | (((long)(addr)) + (len)) | (len)))) == 0)

#define access_ok(addr, len) \
likely(__access_ok((unsigned long)(addr), (unsigned long)(len)))
#include <asm-generic/access_ok.h>

# define __EX_TABLE_SECTION ".section __ex_table,\"a\"\n"

#define user_addr_max() (uaccess_kernel() ? ~0UL : TASK_SIZE)

/*
* Zero Userspace
*/
Expand Down
Loading

0 comments on commit 12700c1

Please sign in to comment.