18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci#include <linux/kernel.h>
38c2ecf20Sopenharmony_ci#include <linux/export.h>
48c2ecf20Sopenharmony_ci#include <linux/uaccess.h>
58c2ecf20Sopenharmony_ci#include <linux/mm.h>
68c2ecf20Sopenharmony_ci#include <linux/bitops.h>
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#include <asm/word-at-a-time.h>
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci/*
118c2ecf20Sopenharmony_ci * Do a strnlen, return length of string *with* final '\0'.
128c2ecf20Sopenharmony_ci * 'count' is the user-supplied count, while 'max' is the
138c2ecf20Sopenharmony_ci * address space maximum.
148c2ecf20Sopenharmony_ci *
158c2ecf20Sopenharmony_ci * Return 0 for exceptions (which includes hitting the address
168c2ecf20Sopenharmony_ci * space maximum), or 'count+1' if hitting the user-supplied
178c2ecf20Sopenharmony_ci * maximum count.
188c2ecf20Sopenharmony_ci *
198c2ecf20Sopenharmony_ci * NOTE! We can sometimes overshoot the user-supplied maximum
208c2ecf20Sopenharmony_ci * if it fits in a aligned 'long'. The caller needs to check
218c2ecf20Sopenharmony_ci * the return value against "> max".
228c2ecf20Sopenharmony_ci */
238c2ecf20Sopenharmony_cistatic inline long do_strnlen_user(const char __user *src, unsigned long count, unsigned long max)
248c2ecf20Sopenharmony_ci{
258c2ecf20Sopenharmony_ci	const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
268c2ecf20Sopenharmony_ci	unsigned long align, res = 0;
278c2ecf20Sopenharmony_ci	unsigned long c;
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci	/*
308c2ecf20Sopenharmony_ci	 * Do everything aligned. But that means that we
318c2ecf20Sopenharmony_ci	 * need to also expand the maximum..
328c2ecf20Sopenharmony_ci	 */
338c2ecf20Sopenharmony_ci	align = (sizeof(unsigned long) - 1) & (unsigned long)src;
348c2ecf20Sopenharmony_ci	src -= align;
358c2ecf20Sopenharmony_ci	max += align;
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci	unsafe_get_user(c, (unsigned long __user *)src, efault);
388c2ecf20Sopenharmony_ci	c |= aligned_byte_mask(align);
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci	for (;;) {
418c2ecf20Sopenharmony_ci		unsigned long data;
428c2ecf20Sopenharmony_ci		if (has_zero(c, &data, &constants)) {
438c2ecf20Sopenharmony_ci			data = prep_zero_mask(c, data, &constants);
448c2ecf20Sopenharmony_ci			data = create_zero_mask(data);
458c2ecf20Sopenharmony_ci			return res + find_zero(data) + 1 - align;
468c2ecf20Sopenharmony_ci		}
478c2ecf20Sopenharmony_ci		res += sizeof(unsigned long);
488c2ecf20Sopenharmony_ci		/* We already handled 'unsigned long' bytes. Did we do it all ? */
498c2ecf20Sopenharmony_ci		if (unlikely(max <= sizeof(unsigned long)))
508c2ecf20Sopenharmony_ci			break;
518c2ecf20Sopenharmony_ci		max -= sizeof(unsigned long);
528c2ecf20Sopenharmony_ci		unsafe_get_user(c, (unsigned long __user *)(src+res), efault);
538c2ecf20Sopenharmony_ci	}
548c2ecf20Sopenharmony_ci	res -= align;
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci	/*
578c2ecf20Sopenharmony_ci	 * Uhhuh. We hit 'max'. But was that the user-specified maximum
588c2ecf20Sopenharmony_ci	 * too? If so, return the marker for "too long".
598c2ecf20Sopenharmony_ci	 */
608c2ecf20Sopenharmony_ci	if (res >= count)
618c2ecf20Sopenharmony_ci		return count+1;
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci	/*
648c2ecf20Sopenharmony_ci	 * Nope: we hit the address space limit, and we still had more
658c2ecf20Sopenharmony_ci	 * characters the caller would have wanted. That's 0.
668c2ecf20Sopenharmony_ci	 */
678c2ecf20Sopenharmony_ciefault:
688c2ecf20Sopenharmony_ci	return 0;
698c2ecf20Sopenharmony_ci}
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci/**
728c2ecf20Sopenharmony_ci * strnlen_user: - Get the size of a user string INCLUDING final NUL.
738c2ecf20Sopenharmony_ci * @str: The string to measure.
748c2ecf20Sopenharmony_ci * @count: Maximum count (including NUL character)
758c2ecf20Sopenharmony_ci *
768c2ecf20Sopenharmony_ci * Context: User context only. This function may sleep if pagefaults are
778c2ecf20Sopenharmony_ci *          enabled.
788c2ecf20Sopenharmony_ci *
798c2ecf20Sopenharmony_ci * Get the size of a NUL-terminated string in user space.
808c2ecf20Sopenharmony_ci *
818c2ecf20Sopenharmony_ci * Returns the size of the string INCLUDING the terminating NUL.
828c2ecf20Sopenharmony_ci * If the string is too long, returns a number larger than @count. User
838c2ecf20Sopenharmony_ci * has to check the return value against "> count".
848c2ecf20Sopenharmony_ci * On exception (or invalid count), returns 0.
858c2ecf20Sopenharmony_ci *
868c2ecf20Sopenharmony_ci * NOTE! You should basically never use this function. There is
878c2ecf20Sopenharmony_ci * almost never any valid case for using the length of a user space
888c2ecf20Sopenharmony_ci * string, since the string can be changed at any time by other
898c2ecf20Sopenharmony_ci * threads. Use "strncpy_from_user()" instead to get a stable copy
908c2ecf20Sopenharmony_ci * of the string.
918c2ecf20Sopenharmony_ci */
928c2ecf20Sopenharmony_cilong strnlen_user(const char __user *str, long count)
938c2ecf20Sopenharmony_ci{
948c2ecf20Sopenharmony_ci	unsigned long max_addr, src_addr;
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci	if (unlikely(count <= 0))
978c2ecf20Sopenharmony_ci		return 0;
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci	max_addr = user_addr_max();
1008c2ecf20Sopenharmony_ci	src_addr = (unsigned long)untagged_addr(str);
1018c2ecf20Sopenharmony_ci	if (likely(src_addr < max_addr)) {
1028c2ecf20Sopenharmony_ci		unsigned long max = max_addr - src_addr;
1038c2ecf20Sopenharmony_ci		long retval;
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci		/*
1068c2ecf20Sopenharmony_ci		 * Truncate 'max' to the user-specified limit, so that
1078c2ecf20Sopenharmony_ci		 * we only have one limit we need to check in the loop
1088c2ecf20Sopenharmony_ci		 */
1098c2ecf20Sopenharmony_ci		if (max > count)
1108c2ecf20Sopenharmony_ci			max = count;
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci		if (user_read_access_begin(str, max)) {
1138c2ecf20Sopenharmony_ci			retval = do_strnlen_user(str, count, max);
1148c2ecf20Sopenharmony_ci			user_read_access_end();
1158c2ecf20Sopenharmony_ci			return retval;
1168c2ecf20Sopenharmony_ci		}
1178c2ecf20Sopenharmony_ci	}
1188c2ecf20Sopenharmony_ci	return 0;
1198c2ecf20Sopenharmony_ci}
1208c2ecf20Sopenharmony_ciEXPORT_SYMBOL(strnlen_user);
121