| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-or-later |
|---|
| 1 | 2 | /* |
|---|
| 2 | | - * This program is free software; you can redistribute it and/or modify |
|---|
| 3 | | - * it under the terms of the GNU General Public License as published by |
|---|
| 4 | | - * the Free Software Foundation; either version 2 of the License, or |
|---|
| 5 | | - * (at your option) any later version. |
|---|
| 6 | | - * |
|---|
| 7 | | - * This program is distributed in the hope that it will be useful, |
|---|
| 8 | | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 9 | | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 10 | | - * GNU General Public License for more details. |
|---|
| 11 | | - * |
|---|
| 12 | | - * You should have received a copy of the GNU General Public License |
|---|
| 13 | | - * along with this program; if not, write to the Free Software |
|---|
| 14 | | - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|---|
| 15 | 3 | * |
|---|
| 16 | 4 | * Copyright (C) IBM Corporation, 2010 |
|---|
| 17 | 5 | * |
|---|
| .. | .. |
|---|
| 24 | 12 | #include <linux/uaccess.h> |
|---|
| 25 | 13 | |
|---|
| 26 | 14 | __wsum csum_and_copy_from_user(const void __user *src, void *dst, |
|---|
| 27 | | - int len, __wsum sum, int *err_ptr) |
|---|
| 15 | + int len) |
|---|
| 28 | 16 | { |
|---|
| 29 | | - unsigned int csum; |
|---|
| 17 | + __wsum csum; |
|---|
| 30 | 18 | |
|---|
| 31 | 19 | might_sleep(); |
|---|
| 20 | + |
|---|
| 21 | + if (unlikely(!access_ok(src, len))) |
|---|
| 22 | + return 0; |
|---|
| 23 | + |
|---|
| 32 | 24 | allow_read_from_user(src, len); |
|---|
| 33 | 25 | |
|---|
| 34 | | - *err_ptr = 0; |
|---|
| 26 | + csum = csum_partial_copy_generic((void __force *)src, dst, len); |
|---|
| 35 | 27 | |
|---|
| 36 | | - if (!len) { |
|---|
| 37 | | - csum = 0; |
|---|
| 38 | | - goto out; |
|---|
| 39 | | - } |
|---|
| 40 | | - |
|---|
| 41 | | - if (unlikely((len < 0) || !access_ok(VERIFY_READ, src, len))) { |
|---|
| 42 | | - *err_ptr = -EFAULT; |
|---|
| 43 | | - csum = (__force unsigned int)sum; |
|---|
| 44 | | - goto out; |
|---|
| 45 | | - } |
|---|
| 46 | | - |
|---|
| 47 | | - csum = csum_partial_copy_generic((void __force *)src, dst, |
|---|
| 48 | | - len, sum, err_ptr, NULL); |
|---|
| 49 | | - |
|---|
| 50 | | - if (unlikely(*err_ptr)) { |
|---|
| 51 | | - int missing = __copy_from_user(dst, src, len); |
|---|
| 52 | | - |
|---|
| 53 | | - if (missing) { |
|---|
| 54 | | - memset(dst + len - missing, 0, missing); |
|---|
| 55 | | - *err_ptr = -EFAULT; |
|---|
| 56 | | - } else { |
|---|
| 57 | | - *err_ptr = 0; |
|---|
| 58 | | - } |
|---|
| 59 | | - |
|---|
| 60 | | - csum = csum_partial(dst, len, sum); |
|---|
| 61 | | - } |
|---|
| 62 | | - |
|---|
| 63 | | -out: |
|---|
| 64 | 28 | prevent_read_from_user(src, len); |
|---|
| 65 | | - return (__force __wsum)csum; |
|---|
| 29 | + return csum; |
|---|
| 66 | 30 | } |
|---|
| 67 | 31 | EXPORT_SYMBOL(csum_and_copy_from_user); |
|---|
| 68 | 32 | |
|---|
| 69 | | -__wsum csum_and_copy_to_user(const void *src, void __user *dst, int len, |
|---|
| 70 | | - __wsum sum, int *err_ptr) |
|---|
| 33 | +__wsum csum_and_copy_to_user(const void *src, void __user *dst, int len) |
|---|
| 71 | 34 | { |
|---|
| 72 | | - unsigned int csum; |
|---|
| 35 | + __wsum csum; |
|---|
| 73 | 36 | |
|---|
| 74 | 37 | might_sleep(); |
|---|
| 38 | + if (unlikely(!access_ok(dst, len))) |
|---|
| 39 | + return 0; |
|---|
| 40 | + |
|---|
| 75 | 41 | allow_write_to_user(dst, len); |
|---|
| 76 | 42 | |
|---|
| 77 | | - *err_ptr = 0; |
|---|
| 43 | + csum = csum_partial_copy_generic(src, (void __force *)dst, len); |
|---|
| 78 | 44 | |
|---|
| 79 | | - if (!len) { |
|---|
| 80 | | - csum = 0; |
|---|
| 81 | | - goto out; |
|---|
| 82 | | - } |
|---|
| 83 | | - |
|---|
| 84 | | - if (unlikely((len < 0) || !access_ok(VERIFY_WRITE, dst, len))) { |
|---|
| 85 | | - *err_ptr = -EFAULT; |
|---|
| 86 | | - csum = -1; /* invalid checksum */ |
|---|
| 87 | | - goto out; |
|---|
| 88 | | - } |
|---|
| 89 | | - |
|---|
| 90 | | - csum = csum_partial_copy_generic(src, (void __force *)dst, |
|---|
| 91 | | - len, sum, NULL, err_ptr); |
|---|
| 92 | | - |
|---|
| 93 | | - if (unlikely(*err_ptr)) { |
|---|
| 94 | | - csum = csum_partial(src, len, sum); |
|---|
| 95 | | - |
|---|
| 96 | | - if (copy_to_user(dst, src, len)) { |
|---|
| 97 | | - *err_ptr = -EFAULT; |
|---|
| 98 | | - csum = -1; /* invalid checksum */ |
|---|
| 99 | | - } |
|---|
| 100 | | - } |
|---|
| 101 | | - |
|---|
| 102 | | -out: |
|---|
| 103 | 45 | prevent_write_to_user(dst, len); |
|---|
| 104 | | - return (__force __wsum)csum; |
|---|
| 46 | + return csum; |
|---|
| 105 | 47 | } |
|---|
| 106 | 48 | EXPORT_SYMBOL(csum_and_copy_to_user); |
|---|