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