mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH master 3/6] usb: gadget: mass-storage: fix clean up of file descriptors
Date: Sat, 19 Mar 2022 12:02:43 +0100	[thread overview]
Message-ID: <20220319110246.2850396-4-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20220319110246.2850396-1-a.fatoum@pengutronix.de>

When encountering an issue during bind time, we currently close
only the last opened file descriptor, leaking others when more
than one LUN was registered. Fix this.

Fixes: 57313f83e83e ("usbgadget: add support for USB mass storage gadget")
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 drivers/usb/gadget/f_mass_storage.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)

diff --git a/drivers/usb/gadget/f_mass_storage.c b/drivers/usb/gadget/f_mass_storage.c
index 753042125d14..a49ac7803337 100644
--- a/drivers/usb/gadget/f_mass_storage.c
+++ b/drivers/usb/gadget/f_mass_storage.c
@@ -2419,7 +2419,7 @@ static int fsg_common_init(struct fsg_common *common,
 	struct usb_gadget *gadget = cdev->gadget;
 	struct file_list_entry *fentry;
 	struct fsg_buffhd *bh;
-	int nluns, i, fd = -1, rc;
+	int nluns, i, rc;
 
 	ums_count = 0;
 
@@ -2436,17 +2436,20 @@ static int fsg_common_init(struct fsg_common *common,
 	file_list_for_each_entry(ums_files, fentry) {
 		unsigned flags = O_RDWR;
 		struct stat st;
+		int fd;
 
 		if (fentry->flags) {
 			pr_err("flags not supported\n");
-			return -ENOSYS;
+			rc = -ENOSYS;
+			goto close;
 		}
 
 		fd = open(fentry->filename, flags);
 		if (fd < 0) {
 			pr_err("open('%s') failed: %pe\n",
 			       fentry->filename, ERR_PTR(fd));
-			return fd;
+			rc = fd;
+			goto close;
 		}
 
 		rc = fstat(fd, &st);
@@ -2543,7 +2546,8 @@ error_release:
 	common->state = FSG_STATE_TERMINATED;	/* The thread is dead */
 	fsg_common_release(common);
 close:
-	close(fd);
+	for (i = 0; i < ums_count; i++)
+		close(ums[i].fd);
 	return rc;
 }
 
-- 
2.30.2


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


  parent reply	other threads:[~2022-03-19 11:04 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-19 11:02 [PATCH master 0/6] usb: gadget: multi: fix bind error path Ahmad Fatoum
2022-03-19 11:02 ` [PATCH master 1/6] usb: gadget: implement and use system_partitions_get_null Ahmad Fatoum
2022-03-19 11:02 ` [PATCH master 2/6] usb: gadget: don't register UMS with empty function Ahmad Fatoum
2022-03-19 11:02 ` Ahmad Fatoum [this message]
2022-03-19 11:02 ` [PATCH master 4/6] usb: gadget: mass-storage: reference count allocations used in bthread Ahmad Fatoum
2022-03-19 11:02 ` [PATCH master 5/6] usb: gadget: multi: fix broken handling of USB function bind error Ahmad Fatoum
2022-03-19 11:02 ` [PATCH master 6/6] usb: gadget: multi: free UMS instance at multi_unbind time Ahmad Fatoum
2022-03-28  8:53 ` [PATCH master 0/6] usb: gadget: multi: fix bind error path Sascha Hauer

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=20220319110246.2850396-4-a.fatoum@pengutronix.de \
    --to=a.fatoum@pengutronix.de \
    --cc=barebox@lists.infradead.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