Skip to content

Commit 12700c1

Browse files
committed
uaccess: generalize access_ok()
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 <[email protected]> Acked-by: Mark Rutland <[email protected]> [arm64, asm-generic] Acked-by: Geert Uytterhoeven <[email protected]> Acked-by: Stafford Horne <[email protected]> Acked-by: Dinh Nguyen <[email protected]> Signed-off-by: Arnd Bergmann <[email protected]>
1 parent 23fc539 commit 12700c1

File tree

32 files changed

+110
-362
lines changed

32 files changed

+110
-362
lines changed

arch/Kconfig

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -898,6 +898,13 @@ config HAVE_SOFTIRQ_ON_OWN_STACK
898898
Architecture provides a function to run __do_softirq() on a
899899
separate stack.
900900

901+
config ALTERNATE_USER_ADDRESS_SPACE
902+
bool
903+
help
904+
Architectures set this when the CPU uses separate address
905+
spaces for kernel and user space pointers. In this case, the
906+
access_ok() check on a __user pointer is skipped.
907+
901908
config PGTABLE_LEVELS
902909
int
903910
default 2

arch/alpha/include/asm/uaccess.h

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -20,28 +20,7 @@
2020
#define get_fs() (current_thread_info()->addr_limit)
2121
#define set_fs(x) (current_thread_info()->addr_limit = (x))
2222

23-
#define uaccess_kernel() (get_fs().seg == KERNEL_DS.seg)
24-
25-
/*
26-
* Is a address valid? This does a straightforward calculation rather
27-
* than tests.
28-
*
29-
* Address valid if:
30-
* - "addr" doesn't have any high-bits set
31-
* - AND "size" doesn't have any high-bits set
32-
* - AND "addr+size-(size != 0)" doesn't have any high-bits set
33-
* - OR we are in kernel mode.
34-
*/
35-
#define __access_ok(addr, size) ({ \
36-
unsigned long __ao_a = (addr), __ao_b = (size); \
37-
unsigned long __ao_end = __ao_a + __ao_b - !!__ao_b; \
38-
(get_fs().seg & (__ao_a | __ao_b | __ao_end)) == 0; })
39-
40-
#define access_ok(addr, size) \
41-
({ \
42-
__chk_user_ptr(addr); \
43-
__access_ok(((unsigned long)(addr)), (size)); \
44-
})
23+
#include <asm-generic/access_ok.h>
4524

4625
/*
4726
* These are the main single-value transfer routines. They automatically
@@ -105,7 +84,7 @@ extern void __get_user_unknown(void);
10584
long __gu_err = -EFAULT; \
10685
unsigned long __gu_val = 0; \
10786
const __typeof__(*(ptr)) __user *__gu_addr = (ptr); \
108-
if (__access_ok((unsigned long)__gu_addr, size)) { \
87+
if (__access_ok(__gu_addr, size)) { \
10988
__gu_err = 0; \
11089
switch (size) { \
11190
case 1: __get_user_8(__gu_addr); break; \
@@ -200,7 +179,7 @@ extern void __put_user_unknown(void);
200179
({ \
201180
long __pu_err = -EFAULT; \
202181
__typeof__(*(ptr)) __user *__pu_addr = (ptr); \
203-
if (__access_ok((unsigned long)__pu_addr, size)) { \
182+
if (__access_ok(__pu_addr, size)) { \
204183
__pu_err = 0; \
205184
switch (size) { \
206185
case 1: __put_user_8(x, __pu_addr); break; \
@@ -316,17 +295,14 @@ raw_copy_to_user(void __user *to, const void *from, unsigned long len)
316295

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

319-
extern inline long
298+
static inline long
320299
clear_user(void __user *to, long len)
321300
{
322-
if (__access_ok((unsigned long)to, len))
301+
if (__access_ok(to, len))
323302
len = __clear_user(to, len);
324303
return len;
325304
}
326305

327-
#define user_addr_max() \
328-
(uaccess_kernel() ? ~0UL : TASK_SIZE)
329-
330306
extern long strncpy_from_user(char *dest, const char __user *src, long count);
331307
extern __must_check long strnlen_user(const char __user *str, long n);
332308

arch/arc/include/asm/uaccess.h

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -23,35 +23,6 @@
2323

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

26-
27-
#define __kernel_ok (uaccess_kernel())
28-
29-
/*
30-
* Algorithmically, for __user_ok() we want do:
31-
* (start < TASK_SIZE) && (start+len < TASK_SIZE)
32-
* where TASK_SIZE could either be retrieved from thread_info->addr_limit or
33-
* emitted directly in code.
34-
*
35-
* This can however be rewritten as follows:
36-
* (len <= TASK_SIZE) && (start+len < TASK_SIZE)
37-
*
38-
* Because it essentially checks if buffer end is within limit and @len is
39-
* non-ngeative, which implies that buffer start will be within limit too.
40-
*
41-
* The reason for rewriting being, for majority of cases, @len is generally
42-
* compile time constant, causing first sub-expression to be compile time
43-
* subsumed.
44-
*
45-
* The second part would generate weird large LIMMs e.g. (0x6000_0000 - 0x10),
46-
* so we check for TASK_SIZE using get_fs() since the addr_limit load from mem
47-
* would already have been done at this call site for __kernel_ok()
48-
*
49-
*/
50-
#define __user_ok(addr, sz) (((sz) <= TASK_SIZE) && \
51-
((addr) <= (get_fs() - (sz))))
52-
#define __access_ok(addr, sz) (unlikely(__kernel_ok) || \
53-
likely(__user_ok((addr), (sz))))
54-
5526
/*********** Single byte/hword/word copies ******************/
5627

5728
#define __get_user_fn(sz, u, k) \

arch/arm/include/asm/uaccess.h

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -55,21 +55,6 @@ extern int __put_user_bad(void);
5555

5656
#ifdef CONFIG_MMU
5757

58-
/*
59-
* We use 33-bit arithmetic here. Success returns zero, failure returns
60-
* addr_limit. We take advantage that addr_limit will be zero for KERNEL_DS,
61-
* so this will always return success in that case.
62-
*/
63-
#define __range_ok(addr, size) ({ \
64-
unsigned long flag, roksum; \
65-
__chk_user_ptr(addr); \
66-
__asm__(".syntax unified\n" \
67-
"adds %1, %2, %3; sbcscc %1, %1, %0; movcc %0, #0" \
68-
: "=&r" (flag), "=&r" (roksum) \
69-
: "r" (addr), "Ir" (size), "0" (TASK_SIZE) \
70-
: "cc"); \
71-
flag; })
72-
7358
/*
7459
* This is a type: either unsigned long, if the argument fits into
7560
* that type, or otherwise unsigned long long.
@@ -241,15 +226,12 @@ extern int __put_user_8(void *, unsigned long long);
241226

242227
#else /* CONFIG_MMU */
243228

244-
#define __addr_ok(addr) ((void)(addr), 1)
245-
#define __range_ok(addr, size) ((void)(addr), 0)
246-
247229
#define get_user(x, p) __get_user(x, p)
248230
#define __put_user_check __put_user_nocheck
249231

250232
#endif /* CONFIG_MMU */
251233

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

254236
#ifdef CONFIG_CPU_SPECTRE
255237
/*

arch/arm64/include/asm/uaccess.h

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,7 @@
2626
#include <asm/memory.h>
2727
#include <asm/extable.h>
2828

29-
static inline int __access_ok(const void __user *ptr, unsigned long size)
30-
{
31-
unsigned long limit = TASK_SIZE_MAX;
32-
unsigned long addr = (unsigned long)ptr;
33-
34-
return (size <= limit) && (addr <= (limit - size));
35-
}
29+
static inline int __access_ok(const void __user *ptr, unsigned long size);
3630

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

5549
return likely(__access_ok(addr, size));
5650
}
51+
#define access_ok access_ok
52+
53+
#include <asm-generic/access_ok.h>
5754

5855
/*
5956
* User access enabling/disabling.

arch/csky/include/asm/uaccess.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,6 @@
55

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

8-
static inline int __access_ok(unsigned long addr, unsigned long size)
9-
{
10-
unsigned long limit = user_addr_max();
11-
12-
return (size <= limit) && (addr <= (limit - size));
13-
}
14-
#define __access_ok __access_ok
15-
168
/*
179
* __put_user_fn
1810
*/

arch/hexagon/include/asm/uaccess.h

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,6 @@
1212
*/
1313
#include <asm/sections.h>
1414

15-
/*
16-
* access_ok: - Checks if a user space pointer is valid
17-
* @addr: User space pointer to start of block to check
18-
* @size: Size of block to check
19-
*
20-
* Context: User context only. This function may sleep if pagefaults are
21-
* enabled.
22-
*
23-
* Checks if a pointer to a block of memory in user space is valid.
24-
*
25-
* Returns true (nonzero) if the memory block *may* be valid, false (zero)
26-
* if it is definitely invalid.
27-
*
28-
*/
29-
#define uaccess_kernel() (get_fs().seg == KERNEL_DS.seg)
30-
#define user_addr_max() (uaccess_kernel() ? ~0UL : TASK_SIZE)
31-
32-
static inline int __access_ok(unsigned long addr, unsigned long size)
33-
{
34-
unsigned long limit = TASK_SIZE;
35-
36-
return (size <= limit) && (addr <= (limit - size));
37-
}
38-
#define __access_ok __access_ok
39-
4015
/*
4116
* When a kernel-mode page fault is taken, the faulting instruction
4217
* address is checked against a table of exception_table_entries.

arch/ia64/include/asm/uaccess.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,6 @@
5050
#define get_fs() (current_thread_info()->addr_limit)
5151
#define set_fs(x) (current_thread_info()->addr_limit = (x))
5252

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

7069
/*
7170
* These are the main single-value transfer routines. They automatically

arch/m68k/Kconfig.cpu

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,7 @@ config CPU_HAS_NO_UNALIGNED
453453

454454
config CPU_HAS_ADDRESS_SPACES
455455
bool
456+
select ALTERNATE_USER_ADDRESS_SPACE
456457

457458
config FPU
458459
bool

arch/m68k/include/asm/uaccess.h

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,7 @@
1010
#include <linux/compiler.h>
1111
#include <linux/types.h>
1212
#include <asm/extable.h>
13-
14-
/* We let the MMU do all checking */
15-
static inline int access_ok(const void __user *ptr,
16-
unsigned long size)
17-
{
18-
unsigned long limit = TASK_SIZE;
19-
unsigned long addr = (unsigned long)ptr;
20-
21-
if (IS_ENABLED(CONFIG_CPU_HAS_ADDRESS_SPACES) ||
22-
!IS_ENABLED(CONFIG_MMU))
23-
return 1;
24-
25-
return (size <= limit) && (addr <= (limit - size));
26-
}
13+
#include <asm-generic/access_ok.h>
2714

2815
/*
2916
* Not all varients of the 68k family support the notion of address spaces.

0 commit comments

Comments
 (0)