Merge tag 'gpio-v4.8-1' of git://git.kernel.org/pub/scm/linux/kernel/git/linusw/linux...
[cascardo/linux.git] / drivers / staging / lustre / lnet / libcfs / linux / linux-module.c
index ebc60ac..38308f8 100644 (file)
  *
  * You should have received a copy of the GNU General Public License
  * version 2 along with this program; If not, see
- * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
- *
- * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
- * CA 95054 USA or visit www.sun.com if you need additional information or
- * have any questions.
+ * http://www.gnu.org/licenses/gpl-2.0.html
  *
  * GPL HEADER END
  */
 
 #define LNET_MINOR 240
 
+static inline size_t libcfs_ioctl_packlen(struct libcfs_ioctl_data *data)
+{
+       size_t len = sizeof(*data);
+
+       len += cfs_size_round(data->ioc_inllen1);
+       len += cfs_size_round(data->ioc_inllen2);
+       return len;
+}
+
+static inline bool libcfs_ioctl_is_invalid(struct libcfs_ioctl_data *data)
+{
+       if (data->ioc_hdr.ioc_len > BIT(30)) {
+               CERROR("LIBCFS ioctl: ioc_len larger than 1<<30\n");
+               return true;
+       }
+       if (data->ioc_inllen1 > BIT(30)) {
+               CERROR("LIBCFS ioctl: ioc_inllen1 larger than 1<<30\n");
+               return true;
+       }
+       if (data->ioc_inllen2 > BIT(30)) {
+               CERROR("LIBCFS ioctl: ioc_inllen2 larger than 1<<30\n");
+               return true;
+       }
+       if (data->ioc_inlbuf1 && !data->ioc_inllen1) {
+               CERROR("LIBCFS ioctl: inlbuf1 pointer but 0 length\n");
+               return true;
+       }
+       if (data->ioc_inlbuf2 && !data->ioc_inllen2) {
+               CERROR("LIBCFS ioctl: inlbuf2 pointer but 0 length\n");
+               return true;
+       }
+       if (data->ioc_pbuf1 && !data->ioc_plen1) {
+               CERROR("LIBCFS ioctl: pbuf1 pointer but 0 length\n");
+               return true;
+       }
+       if (data->ioc_pbuf2 && !data->ioc_plen2) {
+               CERROR("LIBCFS ioctl: pbuf2 pointer but 0 length\n");
+               return true;
+       }
+       if (data->ioc_plen1 && !data->ioc_pbuf1) {
+               CERROR("LIBCFS ioctl: plen1 nonzero but no pbuf1 pointer\n");
+               return true;
+       }
+       if (data->ioc_plen2 && !data->ioc_pbuf2) {
+               CERROR("LIBCFS ioctl: plen2 nonzero but no pbuf2 pointer\n");
+               return true;
+       }
+       if ((__u32)libcfs_ioctl_packlen(data) != data->ioc_hdr.ioc_len) {
+               CERROR("LIBCFS ioctl: packlen != ioc_len\n");
+               return true;
+       }
+       if (data->ioc_inllen1 &&
+           data->ioc_bulk[data->ioc_inllen1 - 1] != '\0') {
+               CERROR("LIBCFS ioctl: inlbuf1 not 0 terminated\n");
+               return true;
+       }
+       if (data->ioc_inllen2 &&
+           data->ioc_bulk[cfs_size_round(data->ioc_inllen1) +
+                          data->ioc_inllen2 - 1] != '\0') {
+               CERROR("LIBCFS ioctl: inlbuf2 not 0 terminated\n");
+               return true;
+       }
+       return false;
+}
+
 int libcfs_ioctl_data_adjust(struct libcfs_ioctl_data *data)
 {
        if (libcfs_ioctl_is_invalid(data)) {
-               CERROR("LNET: ioctl not correctly formatted\n");
+               CERROR("libcfs ioctl: parameter not correctly formatted\n");
                return -EINVAL;
        }
 
@@ -57,68 +118,47 @@ int libcfs_ioctl_data_adjust(struct libcfs_ioctl_data *data)
        return 0;
 }
 
-int libcfs_ioctl_getdata_len(const struct libcfs_ioctl_hdr __user *arg,
-                            __u32 *len)
+int libcfs_ioctl_getdata(struct libcfs_ioctl_hdr **hdr_pp,
+                        const struct libcfs_ioctl_hdr __user *uhdr)
 {
        struct libcfs_ioctl_hdr hdr;
+       int err = 0;
 
-       if (copy_from_user(&hdr, arg, sizeof(hdr)))
+       if (copy_from_user(&hdr, uhdr, sizeof(hdr)))
                return -EFAULT;
 
        if (hdr.ioc_version != LIBCFS_IOCTL_VERSION &&
            hdr.ioc_version != LIBCFS_IOCTL_VERSION2) {
-               CERROR("LNET: version mismatch expected %#x, got %#x\n",
+               CERROR("libcfs ioctl: version mismatch expected %#x, got %#x\n",
                       LIBCFS_IOCTL_VERSION, hdr.ioc_version);
                return -EINVAL;
        }
 
-       *len = hdr.ioc_len;
-
-       return 0;
-}
-
-int libcfs_ioctl_popdata(void __user *arg, void *data, int size)
-{
-       if (copy_to_user(arg, data, size))
-               return -EFAULT;
-       return 0;
-}
-
-static int
-libcfs_psdev_open(struct inode *inode, struct file *file)
-{
-       int    rc = 0;
+       if (hdr.ioc_len < sizeof(struct libcfs_ioctl_data)) {
+               CERROR("libcfs ioctl: user buffer too small for ioctl\n");
+               return -EINVAL;
+       }
 
-       if (!inode)
+       if (hdr.ioc_len > LIBCFS_IOC_DATA_MAX) {
+               CERROR("libcfs ioctl: user buffer is too large %d/%d\n",
+                      hdr.ioc_len, LIBCFS_IOC_DATA_MAX);
                return -EINVAL;
-       if (libcfs_psdev_ops.p_open)
-               rc = libcfs_psdev_ops.p_open(0, NULL);
-       else
-               return -EPERM;
-       return rc;
-}
+       }
 
-/* called when closing /dev/device */
-static int
-libcfs_psdev_release(struct inode *inode, struct file *file)
-{
-       int    rc = 0;
+       LIBCFS_ALLOC(*hdr_pp, hdr.ioc_len);
+       if (!*hdr_pp)
+               return -ENOMEM;
 
-       if (!inode)
-               return -EINVAL;
-       if (libcfs_psdev_ops.p_close)
-               rc = libcfs_psdev_ops.p_close(0, NULL);
-       else
-               rc = -EPERM;
-       return rc;
+       if (copy_from_user(*hdr_pp, uhdr, hdr.ioc_len)) {
+               LIBCFS_FREE(*hdr_pp, hdr.ioc_len);
+               err = -EFAULT;
+       }
+       return err;
 }
 
-static long libcfs_ioctl(struct file *file,
-                        unsigned int cmd, unsigned long arg)
+static long
+libcfs_psdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
 {
-       struct cfs_psdev_file    pfile;
-       int    rc = 0;
-
        if (!capable(CAP_SYS_ADMIN))
                return -EACCES;
 
@@ -130,26 +170,12 @@ static long libcfs_ioctl(struct file *file,
                return -EINVAL;
        }
 
-       /* Handle platform-dependent IOC requests */
-       switch (cmd) {
-       case IOC_LIBCFS_PANIC:
-               if (!capable(CFS_CAP_SYS_BOOT))
-                       return -EPERM;
-               panic("debugctl-invoked panic");
-               return 0;
-       }
-
-       if (libcfs_psdev_ops.p_ioctl)
-               rc = libcfs_psdev_ops.p_ioctl(&pfile, cmd, (void __user *)arg);
-       else
-               rc = -EPERM;
-       return rc;
+       return libcfs_ioctl(cmd, (void __user *)arg);
 }
 
 static const struct file_operations libcfs_fops = {
-       .unlocked_ioctl = libcfs_ioctl,
-       .open           = libcfs_psdev_open,
-       .release        = libcfs_psdev_release,
+       .owner          = THIS_MODULE,
+       .unlocked_ioctl = libcfs_psdev_ioctl,
 };
 
 struct miscdevice libcfs_dev = {