Whamcloud - gitweb
LU-9116 libcfs: avoid overflow of crypto bandwidth caculation
[fs/lustre-release.git] / libcfs / libcfs / linux / linux-module.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2012, 2014, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  */
32
33 #define DEBUG_SUBSYSTEM S_LNET
34
35 #include <libcfs/libcfs.h>
36
37 #define LNET_MINOR 240
38
39 static inline size_t libcfs_ioctl_packlen(struct libcfs_ioctl_data *data)
40 {
41         size_t len = sizeof(*data);
42
43         len += (data->ioc_inllen1 + 7) & ~7;
44         len += (data->ioc_inllen2 + 7) & ~7;
45         return len;
46 }
47
48 static bool libcfs_ioctl_is_invalid(struct libcfs_ioctl_data *data)
49 {
50         if (data->ioc_hdr.ioc_len > BIT(30))
51                 return true;
52
53         if (data->ioc_inllen1 > BIT(30))
54                 return true;
55
56         if (data->ioc_inllen2 > BIT(30))
57                 return true;
58
59         if (data->ioc_inlbuf1 && !data->ioc_inllen1)
60                 return true;
61
62         if (data->ioc_inlbuf2 && !data->ioc_inllen2)
63                 return true;
64
65         if (data->ioc_pbuf1 && !data->ioc_plen1)
66                 return true;
67
68         if (data->ioc_pbuf2 && !data->ioc_plen2)
69                 return true;
70
71         if (data->ioc_plen1 && !data->ioc_pbuf1)
72                 return true;
73
74         if (data->ioc_plen2 && !data->ioc_pbuf2)
75                 return true;
76
77         if (libcfs_ioctl_packlen(data) != data->ioc_hdr.ioc_len)
78                 return true;
79
80         if (data->ioc_inllen1 &&
81             data->ioc_bulk[((data->ioc_inllen1 + 7) & ~7) +
82                              data->ioc_inllen2 - 1] != '\0')
83                 return true;
84
85         return false;
86 }
87
88 int libcfs_ioctl_data_adjust(struct libcfs_ioctl_data *data)
89 {
90         ENTRY;
91
92         if (libcfs_ioctl_is_invalid(data)) {
93                 CERROR("libcfs ioctl: parameter not correctly formatted\n");
94                 RETURN(-EINVAL);
95         }
96
97         if (data->ioc_inllen1 != 0)
98                 data->ioc_inlbuf1 = &data->ioc_bulk[0];
99
100         if (data->ioc_inllen2 != 0)
101                 data->ioc_inlbuf2 = &data->ioc_bulk[0] +
102                                     cfs_size_round(data->ioc_inllen1);
103
104         RETURN(0);
105 }
106
107 int libcfs_ioctl_getdata(struct libcfs_ioctl_hdr **hdr_pp,
108                          struct libcfs_ioctl_hdr __user *uhdr)
109 {
110         struct libcfs_ioctl_hdr   hdr;
111         int err = 0;
112         ENTRY;
113
114         if (copy_from_user(&hdr, uhdr, sizeof(hdr)))
115                 RETURN(-EFAULT);
116
117         if (hdr.ioc_version != LIBCFS_IOCTL_VERSION &&
118             hdr.ioc_version != LIBCFS_IOCTL_VERSION2) {
119                 CERROR("libcfs ioctl: version mismatch expected %#x, got %#x\n",
120                        LIBCFS_IOCTL_VERSION, hdr.ioc_version);
121                 RETURN(-EINVAL);
122         }
123
124         if (hdr.ioc_len < sizeof(struct libcfs_ioctl_hdr)) {
125                 CERROR("libcfs ioctl: user buffer too small for ioctl\n");
126                 RETURN(-EINVAL);
127         }
128
129         if (hdr.ioc_len > LIBCFS_IOC_DATA_MAX) {
130                 CERROR("libcfs ioctl: user buffer is too large %d/%d\n",
131                        hdr.ioc_len, LIBCFS_IOC_DATA_MAX);
132                 RETURN(-EINVAL);
133         }
134
135         LIBCFS_ALLOC(*hdr_pp, hdr.ioc_len);
136         if (*hdr_pp == NULL)
137                 RETURN(-ENOMEM);
138
139         if (copy_from_user(*hdr_pp, uhdr, hdr.ioc_len))
140                 GOTO(failed, err = -EFAULT);
141
142         RETURN(0);
143 failed:
144         LIBCFS_FREE(*hdr_pp, hdr.ioc_len);
145         RETURN(err);
146 }
147
148 static long
149 libcfs_psdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
150 {
151         if (!capable(CAP_SYS_ADMIN))
152                 return -EACCES;
153
154         if (_IOC_TYPE(cmd) != IOC_LIBCFS_TYPE ||
155             _IOC_NR(cmd) < IOC_LIBCFS_MIN_NR ||
156             _IOC_NR(cmd) > IOC_LIBCFS_MAX_NR) {
157                 CDEBUG(D_IOCTL, "invalid ioctl ( type %d, nr %d, size %d )\n",
158                        _IOC_TYPE(cmd), _IOC_NR(cmd), _IOC_SIZE(cmd));
159                 return -EINVAL;
160         }
161
162         return libcfs_ioctl(cmd, (void __user *)arg);
163 }
164
165 static struct file_operations libcfs_fops = {
166         .owner          = THIS_MODULE,
167         .unlocked_ioctl = libcfs_psdev_ioctl,
168 };
169
170 struct miscdevice libcfs_dev = {
171         .minor  = LNET_MINOR,
172         .name   = "lnet",
173         .fops   = &libcfs_fops
174 };