VisionFive2 Linux kernel

StarFive Tech Linux Kernel for VisionFive (JH7110) boards (mirror)

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