xref: /linux/lib/strnlen_user.c (revision 226d44ac)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2a08c5356SLinus Torvalds #include <linux/kernel.h>
3a08c5356SLinus Torvalds #include <linux/export.h>
4a08c5356SLinus Torvalds #include <linux/uaccess.h>
5903f433fSAndrey Konovalov #include <linux/mm.h>
6f5a1a536SAleksa Sarai #include <linux/bitops.h>
7a08c5356SLinus Torvalds 
8a08c5356SLinus Torvalds #include <asm/word-at-a-time.h>
9a08c5356SLinus Torvalds 
10a08c5356SLinus Torvalds /*
11a08c5356SLinus Torvalds  * Do a strnlen, return length of string *with* final '\0'.
12a08c5356SLinus Torvalds  * 'count' is the user-supplied count, while 'max' is the
13a08c5356SLinus Torvalds  * address space maximum.
14a08c5356SLinus Torvalds  *
15a08c5356SLinus Torvalds  * Return 0 for exceptions (which includes hitting the address
16a08c5356SLinus Torvalds  * space maximum), or 'count+1' if hitting the user-supplied
17a08c5356SLinus Torvalds  * maximum count.
18a08c5356SLinus Torvalds  *
19a08c5356SLinus Torvalds  * NOTE! We can sometimes overshoot the user-supplied maximum
20a08c5356SLinus Torvalds  * if it fits in a aligned 'long'. The caller needs to check
21a08c5356SLinus Torvalds  * the return value against "> max".
22a08c5356SLinus Torvalds  */
do_strnlen_user(const char __user * src,unsigned long count,unsigned long max)23226d44acSPeter Zijlstra static __always_inline long do_strnlen_user(const char __user *src, unsigned long count, unsigned long max)
24a08c5356SLinus Torvalds {
25a08c5356SLinus Torvalds 	const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
2629da93feSPeter Zijlstra 	unsigned long align, res = 0;
27a08c5356SLinus Torvalds 	unsigned long c;
28a08c5356SLinus Torvalds 
29a08c5356SLinus Torvalds 	/*
30a08c5356SLinus Torvalds 	 * Do everything aligned. But that means that we
31a08c5356SLinus Torvalds 	 * need to also expand the maximum..
32a08c5356SLinus Torvalds 	 */
3329da93feSPeter Zijlstra 	align = (sizeof(unsigned long) - 1) & (unsigned long)src;
34a08c5356SLinus Torvalds 	src -= align;
35a08c5356SLinus Torvalds 	max += align;
36a08c5356SLinus Torvalds 
371bd4403dSLinus Torvalds 	unsafe_get_user(c, (unsigned long __user *)src, efault);
38a08c5356SLinus Torvalds 	c |= aligned_byte_mask(align);
39a08c5356SLinus Torvalds 
40a08c5356SLinus Torvalds 	for (;;) {
41a08c5356SLinus Torvalds 		unsigned long data;
42a08c5356SLinus Torvalds 		if (has_zero(c, &data, &constants)) {
43a08c5356SLinus Torvalds 			data = prep_zero_mask(c, data, &constants);
44a08c5356SLinus Torvalds 			data = create_zero_mask(data);
45a08c5356SLinus Torvalds 			return res + find_zero(data) + 1 - align;
46a08c5356SLinus Torvalds 		}
47a08c5356SLinus Torvalds 		res += sizeof(unsigned long);
48f18c34e4SJan Kara 		/* We already handled 'unsigned long' bytes. Did we do it all ? */
49f18c34e4SJan Kara 		if (unlikely(max <= sizeof(unsigned long)))
50a08c5356SLinus Torvalds 			break;
51a08c5356SLinus Torvalds 		max -= sizeof(unsigned long);
521bd4403dSLinus Torvalds 		unsafe_get_user(c, (unsigned long __user *)(src+res), efault);
53a08c5356SLinus Torvalds 	}
54a08c5356SLinus Torvalds 	res -= align;
55a08c5356SLinus Torvalds 
56a08c5356SLinus Torvalds 	/*
57a08c5356SLinus Torvalds 	 * Uhhuh. We hit 'max'. But was that the user-specified maximum
58a08c5356SLinus Torvalds 	 * too? If so, return the marker for "too long".
59a08c5356SLinus Torvalds 	 */
60a08c5356SLinus Torvalds 	if (res >= count)
61a08c5356SLinus Torvalds 		return count+1;
62a08c5356SLinus Torvalds 
63a08c5356SLinus Torvalds 	/*
64a08c5356SLinus Torvalds 	 * Nope: we hit the address space limit, and we still had more
65a08c5356SLinus Torvalds 	 * characters the caller would have wanted. That's 0.
66a08c5356SLinus Torvalds 	 */
671bd4403dSLinus Torvalds efault:
68a08c5356SLinus Torvalds 	return 0;
69a08c5356SLinus Torvalds }
70a08c5356SLinus Torvalds 
71a08c5356SLinus Torvalds /**
72a08c5356SLinus Torvalds  * strnlen_user: - Get the size of a user string INCLUDING final NUL.
73a08c5356SLinus Torvalds  * @str: The string to measure.
74a08c5356SLinus Torvalds  * @count: Maximum count (including NUL character)
75a08c5356SLinus Torvalds  *
76b3c395efSDavid Hildenbrand  * Context: User context only. This function may sleep if pagefaults are
77b3c395efSDavid Hildenbrand  *          enabled.
78a08c5356SLinus Torvalds  *
79a08c5356SLinus Torvalds  * Get the size of a NUL-terminated string in user space.
80a08c5356SLinus Torvalds  *
81a08c5356SLinus Torvalds  * Returns the size of the string INCLUDING the terminating NUL.
82226a07efSJan Kara  * If the string is too long, returns a number larger than @count. User
83226a07efSJan Kara  * has to check the return value against "> count".
84a08c5356SLinus Torvalds  * On exception (or invalid count), returns 0.
85226a07efSJan Kara  *
86226a07efSJan Kara  * NOTE! You should basically never use this function. There is
87226a07efSJan Kara  * almost never any valid case for using the length of a user space
88226a07efSJan Kara  * string, since the string can be changed at any time by other
89226a07efSJan Kara  * threads. Use "strncpy_from_user()" instead to get a stable copy
90226a07efSJan Kara  * of the string.
91a08c5356SLinus Torvalds  */
strnlen_user(const char __user * str,long count)92a08c5356SLinus Torvalds long strnlen_user(const char __user *str, long count)
93a08c5356SLinus Torvalds {
94a08c5356SLinus Torvalds 	unsigned long max_addr, src_addr;
95a08c5356SLinus Torvalds 
96a08c5356SLinus Torvalds 	if (unlikely(count <= 0))
97a08c5356SLinus Torvalds 		return 0;
98a08c5356SLinus Torvalds 
99967747bbSArnd Bergmann 	max_addr = TASK_SIZE_MAX;
100903f433fSAndrey Konovalov 	src_addr = (unsigned long)untagged_addr(str);
101a08c5356SLinus Torvalds 	if (likely(src_addr < max_addr)) {
102a08c5356SLinus Torvalds 		unsigned long max = max_addr - src_addr;
1039fd4470fSLinus Torvalds 		long retval;
1049fd4470fSLinus Torvalds 
105ab10ae1cSChristophe Leroy 		/*
106ab10ae1cSChristophe Leroy 		 * Truncate 'max' to the user-specified limit, so that
107ab10ae1cSChristophe Leroy 		 * we only have one limit we need to check in the loop
108ab10ae1cSChristophe Leroy 		 */
109ab10ae1cSChristophe Leroy 		if (max > count)
110ab10ae1cSChristophe Leroy 			max = count;
111ab10ae1cSChristophe Leroy 
11241cd7805SChristophe Leroy 		if (user_read_access_begin(str, max)) {
1139fd4470fSLinus Torvalds 			retval = do_strnlen_user(str, count, max);
11441cd7805SChristophe Leroy 			user_read_access_end();
1159fd4470fSLinus Torvalds 			return retval;
116a08c5356SLinus Torvalds 		}
117594cc251SLinus Torvalds 	}
118a08c5356SLinus Torvalds 	return 0;
119a08c5356SLinus Torvalds }
120a08c5356SLinus Torvalds EXPORT_SYMBOL(strnlen_user);
121