All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] perf tools: avoid possible race condition in copyfile
@ 2015-06-08 14:50 Milos Vyletel
  2015-06-08 14:53 ` Arnaldo Carvalho de Melo
  2015-06-12  8:49 ` [tip:perf/core] perf tools: Avoid possible race condition in copyfile() tip-bot for Milos Vyletel
  0 siblings, 2 replies; 3+ messages in thread
From: Milos Vyletel @ 2015-06-08 14:50 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo, Jiri Olsa,
	Namhyung Kim, Andy Shevchenko, Milos Vyletel, Don Zickus,
	Steven Rostedt (Red Hat), open list:PERFORMANCE EVENT...

Use unique temporary files when copying to buildid dir to prevent races
in case multiple instances are trying to copy same file. This is done by

- creating template in form <path>/.<filename>.XXXXXX where the suffix is
  used by mkstemp() to create unique file
- change file mode
- copy content
- if successful link temp file to target file
- unlink temp file

At this point the only file left at target path should be the desired
one either created by us or other instance if we raced. This should also
prevent not yet fully copied files to be visible to to other perf
instances that could try to parse them.

On top of that slow_copyfile no longer needs to deal with file mode when
creating file since temporary file is already created and mode is set.

Succesfully tested by myself by running perf record, archive and reading
the data on other system and by running perf buildid-cache on perf binary
itself. I also did revert fix from 0635b0f that to exposes previously
fixed race with EEXIST and recreator test passed sucessfully.

Signed-off-by: Milos Vyletel <milos@redhat.com>
Acked-by: Ingo Molnar <mingo@kernel.org>
---
 tools/perf/util/util.c | 46 +++++++++++++++++++++++++++++++---------------
 1 file changed, 31 insertions(+), 15 deletions(-)

v2 - rebased on top of tip/perf/core

diff --git a/tools/perf/util/util.c b/tools/perf/util/util.c
index 0c264bc..edc2d63 100644
--- a/tools/perf/util/util.c
+++ b/tools/perf/util/util.c
@@ -115,20 +115,17 @@ int rm_rf(char *path)
 	return rmdir(path);
 }
 
-static int slow_copyfile(const char *from, const char *to, mode_t mode)
+static int slow_copyfile(const char *from, const char *to)
 {
 	int err = -1;
 	char *line = NULL;
 	size_t n;
 	FILE *from_fp = fopen(from, "r"), *to_fp;
-	mode_t old_umask;
 
 	if (from_fp == NULL)
 		goto out;
 
-	old_umask = umask(mode ^ 0777);
 	to_fp = fopen(to, "w");
-	umask(old_umask);
 	if (to_fp == NULL)
 		goto out_fclose_from;
 
@@ -178,29 +175,48 @@ int copyfile_mode(const char *from, const char *to, mode_t mode)
 	int fromfd, tofd;
 	struct stat st;
 	int err = -1;
+	char *tmp = NULL, *ptr = NULL;
 
 	if (stat(from, &st))
 		goto out;
 
-	if (st.st_size == 0) /* /proc? do it slowly... */
-		return slow_copyfile(from, to, mode);
-
-	fromfd = open(from, O_RDONLY);
-	if (fromfd < 0)
+	/* extra 'x' at the end is to reserve space for '.' */
+	if (asprintf(&tmp, "%s.XXXXXXx", to) < 0) {
+		tmp = NULL;
 		goto out;
+	}
+	ptr = strrchr(tmp, '/');
+	if (!ptr)
+		goto out;
+	ptr = memmove(ptr + 1, ptr, strlen(ptr) - 1);
+	*ptr = '.';
 
-	tofd = creat(to, mode);
+	tofd = mkstemp(tmp);
 	if (tofd < 0)
-		goto out_close_from;
+		goto out;
+
+	if (fchmod(tofd, mode))
+		goto out_close_to;
+
+	if (st.st_size == 0) { /* /proc? do it slowly... */
+		err = slow_copyfile(from, tmp);
+		goto out_close_to;
+	}
+
+	fromfd = open(from, O_RDONLY);
+	if (fromfd < 0)
+		goto out_close_to;
 
 	err = copyfile_offset(fromfd, 0, tofd, 0, st.st_size);
 
-	close(tofd);
-	if (err)
-		unlink(to);
-out_close_from:
 	close(fromfd);
+out_close_to:
+	close(tofd);
+	if (!err)
+		err = link(tmp, to);
+	unlink(tmp);
 out:
+	free(tmp);
 	return err;
 }
 
-- 
2.4.2


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH v2] perf tools: avoid possible race condition in copyfile
  2015-06-08 14:50 [PATCH v2] perf tools: avoid possible race condition in copyfile Milos Vyletel
@ 2015-06-08 14:53 ` Arnaldo Carvalho de Melo
  2015-06-12  8:49 ` [tip:perf/core] perf tools: Avoid possible race condition in copyfile() tip-bot for Milos Vyletel
  1 sibling, 0 replies; 3+ messages in thread
From: Arnaldo Carvalho de Melo @ 2015-06-08 14:53 UTC (permalink / raw)
  To: Milos Vyletel
  Cc: Peter Zijlstra, Ingo Molnar, Jiri Olsa, Namhyung Kim,
	Andy Shevchenko, Don Zickus, Steven Rostedt (Red Hat),
	open list:PERFORMANCE EVENT...

Em Mon, Jun 08, 2015 at 04:50:16PM +0200, Milos Vyletel escreveu:
> Use unique temporary files when copying to buildid dir to prevent races
> in case multiple instances are trying to copy same file. This is done by
> 
> - creating template in form <path>/.<filename>.XXXXXX where the suffix is
>   used by mkstemp() to create unique file
> - change file mode
> - copy content
> - if successful link temp file to target file
> - unlink temp file

Thanks, applied, will be in my next pull request to Ingo,

- Arnaldo

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [tip:perf/core] perf tools: Avoid possible race condition in copyfile()
  2015-06-08 14:50 [PATCH v2] perf tools: avoid possible race condition in copyfile Milos Vyletel
  2015-06-08 14:53 ` Arnaldo Carvalho de Melo
@ 2015-06-12  8:49 ` tip-bot for Milos Vyletel
  1 sibling, 0 replies; 3+ messages in thread
From: tip-bot for Milos Vyletel @ 2015-06-12  8:49 UTC (permalink / raw)
  To: linux-tip-commits
  Cc: dzickus, linux-kernel, acme, andriy.shevchenko, tglx, namhyung,
	rostedt, hpa, milos, jolsa, a.p.zijlstra, mingo

Commit-ID:  d7c72606d97e6f462a99b79e55b39808147d4c8b
Gitweb:     http://git.kernel.org/tip/d7c72606d97e6f462a99b79e55b39808147d4c8b
Author:     Milos Vyletel <milos@redhat.com>
AuthorDate: Mon, 8 Jun 2015 16:50:16 +0200
Committer:  Arnaldo Carvalho de Melo <acme@redhat.com>
CommitDate: Wed, 10 Jun 2015 11:51:24 -0300

perf tools: Avoid possible race condition in copyfile()

Use unique temporary files when copying to buildid dir to prevent races
in case multiple instances are trying to copy same file. This is done by

- creating template in form <path>/.<filename>.XXXXXX where the suffix is
  used by mkstemp() to create unique file
- change file mode
- copy content
- if successful link temp file to target file
- unlink temp file

At this point the only file left at target path should be the desired
one either created by us or other instance if we raced. This should also
prevent not yet fully copied files to be visible to to other perf
instances that could try to parse them.

On top of that slow_copyfile no longer needs to deal with file mode when
creating file since temporary file is already created and mode is set.

Succesfully tested by myself by running perf record, archive and reading
the data on other system and by running perf buildid-cache on perf
binary itself. I also did revert fix from 0635b0f that to exposes
previously fixed race with EEXIST and recreator test passed sucessfully.

Signed-off-by: Milos Vyletel <milos@redhat.com>
Acked-by: Ingo Molnar <mingo@kernel.org>
Cc: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Cc: Don Zickus <dzickus@redhat.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: Steven Rostedt <rostedt@goodmis.org>
Link: http://lkml.kernel.org/r/1433775018-19868-1-git-send-email-milos@redhat.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
---
 tools/perf/util/util.c | 46 +++++++++++++++++++++++++++++++---------------
 1 file changed, 31 insertions(+), 15 deletions(-)

diff --git a/tools/perf/util/util.c b/tools/perf/util/util.c
index 0c264bc..edc2d63 100644
--- a/tools/perf/util/util.c
+++ b/tools/perf/util/util.c
@@ -115,20 +115,17 @@ int rm_rf(char *path)
 	return rmdir(path);
 }
 
-static int slow_copyfile(const char *from, const char *to, mode_t mode)
+static int slow_copyfile(const char *from, const char *to)
 {
 	int err = -1;
 	char *line = NULL;
 	size_t n;
 	FILE *from_fp = fopen(from, "r"), *to_fp;
-	mode_t old_umask;
 
 	if (from_fp == NULL)
 		goto out;
 
-	old_umask = umask(mode ^ 0777);
 	to_fp = fopen(to, "w");
-	umask(old_umask);
 	if (to_fp == NULL)
 		goto out_fclose_from;
 
@@ -178,29 +175,48 @@ int copyfile_mode(const char *from, const char *to, mode_t mode)
 	int fromfd, tofd;
 	struct stat st;
 	int err = -1;
+	char *tmp = NULL, *ptr = NULL;
 
 	if (stat(from, &st))
 		goto out;
 
-	if (st.st_size == 0) /* /proc? do it slowly... */
-		return slow_copyfile(from, to, mode);
-
-	fromfd = open(from, O_RDONLY);
-	if (fromfd < 0)
+	/* extra 'x' at the end is to reserve space for '.' */
+	if (asprintf(&tmp, "%s.XXXXXXx", to) < 0) {
+		tmp = NULL;
 		goto out;
+	}
+	ptr = strrchr(tmp, '/');
+	if (!ptr)
+		goto out;
+	ptr = memmove(ptr + 1, ptr, strlen(ptr) - 1);
+	*ptr = '.';
 
-	tofd = creat(to, mode);
+	tofd = mkstemp(tmp);
 	if (tofd < 0)
-		goto out_close_from;
+		goto out;
+
+	if (fchmod(tofd, mode))
+		goto out_close_to;
+
+	if (st.st_size == 0) { /* /proc? do it slowly... */
+		err = slow_copyfile(from, tmp);
+		goto out_close_to;
+	}
+
+	fromfd = open(from, O_RDONLY);
+	if (fromfd < 0)
+		goto out_close_to;
 
 	err = copyfile_offset(fromfd, 0, tofd, 0, st.st_size);
 
-	close(tofd);
-	if (err)
-		unlink(to);
-out_close_from:
 	close(fromfd);
+out_close_to:
+	close(tofd);
+	if (!err)
+		err = link(tmp, to);
+	unlink(tmp);
 out:
+	free(tmp);
 	return err;
 }
 

^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2015-06-12  8:49 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-06-08 14:50 [PATCH v2] perf tools: avoid possible race condition in copyfile Milos Vyletel
2015-06-08 14:53 ` Arnaldo Carvalho de Melo
2015-06-12  8:49 ` [tip:perf/core] perf tools: Avoid possible race condition in copyfile() tip-bot for Milos Vyletel

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.