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