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