RDMA/ucma: ucma_context reference leak in error path
authorShamir Rabinovitch <shamir.rabinovitch@oracle.com>
Tue, 10 Apr 2018 14:26:23 +0000 (10:26 -0400)
committerJason Gunthorpe <jgg@mellanox.com>
Mon, 16 Apr 2018 15:49:24 +0000 (09:49 -0600)
Validating input parameters should be done before getting the cm_id
otherwise it can leak a cm_id reference.

Fixes: 6a21dfc0d0db ("RDMA/ucma: Limit possible option size")
Signed-off-by: Shamir Rabinovitch <shamir.rabinovitch@oracle.com>
Reviewed-by: Leon Romanovsky <leonro@mellanox.com>
Signed-off-by: Jason Gunthorpe <jgg@mellanox.com>
drivers/infiniband/core/ucma.c

index 74329483af6d424d97970719d1b1c1733737b334..680b3536ad3efe8ad3eef1ee56c9fddebb65f23d 100644 (file)
@@ -1316,13 +1316,13 @@ static ssize_t ucma_set_option(struct ucma_file *file, const char __user *inbuf,
        if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
                return -EFAULT;
 
+       if (unlikely(cmd.optlen > KMALLOC_MAX_SIZE))
+               return -EINVAL;
+
        ctx = ucma_get_ctx(file, cmd.id);
        if (IS_ERR(ctx))
                return PTR_ERR(ctx);
 
-       if (unlikely(cmd.optlen > KMALLOC_MAX_SIZE))
-               return -EINVAL;
-
        optval = memdup_user(u64_to_user_ptr(cmd.optval),
                             cmd.optlen);
        if (IS_ERR(optval)) {