access_ok.h 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __ASM_GENERIC_ACCESS_OK_H__
  3. #define __ASM_GENERIC_ACCESS_OK_H__
  4. /*
  5. * Checking whether a pointer is valid for user space access.
  6. * These definitions work on most architectures, but overrides can
  7. * be used where necessary.
  8. */
  9. /*
  10. * architectures with compat tasks have a variable TASK_SIZE and should
  11. * override this to a constant.
  12. */
  13. #ifndef TASK_SIZE_MAX
  14. #define TASK_SIZE_MAX TASK_SIZE
  15. #endif
  16. #ifndef __access_ok
  17. /*
  18. * 'size' is a compile-time constant for most callers, so optimize for
  19. * this case to turn the check into a single comparison against a constant
  20. * limit and catch all possible overflows.
  21. * On architectures with separate user address space (m68k, s390, parisc,
  22. * sparc64) or those without an MMU, this should always return true.
  23. *
  24. * This version was originally contributed by Jonas Bonn for the
  25. * OpenRISC architecture, and was found to be the most efficient
  26. * for constant 'size' and 'limit' values.
  27. */
  28. static inline int __access_ok(const void __user *ptr, unsigned long size)
  29. {
  30. unsigned long limit = TASK_SIZE_MAX;
  31. unsigned long addr = (unsigned long)ptr;
  32. if (IS_ENABLED(CONFIG_ALTERNATE_USER_ADDRESS_SPACE) ||
  33. !IS_ENABLED(CONFIG_MMU))
  34. return true;
  35. return (size <= limit) && (addr <= (limit - size));
  36. }
  37. #endif
  38. #ifndef access_ok
  39. #define access_ok(addr, size) likely(__access_ok(addr, size))
  40. #endif
  41. #endif