18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci#include <linux/compiler.h>
38c2ecf20Sopenharmony_ci#include <linux/export.h>
48c2ecf20Sopenharmony_ci#include <linux/fault-inject-usercopy.h>
58c2ecf20Sopenharmony_ci#include <linux/kasan-checks.h>
68c2ecf20Sopenharmony_ci#include <linux/thread_info.h>
78c2ecf20Sopenharmony_ci#include <linux/uaccess.h>
88c2ecf20Sopenharmony_ci#include <linux/kernel.h>
98c2ecf20Sopenharmony_ci#include <linux/errno.h>
108c2ecf20Sopenharmony_ci#include <linux/mm.h>
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci#include <asm/byteorder.h>
138c2ecf20Sopenharmony_ci#include <asm/word-at-a-time.h>
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci#ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
168c2ecf20Sopenharmony_ci#define IS_UNALIGNED(src, dst)	0
178c2ecf20Sopenharmony_ci#else
188c2ecf20Sopenharmony_ci#define IS_UNALIGNED(src, dst)	\
198c2ecf20Sopenharmony_ci	(((long) dst | (long) src) & (sizeof(long) - 1))
208c2ecf20Sopenharmony_ci#endif
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci/*
238c2ecf20Sopenharmony_ci * Do a strncpy, return length of string without final '\0'.
248c2ecf20Sopenharmony_ci * 'count' is the user-supplied count (return 'count' if we
258c2ecf20Sopenharmony_ci * hit it), 'max' is the address space maximum (and we return
268c2ecf20Sopenharmony_ci * -EFAULT if we hit it).
278c2ecf20Sopenharmony_ci */
288c2ecf20Sopenharmony_cistatic inline long do_strncpy_from_user(char *dst, const char __user *src,
298c2ecf20Sopenharmony_ci					unsigned long count, unsigned long max)
308c2ecf20Sopenharmony_ci{
318c2ecf20Sopenharmony_ci	const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
328c2ecf20Sopenharmony_ci	unsigned long res = 0;
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci	if (IS_UNALIGNED(src, dst))
358c2ecf20Sopenharmony_ci		goto byte_at_a_time;
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci	while (max >= sizeof(unsigned long)) {
388c2ecf20Sopenharmony_ci		unsigned long c, data, mask;
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci		/* Fall back to byte-at-a-time if we get a page fault */
418c2ecf20Sopenharmony_ci		unsafe_get_user(c, (unsigned long __user *)(src+res), byte_at_a_time);
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci		/*
448c2ecf20Sopenharmony_ci		 * Note that we mask out the bytes following the NUL. This is
458c2ecf20Sopenharmony_ci		 * important to do because string oblivious code may read past
468c2ecf20Sopenharmony_ci		 * the NUL. For those routines, we don't want to give them
478c2ecf20Sopenharmony_ci		 * potentially random bytes after the NUL in `src`.
488c2ecf20Sopenharmony_ci		 *
498c2ecf20Sopenharmony_ci		 * One example of such code is BPF map keys. BPF treats map keys
508c2ecf20Sopenharmony_ci		 * as an opaque set of bytes. Without the post-NUL mask, any BPF
518c2ecf20Sopenharmony_ci		 * maps keyed by strings returned from strncpy_from_user() may
528c2ecf20Sopenharmony_ci		 * have multiple entries for semantically identical strings.
538c2ecf20Sopenharmony_ci		 */
548c2ecf20Sopenharmony_ci		if (has_zero(c, &data, &constants)) {
558c2ecf20Sopenharmony_ci			data = prep_zero_mask(c, data, &constants);
568c2ecf20Sopenharmony_ci			data = create_zero_mask(data);
578c2ecf20Sopenharmony_ci			mask = zero_bytemask(data);
588c2ecf20Sopenharmony_ci			*(unsigned long *)(dst+res) = c & mask;
598c2ecf20Sopenharmony_ci			return res + find_zero(data);
608c2ecf20Sopenharmony_ci		}
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci		*(unsigned long *)(dst+res) = c;
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci		res += sizeof(unsigned long);
658c2ecf20Sopenharmony_ci		max -= sizeof(unsigned long);
668c2ecf20Sopenharmony_ci	}
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_cibyte_at_a_time:
698c2ecf20Sopenharmony_ci	while (max) {
708c2ecf20Sopenharmony_ci		char c;
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci		unsafe_get_user(c,src+res, efault);
738c2ecf20Sopenharmony_ci		dst[res] = c;
748c2ecf20Sopenharmony_ci		if (!c)
758c2ecf20Sopenharmony_ci			return res;
768c2ecf20Sopenharmony_ci		res++;
778c2ecf20Sopenharmony_ci		max--;
788c2ecf20Sopenharmony_ci	}
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci	/*
818c2ecf20Sopenharmony_ci	 * Uhhuh. We hit 'max'. But was that the user-specified maximum
828c2ecf20Sopenharmony_ci	 * too? If so, that's ok - we got as much as the user asked for.
838c2ecf20Sopenharmony_ci	 */
848c2ecf20Sopenharmony_ci	if (res >= count)
858c2ecf20Sopenharmony_ci		return res;
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci	/*
888c2ecf20Sopenharmony_ci	 * Nope: we hit the address space limit, and we still had more
898c2ecf20Sopenharmony_ci	 * characters the caller would have wanted. That's an EFAULT.
908c2ecf20Sopenharmony_ci	 */
918c2ecf20Sopenharmony_ciefault:
928c2ecf20Sopenharmony_ci	return -EFAULT;
938c2ecf20Sopenharmony_ci}
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci/**
968c2ecf20Sopenharmony_ci * strncpy_from_user: - Copy a NUL terminated string from userspace.
978c2ecf20Sopenharmony_ci * @dst:   Destination address, in kernel space.  This buffer must be at
988c2ecf20Sopenharmony_ci *         least @count bytes long.
998c2ecf20Sopenharmony_ci * @src:   Source address, in user space.
1008c2ecf20Sopenharmony_ci * @count: Maximum number of bytes to copy, including the trailing NUL.
1018c2ecf20Sopenharmony_ci *
1028c2ecf20Sopenharmony_ci * Copies a NUL-terminated string from userspace to kernel space.
1038c2ecf20Sopenharmony_ci *
1048c2ecf20Sopenharmony_ci * On success, returns the length of the string (not including the trailing
1058c2ecf20Sopenharmony_ci * NUL).
1068c2ecf20Sopenharmony_ci *
1078c2ecf20Sopenharmony_ci * If access to userspace fails, returns -EFAULT (some data may have been
1088c2ecf20Sopenharmony_ci * copied).
1098c2ecf20Sopenharmony_ci *
1108c2ecf20Sopenharmony_ci * If @count is smaller than the length of the string, copies @count bytes
1118c2ecf20Sopenharmony_ci * and returns @count.
1128c2ecf20Sopenharmony_ci */
1138c2ecf20Sopenharmony_cilong strncpy_from_user(char *dst, const char __user *src, long count)
1148c2ecf20Sopenharmony_ci{
1158c2ecf20Sopenharmony_ci	unsigned long max_addr, src_addr;
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci	might_fault();
1188c2ecf20Sopenharmony_ci	if (should_fail_usercopy())
1198c2ecf20Sopenharmony_ci		return -EFAULT;
1208c2ecf20Sopenharmony_ci	if (unlikely(count <= 0))
1218c2ecf20Sopenharmony_ci		return 0;
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci	max_addr = user_addr_max();
1248c2ecf20Sopenharmony_ci	src_addr = (unsigned long)untagged_addr(src);
1258c2ecf20Sopenharmony_ci	if (likely(src_addr < max_addr)) {
1268c2ecf20Sopenharmony_ci		unsigned long max = max_addr - src_addr;
1278c2ecf20Sopenharmony_ci		long retval;
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci		/*
1308c2ecf20Sopenharmony_ci		 * Truncate 'max' to the user-specified limit, so that
1318c2ecf20Sopenharmony_ci		 * we only have one limit we need to check in the loop
1328c2ecf20Sopenharmony_ci		 */
1338c2ecf20Sopenharmony_ci		if (max > count)
1348c2ecf20Sopenharmony_ci			max = count;
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci		kasan_check_write(dst, count);
1378c2ecf20Sopenharmony_ci		check_object_size(dst, count, false);
1388c2ecf20Sopenharmony_ci		if (user_read_access_begin(src, max)) {
1398c2ecf20Sopenharmony_ci			retval = do_strncpy_from_user(dst, src, count, max);
1408c2ecf20Sopenharmony_ci			user_read_access_end();
1418c2ecf20Sopenharmony_ci			return retval;
1428c2ecf20Sopenharmony_ci		}
1438c2ecf20Sopenharmony_ci	}
1448c2ecf20Sopenharmony_ci	return -EFAULT;
1458c2ecf20Sopenharmony_ci}
1468c2ecf20Sopenharmony_ciEXPORT_SYMBOL(strncpy_from_user);
147