0001
0002 #include <linux/compiler.h>
0003 #include <linux/export.h>
0004 #include <linux/fault-inject-usercopy.h>
0005 #include <linux/kasan-checks.h>
0006 #include <linux/thread_info.h>
0007 #include <linux/uaccess.h>
0008 #include <linux/kernel.h>
0009 #include <linux/errno.h>
0010 #include <linux/mm.h>
0011
0012 #include <asm/byteorder.h>
0013 #include <asm/word-at-a-time.h>
0014
0015 #ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
0016 #define IS_UNALIGNED(src, dst) 0
0017 #else
0018 #define IS_UNALIGNED(src, dst) \
0019 (((long) dst | (long) src) & (sizeof(long) - 1))
0020 #endif
0021
0022
0023
0024
0025
0026
0027
0028 static __always_inline long do_strncpy_from_user(char *dst, const char __user *src,
0029 unsigned long count, unsigned long max)
0030 {
0031 const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
0032 unsigned long res = 0;
0033
0034 if (IS_UNALIGNED(src, dst))
0035 goto byte_at_a_time;
0036
0037 while (max >= sizeof(unsigned long)) {
0038 unsigned long c, data, mask;
0039
0040
0041 unsafe_get_user(c, (unsigned long __user *)(src+res), byte_at_a_time);
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054 if (has_zero(c, &data, &constants)) {
0055 data = prep_zero_mask(c, data, &constants);
0056 data = create_zero_mask(data);
0057 mask = zero_bytemask(data);
0058 *(unsigned long *)(dst+res) = c & mask;
0059 return res + find_zero(data);
0060 }
0061
0062 *(unsigned long *)(dst+res) = c;
0063
0064 res += sizeof(unsigned long);
0065 max -= sizeof(unsigned long);
0066 }
0067
0068 byte_at_a_time:
0069 while (max) {
0070 char c;
0071
0072 unsafe_get_user(c,src+res, efault);
0073 dst[res] = c;
0074 if (!c)
0075 return res;
0076 res++;
0077 max--;
0078 }
0079
0080
0081
0082
0083
0084 if (res >= count)
0085 return res;
0086
0087
0088
0089
0090
0091 efault:
0092 return -EFAULT;
0093 }
0094
0095
0096
0097
0098
0099
0100
0101
0102
0103
0104
0105
0106
0107
0108
0109
0110
0111
0112
0113 long strncpy_from_user(char *dst, const char __user *src, long count)
0114 {
0115 unsigned long max_addr, src_addr;
0116
0117 might_fault();
0118 if (should_fail_usercopy())
0119 return -EFAULT;
0120 if (unlikely(count <= 0))
0121 return 0;
0122
0123 max_addr = TASK_SIZE_MAX;
0124 src_addr = (unsigned long)untagged_addr(src);
0125 if (likely(src_addr < max_addr)) {
0126 unsigned long max = max_addr - src_addr;
0127 long retval;
0128
0129
0130
0131
0132
0133 if (max > count)
0134 max = count;
0135
0136 kasan_check_write(dst, count);
0137 check_object_size(dst, count, false);
0138 if (user_read_access_begin(src, max)) {
0139 retval = do_strncpy_from_user(dst, src, count, max);
0140 user_read_access_end();
0141 return retval;
0142 }
0143 }
0144 return -EFAULT;
0145 }
0146 EXPORT_SYMBOL(strncpy_from_user);