strnlen_user.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/kernel.h>
  3. #include <linux/export.h>
  4. #include <linux/uaccess.h>
  5. #include <linux/mm.h>
  6. #include <linux/bitops.h>
  7. #include <asm/word-at-a-time.h>
  8. /*
  9. * Do a strnlen, return length of string *with* final '\0'.
  10. * 'count' is the user-supplied count, while 'max' is the
  11. * address space maximum.
  12. *
  13. * Return 0 for exceptions (which includes hitting the address
  14. * space maximum), or 'count+1' if hitting the user-supplied
  15. * maximum count.
  16. *
  17. * NOTE! We can sometimes overshoot the user-supplied maximum
  18. * if it fits in a aligned 'long'. The caller needs to check
  19. * the return value against "> max".
  20. */
  21. static inline long do_strnlen_user(const char __user *src, unsigned long count, unsigned long max)
  22. {
  23. const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
  24. unsigned long align, res = 0;
  25. unsigned long c;
  26. /*
  27. * Do everything aligned. But that means that we
  28. * need to also expand the maximum..
  29. */
  30. align = (sizeof(unsigned long) - 1) & (unsigned long)src;
  31. src -= align;
  32. max += align;
  33. unsafe_get_user(c, (unsigned long __user *)src, efault);
  34. c |= aligned_byte_mask(align);
  35. for (;;) {
  36. unsigned long data;
  37. if (has_zero(c, &data, &constants)) {
  38. data = prep_zero_mask(c, data, &constants);
  39. data = create_zero_mask(data);
  40. return res + find_zero(data) + 1 - align;
  41. }
  42. res += sizeof(unsigned long);
  43. /* We already handled 'unsigned long' bytes. Did we do it all ? */
  44. if (unlikely(max <= sizeof(unsigned long)))
  45. break;
  46. max -= sizeof(unsigned long);
  47. unsafe_get_user(c, (unsigned long __user *)(src+res), efault);
  48. }
  49. res -= align;
  50. /*
  51. * Uhhuh. We hit 'max'. But was that the user-specified maximum
  52. * too? If so, return the marker for "too long".
  53. */
  54. if (res >= count)
  55. return count+1;
  56. /*
  57. * Nope: we hit the address space limit, and we still had more
  58. * characters the caller would have wanted. That's 0.
  59. */
  60. efault:
  61. return 0;
  62. }
  63. /**
  64. * strnlen_user: - Get the size of a user string INCLUDING final NUL.
  65. * @str: The string to measure.
  66. * @count: Maximum count (including NUL character)
  67. *
  68. * Context: User context only. This function may sleep if pagefaults are
  69. * enabled.
  70. *
  71. * Get the size of a NUL-terminated string in user space.
  72. *
  73. * Returns the size of the string INCLUDING the terminating NUL.
  74. * If the string is too long, returns a number larger than @count. User
  75. * has to check the return value against "> count".
  76. * On exception (or invalid count), returns 0.
  77. *
  78. * NOTE! You should basically never use this function. There is
  79. * almost never any valid case for using the length of a user space
  80. * string, since the string can be changed at any time by other
  81. * threads. Use "strncpy_from_user()" instead to get a stable copy
  82. * of the string.
  83. */
  84. long strnlen_user(const char __user *str, long count)
  85. {
  86. unsigned long max_addr, src_addr;
  87. if (unlikely(count <= 0))
  88. return 0;
  89. max_addr = user_addr_max();
  90. src_addr = (unsigned long)untagged_addr(str);
  91. if (likely(src_addr < max_addr)) {
  92. unsigned long max = max_addr - src_addr;
  93. long retval;
  94. /*
  95. * Truncate 'max' to the user-specified limit, so that
  96. * we only have one limit we need to check in the loop
  97. */
  98. if (max > count)
  99. max = count;
  100. if (user_access_begin(str, max)) {
  101. retval = do_strnlen_user(str, count, max);
  102. user_access_end();
  103. return retval;
  104. }
  105. }
  106. return 0;
  107. }
  108. EXPORT_SYMBOL(strnlen_user);