162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */ 262306a36Sopenharmony_ci#ifndef __ASM_GENERIC_ACCESS_OK_H__ 362306a36Sopenharmony_ci#define __ASM_GENERIC_ACCESS_OK_H__ 462306a36Sopenharmony_ci 562306a36Sopenharmony_ci/* 662306a36Sopenharmony_ci * Checking whether a pointer is valid for user space access. 762306a36Sopenharmony_ci * These definitions work on most architectures, but overrides can 862306a36Sopenharmony_ci * be used where necessary. 962306a36Sopenharmony_ci */ 1062306a36Sopenharmony_ci 1162306a36Sopenharmony_ci/* 1262306a36Sopenharmony_ci * architectures with compat tasks have a variable TASK_SIZE and should 1362306a36Sopenharmony_ci * override this to a constant. 1462306a36Sopenharmony_ci */ 1562306a36Sopenharmony_ci#ifndef TASK_SIZE_MAX 1662306a36Sopenharmony_ci#define TASK_SIZE_MAX TASK_SIZE 1762306a36Sopenharmony_ci#endif 1862306a36Sopenharmony_ci 1962306a36Sopenharmony_ci#ifndef __access_ok 2062306a36Sopenharmony_ci/* 2162306a36Sopenharmony_ci * 'size' is a compile-time constant for most callers, so optimize for 2262306a36Sopenharmony_ci * this case to turn the check into a single comparison against a constant 2362306a36Sopenharmony_ci * limit and catch all possible overflows. 2462306a36Sopenharmony_ci * On architectures with separate user address space (m68k, s390, parisc, 2562306a36Sopenharmony_ci * sparc64) or those without an MMU, this should always return true. 2662306a36Sopenharmony_ci * 2762306a36Sopenharmony_ci * This version was originally contributed by Jonas Bonn for the 2862306a36Sopenharmony_ci * OpenRISC architecture, and was found to be the most efficient 2962306a36Sopenharmony_ci * for constant 'size' and 'limit' values. 3062306a36Sopenharmony_ci */ 3162306a36Sopenharmony_cistatic inline int __access_ok(const void __user *ptr, unsigned long size) 3262306a36Sopenharmony_ci{ 3362306a36Sopenharmony_ci unsigned long limit = TASK_SIZE_MAX; 3462306a36Sopenharmony_ci unsigned long addr = (unsigned long)ptr; 3562306a36Sopenharmony_ci 3662306a36Sopenharmony_ci if (IS_ENABLED(CONFIG_ALTERNATE_USER_ADDRESS_SPACE) || 3762306a36Sopenharmony_ci !IS_ENABLED(CONFIG_MMU)) 3862306a36Sopenharmony_ci return true; 3962306a36Sopenharmony_ci 4062306a36Sopenharmony_ci return (size <= limit) && (addr <= (limit - size)); 4162306a36Sopenharmony_ci} 4262306a36Sopenharmony_ci#endif 4362306a36Sopenharmony_ci 4462306a36Sopenharmony_ci#ifndef access_ok 4562306a36Sopenharmony_ci#define access_ok(addr, size) likely(__access_ok(addr, size)) 4662306a36Sopenharmony_ci#endif 4762306a36Sopenharmony_ci 4862306a36Sopenharmony_ci#endif 49