Skip to contentSkip to conversation

USB: gadgetfs: Fix use-after-free in gadgetfs_kill_sb

PATCH Superseded Rafael Alejandro Diaz Cruz 2 participants 2 messages Aug 28 USB

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 USB: gadgetfs: Fix use-after-free in gadgetfs_kill_sb

When gadgetfs_fill_super() fails, it's error path calls put_dev() which drops refcount inside the_device to 0 and frees the objet. But the_device pointer is not cleared, leading to point at freed memory.

VFS will then call gadgetfs_kill_sb() after mount failure leading to put_dev() to be called on the already freed pointer.

Fix by setting the_device = NULL during error path before calling put_dev() inside gadgetfs_fill_super() so that gadgetfs_kill_sb() skips put_dev().

---
 drivers/usb/gadget/legacy/inode.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/usb/gadget/legacy/inode.c +2 -0
index d87a8ab51510..77efa984ce84 100644
@@ -2059,6 +2059,7 @@ gadgetfs_fill_super (struct super_block *sb, struct fs_context *fc)
 	rc = gadgetfs_create_file(sb, CHIP, dev, &ep0_operations);
 	if (rc) {
 		put_dev(dev);
+		the_device = NULL;
 		goto Enomem;
 	}
 
@@ -2066,6 +2067,7 @@ gadgetfs_fill_super (struct super_block *sb, struct fs_context *fc)
 	 * from binding to a controller.
 	 */
 	the_device = dev;
+	get_dev(dev);
 	rc = 0;
 	goto Done;
 
Signature · 1 line
2.43.0
#1 lore raw
Quoted 26 lines · Rafael Alejandro Diaz Cruz

When gadgetfs_fill_super() fails, it's error path calls put_dev() which drops refcount inside the_device to 0 and frees the objet. But the_device pointer is not cleared, leading to point at freed memory.

VFS will then call gadgetfs_kill_sb() after mount failure leading to put_dev() to be called on the already freed pointer.

Fix by setting the_device = NULL during error path before calling put_dev() inside gadgetfs_fill_super() so that gadgetfs_kill_sb() skips put_dev().

---
 drivers/usb/gadget/legacy/inode.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/usb/gadget/legacy/inode.c +0 -0
index d87a8ab51510..77efa984ce84 100644

drivers/usb/gadget/legacy/inode.c gadgetfs_fill_super (struct super_block *sb, struct fs_context *fc) · line 2062

Quoted 4 lines
drivers/usb/gadget/legacy/inode.c +1 -0
@@ -2059,6 +2059,7 @@ gadgetfs_fill_super (struct super_block *sb, struct fs_context *fc)
 	rc = gadgetfs_create_file(sb, CHIP, dev, &ep0_operations);
 	if (rc) {
 		put_dev(dev);
+		the_device = NULL;

This is what the description says the patch will do.

drivers/usb/gadget/legacy/inode.c gadgetfs_fill_super (struct super_block *sb, struct fs_context *fc) · line 2070

Quoted 8 lines
drivers/usb/gadget/legacy/inode.c +1 -0
 		goto Enomem;
 	}
 
@@ -2066,6 +2067,7 @@ gadgetfs_fill_super (struct super_block *sb, struct fs_context *fc)
 	 * from binding to a controller.
 	 */
 	the_device = dev;
+	get_dev(dev);

So why is this here?

Alan Stern

Quoted 7 lines
drivers/usb/gadget/legacy/inode.c +0 -0
 	rc = 0;
 	goto Done;
 
Signature · 1 line
2.43.0