From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C60E0399346 for ; Sun, 19 Jul 2026 14:26:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784471186; cv=none; b=LSg9DVX1iGNGgAVmwJ6EimqUggcEWhy2A30b+2Twg7+6PP0jQJBbPGbowArSf3Z8UUMz1DpG2RPprTwzje3sx7GWkZB0zDlayDvldZMbQ6/ncnFbxzme+Zv5Rsw3U2TROS8dEdQlYF3RMlJwgfb25WMf7GAWmA7zAcU4IjqePIw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784471186; c=relaxed/simple; bh=G5RiBfYwMqpURrhxkSgtncqbGrEPvAeCDPLR3EP7qgA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=nhVxqwHML2sFLbTcQ3gjDRVnLprxxM1W6GMmNVL8Lmh1vwHAEIzWWG7xOjYm0TyA/1HN53Krn79b4u8swdQ8DPjMarWpnDBj1e5iBN9Z0eCGYFvj3kwLoDBT+qe6sSYKRQUI17PmWA0TthmF4b7d9zP5rTxwpttVIdrnwVZBM7o= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=TZHxDOAg; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="TZHxDOAg" Received: by smtp.kernel.org (Postfix) with ESMTPSA id EC38D1F00A3A; Sun, 19 Jul 2026 14:26:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1784471185; bh=tXUxa0IPECf24YyFyXN9MSkg4zpv0yFthMA8fqp5d0E=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=TZHxDOAgeOKJog+v1nPzO6XPolw6MRqC8ee+tbcbUz3hZJveF0gxKcSvvs/d5CKuA 09AVM6UQl6rmsnbB1CcxbYEmWXkT+66iWgv86qAsUI/2TWItf5ayszat965d4glkpN mVT/QM8kLO4PmOIXockMRarn4jhM/+4U4v8NrPqMNud7lLhhmxD/t1omRFsEibei6w Ma5weAzDJwllh21rFBKJ6dcYfxqaxK03eyiGdhImeghgBNywuqaUJYhnjoMhDh++1H qPNajf8DIKHKCsZdXBhk8yZHlIpc9gFP9gehW63jVp13iFD3IwJKCxQUiRicPFMm/K KvfInZjvoaKAg== From: Sasha Levin To: stable@vger.kernel.org Cc: Johan Hovold , stable , Yue Sun , Greg Kroah-Hartman , Sasha Levin Subject: [PATCH 6.18.y 2/2] USB: iowarrior: fix use-after-free on disconnect race Date: Sun, 19 Jul 2026 10:26:22 -0400 Message-ID: <20260719142622.4094754-2-sashal@kernel.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260719142622.4094754-1-sashal@kernel.org> References: <2026071455-frostily-swimsuit-a10b@gregkh> <20260719142622.4094754-1-sashal@kernel.org> Precedence: bulk X-Mailing-List: stable@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Johan Hovold [ Upstream commit c602254ba4c10f60a73cd99d147874f86a3f485c ] mutex_unlock() may access the mutex structure after releasing the lock and therefore cannot be used to manage lifetime of objects directly (unlike spinlocks and refcounts). [1][2] Use a kref to release the driver data to avoid use-after-free in mutex_unlock() when release() races with disconnect(). [1] a51749ab34d9 ("locking/mutex: Document that mutex_unlock() is non-atomic") [2] 2b9d9e0a9ba0 ("locking/mutex: Clarify that mutex_unlock(), and most other sleeping locks, can still use the lock object after it's unlocked") Fixes: 946b960d13c1 ("USB: add driver for iowarrior devices.") Cc: stable Reported-by: Yue Sun Link: https://lore.kernel.org/r/20260618080204.38322-1-samsun1006219@gmail.com Signed-off-by: Johan Hovold Link: https://patch.msgid.link/20260622152612.116422-2-johan@kernel.org Signed-off-by: Greg Kroah-Hartman Signed-off-by: Sasha Levin --- drivers/usb/misc/iowarrior.c | 57 +++++++++++++++--------------------- 1 file changed, 24 insertions(+), 33 deletions(-) diff --git a/drivers/usb/misc/iowarrior.c b/drivers/usb/misc/iowarrior.c index f9266ae8164100..827b364c9f5db9 100644 --- a/drivers/usb/misc/iowarrior.c +++ b/drivers/usb/misc/iowarrior.c @@ -71,6 +71,7 @@ static struct usb_driver iowarrior_driver; /* Structure to hold all of our device specific stuff */ struct iowarrior { + struct kref kref; struct mutex mutex; /* locks this structure */ struct usb_device *udev; /* save off the usb device pointer */ struct usb_interface *interface; /* the interface for this device */ @@ -243,8 +244,10 @@ static void iowarrior_write_callback(struct urb *urb) /* * iowarrior_delete */ -static inline void iowarrior_delete(struct iowarrior *dev) +static inline void iowarrior_delete(struct kref *kref) { + struct iowarrior *dev = container_of(kref, struct iowarrior, kref); + kfree(dev->int_in_buffer); usb_free_urb(dev->int_in_urb); kfree(dev->read_queue); @@ -640,6 +643,9 @@ static int iowarrior_open(struct inode *inode, struct file *file) } /* increment our usage count for the driver */ ++dev->opened; + + kref_get(&dev->kref); + /* save our object in the file's private structure */ file->private_data = dev; retval = 0; @@ -655,7 +661,6 @@ static int iowarrior_open(struct inode *inode, struct file *file) static int iowarrior_release(struct inode *inode, struct file *file) { struct iowarrior *dev; - int retval = 0; dev = file->private_data; if (!dev) @@ -663,29 +668,18 @@ static int iowarrior_release(struct inode *inode, struct file *file) /* lock our device */ mutex_lock(&dev->mutex); + dev->opened = 0; /* we're closing now */ - if (dev->opened <= 0) { - retval = -ENODEV; /* close called more than once */ - mutex_unlock(&dev->mutex); - } else { - dev->opened = 0; /* we're closing now */ - retval = 0; - if (dev->present) { - /* - The device is still connected so we only shutdown - pending read-/write-ops. - */ - usb_kill_urb(dev->int_in_urb); - wake_up_interruptible(&dev->read_wait); - wake_up_interruptible(&dev->write_wait); - mutex_unlock(&dev->mutex); - } else { - /* The device was unplugged, cleanup resources */ - mutex_unlock(&dev->mutex); - iowarrior_delete(dev); - } + if (dev->present) { + usb_kill_urb(dev->int_in_urb); + wake_up_interruptible(&dev->read_wait); + wake_up_interruptible(&dev->write_wait); } - return retval; + mutex_unlock(&dev->mutex); + + kref_put(&dev->kref, iowarrior_delete); + + return 0; } static __poll_t iowarrior_poll(struct file *file, poll_table * wait) @@ -770,6 +764,7 @@ static int iowarrior_probe(struct usb_interface *interface, if (!dev) return retval; + kref_init(&dev->kref); mutex_init(&dev->mutex); atomic_set(&dev->intr_idx, 0); @@ -888,7 +883,8 @@ static int iowarrior_probe(struct usb_interface *interface, return retval; error: - iowarrior_delete(dev); + kref_put(&dev->kref, iowarrior_delete); + return retval; } @@ -912,19 +908,14 @@ static void iowarrior_disconnect(struct usb_interface *interface) usb_kill_anchored_urbs(&dev->submitted); if (dev->opened) { - /* There is a process that holds a filedescriptor to the device , - so we only shutdown read-/write-ops going on. - Deleting the device is postponed until close() was called. - */ usb_kill_urb(dev->int_in_urb); wake_up_interruptible(&dev->read_wait); wake_up_interruptible(&dev->write_wait); - mutex_unlock(&dev->mutex); - } else { - /* no process is using the device, cleanup now */ - mutex_unlock(&dev->mutex); - iowarrior_delete(dev); } + + mutex_unlock(&dev->mutex); + + kref_put(&dev->kref, iowarrior_delete); } /* usb specific object needed to register this driver with the usb subsystem */ -- 2.53.0