From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795057785; b=yJP2XlUQJO9F2zTS9gvLuejhwtoklN0VsDqPLiOI89yt8yEADoI5fTDnZpeXIFfTDBEt OWCjDA86lVosRQ6J+xYX8d6w/8W4/lH7dOdmlCJmKTGK1hvzDxllICeao9AfKs8s2Mh/p Aw3nqrZIowJuXZ1E8/xbCSMynzUE/r2BRpZn1MRcVREfARw/q1JvJaOCx6TS2OTpsYESs alDEiN2UVA9tIkhxOoMu9K38nZhR980rPuo677nHdteT8hI1OCp0PrJbRIh+EhYj1Zs7b e4F4M1PmbLBPMJsHfe6iA112tptuw7oMjL7pswDp6q3h6kPpCcyb85xhFYpqpUqtbZg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795057785; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:User-Agent:X-stable:X-Patchwork-Hint: MIME-Version:Content-Transfer-Encoding; bh=TAW3wxQa52Gsgz5IhGbhCBo9Gnnf1DQlirPBNb4+Ig4=; b=bMVYGM4Lf6DwhzzmXo+jPZIrPJKpVMsyws6k0NL4lvOdbeVXtCsF3Fdorn07p16vyuPA 0rAVsFkiTPc1BvCCRIT0BKiyf3lD/sixn1HZ6W2AtYCI+P2juY/QLjBl7Aq0eIWXkwXD+ d4pVt4Ot9UhYJkWdVsSmt2AC4+JH+YJE8+LBbOCe8XQNWYYg8rumFofEHrEjzM1Zi6yo4 SZNH0lBQwEhGErk2vVkzc7KgwcUpHizxA0zyxT/dm+cVLMUa4pjoQ3NMsJJpkwLgk2Ez+ f0IaMWqy3H7LcPR7nNPC0PKD+f09REi7vYpHXZTHsn8x9JI6fB6nP4QbN881V+ZvyVQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=kXW4FhAI; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B31AB606A31C; Thu, 19 Nov 2026 03:09:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=kXW4FhAI Received: by smtp.kernel.org (Postfix) with ESMTPSA id 14F381F000C4; Thu, 19 Nov 2026 03:09:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1795057785; bh=TAW3wxQa52Gsgz5IhGbhCBo9Gnnf1DQlirPBNb4+Ig4=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=kXW4FhAIj58Rzju9qV53CdBzX2mTyRZ4wYQHi4v9lp/A+jQU87Dio0tPDwJBNTGsI 7mf+BXxS0Da0Gp/+SHRHN52EKCJ00nHAyTFUf3I7zsl/oEqGRLdNpyd4Tn20FirrXg kShB23BOFzL/EL/NOitQHCZCazKo/BfL+3QmYVsI= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, syzbot+563191a4939ddbfe73d4@syzkaller.appspotmail.com, Yousef Alhouseen , Jiri Kosina Subject: [PATCH 5.10 0252/1848] HID: hiddev: keep state alive through disconnect unlock Date: Wed, 18 Nov 2026 15:38:10 +0100 Message-ID: <20261118145922.608131276@linuxfoundation.org> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261118145911.150052362@linuxfoundation.org> References: <20261118145911.150052362@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: patches@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 5.10-stable review patch. If anyone has any objections, please let me know. ------------------ From: Yousef Alhouseen commit 03f449b5de36a25fc5eba7dabf2b4494389bdb91 upstream. mutex_unlock() clears the mutex owner before taking its wait lock. A thread spinning in the final hiddev file release can acquire existancelock after hiddev_disconnect() clears the owner, then free hiddev before the disconnecting thread reaches the mutex wait lock. This causes a use-after-free in the mutex slow unlock path. Give the connection and each open file an explicit reference. Drop each reference only after its existancelock critical section has completed, so neither unlock path can free the mutex while the other is still using it. Fixes: 079034073faf ("HID: hiddev cleanup -- handle all error conditions properly") Reported-by: syzbot+563191a4939ddbfe73d4@syzkaller.appspotmail.com Closes: https://syzkaller.appspot.com/bug?extid=563191a4939ddbfe73d4 Cc: stable@vger.kernel.org Signed-off-by: Yousef Alhouseen Link: https://syzkaller.appspot.com/bug?extid=563191a4939ddbfe73d4 Signed-off-by: Jiri Kosina Signed-off-by: Greg Kroah-Hartman --- drivers/hid/usbhid/hiddev.c | 37 +++++++++++++++++++------------------ include/linux/hiddev.h | 2 ++ 2 files changed, 21 insertions(+), 18 deletions(-) --- a/drivers/hid/usbhid/hiddev.c +++ b/drivers/hid/usbhid/hiddev.c @@ -46,6 +46,12 @@ struct hiddev_list { struct mutex thread_lock; }; +static void hiddev_put(struct hiddev *hiddev) +{ + if (refcount_dec_and_test(&hiddev->refcount)) + kfree(hiddev); +} + /* * Find a report, given the report's type and ID. The ID can be specified * indirectly by REPORT_ID_FIRST (which returns the first report of the given @@ -216,26 +222,21 @@ static int hiddev_fasync(int fd, struct static int hiddev_release(struct inode * inode, struct file * file) { struct hiddev_list *list = file->private_data; + struct hiddev *hiddev = list->hiddev; unsigned long flags; - spin_lock_irqsave(&list->hiddev->list_lock, flags); + spin_lock_irqsave(&hiddev->list_lock, flags); list_del(&list->node); - spin_unlock_irqrestore(&list->hiddev->list_lock, flags); + spin_unlock_irqrestore(&hiddev->list_lock, flags); - mutex_lock(&list->hiddev->existancelock); - if (!--list->hiddev->open) { - if (list->hiddev->exist) { - hid_hw_close(list->hiddev->hid); - hid_hw_power(list->hiddev->hid, PM_HINT_NORMAL); - } else { - mutex_unlock(&list->hiddev->existancelock); - kfree(list->hiddev); - vfree(list); - return 0; - } + mutex_lock(&hiddev->existancelock); + if (!--hiddev->open && hiddev->exist) { + hid_hw_close(hiddev->hid); + hid_hw_power(hiddev->hid, PM_HINT_NORMAL); } - mutex_unlock(&list->hiddev->existancelock); + mutex_unlock(&hiddev->existancelock); + hiddev_put(hiddev); vfree(list); return 0; @@ -270,6 +271,7 @@ static int __hiddev_open(struct hiddev * spin_unlock_irq(&hiddev->list_lock); file->private_data = list; + refcount_inc(&hiddev->refcount); return 0; @@ -897,6 +899,7 @@ int hiddev_connect(struct hid_device *hi INIT_LIST_HEAD(&hiddev->list); spin_lock_init(&hiddev->list_lock); mutex_init(&hiddev->existancelock); + refcount_set(&hiddev->refcount, 1); hid->hiddev = hiddev; hiddev->hid = hid; hiddev->exist = 1; @@ -937,9 +940,7 @@ void hiddev_disconnect(struct hid_device if (hiddev->open) { hid_hw_close(hiddev->hid); wake_up_interruptible(&hiddev->wait); - mutex_unlock(&hiddev->existancelock); - } else { - mutex_unlock(&hiddev->existancelock); - kfree(hiddev); } + mutex_unlock(&hiddev->existancelock); + hiddev_put(hiddev); } --- a/include/linux/hiddev.h +++ b/include/linux/hiddev.h @@ -13,6 +13,7 @@ #ifndef _HIDDEV_H #define _HIDDEV_H +#include #include @@ -24,6 +25,7 @@ struct hiddev { int minor; int exist; int open; + refcount_t refcount; struct mutex existancelock; wait_queue_head_t wait; struct hid_device *hid;