vhost: vringh: Use matching allocation type in resize_iovec()
authorKees Cook <kees@kernel.org>
Sat, 26 Apr 2025 06:22:15 +0000 (23:22 -0700)
committerMichael S. Tsirkin <mst@redhat.com>
Tue, 27 May 2025 14:27:53 +0000 (10:27 -0400)
In preparation for making the kmalloc family of allocators type aware,
we need to make sure that the returned type from the allocation matches
the type of the variable being assigned. (Before, the allocator would
always return "void *", which can be implicitly cast to any pointer type.)

The assigned type is "struct kvec *", but the returned type will be
"struct iovec *". These have the same allocation size, so there is no
bug:

struct kvec {
        void *iov_base; /* and that should *never* hold a userland pointer */
        size_t iov_len;
};

struct iovec
{
        void __user *iov_base;  /* BSD uses caddr_t (1003.1g requires void *) */
        __kernel_size_t iov_len; /* Must be size_t (1003.1g) */
};

Adjust the allocation type to match the assignment.

Signed-off-by: Kees Cook <kees@kernel.org>
Message-Id: <20250426062214.work.334-kees@kernel.org>
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
drivers/vhost/vringh.c

index 73e153f..93735fc 100644 (file)
@@ -225,10 +225,9 @@ static int resize_iovec(struct vringh_kiov *iov, gfp_t gfp)
 
        flag = (iov->max_num & VRINGH_IOV_ALLOCATED);
        if (flag)
-               new = krealloc_array(iov->iov, new_num,
-                                    sizeof(struct iovec), gfp);
+               new = krealloc_array(iov->iov, new_num, sizeof(*new), gfp);
        else {
-               new = kmalloc_array(new_num, sizeof(struct iovec), gfp);
+               new = kmalloc_array(new_num, sizeof(*new), gfp);
                if (new) {
                        memcpy(new, iov->iov,
                               iov->max_num * sizeof(struct iovec));