 USB: gadget: Fix the memory leak in raw_gadget driver · gregkh/linux@de77000 · GitHub Skip to content Navigation Menu Toggle navigation Sign in Appearance settings Platform AI CODE CREATION GitHub Copilot Write better code with AI GitHub Spark Build and deploy intelligent apps GitHub Models Manage and compare prompts MCP Registry New Integrate external tools DEVELOPER WORKFLOWS Actions Automate any workflow Codespaces Instant dev environments Issues Plan and track work Code Review Manage code changes APPLICATION SECURITY GitHub Advanced Security Find and fix vulnerabilities Code security Secure your code as you build Secret protection  EXPLORE Why GitHub Documentation Blog Changelog Marketplace View all features Solutions BY COMPANY SIZE Enterprises Small and medium teams Startups Nonprofits BY USE CASE App Modernization DevSecOps DevOps CI/CD View all use cases BY INDUSTRY Healthcare Financial services Manufacturing Government View all industries View all solutions Resources EXPLORE BY TOPIC AI Software Development DevOps Security View all topics EXPLORE BY TYPE Customer stories Events &amp; webinars Ebooks &amp; reports Business insights GitHub Skills SUPPORT &amp; SERVICES Documentation Customer support Community forum Trust center Partners View all resources Open Source COMMUNITY GitHub Sponsors Fund open source developers PROGRAMS Security Lab Maintainer Community Accelerator GitHub Stars Archive Program REPOSITORIES Topics Trending Collections Enterprise ENTERPRISE SOLUTIONS Enterprise platform AI-powered developer platform AVAILABLE ADD-ONS GitHub Advanced Security Enterprise-grade security features Copilot for Business Enterprise-grade AI features Premium Support Enterprise-grade 24/7 support Pricing Search or jump to... Search code, repositories, users, issues, pull requests... --> Search Clear Search syntax tips Provide feedback --> We read every piece of feedback, and take your input very seriously.  Cancel Submit feedback Saved searches Use saved searches to filter your results more quickly --> Name Query To see all available qualifiers, see our documentation . Cancel Create saved search Sign in Sign up Appearance settings Resetting focus You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session. Dismiss alert {{ message }} gregkh / linux Public Notifications You must be signed in to change notification settings Fork 686 Star 644 Code Actions Security and quality 0 Insights Additional navigation options Code Actions Security and quality Insights Commit de77000 Browse files Browse files Zqiang authored and gregkh committed USB: gadget: Fix the memory leak in raw_gadget driver commit 83e30f2 upstream. Currently, increasing raw_dev-&gt;count happens before invoke the raw_queue_event(), if the raw_queue_event() return error, invoke raw_release() will not trigger the dev_free() to be called. [ 268.905865][ T5067] raw-gadget.0 gadget.0: failed to queue event [ 268.912053][ T5067] udc dummy_udc.0: failed to start USB Raw Gadget: -12 [ 268.918885][ T5067] raw-gadget.0: probe of gadget.0 failed with error -12 [ 268.925956][ T5067] UDC core: USB Raw Gadget: couldn't find an available UDC or it's busy [ 268.934657][ T5067] misc raw-gadget: fail, usb_gadget_register_driver returned -16 BUG: memory leak [&lt;ffffffff8154bf94&gt;] kmalloc_trace+0x24/0x90 mm/slab_common.c:1076 [&lt;ffffffff8347eb55&gt;] kmalloc include/linux/slab.h:582 [inline] [&lt;ffffffff8347eb55&gt;] kzalloc include/linux/slab.h:703 [inline] [&lt;ffffffff8347eb55&gt;] dev_new drivers/usb/gadget/legacy/raw_gadget.c:191 [inline] [&lt;ffffffff8347eb55&gt;] raw_open+0x45/0x110 drivers/usb/gadget/legacy/raw_gadget.c:385 [&lt;ffffffff827d1d09&gt;] misc_open+0x1a9/0x1f0 drivers/char/misc.c:165 [&lt;ffffffff8154bf94&gt;] kmalloc_trace+0x24/0x90 mm/slab_common.c:1076 [&lt;ffffffff8347cd2f&gt;] kmalloc include/linux/slab.h:582 [inline] [&lt;ffffffff8347cd2f&gt;] raw_ioctl_init+0xdf/0x410 drivers/usb/gadget/legacy/raw_gadget.c:460 [&lt;ffffffff8347dfe9&gt;] raw_ioctl+0x5f9/0x1120 drivers/usb/gadget/legacy/raw_gadget.c:1250 [&lt;ffffffff81685173&gt;] vfs_ioctl fs/ioctl.c:51 [inline] [&lt;ffffffff8154bf94&gt;] kmalloc_trace+0x24/0x90 mm/slab_common.c:1076 [&lt;ffffffff833ecc6a&gt;] kmalloc include/linux/slab.h:582 [inline] [&lt;ffffffff833ecc6a&gt;] kzalloc include/linux/slab.h:703 [inline] [&lt;ffffffff833ecc6a&gt;] dummy_alloc_request+0x5a/0xe0 drivers/usb/gadget/udc/dummy_hcd.c:665 [&lt;ffffffff833e9132&gt;] usb_ep_alloc_request+0x22/0xd0 drivers/usb/gadget/udc/core.c:196 [&lt;ffffffff8347f13d&gt;] gadget_bind+0x6d/0x370 drivers/usb/gadget/legacy/raw_gadget.c:292 This commit therefore invoke kref_get() under the condition that raw_queue_event() return success. Reported-by: syzbot+feb045d335c1fdde5bf7@syzkaller.appspotmail.com Cc: stable &lt;stable@kernel.org&gt; Closes: https://syzkaller.appspot.com/bug?extid=feb045d335c1fdde5bf7 Signed-off-by: Zqiang &lt;qiang.zhang1211@gmail.com&gt; Reviewed-by: Andrey Konovalov &lt;andreyknvl@gmail.com&gt; Tested-by: Andrey Konovalov &lt;andreyknvl@gmail.com&gt; Link: https://lore.kernel.org/r/20230714074011.20989-1-qiang.zhang1211@gmail.com Signed-off-by: Greg Kroah-Hartman &lt;gregkh@linuxfoundation.org&gt; 1 parent 0f23a9e commit de77000 Copy full SHA for de77000 1 file changed + 6 - 4 Lines changed: 6 additions &amp; 4 deletions File tree Expand file tree Collapse file tree Open diff view settings Filter options drivers/usb/gadget/legacy raw_gadget.c Expand file tree Collapse file tree Open diff view settings Collapse file ‎ drivers/usb/gadget/legacy/raw_gadget.c ‎ Copy file name to clipboard Expand all lines: drivers/usb/gadget/legacy/raw_gadget.c + 6 - 4 Lines changed: 6 additions &amp; 4 deletions Original file line number Diff line number Diff line change @@ -310,13 +310,15 @@ static int gadget_bind(struct usb_gadget *gadget, 310 310 dev -&gt; eps_num = i ; 311 311 spin_unlock_irqrestore ( &amp; dev -&gt; lock , flags ); 312 312 313 - /* Matches kref_put() in gadget_unbind(). */ 314 - kref_get ( &amp; dev -&gt; count ); 315 - 316 313 ret = raw_queue_event ( dev , USB_RAW_EVENT_CONNECT , 0 , NULL ); 317 - if ( ret &lt; 0 ) 314 + if ( ret &lt; 0 ) { 318 315 dev_err ( &amp; gadget -&gt; dev , &quot;failed to queue event\n&quot; ); 316 + set_gadget_data ( gadget , NULL ); 317 + return ret ; 318 + } 319 319 320 + /* Matches kref_put() in gadget_unbind(). */ 321 + kref_get ( &amp; dev -&gt; count ); 320 322 return ret ; 321 323 } 322 324 0 commit comments Comments 0 ( 0 ) Footer &copy; 2026 GitHub,&nbsp;Inc. Footer navigation Terms Privacy Security Status Community Docs Contact Manage cookies Do not share my personal information You can’t perform that action at this time. 