LKML Archive on lore.kernel.org
help / color / mirror / Atom feed
From: Jiazi Li <jqqlijiazi@gmail.com>
To: Arnd Bergmann <arnd@arndb.de>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: Jiazi Li <lijiazi@xiaomi.com>, linux-kernel@vger.kernel.org
Subject: [PATCH] char: misc: init list head only when needed
Date: Mon, 16 Aug 2021 18:12:32 +0800	[thread overview]
Message-ID: <853e8529c18c4a71c36b49f9598961fbc3f39682.1628921932.git.lijiazi@xiaomi.com> (raw)

If a module successfully registers a misc device.
Then, due to some bugs, use same address register misc device
again, init list head will corrupt misc_list, resulting in oops
when using misc_list.

In this scenario, do not init list head, if registration is
successful, init list head is also not required.

Signed-off-by: Jiazi Li <lijiazi@xiaomi.com>
---
 drivers/char/misc.c | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)

diff --git a/drivers/char/misc.c b/drivers/char/misc.c
index ca5141e..2451640 100644
--- a/drivers/char/misc.c
+++ b/drivers/char/misc.c
@@ -176,7 +176,6 @@ int misc_register(struct miscdevice *misc)
 	int err = 0;
 	bool is_dynamic = (misc->minor == MISC_DYNAMIC_MINOR);
 
-	INIT_LIST_HEAD(&misc->list);
 
 	mutex_lock(&misc_mtx);
 
@@ -185,7 +184,7 @@ int misc_register(struct miscdevice *misc)
 
 		if (i >= DYNAMIC_MINORS) {
 			err = -EBUSY;
-			goto out;
+			goto err2;
 		}
 		misc->minor = DYNAMIC_MINORS - i - 1;
 		set_bit(i, misc_minors);
@@ -195,7 +194,13 @@ int misc_register(struct miscdevice *misc)
 		list_for_each_entry(c, &misc_list, list) {
 			if (c->minor == misc->minor) {
 				err = -EBUSY;
-				goto out;
+				/*
+				 * if module use same address double register,
+				 * init list will corrupt misc_list
+				 */
+				if (c == misc)
+					goto err1;
+				goto err2;
 			}
 		}
 	}
@@ -214,7 +219,7 @@ int misc_register(struct miscdevice *misc)
 			misc->minor = MISC_DYNAMIC_MINOR;
 		}
 		err = PTR_ERR(misc->this_device);
-		goto out;
+		goto err2;
 	}
 
 	/*
@@ -222,7 +227,11 @@ int misc_register(struct miscdevice *misc)
 	 * earlier defaults
 	 */
 	list_add(&misc->list, &misc_list);
- out:
+	mutex_unlock(&misc_mtx);
+	return 0;
+ err2:
+	INIT_LIST_HEAD(&misc->list);
+ err1:
 	mutex_unlock(&misc_mtx);
 	return err;
 }
-- 
2.7.4


             reply	other threads:[~2021-08-16 10:14 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-16 10:12 Jiazi Li [this message]
2021-09-04  6:32 ` [PATCH] char: misc: init list head only when needed Greg Kroah-Hartman

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=853e8529c18c4a71c36b49f9598961fbc3f39682.1628921932.git.lijiazi@xiaomi.com \
    --to=jqqlijiazi@gmail.com \
    --cc=arnd@arndb.de \
    --cc=gregkh@linuxfoundation.org \
    --cc=lijiazi@xiaomi.com \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).