[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20250704-rfc_miscdev-v4-3-b48986112d6a@oss.qualcomm.com>
Date: Fri, 04 Jul 2025 21:26:01 +0800
From: Zijun Hu <zijun_hu@...oud.com>
To: Arnd Bergmann <arnd@...db.de>,
Greg Kroah-Hartman <gregkh@...uxfoundation.org>,
"James E.J. Bottomley" <James.Bottomley@...senPartnership.com>,
Helge Deller <deller@....de>, "David S. Miller" <davem@...emloft.net>,
Andreas Larsson <andreas@...sler.com>
Cc: Thadeu Lima de Souza Cascardo <cascardo@...lia.com>,
Zijun Hu <zijun_hu@...oud.com>, linux-kernel@...r.kernel.org,
linux-parisc@...r.kernel.org, sparclinux@...r.kernel.org,
Zijun Hu <zijun.hu@....qualcomm.com>
Subject: [PATCH v4 3/8] char: misc: Disallow registering miscdevice whose
minor > MISC_DYNAMIC_MINOR
From: Zijun Hu <zijun.hu@....qualcomm.com>
Currently, It is allowed to register miscdevice with minor > 255
which is defined by macro MISC_DYNAMIC_MINOR, and cause:
- Chaos regarding division and management of minor codes.
- Registering failure if the minor was allocated to other dynamic request.
Fortunately, in-kernel users have not had such usage yet.
Fix by refusing to register miscdevice whose minor > 255.
Also bring in a very simple minor code space division and management:
|< 255 : Fixed minor code
|== 255 : Indicator to request dynamic minor code
|> 255 : Dynamic minor code requested, 1048320 minor codes totally
And all fixed minors allocated should be registered in 'linux/miscdevice.h'
Signed-off-by: Zijun Hu <zijun.hu@....qualcomm.com>
---
drivers/char/misc.c | 6 ++++++
include/linux/miscdevice.h | 8 ++++++++
2 files changed, 14 insertions(+)
diff --git a/drivers/char/misc.c b/drivers/char/misc.c
index 558302a64dd908aee30341547a5413df1af71459..b8e66466184fa21fb66d968db7950e0b5669ac43 100644
--- a/drivers/char/misc.c
+++ b/drivers/char/misc.c
@@ -210,6 +210,12 @@ int misc_register(struct miscdevice *misc)
int err = 0;
bool is_dynamic = (misc->minor == MISC_DYNAMIC_MINOR);
+ if (misc->minor > MISC_DYNAMIC_MINOR) {
+ pr_err("Invalid fixed minor %d for miscdevice '%s'\n",
+ misc->minor, misc->name);
+ return -EINVAL;
+ }
+
INIT_LIST_HEAD(&misc->list);
mutex_lock(&misc_mtx);
diff --git a/include/linux/miscdevice.h b/include/linux/miscdevice.h
index 3e6deb00fc8535a7571f85489c74979e18385bad..565b88efeb23d02b7d91df1cd7df4bdcf2898224 100644
--- a/include/linux/miscdevice.h
+++ b/include/linux/miscdevice.h
@@ -71,6 +71,14 @@
#define USERIO_MINOR 240
#define VHOST_VSOCK_MINOR 241
#define RFKILL_MINOR 242
+
+/*
+ * Misc char device minor code space division related to below macro:
+ *
+ * < 255 : Fixed minor code
+ * == 255 : Indicator to request dynamic minor code
+ * > 255 : Dynamic minor code requested, 1048320 minor codes totally.
+ */
#define MISC_DYNAMIC_MINOR 255
struct miscdevice {
--
2.34.1
Powered by blists - more mailing lists