Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
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  * Do a strncpy, return length of string without final '\0'.
0024  * 'count' is the user-supplied count (return 'count' if we
0025  * hit it), 'max' is the address space maximum (and we return
0026  * -EFAULT if we hit it).
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         /* Fall back to byte-at-a-time if we get a page fault */
0041         unsafe_get_user(c, (unsigned long __user *)(src+res), byte_at_a_time);
0042 
0043         /*
0044          * Note that we mask out the bytes following the NUL. This is
0045          * important to do because string oblivious code may read past
0046          * the NUL. For those routines, we don't want to give them
0047          * potentially random bytes after the NUL in `src`.
0048          *
0049          * One example of such code is BPF map keys. BPF treats map keys
0050          * as an opaque set of bytes. Without the post-NUL mask, any BPF
0051          * maps keyed by strings returned from strncpy_from_user() may
0052          * have multiple entries for semantically identical strings.
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      * Uhhuh. We hit 'max'. But was that the user-specified maximum
0082      * too? If so, that's ok - we got as much as the user asked for.
0083      */
0084     if (res >= count)
0085         return res;
0086 
0087     /*
0088      * Nope: we hit the address space limit, and we still had more
0089      * characters the caller would have wanted. That's an EFAULT.
0090      */
0091 efault:
0092     return -EFAULT;
0093 }
0094 
0095 /**
0096  * strncpy_from_user: - Copy a NUL terminated string from userspace.
0097  * @dst:   Destination address, in kernel space.  This buffer must be at
0098  *         least @count bytes long.
0099  * @src:   Source address, in user space.
0100  * @count: Maximum number of bytes to copy, including the trailing NUL.
0101  *
0102  * Copies a NUL-terminated string from userspace to kernel space.
0103  *
0104  * On success, returns the length of the string (not including the trailing
0105  * NUL).
0106  *
0107  * If access to userspace fails, returns -EFAULT (some data may have been
0108  * copied).
0109  *
0110  * If @count is smaller than the length of the string, copies @count bytes
0111  * and returns @count.
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          * Truncate 'max' to the user-specified limit, so that
0131          * we only have one limit we need to check in the loop
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);