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

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