cluster-devel.redhat.com archive mirror
 help / color / mirror / Atom feed
From: Andrew Price <anprice@redhat.com>
To: cluster-devel.redhat.com
Subject: [Cluster-devel] [PATCH 1/2] mkfs.gfs2: Avoid a rename race when checking file contents
Date: Mon,  9 Jul 2012 17:06:51 +0100	[thread overview]
Message-ID: <1341850012-28904-1-git-send-email-anprice@redhat.com> (raw)

Currently there is a slight chance that mkfs.gfs2 could report the
incorrect contents of the target if another file is renamed into place.
This is because we pass the target name to the file command and file
opens it again. This patch tries to avoid that condition by using the
/proc/$pid/fd/$device_fd symlink, which changes when the open file is
renamed, instead.

Signed-off-by: Andrew Price <anprice@redhat.com>
---
 gfs2/mkfs/main_mkfs.c |   11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/gfs2/mkfs/main_mkfs.c b/gfs2/mkfs/main_mkfs.c
index 957b144..2d529d7 100644
--- a/gfs2/mkfs/main_mkfs.c
+++ b/gfs2/mkfs/main_mkfs.c
@@ -426,7 +426,7 @@ static void check_dev_content(const char *devname)
 	char content[1024] = { 0, };
 	char * args[] = {
 		(char *)"/usr/bin/file",
-		(char *)"-bs",
+		(char *)"-bsL",
 		(char *)devname,
 		NULL };
 	int p[2] = {-1, -1};
@@ -557,6 +557,7 @@ void main_mkfs(int argc, char *argv[])
 	int rgsize_specified = 0;
 	unsigned char uuid[16];
 	char *absname = NULL;
+	char *fdpath = NULL;
 	int islnk = 0;
 
 	memset(sdp, 0, sizeof(struct gfs2_sbd));
@@ -587,13 +588,19 @@ void main_mkfs(int argc, char *argv[])
 		exit(EXIT_FAILURE);
 	}
 
+	if (asprintf(&fdpath, "/proc/%d/fd/%d", getpid(), sdp->device_fd) < 0) {
+		perror(_("Failed to build string"));
+		exit(EXIT_FAILURE);
+	}
+
 	if (!sdp->override) {
 		islnk = is_symlink(sdp->device_name, &absname);
 		printf(_("This will destroy any data on %s.\n"), islnk ? absname : sdp->device_name);
-		check_dev_content(islnk ? absname : sdp->device_name);
 		free(absname);
+		check_dev_content(fdpath);
 		are_you_sure();
 	}
+	free(fdpath);
 
 	if (sdp->bsize == -1) {
 		if (S_ISREG(sdp->dinfo.stat.st_mode))
-- 
1.7.10.4



             reply	other threads:[~2012-07-09 16:06 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-07-09 16:06 Andrew Price [this message]
2012-07-09 16:06 ` [Cluster-devel] [PATCH 2/2] fsck.gfs2: Fix buffer overflow in get_lockproto_table Andrew Price
2012-07-12 16:08   ` Bob Peterson

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=1341850012-28904-1-git-send-email-anprice@redhat.com \
    --to=anprice@redhat.com \
    /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;
as well as URLs for NNTP newsgroup(s).