Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Aleksa Sarai | f5a1a53 | 2019-10-01 11:10:52 +1000 | [diff] [blame] | 2 | #include <linux/bitops.h> |
Marco Elver | 76d6f06 | 2020-01-21 17:05:12 +0100 | [diff] [blame^] | 3 | #include <linux/instrumented.h> |
| 4 | #include <linux/uaccess.h> |
Al Viro | d597580 | 2017-03-20 21:56:06 -0400 | [diff] [blame] | 5 | |
| 6 | /* out-of-line parts */ |
| 7 | |
| 8 | #ifndef INLINE_COPY_FROM_USER |
| 9 | unsigned long _copy_from_user(void *to, const void __user *from, unsigned long n) |
| 10 | { |
| 11 | unsigned long res = n; |
Al Viro | 9c5f690 | 2017-06-29 21:39:54 -0400 | [diff] [blame] | 12 | might_fault(); |
Linus Torvalds | 96d4f26 | 2019-01-03 18:57:57 -0800 | [diff] [blame] | 13 | if (likely(access_ok(from, n))) { |
Marco Elver | 76d6f06 | 2020-01-21 17:05:12 +0100 | [diff] [blame^] | 14 | instrument_copy_from_user(to, from, n); |
Al Viro | d597580 | 2017-03-20 21:56:06 -0400 | [diff] [blame] | 15 | res = raw_copy_from_user(to, from, n); |
Al Viro | 9c5f690 | 2017-06-29 21:39:54 -0400 | [diff] [blame] | 16 | } |
Al Viro | d597580 | 2017-03-20 21:56:06 -0400 | [diff] [blame] | 17 | if (unlikely(res)) |
| 18 | memset(to + (n - res), 0, res); |
| 19 | return res; |
| 20 | } |
| 21 | EXPORT_SYMBOL(_copy_from_user); |
| 22 | #endif |
| 23 | |
| 24 | #ifndef INLINE_COPY_TO_USER |
Christophe Leroy | a0e9459 | 2017-12-09 17:24:24 +0100 | [diff] [blame] | 25 | unsigned long _copy_to_user(void __user *to, const void *from, unsigned long n) |
Al Viro | d597580 | 2017-03-20 21:56:06 -0400 | [diff] [blame] | 26 | { |
Al Viro | 9c5f690 | 2017-06-29 21:39:54 -0400 | [diff] [blame] | 27 | might_fault(); |
Linus Torvalds | 96d4f26 | 2019-01-03 18:57:57 -0800 | [diff] [blame] | 28 | if (likely(access_ok(to, n))) { |
Marco Elver | 76d6f06 | 2020-01-21 17:05:12 +0100 | [diff] [blame^] | 29 | instrument_copy_to_user(to, from, n); |
Al Viro | d597580 | 2017-03-20 21:56:06 -0400 | [diff] [blame] | 30 | n = raw_copy_to_user(to, from, n); |
Al Viro | 9c5f690 | 2017-06-29 21:39:54 -0400 | [diff] [blame] | 31 | } |
Al Viro | d597580 | 2017-03-20 21:56:06 -0400 | [diff] [blame] | 32 | return n; |
| 33 | } |
| 34 | EXPORT_SYMBOL(_copy_to_user); |
| 35 | #endif |
Aleksa Sarai | f5a1a53 | 2019-10-01 11:10:52 +1000 | [diff] [blame] | 36 | |
| 37 | /** |
| 38 | * check_zeroed_user: check if a userspace buffer only contains zero bytes |
| 39 | * @from: Source address, in userspace. |
| 40 | * @size: Size of buffer. |
| 41 | * |
| 42 | * This is effectively shorthand for "memchr_inv(from, 0, size) == NULL" for |
| 43 | * userspace addresses (and is more efficient because we don't care where the |
| 44 | * first non-zero byte is). |
| 45 | * |
| 46 | * Returns: |
| 47 | * * 0: There were non-zero bytes present in the buffer. |
| 48 | * * 1: The buffer was full of zero bytes. |
| 49 | * * -EFAULT: access to userspace failed. |
| 50 | */ |
| 51 | int check_zeroed_user(const void __user *from, size_t size) |
| 52 | { |
| 53 | unsigned long val; |
| 54 | uintptr_t align = (uintptr_t) from % sizeof(unsigned long); |
| 55 | |
| 56 | if (unlikely(size == 0)) |
| 57 | return 1; |
| 58 | |
| 59 | from -= align; |
| 60 | size += align; |
| 61 | |
| 62 | if (!user_access_begin(from, size)) |
| 63 | return -EFAULT; |
| 64 | |
| 65 | unsafe_get_user(val, (unsigned long __user *) from, err_fault); |
| 66 | if (align) |
| 67 | val &= ~aligned_byte_mask(align); |
| 68 | |
| 69 | while (size > sizeof(unsigned long)) { |
| 70 | if (unlikely(val)) |
| 71 | goto done; |
| 72 | |
| 73 | from += sizeof(unsigned long); |
| 74 | size -= sizeof(unsigned long); |
| 75 | |
| 76 | unsafe_get_user(val, (unsigned long __user *) from, err_fault); |
| 77 | } |
| 78 | |
| 79 | if (size < sizeof(unsigned long)) |
| 80 | val &= aligned_byte_mask(size); |
| 81 | |
| 82 | done: |
| 83 | user_access_end(); |
| 84 | return (val == 0); |
| 85 | err_fault: |
| 86 | user_access_end(); |
| 87 | return -EFAULT; |
| 88 | } |
| 89 | EXPORT_SYMBOL(check_zeroed_user); |