[PATCH] Fix for Coverity finding in tfork

Ralph Böhme slow at samba.org
Tue Apr 25 15:53:23 UTC 2017


Hi!

Volker pointed me at a Coverity finding in tfork. Here's the (hopefully correct)
patch.

Please review and push if happy. Thanks!

-slow
-------------- next part --------------
From c2ab090a6eef84d24790d8c9c56d822a72ff8900 Mon Sep 17 00:00:00 2001
From: Ralph Boehme <slow at samba.org>
Date: Tue, 25 Apr 2017 17:47:57 +0200
Subject: [PATCH] lib/util: fix a Coverity finding in tfork

If dup2() fails, fd is -1 and is later used in sys_write().

Signed-off-by: Ralph Boehme <slow at samba.org>
---
 lib/util/tfork.c | 23 ++++++++++++++++++-----
 1 file changed, 18 insertions(+), 5 deletions(-)

diff --git a/lib/util/tfork.c b/lib/util/tfork.c
index 27b6cc0..37c00e6 100644
--- a/lib/util/tfork.c
+++ b/lib/util/tfork.c
@@ -131,11 +131,24 @@ static pid_t level2_fork_and_wait(int child_ready_fd)
 	 * We're going to stay around until child3 exits, so lets close all fds
 	 * other then the pipe fd we may have inherited from the caller.
 	 */
-	fd = dup2(tfork_global->status_pipe[1], 0);
-	if (fd == -1) {
-		status = errno;
-		kill(tfork_global->level3_pid, SIGKILL);
-		wait = false;
+	while (true) {
+		fd = dup2(tfork_global->status_pipe[1], 0);
+		if (fd == -1) {
+			if (errno == EINTR) {
+				continue;
+			}
+			status = errno;
+
+			kill(tfork_global->level3_pid, SIGKILL);
+
+			written = sys_write(tfork_global->status_pipe[1],
+					    &status, sizeof(status));
+			if (written != sizeof(status)) {
+				abort();
+			}
+			_exit(0);
+		}
+		break;
 	}
 	closefrom(1);
 
-- 
2.9.3



More information about the samba-technical mailing list