mirror of
https://github.com/torvalds/linux.git
synced 2024-11-21 19:46:16 +00:00
48b9a8dabc
When removing a resource from vmci_resource_table in
vmci_resource_remove(), the search is performed using the resource
handle by comparing context and resource fields.
It is possible though to create two resources with different types
but same handle (same context and resource fields).
When trying to remove one of the resources, vmci_resource_remove()
may not remove the intended one, but the object will still be freed
as in the case of the datagram type in vmci_datagram_destroy_handle().
vmci_resource_table will still hold a pointer to this freed resource
leading to a use-after-free vulnerability.
BUG: KASAN: use-after-free in vmci_handle_is_equal include/linux/vmw_vmci_defs.h:142 [inline]
BUG: KASAN: use-after-free in vmci_resource_remove+0x3a1/0x410 drivers/misc/vmw_vmci/vmci_resource.c:147
Read of size 4 at addr ffff88801c16d800 by task syz-executor197/1592
Call Trace:
<TASK>
__dump_stack lib/dump_stack.c:88 [inline]
dump_stack_lvl+0x82/0xa9 lib/dump_stack.c:106
print_address_description.constprop.0+0x21/0x366 mm/kasan/report.c:239
__kasan_report.cold+0x7f/0x132 mm/kasan/report.c:425
kasan_report+0x38/0x51 mm/kasan/report.c:442
vmci_handle_is_equal include/linux/vmw_vmci_defs.h:142 [inline]
vmci_resource_remove+0x3a1/0x410 drivers/misc/vmw_vmci/vmci_resource.c:147
vmci_qp_broker_detach+0x89a/0x11b9 drivers/misc/vmw_vmci/vmci_queue_pair.c:2182
ctx_free_ctx+0x473/0xbe1 drivers/misc/vmw_vmci/vmci_context.c:444
kref_put include/linux/kref.h:65 [inline]
vmci_ctx_put drivers/misc/vmw_vmci/vmci_context.c:497 [inline]
vmci_ctx_destroy+0x170/0x1d6 drivers/misc/vmw_vmci/vmci_context.c:195
vmci_host_close+0x125/0x1ac drivers/misc/vmw_vmci/vmci_host.c:143
__fput+0x261/0xa34 fs/file_table.c:282
task_work_run+0xf0/0x194 kernel/task_work.c:164
tracehook_notify_resume include/linux/tracehook.h:189 [inline]
exit_to_user_mode_loop+0x184/0x189 kernel/entry/common.c:187
exit_to_user_mode_prepare+0x11b/0x123 kernel/entry/common.c:220
__syscall_exit_to_user_mode_work kernel/entry/common.c:302 [inline]
syscall_exit_to_user_mode+0x18/0x42 kernel/entry/common.c:313
do_syscall_64+0x41/0x85 arch/x86/entry/common.c:86
entry_SYSCALL_64_after_hwframe+0x6e/0x0
This change ensures the type is also checked when removing
the resource from vmci_resource_table in vmci_resource_remove().
Fixes: bc63dedb7d
("VMCI: resource object implementation.")
Cc: stable@vger.kernel.org
Reported-by: George Kennedy <george.kennedy@oracle.com>
Signed-off-by: David Fernandez Gonzalez <david.fernandez.gonzalez@oracle.com>
Link: https://lore.kernel.org/r/20240828154338.754746-1-david.fernandez.gonzalez@oracle.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
223 lines
5.4 KiB
C
223 lines
5.4 KiB
C
// SPDX-License-Identifier: GPL-2.0-only
|
|
/*
|
|
* VMware VMCI Driver
|
|
*
|
|
* Copyright (C) 2012 VMware, Inc. All rights reserved.
|
|
*/
|
|
|
|
#include <linux/vmw_vmci_defs.h>
|
|
#include <linux/hash.h>
|
|
#include <linux/types.h>
|
|
#include <linux/rculist.h>
|
|
#include <linux/completion.h>
|
|
|
|
#include "vmci_resource.h"
|
|
#include "vmci_driver.h"
|
|
|
|
|
|
#define VMCI_RESOURCE_HASH_BITS 7
|
|
#define VMCI_RESOURCE_HASH_BUCKETS (1 << VMCI_RESOURCE_HASH_BITS)
|
|
|
|
struct vmci_hash_table {
|
|
spinlock_t lock;
|
|
struct hlist_head entries[VMCI_RESOURCE_HASH_BUCKETS];
|
|
};
|
|
|
|
static struct vmci_hash_table vmci_resource_table = {
|
|
.lock = __SPIN_LOCK_UNLOCKED(vmci_resource_table.lock),
|
|
};
|
|
|
|
static unsigned int vmci_resource_hash(struct vmci_handle handle)
|
|
{
|
|
return hash_32(handle.resource, VMCI_RESOURCE_HASH_BITS);
|
|
}
|
|
|
|
/*
|
|
* Gets a resource (if one exists) matching given handle from the hash table.
|
|
*/
|
|
static struct vmci_resource *vmci_resource_lookup(struct vmci_handle handle,
|
|
enum vmci_resource_type type)
|
|
{
|
|
struct vmci_resource *r, *resource = NULL;
|
|
unsigned int idx = vmci_resource_hash(handle);
|
|
|
|
rcu_read_lock();
|
|
hlist_for_each_entry_rcu(r,
|
|
&vmci_resource_table.entries[idx], node) {
|
|
u32 cid = r->handle.context;
|
|
u32 rid = r->handle.resource;
|
|
|
|
if (r->type == type &&
|
|
rid == handle.resource &&
|
|
(cid == handle.context || cid == VMCI_INVALID_ID ||
|
|
handle.context == VMCI_INVALID_ID)) {
|
|
resource = r;
|
|
break;
|
|
}
|
|
}
|
|
rcu_read_unlock();
|
|
|
|
return resource;
|
|
}
|
|
|
|
/*
|
|
* Find an unused resource ID and return it. The first
|
|
* VMCI_RESERVED_RESOURCE_ID_MAX are reserved so we start from
|
|
* its value + 1.
|
|
* Returns VMCI resource id on success, VMCI_INVALID_ID on failure.
|
|
*/
|
|
static u32 vmci_resource_find_id(u32 context_id,
|
|
enum vmci_resource_type resource_type)
|
|
{
|
|
static u32 resource_id = VMCI_RESERVED_RESOURCE_ID_MAX + 1;
|
|
u32 old_rid = resource_id;
|
|
u32 current_rid;
|
|
|
|
/*
|
|
* Generate a unique resource ID. Keep on trying until we wrap around
|
|
* in the RID space.
|
|
*/
|
|
do {
|
|
struct vmci_handle handle;
|
|
|
|
current_rid = resource_id;
|
|
resource_id++;
|
|
if (unlikely(resource_id == VMCI_INVALID_ID)) {
|
|
/* Skip the reserved rids. */
|
|
resource_id = VMCI_RESERVED_RESOURCE_ID_MAX + 1;
|
|
}
|
|
|
|
handle = vmci_make_handle(context_id, current_rid);
|
|
if (!vmci_resource_lookup(handle, resource_type))
|
|
return current_rid;
|
|
} while (resource_id != old_rid);
|
|
|
|
return VMCI_INVALID_ID;
|
|
}
|
|
|
|
|
|
int vmci_resource_add(struct vmci_resource *resource,
|
|
enum vmci_resource_type resource_type,
|
|
struct vmci_handle handle)
|
|
|
|
{
|
|
unsigned int idx;
|
|
int result;
|
|
|
|
spin_lock(&vmci_resource_table.lock);
|
|
|
|
if (handle.resource == VMCI_INVALID_ID) {
|
|
handle.resource = vmci_resource_find_id(handle.context,
|
|
resource_type);
|
|
if (handle.resource == VMCI_INVALID_ID) {
|
|
result = VMCI_ERROR_NO_HANDLE;
|
|
goto out;
|
|
}
|
|
} else if (vmci_resource_lookup(handle, resource_type)) {
|
|
result = VMCI_ERROR_ALREADY_EXISTS;
|
|
goto out;
|
|
}
|
|
|
|
resource->handle = handle;
|
|
resource->type = resource_type;
|
|
INIT_HLIST_NODE(&resource->node);
|
|
kref_init(&resource->kref);
|
|
init_completion(&resource->done);
|
|
|
|
idx = vmci_resource_hash(resource->handle);
|
|
hlist_add_head_rcu(&resource->node, &vmci_resource_table.entries[idx]);
|
|
|
|
result = VMCI_SUCCESS;
|
|
|
|
out:
|
|
spin_unlock(&vmci_resource_table.lock);
|
|
return result;
|
|
}
|
|
|
|
void vmci_resource_remove(struct vmci_resource *resource)
|
|
{
|
|
struct vmci_handle handle = resource->handle;
|
|
unsigned int idx = vmci_resource_hash(handle);
|
|
struct vmci_resource *r;
|
|
|
|
/* Remove resource from hash table. */
|
|
spin_lock(&vmci_resource_table.lock);
|
|
|
|
hlist_for_each_entry(r, &vmci_resource_table.entries[idx], node) {
|
|
if (vmci_handle_is_equal(r->handle, resource->handle) &&
|
|
resource->type == r->type) {
|
|
hlist_del_init_rcu(&r->node);
|
|
break;
|
|
}
|
|
}
|
|
|
|
spin_unlock(&vmci_resource_table.lock);
|
|
synchronize_rcu();
|
|
|
|
vmci_resource_put(resource);
|
|
wait_for_completion(&resource->done);
|
|
}
|
|
|
|
struct vmci_resource *
|
|
vmci_resource_by_handle(struct vmci_handle resource_handle,
|
|
enum vmci_resource_type resource_type)
|
|
{
|
|
struct vmci_resource *r, *resource = NULL;
|
|
|
|
rcu_read_lock();
|
|
|
|
r = vmci_resource_lookup(resource_handle, resource_type);
|
|
if (r &&
|
|
(resource_type == r->type ||
|
|
resource_type == VMCI_RESOURCE_TYPE_ANY)) {
|
|
resource = vmci_resource_get(r);
|
|
}
|
|
|
|
rcu_read_unlock();
|
|
|
|
return resource;
|
|
}
|
|
|
|
/*
|
|
* Get a reference to given resource.
|
|
*/
|
|
struct vmci_resource *vmci_resource_get(struct vmci_resource *resource)
|
|
{
|
|
kref_get(&resource->kref);
|
|
|
|
return resource;
|
|
}
|
|
|
|
static void vmci_release_resource(struct kref *kref)
|
|
{
|
|
struct vmci_resource *resource =
|
|
container_of(kref, struct vmci_resource, kref);
|
|
|
|
/* Verify the resource has been unlinked from hash table */
|
|
WARN_ON(!hlist_unhashed(&resource->node));
|
|
|
|
/* Signal that container of this resource can now be destroyed */
|
|
complete(&resource->done);
|
|
}
|
|
|
|
/*
|
|
* Resource's release function will get called if last reference.
|
|
* If it is the last reference, then we are sure that nobody else
|
|
* can increment the count again (it's gone from the resource hash
|
|
* table), so there's no need for locking here.
|
|
*/
|
|
int vmci_resource_put(struct vmci_resource *resource)
|
|
{
|
|
/*
|
|
* We propagate the information back to caller in case it wants to know
|
|
* whether entry was freed.
|
|
*/
|
|
return kref_put(&resource->kref, vmci_release_resource) ?
|
|
VMCI_SUCCESS_ENTRY_DEAD : VMCI_SUCCESS;
|
|
}
|
|
|
|
struct vmci_handle vmci_resource_handle(struct vmci_resource *resource)
|
|
{
|
|
return resource->handle;
|
|
}
|