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