char: misc: Disallow registering miscdevice whose minor > MISC_DYNAMIC_MINOR

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@oss.qualcomm.com>
Link: https://lore.kernel.org/r/20250714-rfc_miscdev-v6-3-2ed949665bde@oss.qualcomm.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
This commit is contained in:
Zijun Hu
2025-08-19 12:41:17 +02:00
committed by Greg Kroah-Hartman
parent f4e47affdb
commit f5597840ac
2 changed files with 14 additions and 0 deletions
+6
View File
@@ -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);
+8
View File
@@ -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 {