^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) #include <linux/bitops.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #include <linux/fault-inject-usercopy.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #include <linux/instrumented.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) /* out-of-line parts */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #ifndef INLINE_COPY_FROM_USER
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) unsigned long _copy_from_user(void *to, const void __user *from, unsigned long n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) unsigned long res = n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) might_fault();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) if (!should_fail_usercopy() && likely(access_ok(from, n))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) instrument_copy_from_user(to, from, n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) res = raw_copy_from_user(to, from, n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) if (unlikely(res))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) memset(to + (n - res), 0, res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) EXPORT_SYMBOL(_copy_from_user);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #ifndef INLINE_COPY_TO_USER
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) unsigned long _copy_to_user(void __user *to, const void *from, unsigned long n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) might_fault();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) if (should_fail_usercopy())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) return n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) if (likely(access_ok(to, n))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) instrument_copy_to_user(to, from, n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) n = raw_copy_to_user(to, from, n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) return n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) EXPORT_SYMBOL(_copy_to_user);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) * check_zeroed_user: check if a userspace buffer only contains zero bytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) * @from: Source address, in userspace.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) * @size: Size of buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) * This is effectively shorthand for "memchr_inv(from, 0, size) == NULL" for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) * userspace addresses (and is more efficient because we don't care where the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) * first non-zero byte is).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) * Returns:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) * * 0: There were non-zero bytes present in the buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) * * 1: The buffer was full of zero bytes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) * * -EFAULT: access to userspace failed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) int check_zeroed_user(const void __user *from, size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) unsigned long val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) uintptr_t align = (uintptr_t) from % sizeof(unsigned long);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) if (unlikely(size == 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) from -= align;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) size += align;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) if (!user_read_access_begin(from, size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) unsafe_get_user(val, (unsigned long __user *) from, err_fault);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) if (align)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) val &= ~aligned_byte_mask(align);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) while (size > sizeof(unsigned long)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) if (unlikely(val))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) from += sizeof(unsigned long);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) size -= sizeof(unsigned long);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) unsafe_get_user(val, (unsigned long __user *) from, err_fault);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) if (size < sizeof(unsigned long))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) val &= aligned_byte_mask(size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) user_read_access_end();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) return (val == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) err_fault:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) user_read_access_end();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) EXPORT_SYMBOL(check_zeroed_user);