From: John Keeping <john@metanate.com>
To: linux-usb@vger.kernel.org
Cc: Felipe Balbi <balbi@kernel.org>,
Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
linux-kernel@vger.kernel.org, John Keeping <john@metanate.com>
Subject: [PATCH 1/6] USB: gadget: f_hid: move chardev setup to module init
Date: Thu, 24 Oct 2019 17:45:33 +0100 [thread overview]
Message-ID: <20191024164538.3161474-2-john@metanate.com> (raw)
In-Reply-To: <20191024164538.3161474-1-john@metanate.com>
Lifetime of the objects in the HID gadget is currently tied to the USB
function, which makes it very easy to trigger a use-after-free by
holding a file descriptor on the HID device after deleting the gadget.
As a first step towards fixing this, move the character device
allocation to module initialisation so that we don't have to worry about
when the allocation can be released after closing all of the open
handles on the device.
Signed-off-by: John Keeping <john@metanate.com>
---
drivers/usb/gadget/function/f_hid.c | 27 +++++++--------------------
drivers/usb/gadget/function/u_hid.h | 3 ---
2 files changed, 7 insertions(+), 23 deletions(-)
diff --git a/drivers/usb/gadget/function/f_hid.c b/drivers/usb/gadget/function/f_hid.c
index f3816a5c861e..e9e45f9eae80 100644
--- a/drivers/usb/gadget/function/f_hid.c
+++ b/drivers/usb/gadget/function/f_hid.c
@@ -1006,11 +1006,7 @@ static void hidg_free_inst(struct usb_function_instance *f)
opts = container_of(f, struct f_hid_opts, func_inst);
mutex_lock(&hidg_ida_lock);
-
hidg_put_minor(opts->minor);
- if (ida_is_empty(&hidg_ida))
- ghid_cleanup();
-
mutex_unlock(&hidg_ida_lock);
if (opts->report_desc_alloc)
@@ -1023,7 +1019,6 @@ static struct usb_function_instance *hidg_alloc_inst(void)
{
struct f_hid_opts *opts;
struct usb_function_instance *ret;
- int status = 0;
opts = kzalloc(sizeof(*opts), GFP_KERNEL);
if (!opts)
@@ -1034,21 +1029,10 @@ static struct usb_function_instance *hidg_alloc_inst(void)
mutex_lock(&hidg_ida_lock);
- if (ida_is_empty(&hidg_ida)) {
- status = ghid_setup(NULL, HIDG_MINORS);
- if (status) {
- ret = ERR_PTR(status);
- kfree(opts);
- goto unlock;
- }
- }
-
opts->minor = hidg_get_minor();
if (opts->minor < 0) {
ret = ERR_PTR(opts->minor);
kfree(opts);
- if (ida_is_empty(&hidg_ida))
- ghid_cleanup();
goto unlock;
}
config_group_init_type_name(&opts->func_inst.group, "", &hid_func_type);
@@ -1133,7 +1117,7 @@ DECLARE_USB_FUNCTION_INIT(hid, hidg_alloc_inst, hidg_alloc);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Fabien Chouteau");
-int ghid_setup(struct usb_gadget *g, int count)
+static int ghid_setup(void)
{
int status;
dev_t dev;
@@ -1145,7 +1129,7 @@ int ghid_setup(struct usb_gadget *g, int count)
return status;
}
- status = alloc_chrdev_region(&dev, 0, count, "hidg");
+ status = alloc_chrdev_region(&dev, 0, HIDG_MINORS, "hidg");
if (status) {
class_destroy(hidg_class);
hidg_class = NULL;
@@ -1153,12 +1137,12 @@ int ghid_setup(struct usb_gadget *g, int count)
}
major = MAJOR(dev);
- minors = count;
+ minors = HIDG_MINORS;
return 0;
}
-void ghid_cleanup(void)
+static void ghid_cleanup(void)
{
if (major) {
unregister_chrdev_region(MKDEV(major, 0), minors);
@@ -1168,3 +1152,6 @@ void ghid_cleanup(void)
class_destroy(hidg_class);
hidg_class = NULL;
}
+
+module_init(ghid_setup);
+module_exit(ghid_cleanup);
diff --git a/drivers/usb/gadget/function/u_hid.h b/drivers/usb/gadget/function/u_hid.h
index 1594bfa312eb..d52acc977c7e 100644
--- a/drivers/usb/gadget/function/u_hid.h
+++ b/drivers/usb/gadget/function/u_hid.h
@@ -33,7 +33,4 @@ struct f_hid_opts {
int refcnt;
};
-int ghid_setup(struct usb_gadget *g, int count);
-void ghid_cleanup(void);
-
#endif /* U_HID_H */
--
2.23.0
next prev parent reply other threads:[~2019-10-24 17:05 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-10-24 16:45 [PATCH 0/6] USB: gadget: f_hid: fix lifetime issues John Keeping
2019-10-24 16:45 ` John Keeping [this message]
2019-10-27 17:13 ` [PATCH 1/6] USB: gadget: f_hid: move chardev setup to module init kbuild test robot
2019-10-24 16:45 ` [PATCH 2/6] USB: gadget: f_hid: switch to IDR for tracking minors John Keeping
2019-10-24 16:45 ` [PATCH 3/6] USB: gadget: f_hid: find f_hidg by IDR lookup on open John Keeping
2019-10-24 16:45 ` [PATCH 4/6] USB: gadget: f_hid: decouple cdev from f_hidg lifetime John Keeping
2019-10-24 16:45 ` [PATCH 5/6] USB: gadget: f_hid: refcount f_hidg structure John Keeping
2019-10-24 16:45 ` [PATCH 6/6] USB: gadget: f_hid: return ENODEV from read/write after deletion John Keeping
2019-10-28 11:42 ` [PATCH v2 0/6] USB: gadget: f_hid: fix lifetime issues John Keeping
2019-10-28 11:42 ` [PATCH v2 1/6] USB: gadget: f_hid: move chardev setup to module init John Keeping
2019-10-28 11:42 ` [PATCH v2 2/6] USB: gadget: f_hid: switch to IDR for tracking minors John Keeping
2019-10-28 11:42 ` [PATCH v2 3/6] USB: gadget: f_hid: find f_hidg by IDR lookup on open John Keeping
2019-10-28 11:42 ` [PATCH v2 4/6] USB: gadget: f_hid: decouple cdev from f_hidg lifetime John Keeping
2019-10-28 11:42 ` [PATCH v2 5/6] USB: gadget: f_hid: refcount f_hidg structure John Keeping
2019-10-28 11:42 ` [PATCH v2 6/6] USB: gadget: f_hid: return ENODEV from read/write after deletion John Keeping
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=20191024164538.3161474-2-john@metanate.com \
--to=john@metanate.com \
--cc=balbi@kernel.org \
--cc=gregkh@linuxfoundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-usb@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
all inboxes | Powered by JetHome®