Skip to content

Commit 99f98a7

Browse files
committed
iommufd: IOMMUFD_DESTROY should not increase the refcount
syzkaller found a race where IOMMUFD_DESTROY increments the refcount: obj = iommufd_get_object(ucmd->ictx, cmd->id, IOMMUFD_OBJ_ANY); if (IS_ERR(obj)) return PTR_ERR(obj); iommufd_ref_to_users(obj); /* See iommufd_ref_to_users() */ if (!iommufd_object_destroy_user(ucmd->ictx, obj)) As part of the sequence to join the two existing primitives together. Allowing the refcount the be elevated without holding the destroy_rwsem violates the assumption that all temporary refcount elevations are protected by destroy_rwsem. Racing IOMMUFD_DESTROY with iommufd_object_destroy_user() will cause spurious failures: WARNING: CPU: 0 PID: 3076 at drivers/iommu/iommufd/device.c:477 iommufd_access_destroy+0x18/0x20 drivers/iommu/iommufd/device.c:478 Modules linked in: CPU: 0 PID: 3076 Comm: syz-executor.0 Not tainted 6.3.0-rc1-syzkaller #0 Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 07/03/2023 RIP: 0010:iommufd_access_destroy+0x18/0x20 drivers/iommu/iommufd/device.c:477 Code: e8 3d 4e 00 00 84 c0 74 01 c3 0f 0b c3 0f 1f 44 00 00 f3 0f 1e fa 48 89 fe 48 8b bf a8 00 00 00 e8 1d 4e 00 00 84 c0 74 01 c3 <0f> 0b c3 0f 1f 44 00 00 41 57 41 56 41 55 4c 8d ae d0 00 00 00 41 RSP: 0018:ffffc90003067e08 EFLAGS: 00010246 RAX: 0000000000000000 RBX: ffff888109ea0300 RCX: 0000000000000000 RDX: 0000000000000001 RSI: 0000000000000000 RDI: 00000000ffffffff RBP: 0000000000000004 R08: 0000000000000000 R09: ffff88810bbb3500 R10: ffff88810bbb3e48 R11: 0000000000000000 R12: ffffc90003067e88 R13: ffffc90003067ea8 R14: ffff888101249800 R15: 00000000fffffffe FS: 00007ff7254fe6c0(0000) GS:ffff888237c00000(0000) knlGS:0000000000000000 CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 CR2: 0000555557262da8 CR3: 000000010a6fd000 CR4: 0000000000350ef0 Call Trace: <TASK> iommufd_test_create_access drivers/iommu/iommufd/selftest.c:596 [inline] iommufd_test+0x71c/0xcf0 drivers/iommu/iommufd/selftest.c:813 iommufd_fops_ioctl+0x10f/0x1b0 drivers/iommu/iommufd/main.c:337 vfs_ioctl fs/ioctl.c:51 [inline] __do_sys_ioctl fs/ioctl.c:870 [inline] __se_sys_ioctl fs/ioctl.c:856 [inline] __x64_sys_ioctl+0x84/0xc0 fs/ioctl.c:856 do_syscall_x64 arch/x86/entry/common.c:50 [inline] do_syscall_64+0x38/0x80 arch/x86/entry/common.c:80 entry_SYSCALL_64_after_hwframe+0x63/0xcd The solution is to not increment the refcount on the IOMMUFD_DESTROY path at all. Instead use the xa_lock to serialize everything. The refcount check == 1 and xa_erase can be done under a single critical region. This avoids the need for any refcount incrementing. It has the downside that if userspace races destroy with other operations it will get an EBUSY instead of waiting, but this is kind of racing is already dangerous. Fixes: 2ff4bed ("iommufd: File descriptor, context, kconfig and makefiles") Link: https://lore.kernel.org/r/[email protected] Reviewed-by: Kevin Tian <[email protected]> Reported-by: [email protected] Signed-off-by: Jason Gunthorpe <[email protected]>
1 parent 6eaae19 commit 99f98a7

File tree

3 files changed

+75
-30
lines changed

3 files changed

+75
-30
lines changed

drivers/iommu/iommufd/device.c

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,7 @@ EXPORT_SYMBOL_NS_GPL(iommufd_device_bind, IOMMUFD);
109109
*/
110110
void iommufd_device_unbind(struct iommufd_device *idev)
111111
{
112-
bool was_destroyed;
113-
114-
was_destroyed = iommufd_object_destroy_user(idev->ictx, &idev->obj);
115-
WARN_ON(!was_destroyed);
112+
iommufd_object_destroy_user(idev->ictx, &idev->obj);
116113
}
117114
EXPORT_SYMBOL_NS_GPL(iommufd_device_unbind, IOMMUFD);
118115

@@ -382,7 +379,7 @@ void iommufd_device_detach(struct iommufd_device *idev)
382379
mutex_unlock(&hwpt->devices_lock);
383380

384381
if (hwpt->auto_domain)
385-
iommufd_object_destroy_user(idev->ictx, &hwpt->obj);
382+
iommufd_object_deref_user(idev->ictx, &hwpt->obj);
386383
else
387384
refcount_dec(&hwpt->obj.users);
388385

@@ -456,10 +453,7 @@ EXPORT_SYMBOL_NS_GPL(iommufd_access_create, IOMMUFD);
456453
*/
457454
void iommufd_access_destroy(struct iommufd_access *access)
458455
{
459-
bool was_destroyed;
460-
461-
was_destroyed = iommufd_object_destroy_user(access->ictx, &access->obj);
462-
WARN_ON(!was_destroyed);
456+
iommufd_object_destroy_user(access->ictx, &access->obj);
463457
}
464458
EXPORT_SYMBOL_NS_GPL(iommufd_access_destroy, IOMMUFD);
465459

drivers/iommu/iommufd/iommufd_private.h

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,19 @@ void iommufd_object_abort_and_destroy(struct iommufd_ctx *ictx,
176176
struct iommufd_object *obj);
177177
void iommufd_object_finalize(struct iommufd_ctx *ictx,
178178
struct iommufd_object *obj);
179-
bool iommufd_object_destroy_user(struct iommufd_ctx *ictx,
180-
struct iommufd_object *obj);
179+
void __iommufd_object_destroy_user(struct iommufd_ctx *ictx,
180+
struct iommufd_object *obj, bool allow_fail);
181+
static inline void iommufd_object_destroy_user(struct iommufd_ctx *ictx,
182+
struct iommufd_object *obj)
183+
{
184+
__iommufd_object_destroy_user(ictx, obj, false);
185+
}
186+
static inline void iommufd_object_deref_user(struct iommufd_ctx *ictx,
187+
struct iommufd_object *obj)
188+
{
189+
__iommufd_object_destroy_user(ictx, obj, true);
190+
}
191+
181192
struct iommufd_object *_iommufd_object_alloc(struct iommufd_ctx *ictx,
182193
size_t size,
183194
enum iommufd_object_type type);

drivers/iommu/iommufd/main.c

Lines changed: 59 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -116,51 +116,91 @@ struct iommufd_object *iommufd_get_object(struct iommufd_ctx *ictx, u32 id,
116116
return obj;
117117
}
118118

119+
/*
120+
* Remove the given object id from the xarray if the only reference to the
121+
* object is held by the xarray. The caller must call ops destroy().
122+
*/
123+
static struct iommufd_object *iommufd_object_remove(struct iommufd_ctx *ictx,
124+
u32 id, bool extra_put)
125+
{
126+
struct iommufd_object *obj;
127+
XA_STATE(xas, &ictx->objects, id);
128+
129+
xa_lock(&ictx->objects);
130+
obj = xas_load(&xas);
131+
if (xa_is_zero(obj) || !obj) {
132+
obj = ERR_PTR(-ENOENT);
133+
goto out_xa;
134+
}
135+
136+
/*
137+
* If the caller is holding a ref on obj we put it here under the
138+
* spinlock.
139+
*/
140+
if (extra_put)
141+
refcount_dec(&obj->users);
142+
143+
if (!refcount_dec_if_one(&obj->users)) {
144+
obj = ERR_PTR(-EBUSY);
145+
goto out_xa;
146+
}
147+
148+
xas_store(&xas, NULL);
149+
if (ictx->vfio_ioas == container_of(obj, struct iommufd_ioas, obj))
150+
ictx->vfio_ioas = NULL;
151+
152+
out_xa:
153+
xa_unlock(&ictx->objects);
154+
155+
/* The returned object reference count is zero */
156+
return obj;
157+
}
158+
119159
/*
120160
* The caller holds a users refcount and wants to destroy the object. Returns
121161
* true if the object was destroyed. In all cases the caller no longer has a
122162
* reference on obj.
123163
*/
124-
bool iommufd_object_destroy_user(struct iommufd_ctx *ictx,
125-
struct iommufd_object *obj)
164+
void __iommufd_object_destroy_user(struct iommufd_ctx *ictx,
165+
struct iommufd_object *obj, bool allow_fail)
126166
{
167+
struct iommufd_object *ret;
168+
127169
/*
128170
* The purpose of the destroy_rwsem is to ensure deterministic
129171
* destruction of objects used by external drivers and destroyed by this
130172
* function. Any temporary increment of the refcount must hold the read
131173
* side of this, such as during ioctl execution.
132174
*/
133175
down_write(&obj->destroy_rwsem);
134-
xa_lock(&ictx->objects);
135-
refcount_dec(&obj->users);
136-
if (!refcount_dec_if_one(&obj->users)) {
137-
xa_unlock(&ictx->objects);
138-
up_write(&obj->destroy_rwsem);
139-
return false;
140-
}
141-
__xa_erase(&ictx->objects, obj->id);
142-
if (ictx->vfio_ioas && &ictx->vfio_ioas->obj == obj)
143-
ictx->vfio_ioas = NULL;
144-
xa_unlock(&ictx->objects);
176+
ret = iommufd_object_remove(ictx, obj->id, true);
145177
up_write(&obj->destroy_rwsem);
146178

179+
if (allow_fail && IS_ERR(ret))
180+
return;
181+
182+
/*
183+
* If there is a bug and we couldn't destroy the object then we did put
184+
* back the caller's refcount and will eventually try to free it again
185+
* during close.
186+
*/
187+
if (WARN_ON(IS_ERR(ret)))
188+
return;
189+
147190
iommufd_object_ops[obj->type].destroy(obj);
148191
kfree(obj);
149-
return true;
150192
}
151193

152194
static int iommufd_destroy(struct iommufd_ucmd *ucmd)
153195
{
154196
struct iommu_destroy *cmd = ucmd->cmd;
155197
struct iommufd_object *obj;
156198

157-
obj = iommufd_get_object(ucmd->ictx, cmd->id, IOMMUFD_OBJ_ANY);
199+
obj = iommufd_object_remove(ucmd->ictx, cmd->id, false);
158200
if (IS_ERR(obj))
159201
return PTR_ERR(obj);
160-
iommufd_ref_to_users(obj);
161-
/* See iommufd_ref_to_users() */
162-
if (!iommufd_object_destroy_user(ucmd->ictx, obj))
163-
return -EBUSY;
202+
iommufd_object_ops[obj->type].destroy(obj);
203+
kfree(obj);
164204
return 0;
165205
}
166206

0 commit comments

Comments
 (0)