Преглед изворни кода

libcompat: Use a different temporary file per process on vsnprintf()

Avoid race conditions from childs after fork(2).

Closes: #655411

Reported-by: Daniel Ruoso <daniel@ruoso.com>
Guillem Jover пре 14 година
родитељ
комит
bf44310d76
2 измењених фајлова са 14 додато и 1 уклоњено
  1. 3 0
      debian/changelog
  2. 11 1
      lib/compat/vsnprintf.c

+ 3 - 0
debian/changelog

@@ -47,6 +47,9 @@ dpkg (1.16.2) UNRELEASED; urgency=low
     Closes: #192619, #427945
   * Add support for virtual output source:Package and source:Version fields.
     Closes: #653575
+  * Use a different temporary file per process on libcompat's vsnprintf()
+    function to avoid race conditions from childs after fork(3).
+    Reported by Daniel Ruoso <daniel@ruoso.com>. Closes: #655411
 
   [ Raphaël Hertzog ]
   * Update Dpkg::Shlibs to look into multiarch paths when cross-building

+ 11 - 1
lib/compat/vsnprintf.c

@@ -2,7 +2,7 @@
  * libcompat - system compatibility library
  *
  * Copyright © 1995 Ian Jackson <ian@chiark.greenend.org.uk>
- * Copyright © 2008, 2009 Guillem Jover <guillem@debian.org>
+ * Copyright © 2008-2012 Guillem Jover <guillem@debian.org>
  *
  * This is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -20,6 +20,8 @@
 
 #include <config.h>
 
+#include <sys/types.h>
+
 #include <unistd.h>
 #include <stdarg.h>
 #include <stdio.h>
@@ -28,6 +30,7 @@ int
 vsnprintf(char *buf, size_t maxsize, const char *fmt, va_list args)
 {
 	static FILE *file = NULL;
+	static pid_t file_pid;
 
 	size_t want, nr;
 	int total;
@@ -35,10 +38,17 @@ vsnprintf(char *buf, size_t maxsize, const char *fmt, va_list args)
 	if (maxsize != 0 && buf == NULL)
 		return -1;
 
+	/* Avoid race conditions from childs after a fork(2). */
+	if (file_pid > 0 && file_pid != getpid()) {
+		fclose(file);
+		file = NULL;
+	}
+
 	if (!file) {
 		file = tmpfile();
 		if (!file)
 			return -1;
+		file_pid = getpid();
 	} else {
 		if (fseek(file, 0, 0))
 			return -1;