Blame SOURCES/0008-Make-sure-all-open-file-descriptors-are-closed-on-ex.patch

df4638
From d2da34706f140101c34f6a9806c258411806a939 Mon Sep 17 00:00:00 2001
df4638
From: Pavel Zhukov <pzhukov@redhat.com>
df4638
Date: Thu, 21 Feb 2019 10:27:18 +0100
df4638
Subject: [PATCH 08/26] Make sure all open file descriptors are closed-on-exec
df4638
 for SELinux
df4638
Cc: pzhukov@redhat.com
df4638
df4638
ISC-bug: #19148
df4638
---
df4638
 client/clparse.c  |  4 ++--
df4638
 client/dhclient.c | 28 ++++++++++++++--------------
df4638
 common/bpf.c      |  2 +-
df4638
 common/dlpi.c     |  2 +-
df4638
 common/nit.c      |  2 +-
df4638
 common/resolv.c   |  2 +-
df4638
 common/upf.c      |  2 +-
df4638
 omapip/trace.c    |  6 +++---
df4638
 relay/dhcrelay.c  | 10 +++++-----
df4638
 server/confpars.c |  2 +-
df4638
 server/db.c       |  4 ++--
df4638
 server/dhcpd.c    | 14 +++++++-------
df4638
 server/ldap.c     |  2 +-
df4638
 13 files changed, 40 insertions(+), 40 deletions(-)
df4638
df4638
diff --git a/client/clparse.c b/client/clparse.c
df4638
index 39b95a0..44387ed 100644
df4638
--- a/client/clparse.c
df4638
+++ b/client/clparse.c
df4638
@@ -288,7 +288,7 @@ int read_client_conf_file (const char *name, struct interface_info *ip,
df4638
 	int token;
df4638
 	isc_result_t status;
df4638
 
df4638
-	if ((file = open (name, O_RDONLY)) < 0)
df4638
+	if ((file = open (name, O_RDONLY | O_CLOEXEC)) < 0)
df4638
 		return uerr2isc (errno);
df4638
 
df4638
 	cfile = NULL;
df4638
@@ -364,7 +364,7 @@ void read_client_leases ()
df4638
 
df4638
 	/* Open the lease file.   If we can't open it, just return -
df4638
 	   we can safely trust the server to remember our state. */
df4638
-	if ((file = open (path_dhclient_db, O_RDONLY)) < 0)
df4638
+	if ((file = open (path_dhclient_db, O_RDONLY | O_CLOEXEC)) < 0)
df4638
 		return;
df4638
 
df4638
 	cfile = NULL;
df4638
diff --git a/client/dhclient.c b/client/dhclient.c
df4638
index 2a2e9e6..a86ab9e 100644
df4638
--- a/client/dhclient.c
df4638
+++ b/client/dhclient.c
df4638
@@ -273,11 +273,11 @@ main(int argc, char **argv) {
df4638
 	/* Make sure that file descriptors 0 (stdin), 1, (stdout), and
df4638
 	   2 (stderr) are open. To do this, we assume that when we
df4638
 	   open a file the lowest available file descriptor is used. */
df4638
-	fd = open("/dev/null", O_RDWR);
df4638
+	fd = open("/dev/null", O_RDWR | O_CLOEXEC);
df4638
 	if (fd == 0)
df4638
-		fd = open("/dev/null", O_RDWR);
df4638
+		fd = open("/dev/null", O_RDWR | O_CLOEXEC);
df4638
 	if (fd == 1)
df4638
-		fd = open("/dev/null", O_RDWR);
df4638
+		fd = open("/dev/null", O_RDWR | O_CLOEXEC);
df4638
 	if (fd == 2)
df4638
 		log_perror = 0; /* No sense logging to /dev/null. */
df4638
 	else if (fd != -1)
df4638
@@ -765,7 +765,7 @@ main(int argc, char **argv) {
df4638
 		long temp;
df4638
 		int e;
df4638
 
df4638
-		if ((pidfd = fopen(path_dhclient_pid, "r")) != NULL) {
df4638
+		if ((pidfd = fopen(path_dhclient_pid, "re")) != NULL) {
df4638
 			e = fscanf(pidfd, "%ld\n", &temp);
df4638
 			oldpid = (pid_t)temp;
df4638
 
df4638
@@ -820,7 +820,7 @@ main(int argc, char **argv) {
df4638
 					strncpy(new_path_dhclient_pid, path_dhclient_pid, pfx);
df4638
 					sprintf(new_path_dhclient_pid + pfx, "-%s.pid", ip->name);
df4638
 
df4638
-					if ((pidfd = fopen(new_path_dhclient_pid, "r")) != NULL) {
df4638
+					if ((pidfd = fopen(new_path_dhclient_pid, "re")) != NULL) {
df4638
 						e = fscanf(pidfd, "%ld\n", &temp);
df4638
 						oldpid = (pid_t)temp;
df4638
 
df4638
@@ -845,7 +845,7 @@ main(int argc, char **argv) {
df4638
 		int dhc_running = 0;
df4638
 		char procfn[256] = "";
df4638
 
df4638
-		if ((pidfp = fopen(path_dhclient_pid, "r")) != NULL) {
df4638
+		if ((pidfp = fopen(path_dhclient_pid, "re")) != NULL) {
df4638
 			if ((fscanf(pidfp, "%ld", &temp)==1) && ((dhcpid=(pid_t)temp) > 0)) {
df4638
 				snprintf(procfn,256,"/proc/%u",dhcpid);
df4638
 				dhc_running = (access(procfn, F_OK) == 0);
df4638
@@ -3808,7 +3808,7 @@ void rewrite_client_leases ()
df4638
 
df4638
 	if (leaseFile != NULL)
df4638
 		fclose (leaseFile);
df4638
-	leaseFile = fopen (path_dhclient_db, "w");
df4638
+	leaseFile = fopen (path_dhclient_db, "we");
df4638
 	if (leaseFile == NULL) {
df4638
 		log_error ("can't create %s: %m", path_dhclient_db);
df4638
 		return;
df4638
@@ -4003,7 +4003,7 @@ write_duid(struct data_string *duid)
df4638
 		return DHCP_R_INVALIDARG;
df4638
 
df4638
 	if (leaseFile == NULL) {	/* XXX? */
df4638
-		leaseFile = fopen(path_dhclient_db, "w");
df4638
+		leaseFile = fopen(path_dhclient_db, "we");
df4638
 		if (leaseFile == NULL) {
df4638
 			log_error("can't create %s: %m", path_dhclient_db);
df4638
 			return ISC_R_IOERROR;
df4638
@@ -4207,7 +4207,7 @@ int write_client_lease (client, lease, rewrite, makesure)
df4638
 		return 1;
df4638
 
df4638
 	if (leaseFile == NULL) {	/* XXX */
df4638
-		leaseFile = fopen (path_dhclient_db, "w");
df4638
+		leaseFile = fopen (path_dhclient_db, "we");
df4638
 		if (leaseFile == NULL) {
df4638
 			log_error ("can't create %s: %m", path_dhclient_db);
df4638
 			return 0;
df4638
@@ -4786,9 +4786,9 @@ void detach ()
df4638
 	(void) close(2);
df4638
 
df4638
 	/* Reopen them on /dev/null. */
df4638
-	(void) open("/dev/null", O_RDWR);
df4638
-	(void) open("/dev/null", O_RDWR);
df4638
-	(void) open("/dev/null", O_RDWR);
df4638
+	(void) open("/dev/null", O_RDWR | O_CLOEXEC);
df4638
+	(void) open("/dev/null", O_RDWR | O_CLOEXEC);
df4638
+	(void) open("/dev/null", O_RDWR | O_CLOEXEC);
df4638
 
df4638
 	write_client_pid_file ();
df4638
 
df4638
@@ -4806,14 +4806,14 @@ void write_client_pid_file ()
df4638
 		return;
df4638
 	}
df4638
 
df4638
-	pfdesc = open (path_dhclient_pid, O_CREAT | O_TRUNC | O_WRONLY, 0644);
df4638
+	pfdesc = open (path_dhclient_pid, O_CREAT | O_TRUNC | O_WRONLY | O_CLOEXEC, 0644);
df4638
 
df4638
 	if (pfdesc < 0) {
df4638
 		log_error ("Can't create %s: %m", path_dhclient_pid);
df4638
 		return;
df4638
 	}
df4638
 
df4638
-	pf = fdopen (pfdesc, "w");
df4638
+	pf = fdopen (pfdesc, "we");
df4638
 	if (!pf) {
df4638
 		close(pfdesc);
df4638
 		log_error ("Can't fdopen %s: %m", path_dhclient_pid);
df4638
diff --git a/common/bpf.c b/common/bpf.c
df4638
index 16076fe..67b6d64 100644
df4638
--- a/common/bpf.c
df4638
+++ b/common/bpf.c
df4638
@@ -94,7 +94,7 @@ int if_register_bpf (info)
df4638
 	for (b = 0; 1; b++) {
df4638
 		/* %Audit% 31 bytes max. %2004.06.17,Safe% */
df4638
 		sprintf(filename, BPF_FORMAT, b);
df4638
-		sock = open (filename, O_RDWR, 0);
df4638
+		sock = open (filename, O_RDWR | O_CLOEXEC, 0);
df4638
 		if (sock < 0) {
df4638
 			if (errno == EBUSY) {
df4638
 				continue;
df4638
diff --git a/common/dlpi.c b/common/dlpi.c
df4638
index 3990bf1..a941258 100644
df4638
--- a/common/dlpi.c
df4638
+++ b/common/dlpi.c
df4638
@@ -817,7 +817,7 @@ dlpiopen(const char *ifname) {
df4638
 	}
df4638
 	*dp = '\0';
df4638
 	
df4638
-	return open (devname, O_RDWR, 0);
df4638
+	return open (devname, O_RDWR | O_CLOEXEC, 0);
df4638
 }
df4638
 
df4638
 /*
df4638
diff --git a/common/nit.c b/common/nit.c
df4638
index d822c15..a9132bc 100644
df4638
--- a/common/nit.c
df4638
+++ b/common/nit.c
df4638
@@ -75,7 +75,7 @@ int if_register_nit (info)
df4638
 	struct strioctl sio;
df4638
 
df4638
 	/* Open a NIT device */
df4638
-	sock = open ("/dev/nit", O_RDWR);
df4638
+	sock = open ("/dev/nit", O_RDWR | O_CLOEXEC);
df4638
 	if (sock < 0)
df4638
 		log_fatal ("Can't open NIT device for %s: %m", info -> name);
df4638
 
df4638
diff --git a/common/resolv.c b/common/resolv.c
df4638
index a01f520..b209e3f 100644
df4638
--- a/common/resolv.c
df4638
+++ b/common/resolv.c
df4638
@@ -43,7 +43,7 @@ void read_resolv_conf (parse_time)
df4638
 	struct domain_search_list *dp, *dl, *nd;
df4638
 	isc_result_t status;
df4638
 
df4638
-	if ((file = open (path_resolv_conf, O_RDONLY)) < 0) {
df4638
+	if ((file = open (path_resolv_conf, O_RDONLY | O_CLOEXEC)) < 0) {
df4638
 		log_error ("Can't open %s: %m", path_resolv_conf);
df4638
 		return;
df4638
 	}
df4638
diff --git a/common/upf.c b/common/upf.c
df4638
index 9785879..e0a524f 100644
df4638
--- a/common/upf.c
df4638
+++ b/common/upf.c
df4638
@@ -71,7 +71,7 @@ int if_register_upf (info)
df4638
 		/* %Audit% Cannot exceed 36 bytes. %2004.06.17,Safe% */
df4638
 		sprintf(filename, "/dev/pf/pfilt%d", b);
df4638
 
df4638
-		sock = open (filename, O_RDWR, 0);
df4638
+		sock = open (filename, O_RDWR | O_CLOEXEC, 0);
df4638
 		if (sock < 0) {
df4638
 			if (errno == EBUSY) {
df4638
 				continue;
df4638
diff --git a/omapip/trace.c b/omapip/trace.c
df4638
index 45bd508..5ea7486 100644
df4638
--- a/omapip/trace.c
df4638
+++ b/omapip/trace.c
df4638
@@ -136,10 +136,10 @@ isc_result_t trace_begin (const char *filename,
df4638
 		return DHCP_R_INVALIDARG;
df4638
 	}
df4638
 
df4638
-	traceoutfile = open (filename, O_CREAT | O_WRONLY | O_EXCL, 0600);
df4638
+	traceoutfile = open (filename, O_CREAT | O_WRONLY | O_EXCL | O_CLOEXEC, 0600);
df4638
 	if (traceoutfile < 0 && errno == EEXIST) {
df4638
 		log_error ("WARNING: Overwriting trace file \"%s\"", filename);
df4638
-		traceoutfile = open (filename, O_WRONLY | O_EXCL | O_TRUNC,
df4638
+		traceoutfile = open (filename, O_WRONLY | O_EXCL | O_TRUNC | O_CLOEXEC,
df4638
 				     0600);
df4638
 	}
df4638
 
df4638
@@ -427,7 +427,7 @@ void trace_file_replay (const char *filename)
df4638
 	isc_result_t result;
df4638
 	int len;
df4638
 
df4638
-	traceinfile = fopen (filename, "r");
df4638
+	traceinfile = fopen (filename, "re");
df4638
 	if (!traceinfile) {
df4638
 		log_error("Can't open tracefile %s: %m", filename);
df4638
 		return;
df4638
diff --git a/relay/dhcrelay.c b/relay/dhcrelay.c
df4638
index d8caaaf..ea1be18 100644
df4638
--- a/relay/dhcrelay.c
df4638
+++ b/relay/dhcrelay.c
df4638
@@ -296,11 +296,11 @@ main(int argc, char **argv) {
df4638
 	/* Make sure that file descriptors 0(stdin), 1,(stdout), and
df4638
 	   2(stderr) are open. To do this, we assume that when we
df4638
 	   open a file the lowest available file descriptor is used. */
df4638
-	fd = open("/dev/null", O_RDWR);
df4638
+	fd = open("/dev/null", O_RDWR | O_CLOEXEC);
df4638
 	if (fd == 0)
df4638
-		fd = open("/dev/null", O_RDWR);
df4638
+		fd = open("/dev/null", O_RDWR | O_CLOEXEC);
df4638
 	if (fd == 1)
df4638
-		fd = open("/dev/null", O_RDWR);
df4638
+		fd = open("/dev/null", O_RDWR | O_CLOEXEC);
df4638
 	if (fd == 2)
df4638
 		log_perror = 0; /* No sense logging to /dev/null. */
df4638
 	else if (fd != -1)
df4638
@@ -776,13 +776,13 @@ main(int argc, char **argv) {
df4638
 		/* Create the pid file. */
df4638
 		if (no_pid_file == ISC_FALSE) {
df4638
 			pfdesc = open(path_dhcrelay_pid,
df4638
-				      O_CREAT | O_TRUNC | O_WRONLY, 0644);
df4638
+				      O_CREAT | O_TRUNC | O_WRONLY | O_CLOEXEC, 0644);
df4638
 
df4638
 			if (pfdesc < 0) {
df4638
 				log_error("Can't create %s: %m",
df4638
 					  path_dhcrelay_pid);
df4638
 			} else {
df4638
-				pf = fdopen(pfdesc, "w");
df4638
+				pf = fdopen(pfdesc, "we");
df4638
 				if (!pf)
df4638
 					log_error("Can't fdopen %s: %m",
df4638
 						  path_dhcrelay_pid);
df4638
diff --git a/server/confpars.c b/server/confpars.c
df4638
index d2cedfe..2743979 100644
df4638
--- a/server/confpars.c
df4638
+++ b/server/confpars.c
df4638
@@ -118,7 +118,7 @@ isc_result_t read_conf_file (const char *filename, struct group *group,
df4638
 	}
df4638
 #endif
df4638
 
df4638
-	if ((file = open (filename, O_RDONLY)) < 0) {
df4638
+	if ((file = open (filename, O_RDONLY | O_CLOEXEC)) < 0) {
df4638
 		if (leasep) {
df4638
 			log_error ("Can't open lease database %s: %m --",
df4638
 				   path_dhcpd_db);
df4638
diff --git a/server/db.c b/server/db.c
df4638
index 67e6cc1..6181528 100644
df4638
--- a/server/db.c
df4638
+++ b/server/db.c
df4638
@@ -1154,7 +1154,7 @@ int new_lease_file (int test_mode)
df4638
 		     path_dhcpd_db, (int)t) >= sizeof newfname)
df4638
 		log_fatal("new_lease_file: lease file path too long");
df4638
 
df4638
-	db_fd = open (newfname, O_WRONLY | O_TRUNC | O_CREAT, 0664);
df4638
+	db_fd = open (newfname, O_WRONLY | O_TRUNC | O_CREAT | O_CLOEXEC, 0664);
df4638
 	if (db_fd < 0) {
df4638
 		log_error ("Can't create new lease file: %m");
df4638
 		return 0;
df4638
@@ -1175,7 +1175,7 @@ int new_lease_file (int test_mode)
df4638
 	}
df4638
 #endif /* PARANOIA */
df4638
 
df4638
-	if ((new_db_file = fdopen(db_fd, "w")) == NULL) {
df4638
+	if ((new_db_file = fdopen(db_fd, "we")) == NULL) {
df4638
 		log_error("Can't fdopen new lease file: %m");
df4638
 		close(db_fd);
df4638
 		goto fdfail;
df4638
diff --git a/server/dhcpd.c b/server/dhcpd.c
df4638
index 55ffae7..530a923 100644
df4638
--- a/server/dhcpd.c
df4638
+++ b/server/dhcpd.c
df4638
@@ -300,11 +300,11 @@ main(int argc, char **argv) {
df4638
         /* Make sure that file descriptors 0 (stdin), 1, (stdout), and
df4638
            2 (stderr) are open. To do this, we assume that when we
df4638
            open a file the lowest available file descriptor is used. */
df4638
-        fd = open("/dev/null", O_RDWR);
df4638
+        fd = open("/dev/null", O_RDWR | O_CLOEXEC);
df4638
         if (fd == 0)
df4638
-                fd = open("/dev/null", O_RDWR);
df4638
+                fd = open("/dev/null", O_RDWR | O_CLOEXEC);
df4638
         if (fd == 1)
df4638
-                fd = open("/dev/null", O_RDWR);
df4638
+                fd = open("/dev/null", O_RDWR | O_CLOEXEC);
df4638
         if (fd == 2)
df4638
                 log_perror = 0; /* No sense logging to /dev/null. */
df4638
         else if (fd != -1)
df4638
@@ -975,7 +975,7 @@ main(int argc, char **argv) {
df4638
 	 * appropriate.
df4638
 	 */
df4638
 	if (no_pid_file == ISC_FALSE) {
df4638
-		i = open(path_dhcpd_pid, O_WRONLY|O_CREAT|O_TRUNC, 0644);
df4638
+		i = open(path_dhcpd_pid, O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, 0644);
df4638
 		if (i >= 0) {
df4638
 			sprintf(pbuf, "%d\n", (int) getpid());
df4638
 			IGNORE_RET(write(i, pbuf, strlen(pbuf)));
df4638
@@ -1028,9 +1028,9 @@ main(int argc, char **argv) {
df4638
                 (void) close(2);
df4638
 
df4638
                 /* Reopen them on /dev/null. */
df4638
-                (void) open("/dev/null", O_RDWR);
df4638
-                (void) open("/dev/null", O_RDWR);
df4638
-                (void) open("/dev/null", O_RDWR);
df4638
+                (void) open("/dev/null", O_RDWR | O_CLOEXEC);
df4638
+                (void) open("/dev/null", O_RDWR | O_CLOEXEC);
df4638
+                (void) open("/dev/null", O_RDWR | O_CLOEXEC);
df4638
                 log_perror = 0; /* No sense logging to /dev/null. */
df4638
 
df4638
        		IGNORE_RET (chdir("/"));
df4638
diff --git a/server/ldap.c b/server/ldap.c
df4638
index 5126d24..555545c 100644
df4638
--- a/server/ldap.c
df4638
+++ b/server/ldap.c
df4638
@@ -1446,7 +1446,7 @@ ldap_start (void)
df4638
 
df4638
   if (ldap_debug_file != NULL && ldap_debug_fd == -1)
df4638
     {
df4638
-      if ((ldap_debug_fd = open (ldap_debug_file, O_CREAT | O_TRUNC | O_WRONLY,
df4638
+      if ((ldap_debug_fd = open (ldap_debug_file, O_CREAT | O_TRUNC | O_WRONLY | O_CLOEXEC,
df4638
                                  S_IRUSR | S_IWUSR)) < 0)
df4638
         log_error ("Error opening debug LDAP log file %s: %s", ldap_debug_file,
df4638
                    strerror (errno));
df4638
-- 
df4638
2.14.5
df4638