From nobody Thu Apr 23 09:11:12 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 94A6FC433EF for ; Mon, 4 Jul 2022 06:44:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232944AbiGDGoU (ORCPT ); Mon, 4 Jul 2022 02:44:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51624 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229576AbiGDGoS (ORCPT ); Mon, 4 Jul 2022 02:44:18 -0400 Received: from www262.sakura.ne.jp (www262.sakura.ne.jp [202.181.97.72]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B681F3899 for ; Sun, 3 Jul 2022 23:44:16 -0700 (PDT) Received: from fsav115.sakura.ne.jp (fsav115.sakura.ne.jp [27.133.134.242]) by www262.sakura.ne.jp (8.15.2/8.15.2) with ESMTP id 2646i7Ao029402; Mon, 4 Jul 2022 15:44:07 +0900 (JST) (envelope-from penguin-kernel@I-love.SAKURA.ne.jp) Received: from www262.sakura.ne.jp (202.181.97.72) by fsav115.sakura.ne.jp (F-Secure/fsigk_smtp/550/fsav115.sakura.ne.jp); Mon, 04 Jul 2022 15:44:07 +0900 (JST) X-Virus-Status: clean(F-Secure/fsigk_smtp/550/fsav115.sakura.ne.jp) Received: from [192.168.1.9] (M106072142033.v4.enabler.ne.jp [106.72.142.33]) (authenticated bits=0) by www262.sakura.ne.jp (8.15.2/8.15.2) with ESMTPSA id 2646i7QA029398 (version=TLSv1.2 cipher=AES256-GCM-SHA384 bits=256 verify=NO); Mon, 4 Jul 2022 15:44:07 +0900 (JST) (envelope-from penguin-kernel@I-love.SAKURA.ne.jp) Message-ID: <72e74af9-f1b6-e383-a2c3-6ee8a0aea5e0@I-love.SAKURA.ne.jp> Date: Mon, 4 Jul 2022 15:44:07 +0900 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 6.3; Win64; x64; rv:91.0) Gecko/20100101 Thunderbird/91.11.0 Subject: [PATCH] char: misc: make misc_open() and misc_register() killable Content-Language: en-US To: arnd@arndb.de, gregkh@linuxfoundation.org References: <000000000000d9ff3a05bb37069e@google.com> Cc: linux-kernel@vger.kernel.org From: Tetsuo Handa In-Reply-To: <000000000000d9ff3a05bb37069e@google.com> Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" syzbot is reporting hung task at misc_open() [1], for snapshot_open() from misc_open() might sleep for long with misc_mtx held whereas userspace can flood with concurrent misc_open() requests. Mitigate this problem by making misc_open() and misc_register() killable. Link: https://syzkaller.appspot.com/bug?extid=3D358c9ab4c93da7b7238c [1] Reported-by: syzbot Signed-off-by: Tetsuo Handa --- drivers/char/misc.c | 57 +++++++++++++++++++-------------------------- 1 file changed, 24 insertions(+), 33 deletions(-) diff --git a/drivers/char/misc.c b/drivers/char/misc.c index cba19bfdc44d..b9a494bc4228 100644 --- a/drivers/char/misc.c +++ b/drivers/char/misc.c @@ -100,49 +100,39 @@ static const struct seq_operations misc_seq_ops =3D { static int misc_open(struct inode *inode, struct file *file) { int minor =3D iminor(inode); - struct miscdevice *c =3D NULL, *iter; + struct miscdevice *iter; int err =3D -ENODEV; const struct file_operations *new_fops =3D NULL; + bool retried =3D false; =20 - mutex_lock(&misc_mtx); - + retry: + if (mutex_lock_killable(&misc_mtx)) + return -EINTR; list_for_each_entry(iter, &misc_list, list) { if (iter->minor !=3D minor) continue; - c =3D iter; new_fops =3D fops_get(iter->fops); + if (!new_fops) + break; + /* + * Place the miscdevice in the file's + * private_data so it can be used by the + * file operations, including f_op->open below + */ + file->private_data =3D iter; + + err =3D 0; + replace_fops(file, new_fops); + if (file->f_op->open) + err =3D file->f_op->open(inode, file); break; } - - if (!new_fops) { - mutex_unlock(&misc_mtx); + mutex_unlock(&misc_mtx); + if (!new_fops && !retried) { request_module("char-major-%d-%d", MISC_MAJOR, minor); - mutex_lock(&misc_mtx); - - list_for_each_entry(iter, &misc_list, list) { - if (iter->minor !=3D minor) - continue; - c =3D iter; - new_fops =3D fops_get(iter->fops); - break; - } - if (!new_fops) - goto fail; + retried =3D true; + goto retry; } - - /* - * Place the miscdevice in the file's - * private_data so it can be used by the - * file operations, including f_op->open below - */ - file->private_data =3D c; - - err =3D 0; - replace_fops(file, new_fops); - if (file->f_op->open) - err =3D file->f_op->open(inode, file); -fail: - mutex_unlock(&misc_mtx); return err; } =20 @@ -180,7 +170,8 @@ int misc_register(struct miscdevice *misc) =20 INIT_LIST_HEAD(&misc->list); =20 - mutex_lock(&misc_mtx); + if (mutex_lock_killable(&misc_mtx)) + return -EINTR; =20 if (is_dynamic) { int i =3D find_first_zero_bit(misc_minors, DYNAMIC_MINORS); --=20 2.18.4