All of lore.kernel.org
 help / color / mirror / Atom feed
From: Eric Sandeen <sandeen@redhat.com>
To: linux-btrfs@vger.kernel.org
Cc: Eric Sandeen <sandeen@redhat.com>
Subject: [PATCH 04/14] btrfs-progs: don't leak fd in get_fs_info
Date: Mon,  4 Mar 2013 16:39:54 -0600	[thread overview]
Message-ID: <1362436804-16766-5-git-send-email-sandeen@redhat.com> (raw)
In-Reply-To: <1362436804-16766-1-git-send-email-sandeen@redhat.com>

If we discover that a passed-in fd is not a mountpoint,
we determine whether it is a device, and issue another
open() against the device's mount point if it is mounted.

If we do so, ensure this 2nd fd gets closed before we return
so that it does not leak, by consolidating error returns.

Signed-off-by: Eric Sandeen <sandeen@redhat.com>
---
 utils.c |   21 ++++++++++++++-------
 1 files changed, 14 insertions(+), 7 deletions(-)

diff --git a/utils.c b/utils.c
index 1813dda..54d577c 100644
--- a/utils.c
+++ b/utils.c
@@ -1462,6 +1462,7 @@ int get_fs_info(int fd, char *path, struct btrfs_ioctl_fs_info_args *fi_args,
 		struct btrfs_ioctl_dev_info_args **di_ret)
 {
 	int ret = 0;
+	int fd2 = -1;
 	int ndevs = 0;
 	int i = 1;
 	struct btrfs_fs_devices *fs_devices_mnt = NULL;
@@ -1484,19 +1485,22 @@ int get_fs_info(int fd, char *path, struct btrfs_ioctl_fs_info_args *fi_args,
 		i = fs_devices_mnt->latest_devid;
 		memcpy(fi_args->fsid, fs_devices_mnt->fsid, BTRFS_FSID_SIZE);
 		close(fd);
-		fd = open_file_or_dir(mp);
-		if (fd < 0)
+		fd2 = open_file_or_dir(mp);
+		if (fd2 < 0)
 			return -errno;
+		fd = fd2;
 	} else if (ret) {
 		return -errno;
 	}
 
 	if (!fi_args->num_devices)
-		return 0;
+		goto out;
 
 	di_args = *di_ret = malloc(fi_args->num_devices * sizeof(*di_args));
-	if (!di_args)
-		return -errno;
+	if (!di_args) {
+		ret = -errno;
+		goto out;
+	}
 
 	for (; i <= fi_args->max_id; ++i) {
 		BUG_ON(ndevs >= fi_args->num_devices);
@@ -1504,13 +1508,16 @@ int get_fs_info(int fd, char *path, struct btrfs_ioctl_fs_info_args *fi_args,
 		if (ret == -ENODEV)
 			continue;
 		if (ret)
-			return ret;
+			goto out;
 		ndevs++;
 	}
 
 	BUG_ON(ndevs == 0);
 
-	return 0;
+out:
+	if (fd2 != -1)
+		close(fd2);
+	return ret;
 }
 
 #define isoctal(c)	(((c) & ~7) == '0')
-- 
1.7.1


  parent reply	other threads:[~2013-03-04 21:40 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-03-04 22:39 [PATCH 00/14] btrfs-progs: more Coverity cleanups Eric Sandeen
2013-03-04 22:02 ` Zach Brown
2013-03-04 22:39 ` [PATCH 01/14] btrfs-progs: close fd on cmd_subvol_list return Eric Sandeen
2013-03-04 22:39 ` [PATCH 02/14] btrfs-progs: close fd on do_convert error returns Eric Sandeen
2013-03-04 22:39 ` [PATCH 03/14] btrfs-progs: free resources on do_rollback " Eric Sandeen
2013-03-04 22:39 ` Eric Sandeen [this message]
2013-03-05 23:41   ` [PATCH 04/14] btrfs-progs: don't leak fd in get_fs_info Eric Sandeen
2013-03-08 15:27     ` Eric Sandeen
2013-03-04 22:39 ` [PATCH 05/14] btrfs-progs: free allocated metadump structure on restore failure Eric Sandeen
2013-03-04 22:39 ` [PATCH 06/14] btrfs-progs: check for null string in parse_size Eric Sandeen
2013-03-04 22:39 ` [PATCH 07/14] btrfs-progs: tidy up cmd_snapshot() whitespace & returns Eric Sandeen
2013-03-04 22:39 ` [PATCH 08/14] btrfs-progs: Free resources when returning error from cmd_snapshot() Eric Sandeen
2013-03-04 22:39 ` [PATCH 09/14] btrfs-progs: tidy up cmd_subvol_create() whitespace & returns Eric Sandeen
2013-03-04 22:40 ` [PATCH 10/14] btrfs-progs: Free resources when returning error from cmd_subvol_create() Eric Sandeen
2013-03-04 22:40 ` [PATCH 11/14] btrfs-progs: check return of posix_fadvise Eric Sandeen
2013-03-04 22:40 ` [PATCH 12/14] btrfs-progs: Issue warnings if ioctls fail in sigint handlers Eric Sandeen
2013-03-04 21:57   ` Zach Brown
2013-03-04 22:34     ` Eric Sandeen
2013-03-04 22:35   ` [PATCH 12/14 V2] " Eric Sandeen
2013-03-04 22:40 ` [PATCH 13/14] btrfs-progs: better option/error handling for btrfs-vol Eric Sandeen
2013-03-04 22:40 ` [PATCH 14/14] btrfs-progs: Error handling in scrub_progress_cycle() thread Eric Sandeen
2013-03-04 22:00   ` Zach Brown
2013-03-04 22:34     ` Eric Sandeen
2013-03-04 22:45   ` [PATCH 14/14 V2] " Eric Sandeen
2013-03-04 23:12     ` Zach Brown
2013-03-04 22:49 ` [PATCH 15/14] btrfs-progs: fix scrub error return from pthread_mutex_lock Eric Sandeen
2013-03-10 15:19 ` [PATCH 00/14] btrfs-progs: more Coverity cleanups David Sterba

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=1362436804-16766-5-git-send-email-sandeen@redhat.com \
    --to=sandeen@redhat.com \
    --cc=linux-btrfs@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.