]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
usb: gadget: f_printer: take kref only for successful open
authorXu Rao <raoxu@uniontech.com>
Fri, 26 Jun 2026 06:46:17 +0000 (14:46 +0800)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Wed, 8 Jul 2026 11:29:57 +0000 (13:29 +0200)
printer_open() returns -EBUSY when the character device is already
open, but it increments dev->kref regardless of the return value. VFS
does not call ->release() for a failed open, so every rejected second
open permanently leaks one reference.

Move kref_get() into the successful-open branch.

Fixes: e8d5f92b8d30 ("usb: gadget: function: printer: fix use-after-free in __lock_acquire")
Cc: stable <stable@kernel.org>
Signed-off-by: Xu Rao <raoxu@uniontech.com>
Link: https://patch.msgid.link/80295742B820DA9B+20260626064617.4090626-1-raoxu@uniontech.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
drivers/usb/gadget/function/f_printer.c

index e4f7828ae75df7c6dd0a9c2b0ff620460e4485cd..837f753d0cae594b66fbcd51b3ffcc291221c952 100644 (file)
@@ -363,12 +363,11 @@ printer_open(struct inode *inode, struct file *fd)
                ret = 0;
                /* Change the printer status to show that it's on-line. */
                dev->printer_status |= PRINTER_SELECTED;
+               kref_get(&dev->kref);
        }
 
        spin_unlock_irqrestore(&dev->lock, flags);
 
-       kref_get(&dev->kref);
-
        return ret;
 }