Back to home page

LXR

 
 

    


0001 #include <linux/compiler.h>
0002 #include <linux/export.h>
0003 #include <linux/kasan-checks.h>
0004 #include <linux/thread_info.h>
0005 #include <linux/uaccess.h>
0006 #include <linux/kernel.h>
0007 #include <linux/errno.h>
0008 
0009 #include <asm/byteorder.h>
0010 #include <asm/word-at-a-time.h>
0011 
0012 #ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
0013 #define IS_UNALIGNED(src, dst)  0
0014 #else
0015 #define IS_UNALIGNED(src, dst)  \
0016     (((long) dst | (long) src) & (sizeof(long) - 1))
0017 #endif
0018 
0019 /*
0020  * Do a strncpy, return length of string without final '\0'.
0021  * 'count' is the user-supplied count (return 'count' if we
0022  * hit it), 'max' is the address space maximum (and we return
0023  * -EFAULT if we hit it).
0024  */
0025 static inline long do_strncpy_from_user(char *dst, const char __user *src, long count, unsigned long max)
0026 {
0027     const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
0028     long res = 0;
0029 
0030     /*
0031      * Truncate 'max' to the user-specified limit, so that
0032      * we only have one limit we need to check in the loop
0033      */
0034     if (max > count)
0035         max = count;
0036 
0037     if (IS_UNALIGNED(src, dst))
0038         goto byte_at_a_time;
0039 
0040     while (max >= sizeof(unsigned long)) {
0041         unsigned long c, data;
0042 
0043         /* Fall back to byte-at-a-time if we get a page fault */
0044         unsafe_get_user(c, (unsigned long __user *)(src+res), byte_at_a_time);
0045 
0046         *(unsigned long *)(dst+res) = c;
0047         if (has_zero(c, &data, &constants)) {
0048             data = prep_zero_mask(c, data, &constants);
0049             data = create_zero_mask(data);
0050             return res + find_zero(data);
0051         }
0052         res += sizeof(unsigned long);
0053         max -= sizeof(unsigned long);
0054     }
0055 
0056 byte_at_a_time:
0057     while (max) {
0058         char c;
0059 
0060         unsafe_get_user(c,src+res, efault);
0061         dst[res] = c;
0062         if (!c)
0063             return res;
0064         res++;
0065         max--;
0066     }
0067 
0068     /*
0069      * Uhhuh. We hit 'max'. But was that the user-specified maximum
0070      * too? If so, that's ok - we got as much as the user asked for.
0071      */
0072     if (res >= count)
0073         return res;
0074 
0075     /*
0076      * Nope: we hit the address space limit, and we still had more
0077      * characters the caller would have wanted. That's an EFAULT.
0078      */
0079 efault:
0080     return -EFAULT;
0081 }
0082 
0083 /**
0084  * strncpy_from_user: - Copy a NUL terminated string from userspace.
0085  * @dst:   Destination address, in kernel space.  This buffer must be at
0086  *         least @count bytes long.
0087  * @src:   Source address, in user space.
0088  * @count: Maximum number of bytes to copy, including the trailing NUL.
0089  *
0090  * Copies a NUL-terminated string from userspace to kernel space.
0091  *
0092  * On success, returns the length of the string (not including the trailing
0093  * NUL).
0094  *
0095  * If access to userspace fails, returns -EFAULT (some data may have been
0096  * copied).
0097  *
0098  * If @count is smaller than the length of the string, copies @count bytes
0099  * and returns @count.
0100  */
0101 long strncpy_from_user(char *dst, const char __user *src, long count)
0102 {
0103     unsigned long max_addr, src_addr;
0104 
0105     if (unlikely(count <= 0))
0106         return 0;
0107 
0108     max_addr = user_addr_max();
0109     src_addr = (unsigned long)src;
0110     if (likely(src_addr < max_addr)) {
0111         unsigned long max = max_addr - src_addr;
0112         long retval;
0113 
0114         kasan_check_write(dst, count);
0115         check_object_size(dst, count, false);
0116         user_access_begin();
0117         retval = do_strncpy_from_user(dst, src, count, max);
0118         user_access_end();
0119         return retval;
0120     }
0121     return -EFAULT;
0122 }
0123 EXPORT_SYMBOL(strncpy_from_user);