xref: /OK3568_Linux_fs/kernel/lib/strncpy_from_user.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun #include <linux/compiler.h>
3*4882a593Smuzhiyun #include <linux/export.h>
4*4882a593Smuzhiyun #include <linux/fault-inject-usercopy.h>
5*4882a593Smuzhiyun #include <linux/kasan-checks.h>
6*4882a593Smuzhiyun #include <linux/thread_info.h>
7*4882a593Smuzhiyun #include <linux/uaccess.h>
8*4882a593Smuzhiyun #include <linux/kernel.h>
9*4882a593Smuzhiyun #include <linux/errno.h>
10*4882a593Smuzhiyun #include <linux/mm.h>
11*4882a593Smuzhiyun 
12*4882a593Smuzhiyun #include <asm/byteorder.h>
13*4882a593Smuzhiyun #include <asm/word-at-a-time.h>
14*4882a593Smuzhiyun 
15*4882a593Smuzhiyun #ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
16*4882a593Smuzhiyun #define IS_UNALIGNED(src, dst)	0
17*4882a593Smuzhiyun #else
18*4882a593Smuzhiyun #define IS_UNALIGNED(src, dst)	\
19*4882a593Smuzhiyun 	(((long) dst | (long) src) & (sizeof(long) - 1))
20*4882a593Smuzhiyun #endif
21*4882a593Smuzhiyun 
22*4882a593Smuzhiyun /*
23*4882a593Smuzhiyun  * Do a strncpy, return length of string without final '\0'.
24*4882a593Smuzhiyun  * 'count' is the user-supplied count (return 'count' if we
25*4882a593Smuzhiyun  * hit it), 'max' is the address space maximum (and we return
26*4882a593Smuzhiyun  * -EFAULT if we hit it).
27*4882a593Smuzhiyun  */
do_strncpy_from_user(char * dst,const char __user * src,unsigned long count,unsigned long max)28*4882a593Smuzhiyun static inline long do_strncpy_from_user(char *dst, const char __user *src,
29*4882a593Smuzhiyun 					unsigned long count, unsigned long max)
30*4882a593Smuzhiyun {
31*4882a593Smuzhiyun 	const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
32*4882a593Smuzhiyun 	unsigned long res = 0;
33*4882a593Smuzhiyun 
34*4882a593Smuzhiyun 	if (IS_UNALIGNED(src, dst))
35*4882a593Smuzhiyun 		goto byte_at_a_time;
36*4882a593Smuzhiyun 
37*4882a593Smuzhiyun 	while (max >= sizeof(unsigned long)) {
38*4882a593Smuzhiyun 		unsigned long c, data, mask;
39*4882a593Smuzhiyun 
40*4882a593Smuzhiyun 		/* Fall back to byte-at-a-time if we get a page fault */
41*4882a593Smuzhiyun 		unsafe_get_user(c, (unsigned long __user *)(src+res), byte_at_a_time);
42*4882a593Smuzhiyun 
43*4882a593Smuzhiyun 		/*
44*4882a593Smuzhiyun 		 * Note that we mask out the bytes following the NUL. This is
45*4882a593Smuzhiyun 		 * important to do because string oblivious code may read past
46*4882a593Smuzhiyun 		 * the NUL. For those routines, we don't want to give them
47*4882a593Smuzhiyun 		 * potentially random bytes after the NUL in `src`.
48*4882a593Smuzhiyun 		 *
49*4882a593Smuzhiyun 		 * One example of such code is BPF map keys. BPF treats map keys
50*4882a593Smuzhiyun 		 * as an opaque set of bytes. Without the post-NUL mask, any BPF
51*4882a593Smuzhiyun 		 * maps keyed by strings returned from strncpy_from_user() may
52*4882a593Smuzhiyun 		 * have multiple entries for semantically identical strings.
53*4882a593Smuzhiyun 		 */
54*4882a593Smuzhiyun 		if (has_zero(c, &data, &constants)) {
55*4882a593Smuzhiyun 			data = prep_zero_mask(c, data, &constants);
56*4882a593Smuzhiyun 			data = create_zero_mask(data);
57*4882a593Smuzhiyun 			mask = zero_bytemask(data);
58*4882a593Smuzhiyun 			*(unsigned long *)(dst+res) = c & mask;
59*4882a593Smuzhiyun 			return res + find_zero(data);
60*4882a593Smuzhiyun 		}
61*4882a593Smuzhiyun 
62*4882a593Smuzhiyun 		*(unsigned long *)(dst+res) = c;
63*4882a593Smuzhiyun 
64*4882a593Smuzhiyun 		res += sizeof(unsigned long);
65*4882a593Smuzhiyun 		max -= sizeof(unsigned long);
66*4882a593Smuzhiyun 	}
67*4882a593Smuzhiyun 
68*4882a593Smuzhiyun byte_at_a_time:
69*4882a593Smuzhiyun 	while (max) {
70*4882a593Smuzhiyun 		char c;
71*4882a593Smuzhiyun 
72*4882a593Smuzhiyun 		unsafe_get_user(c,src+res, efault);
73*4882a593Smuzhiyun 		dst[res] = c;
74*4882a593Smuzhiyun 		if (!c)
75*4882a593Smuzhiyun 			return res;
76*4882a593Smuzhiyun 		res++;
77*4882a593Smuzhiyun 		max--;
78*4882a593Smuzhiyun 	}
79*4882a593Smuzhiyun 
80*4882a593Smuzhiyun 	/*
81*4882a593Smuzhiyun 	 * Uhhuh. We hit 'max'. But was that the user-specified maximum
82*4882a593Smuzhiyun 	 * too? If so, that's ok - we got as much as the user asked for.
83*4882a593Smuzhiyun 	 */
84*4882a593Smuzhiyun 	if (res >= count)
85*4882a593Smuzhiyun 		return res;
86*4882a593Smuzhiyun 
87*4882a593Smuzhiyun 	/*
88*4882a593Smuzhiyun 	 * Nope: we hit the address space limit, and we still had more
89*4882a593Smuzhiyun 	 * characters the caller would have wanted. That's an EFAULT.
90*4882a593Smuzhiyun 	 */
91*4882a593Smuzhiyun efault:
92*4882a593Smuzhiyun 	return -EFAULT;
93*4882a593Smuzhiyun }
94*4882a593Smuzhiyun 
95*4882a593Smuzhiyun /**
96*4882a593Smuzhiyun  * strncpy_from_user: - Copy a NUL terminated string from userspace.
97*4882a593Smuzhiyun  * @dst:   Destination address, in kernel space.  This buffer must be at
98*4882a593Smuzhiyun  *         least @count bytes long.
99*4882a593Smuzhiyun  * @src:   Source address, in user space.
100*4882a593Smuzhiyun  * @count: Maximum number of bytes to copy, including the trailing NUL.
101*4882a593Smuzhiyun  *
102*4882a593Smuzhiyun  * Copies a NUL-terminated string from userspace to kernel space.
103*4882a593Smuzhiyun  *
104*4882a593Smuzhiyun  * On success, returns the length of the string (not including the trailing
105*4882a593Smuzhiyun  * NUL).
106*4882a593Smuzhiyun  *
107*4882a593Smuzhiyun  * If access to userspace fails, returns -EFAULT (some data may have been
108*4882a593Smuzhiyun  * copied).
109*4882a593Smuzhiyun  *
110*4882a593Smuzhiyun  * If @count is smaller than the length of the string, copies @count bytes
111*4882a593Smuzhiyun  * and returns @count.
112*4882a593Smuzhiyun  */
strncpy_from_user(char * dst,const char __user * src,long count)113*4882a593Smuzhiyun long strncpy_from_user(char *dst, const char __user *src, long count)
114*4882a593Smuzhiyun {
115*4882a593Smuzhiyun 	unsigned long max_addr, src_addr;
116*4882a593Smuzhiyun 
117*4882a593Smuzhiyun 	might_fault();
118*4882a593Smuzhiyun 	if (should_fail_usercopy())
119*4882a593Smuzhiyun 		return -EFAULT;
120*4882a593Smuzhiyun 	if (unlikely(count <= 0))
121*4882a593Smuzhiyun 		return 0;
122*4882a593Smuzhiyun 
123*4882a593Smuzhiyun 	max_addr = user_addr_max();
124*4882a593Smuzhiyun 	src_addr = (unsigned long)untagged_addr(src);
125*4882a593Smuzhiyun 	if (likely(src_addr < max_addr)) {
126*4882a593Smuzhiyun 		unsigned long max = max_addr - src_addr;
127*4882a593Smuzhiyun 		long retval;
128*4882a593Smuzhiyun 
129*4882a593Smuzhiyun 		/*
130*4882a593Smuzhiyun 		 * Truncate 'max' to the user-specified limit, so that
131*4882a593Smuzhiyun 		 * we only have one limit we need to check in the loop
132*4882a593Smuzhiyun 		 */
133*4882a593Smuzhiyun 		if (max > count)
134*4882a593Smuzhiyun 			max = count;
135*4882a593Smuzhiyun 
136*4882a593Smuzhiyun 		kasan_check_write(dst, count);
137*4882a593Smuzhiyun 		check_object_size(dst, count, false);
138*4882a593Smuzhiyun 		if (user_read_access_begin(src, max)) {
139*4882a593Smuzhiyun 			retval = do_strncpy_from_user(dst, src, count, max);
140*4882a593Smuzhiyun 			user_read_access_end();
141*4882a593Smuzhiyun 			return retval;
142*4882a593Smuzhiyun 		}
143*4882a593Smuzhiyun 	}
144*4882a593Smuzhiyun 	return -EFAULT;
145*4882a593Smuzhiyun }
146*4882a593Smuzhiyun EXPORT_SYMBOL(strncpy_from_user);
147