Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 #ifndef __ASM_GENERIC_ACCESS_OK_H__
0003 #define __ASM_GENERIC_ACCESS_OK_H__
0004 
0005 /*
0006  * Checking whether a pointer is valid for user space access.
0007  * These definitions work on most architectures, but overrides can
0008  * be used where necessary.
0009  */
0010 
0011 /*
0012  * architectures with compat tasks have a variable TASK_SIZE and should
0013  * override this to a constant.
0014  */
0015 #ifndef TASK_SIZE_MAX
0016 #define TASK_SIZE_MAX           TASK_SIZE
0017 #endif
0018 
0019 #ifndef __access_ok
0020 /*
0021  * 'size' is a compile-time constant for most callers, so optimize for
0022  * this case to turn the check into a single comparison against a constant
0023  * limit and catch all possible overflows.
0024  * On architectures with separate user address space (m68k, s390, parisc,
0025  * sparc64) or those without an MMU, this should always return true.
0026  *
0027  * This version was originally contributed by Jonas Bonn for the
0028  * OpenRISC architecture, and was found to be the most efficient
0029  * for constant 'size' and 'limit' values.
0030  */
0031 static inline int __access_ok(const void __user *ptr, unsigned long size)
0032 {
0033     unsigned long limit = TASK_SIZE_MAX;
0034     unsigned long addr = (unsigned long)ptr;
0035 
0036     if (IS_ENABLED(CONFIG_ALTERNATE_USER_ADDRESS_SPACE) ||
0037         !IS_ENABLED(CONFIG_MMU))
0038         return true;
0039 
0040     return (size <= limit) && (addr <= (limit - size));
0041 }
0042 #endif
0043 
0044 #ifndef access_ok
0045 #define access_ok(addr, size) likely(__access_ok(addr, size))
0046 #endif
0047 
0048 #endif