Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 #include <linux/kernel.h>
0003 #include <linux/export.h>
0004 #include <linux/uaccess.h>
0005 #include <linux/mm.h>
0006 #include <linux/bitops.h>
0007 
0008 #include <asm/word-at-a-time.h>
0009 
0010 /*
0011  * Do a strnlen, return length of string *with* final '\0'.
0012  * 'count' is the user-supplied count, while 'max' is the
0013  * address space maximum.
0014  *
0015  * Return 0 for exceptions (which includes hitting the address
0016  * space maximum), or 'count+1' if hitting the user-supplied
0017  * maximum count.
0018  *
0019  * NOTE! We can sometimes overshoot the user-supplied maximum
0020  * if it fits in a aligned 'long'. The caller needs to check
0021  * the return value against "> max".
0022  */
0023 static __always_inline long do_strnlen_user(const char __user *src, unsigned long count, unsigned long max)
0024 {
0025     const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
0026     unsigned long align, res = 0;
0027     unsigned long c;
0028 
0029     /*
0030      * Do everything aligned. But that means that we
0031      * need to also expand the maximum..
0032      */
0033     align = (sizeof(unsigned long) - 1) & (unsigned long)src;
0034     src -= align;
0035     max += align;
0036 
0037     unsafe_get_user(c, (unsigned long __user *)src, efault);
0038     c |= aligned_byte_mask(align);
0039 
0040     for (;;) {
0041         unsigned long data;
0042         if (has_zero(c, &data, &constants)) {
0043             data = prep_zero_mask(c, data, &constants);
0044             data = create_zero_mask(data);
0045             return res + find_zero(data) + 1 - align;
0046         }
0047         res += sizeof(unsigned long);
0048         /* We already handled 'unsigned long' bytes. Did we do it all ? */
0049         if (unlikely(max <= sizeof(unsigned long)))
0050             break;
0051         max -= sizeof(unsigned long);
0052         unsafe_get_user(c, (unsigned long __user *)(src+res), efault);
0053     }
0054     res -= align;
0055 
0056     /*
0057      * Uhhuh. We hit 'max'. But was that the user-specified maximum
0058      * too? If so, return the marker for "too long".
0059      */
0060     if (res >= count)
0061         return count+1;
0062 
0063     /*
0064      * Nope: we hit the address space limit, and we still had more
0065      * characters the caller would have wanted. That's 0.
0066      */
0067 efault:
0068     return 0;
0069 }
0070 
0071 /**
0072  * strnlen_user: - Get the size of a user string INCLUDING final NUL.
0073  * @str: The string to measure.
0074  * @count: Maximum count (including NUL character)
0075  *
0076  * Context: User context only. This function may sleep if pagefaults are
0077  *          enabled.
0078  *
0079  * Get the size of a NUL-terminated string in user space.
0080  *
0081  * Returns the size of the string INCLUDING the terminating NUL.
0082  * If the string is too long, returns a number larger than @count. User
0083  * has to check the return value against "> count".
0084  * On exception (or invalid count), returns 0.
0085  *
0086  * NOTE! You should basically never use this function. There is
0087  * almost never any valid case for using the length of a user space
0088  * string, since the string can be changed at any time by other
0089  * threads. Use "strncpy_from_user()" instead to get a stable copy
0090  * of the string.
0091  */
0092 long strnlen_user(const char __user *str, long count)
0093 {
0094     unsigned long max_addr, src_addr;
0095 
0096     if (unlikely(count <= 0))
0097         return 0;
0098 
0099     max_addr = TASK_SIZE_MAX;
0100     src_addr = (unsigned long)untagged_addr(str);
0101     if (likely(src_addr < max_addr)) {
0102         unsigned long max = max_addr - src_addr;
0103         long retval;
0104 
0105         /*
0106          * Truncate 'max' to the user-specified limit, so that
0107          * we only have one limit we need to check in the loop
0108          */
0109         if (max > count)
0110             max = count;
0111 
0112         if (user_read_access_begin(str, max)) {
0113             retval = do_strnlen_user(str, count, max);
0114             user_read_access_end();
0115             return retval;
0116         }
0117     }
0118     return 0;
0119 }
0120 EXPORT_SYMBOL(strnlen_user);