740675
diff -up dhcp-4.3.3b1/client/clparse.c.cloexec dhcp-4.3.3b1/client/clparse.c
740675
--- dhcp-4.3.3b1/client/clparse.c.cloexec	2015-08-10 10:46:20.264755543 +0200
740675
+++ dhcp-4.3.3b1/client/clparse.c	2015-08-10 10:46:20.274755510 +0200
9568cf
@@ -247,7 +247,7 @@ int read_client_conf_file (const char *n
bb77af
 	int token;
bb77af
 	isc_result_t status;
bb77af
 
bb77af
-	if ((file = open (name, O_RDONLY)) < 0)
bb77af
+	if ((file = open (name, O_RDONLY | O_CLOEXEC)) < 0)
bb77af
 		return uerr2isc (errno);
bb77af
 
bb77af
 	cfile = NULL;
9568cf
@@ -323,7 +323,7 @@ void read_client_leases ()
bb77af
 
bb77af
 	/* Open the lease file.   If we can't open it, just return -
bb77af
 	   we can safely trust the server to remember our state. */
bb77af
-	if ((file = open (path_dhclient_db, O_RDONLY)) < 0)
bb77af
+	if ((file = open (path_dhclient_db, O_RDONLY | O_CLOEXEC)) < 0)
bb77af
 		return;
bb77af
 
bb77af
 	cfile = NULL;
740675
diff -up dhcp-4.3.3b1/client/dhclient.c.cloexec dhcp-4.3.3b1/client/dhclient.c
740675
--- dhcp-4.3.3b1/client/dhclient.c.cloexec	2015-08-10 10:46:20.260755556 +0200
740675
+++ dhcp-4.3.3b1/client/dhclient.c	2015-08-10 10:46:20.275755506 +0200
740675
@@ -153,11 +153,11 @@ main(int argc, char **argv) {
bb77af
 	/* Make sure that file descriptors 0 (stdin), 1, (stdout), and
bb77af
 	   2 (stderr) are open. To do this, we assume that when we
bb77af
 	   open a file the lowest available file descriptor is used. */
bb77af
-	fd = open("/dev/null", O_RDWR);
bb77af
+	fd = open("/dev/null", O_RDWR | O_CLOEXEC);
bb77af
 	if (fd == 0)
bb77af
-		fd = open("/dev/null", O_RDWR);
bb77af
+		fd = open("/dev/null", O_RDWR | O_CLOEXEC);
bb77af
 	if (fd == 1)
bb77af
-		fd = open("/dev/null", O_RDWR);
bb77af
+		fd = open("/dev/null", O_RDWR | O_CLOEXEC);
bb77af
 	if (fd == 2)
bb77af
 		log_perror = 0; /* No sense logging to /dev/null. */
bb77af
 	else if (fd != -1)
740675
@@ -519,7 +519,7 @@ main(int argc, char **argv) {
e83fb1
 		long temp;
bb77af
 		int e;
bb77af
 
bb77af
-		if ((pidfd = fopen(path_dhclient_pid, "r")) != NULL) {
bb77af
+		if ((pidfd = fopen(path_dhclient_pid, "re")) != NULL) {
bb77af
 			e = fscanf(pidfd, "%ld\n", &temp);
bb77af
 			oldpid = (pid_t)temp;
bb77af
 
740675
@@ -574,7 +574,7 @@ main(int argc, char **argv) {
bb77af
 					strncpy(new_path_dhclient_pid, path_dhclient_pid, pfx);
bb77af
 					sprintf(new_path_dhclient_pid + pfx, "-%s.pid", ip->name);
bb77af
 
bb77af
-					if ((pidfd = fopen(new_path_dhclient_pid, "r")) != NULL) {
bb77af
+					if ((pidfd = fopen(new_path_dhclient_pid, "re")) != NULL) {
bb77af
 						e = fscanf(pidfd, "%ld\n", &temp);
bb77af
 						oldpid = (pid_t)temp;
bb77af
 
740675
@@ -599,7 +599,7 @@ main(int argc, char **argv) {
bb77af
 		int dhc_running = 0;
bb77af
 		char procfn[256] = "";
bb77af
 
bb77af
-		if ((pidfp = fopen(path_dhclient_pid, "r")) != NULL) {
bb77af
+		if ((pidfp = fopen(path_dhclient_pid, "re")) != NULL) {
bb77af
 			if ((fscanf(pidfp, "%ld", &temp)==1) && ((dhcpid=(pid_t)temp) > 0)) {
bb77af
 				snprintf(procfn,256,"/proc/%u",dhcpid);
bb77af
 				dhc_running = (access(procfn, F_OK) == 0);
740675
@@ -3120,7 +3120,7 @@ void rewrite_client_leases ()
bb77af
 
bb77af
 	if (leaseFile != NULL)
bb77af
 		fclose (leaseFile);
bb77af
-	leaseFile = fopen (path_dhclient_db, "w");
bb77af
+	leaseFile = fopen (path_dhclient_db, "we");
bb77af
 	if (leaseFile == NULL) {
bb77af
 		log_error ("can't create %s: %m", path_dhclient_db);
bb77af
 		return;
740675
@@ -3313,7 +3313,7 @@ write_duid(struct data_string *duid)
bb77af
 		return DHCP_R_INVALIDARG;
bb77af
 
bb77af
 	if (leaseFile == NULL) {	/* XXX? */
bb77af
-		leaseFile = fopen(path_dhclient_db, "w");
bb77af
+		leaseFile = fopen(path_dhclient_db, "we");
bb77af
 		if (leaseFile == NULL) {
bb77af
 			log_error("can't create %s: %m", path_dhclient_db);
bb77af
 			return ISC_R_IOERROR;
740675
@@ -3493,7 +3493,7 @@ int write_client_lease (client, lease, r
bb77af
 		return 1;
bb77af
 
bb77af
 	if (leaseFile == NULL) {	/* XXX */
bb77af
-		leaseFile = fopen (path_dhclient_db, "w");
bb77af
+		leaseFile = fopen (path_dhclient_db, "we");
bb77af
 		if (leaseFile == NULL) {
bb77af
 			log_error ("can't create %s: %m", path_dhclient_db);
bb77af
 			return 0;
740675
@@ -4011,9 +4011,9 @@ void go_daemon ()
e83fb1
 	(void) close(2);
bb77af
 
bb77af
 	/* Reopen them on /dev/null. */
e83fb1
-	(void) open("/dev/null", O_RDWR);
e83fb1
-	(void) open("/dev/null", O_RDWR);
e83fb1
-	(void) open("/dev/null", O_RDWR);
e83fb1
+	(void) open("/dev/null", O_RDWR | O_CLOEXEC);
e83fb1
+	(void) open("/dev/null", O_RDWR | O_CLOEXEC);
e83fb1
+	(void) open("/dev/null", O_RDWR | O_CLOEXEC);
bb77af
 
bb77af
 	write_client_pid_file ();
bb77af
 
740675
@@ -4030,14 +4030,14 @@ void write_client_pid_file ()
bb77af
 		return;
bb77af
 	}
bb77af
 
bb77af
-	pfdesc = open (path_dhclient_pid, O_CREAT | O_TRUNC | O_WRONLY, 0644);
bb77af
+	pfdesc = open (path_dhclient_pid, O_CREAT | O_TRUNC | O_WRONLY | O_CLOEXEC, 0644);
bb77af
 
bb77af
 	if (pfdesc < 0) {
bb77af
 		log_error ("Can't create %s: %m", path_dhclient_pid);
bb77af
 		return;
bb77af
 	}
bb77af
 
bb77af
-	pf = fdopen (pfdesc, "w");
bb77af
+	pf = fdopen (pfdesc, "we");
bb77af
 	if (!pf) {
bb77af
 		close(pfdesc);
bb77af
 		log_error ("Can't fdopen %s: %m", path_dhclient_pid);
740675
diff -up dhcp-4.3.3b1/common/bpf.c.cloexec dhcp-4.3.3b1/common/bpf.c
740675
--- dhcp-4.3.3b1/common/bpf.c.cloexec	2015-07-30 15:17:16.000000000 +0200
740675
+++ dhcp-4.3.3b1/common/bpf.c	2015-08-10 10:46:20.275755506 +0200
e83fb1
@@ -95,7 +95,7 @@ int if_register_bpf (info)
bb77af
 	for (b = 0; 1; b++) {
bb77af
 		/* %Audit% 31 bytes max. %2004.06.17,Safe% */
bb77af
 		sprintf(filename, BPF_FORMAT, b);
bb77af
-		sock = open (filename, O_RDWR, 0);
bb77af
+		sock = open (filename, O_RDWR | O_CLOEXEC, 0);
bb77af
 		if (sock < 0) {
bb77af
 			if (errno == EBUSY) {
bb77af
 				continue;
740675
diff -up dhcp-4.3.3b1/common/dlpi.c.cloexec dhcp-4.3.3b1/common/dlpi.c
740675
--- dhcp-4.3.3b1/common/dlpi.c.cloexec	2015-07-30 15:17:16.000000000 +0200
740675
+++ dhcp-4.3.3b1/common/dlpi.c	2015-08-10 10:46:20.275755506 +0200
e83fb1
@@ -804,7 +804,7 @@ dlpiopen(const char *ifname) {
bb77af
 	}
bb77af
 	*dp = '\0';
bb77af
 	
bb77af
-	return open (devname, O_RDWR, 0);
bb77af
+	return open (devname, O_RDWR | O_CLOEXEC, 0);
bb77af
 }
bb77af
 
bb77af
 /*
740675
diff -up dhcp-4.3.3b1/common/nit.c.cloexec dhcp-4.3.3b1/common/nit.c
740675
--- dhcp-4.3.3b1/common/nit.c.cloexec	2015-07-30 15:17:16.000000000 +0200
740675
+++ dhcp-4.3.3b1/common/nit.c	2015-08-10 10:46:20.275755506 +0200
9568cf
@@ -75,7 +75,7 @@ int if_register_nit (info)
bb77af
 	struct strioctl sio;
bb77af
 
bb77af
 	/* Open a NIT device */
bb77af
-	sock = open ("/dev/nit", O_RDWR);
bb77af
+	sock = open ("/dev/nit", O_RDWR | O_CLOEXEC);
bb77af
 	if (sock < 0)
bb77af
 		log_fatal ("Can't open NIT device for %s: %m", info -> name);
bb77af
 
740675
diff -up dhcp-4.3.3b1/common/resolv.c.cloexec dhcp-4.3.3b1/common/resolv.c
740675
--- dhcp-4.3.3b1/common/resolv.c.cloexec	2015-07-30 15:17:16.000000000 +0200
740675
+++ dhcp-4.3.3b1/common/resolv.c	2015-08-10 10:46:20.276755503 +0200
9568cf
@@ -44,7 +44,7 @@ void read_resolv_conf (parse_time)
bb77af
 	struct domain_search_list *dp, *dl, *nd;
bb77af
 	isc_result_t status;
bb77af
 
bb77af
-	if ((file = open (path_resolv_conf, O_RDONLY)) < 0) {
bb77af
+	if ((file = open (path_resolv_conf, O_RDONLY | O_CLOEXEC)) < 0) {
bb77af
 		log_error ("Can't open %s: %m", path_resolv_conf);
bb77af
 		return;
bb77af
 	}
740675
diff -up dhcp-4.3.3b1/common/upf.c.cloexec dhcp-4.3.3b1/common/upf.c
740675
--- dhcp-4.3.3b1/common/upf.c.cloexec	2015-07-30 15:17:16.000000000 +0200
740675
+++ dhcp-4.3.3b1/common/upf.c	2015-08-10 10:46:20.276755503 +0200
9568cf
@@ -71,7 +71,7 @@ int if_register_upf (info)
bb77af
 		/* %Audit% Cannot exceed 36 bytes. %2004.06.17,Safe% */
bb77af
 		sprintf(filename, "/dev/pf/pfilt%d", b);
bb77af
 
bb77af
-		sock = open (filename, O_RDWR, 0);
bb77af
+		sock = open (filename, O_RDWR | O_CLOEXEC, 0);
bb77af
 		if (sock < 0) {
bb77af
 			if (errno == EBUSY) {
bb77af
 				continue;
740675
diff -up dhcp-4.3.3b1/omapip/trace.c.cloexec dhcp-4.3.3b1/omapip/trace.c
740675
--- dhcp-4.3.3b1/omapip/trace.c.cloexec	2015-07-30 15:17:16.000000000 +0200
740675
+++ dhcp-4.3.3b1/omapip/trace.c	2015-08-10 10:46:20.276755503 +0200
9568cf
@@ -138,10 +138,10 @@ isc_result_t trace_begin (const char *fi
bb77af
 		return DHCP_R_INVALIDARG;
bb77af
 	}
bb77af
 
bb77af
-	traceoutfile = open (filename, O_CREAT | O_WRONLY | O_EXCL, 0600);
bb77af
+	traceoutfile = open (filename, O_CREAT | O_WRONLY | O_EXCL | O_CLOEXEC, 0600);
bb77af
 	if (traceoutfile < 0 && errno == EEXIST) {
bb77af
 		log_error ("WARNING: Overwriting trace file \"%s\"", filename);
bb77af
-		traceoutfile = open (filename, O_WRONLY | O_EXCL | O_TRUNC,
bb77af
+		traceoutfile = open (filename, O_WRONLY | O_EXCL | O_TRUNC | O_CLOEXEC,
bb77af
 				     0600);
bb77af
 	}
bb77af
 
9568cf
@@ -429,7 +429,7 @@ void trace_file_replay (const char *file
bb77af
 	isc_result_t result;
bb77af
 	int len;
bb77af
 
bb77af
-	traceinfile = fopen (filename, "r");
bb77af
+	traceinfile = fopen (filename, "re");
bb77af
 	if (!traceinfile) {
bb77af
 		log_error("Can't open tracefile %s: %m", filename);
bb77af
 		return;
740675
diff -up dhcp-4.3.3b1/relay/dhcrelay.c.cloexec dhcp-4.3.3b1/relay/dhcrelay.c
740675
--- dhcp-4.3.3b1/relay/dhcrelay.c.cloexec	2015-07-30 15:17:16.000000000 +0200
740675
+++ dhcp-4.3.3b1/relay/dhcrelay.c	2015-08-10 10:46:20.276755503 +0200
9568cf
@@ -187,11 +187,11 @@ main(int argc, char **argv) {
bb77af
 	/* Make sure that file descriptors 0(stdin), 1,(stdout), and
bb77af
 	   2(stderr) are open. To do this, we assume that when we
bb77af
 	   open a file the lowest available file descriptor is used. */
bb77af
-	fd = open("/dev/null", O_RDWR);
bb77af
+	fd = open("/dev/null", O_RDWR | O_CLOEXEC);
bb77af
 	if (fd == 0)
bb77af
-		fd = open("/dev/null", O_RDWR);
bb77af
+		fd = open("/dev/null", O_RDWR | O_CLOEXEC);
bb77af
 	if (fd == 1)
bb77af
-		fd = open("/dev/null", O_RDWR);
bb77af
+		fd = open("/dev/null", O_RDWR | O_CLOEXEC);
bb77af
 	if (fd == 2)
bb77af
 		log_perror = 0; /* No sense logging to /dev/null. */
bb77af
 	else if (fd != -1)
9568cf
@@ -558,13 +558,13 @@ main(int argc, char **argv) {
bb77af
 
bb77af
 		if (no_pid_file == ISC_FALSE) {
bb77af
 			pfdesc = open(path_dhcrelay_pid,
bb77af
-				      O_CREAT | O_TRUNC | O_WRONLY, 0644);
bb77af
+				      O_CREAT | O_TRUNC | O_WRONLY | O_CLOEXEC, 0644);
bb77af
 
bb77af
 			if (pfdesc < 0) {
bb77af
 				log_error("Can't create %s: %m",
bb77af
 					  path_dhcrelay_pid);
bb77af
 			} else {
bb77af
-				pf = fdopen(pfdesc, "w");
bb77af
+				pf = fdopen(pfdesc, "we");
bb77af
 				if (!pf)
bb77af
 					log_error("Can't fdopen %s: %m",
bb77af
 						  path_dhcrelay_pid);
740675
diff -up dhcp-4.3.3b1/server/confpars.c.cloexec dhcp-4.3.3b1/server/confpars.c
740675
--- dhcp-4.3.3b1/server/confpars.c.cloexec	2015-07-30 15:17:16.000000000 +0200
740675
+++ dhcp-4.3.3b1/server/confpars.c	2015-08-10 10:46:20.277755500 +0200
9568cf
@@ -111,7 +111,7 @@ isc_result_t read_conf_file (const char
bb77af
 	}
bb77af
 #endif
bb77af
 
bb77af
-	if ((file = open (filename, O_RDONLY)) < 0) {
bb77af
+	if ((file = open (filename, O_RDONLY | O_CLOEXEC)) < 0) {
bb77af
 		if (leasep) {
bb77af
 			log_error ("Can't open lease database %s: %m --",
bb77af
 				   path_dhcpd_db);
740675
diff -up dhcp-4.3.3b1/server/db.c.cloexec dhcp-4.3.3b1/server/db.c
740675
--- dhcp-4.3.3b1/server/db.c.cloexec	2015-07-30 15:17:16.000000000 +0200
740675
+++ dhcp-4.3.3b1/server/db.c	2015-08-10 10:47:32.644518358 +0200
740675
@@ -1072,7 +1072,7 @@ void db_startup (testp)
bb77af
 	}
bb77af
 #endif
bb77af
 	if (!testp) {
bb77af
-		db_file = fopen (path_dhcpd_db, "a");
bb77af
+		db_file = fopen (path_dhcpd_db, "ae");
bb77af
 		if (!db_file)
bb77af
 			log_fatal ("Can't open %s for append.", path_dhcpd_db);
bb77af
 		expire_all_pools ();
740675
@@ -1120,7 +1120,7 @@ int new_lease_file ()
bb77af
 		     path_dhcpd_db, (int)t) >= sizeof newfname)
bb77af
 		log_fatal("new_lease_file: lease file path too long");
bb77af
 
bb77af
-	db_fd = open (newfname, O_WRONLY | O_TRUNC | O_CREAT, 0664);
bb77af
+	db_fd = open (newfname, O_WRONLY | O_TRUNC | O_CREAT | O_CLOEXEC, 0664);
bb77af
 	if (db_fd < 0) {
bb77af
 		log_error ("Can't create new lease file: %m");
bb77af
 		return 0;
740675
@@ -1141,7 +1141,7 @@ int new_lease_file ()
bb77af
 	}
740675
 #endif /* PARANOIA */
740675
 
bb77af
-	if ((new_db_file = fdopen(db_fd, "w")) == NULL) {
bb77af
+	if ((new_db_file = fdopen(db_fd, "we")) == NULL) {
bb77af
 		log_error("Can't fdopen new lease file: %m");
bb77af
 		close(db_fd);
bb77af
 		goto fdfail;
740675
diff -up dhcp-4.3.3b1/server/dhcpd.c.cloexec dhcp-4.3.3b1/server/dhcpd.c
740675
--- dhcp-4.3.3b1/server/dhcpd.c.cloexec	2015-07-30 15:17:16.000000000 +0200
740675
+++ dhcp-4.3.3b1/server/dhcpd.c	2015-08-10 10:46:20.278755497 +0200
740675
@@ -194,11 +194,11 @@ main(int argc, char **argv) {
bb77af
         /* Make sure that file descriptors 0 (stdin), 1, (stdout), and
bb77af
            2 (stderr) are open. To do this, we assume that when we
bb77af
            open a file the lowest available file descriptor is used. */
bb77af
-        fd = open("/dev/null", O_RDWR);
bb77af
+        fd = open("/dev/null", O_RDWR | O_CLOEXEC);
bb77af
         if (fd == 0)
bb77af
-                fd = open("/dev/null", O_RDWR);
bb77af
+                fd = open("/dev/null", O_RDWR | O_CLOEXEC);
bb77af
         if (fd == 1)
bb77af
-                fd = open("/dev/null", O_RDWR);
bb77af
+                fd = open("/dev/null", O_RDWR | O_CLOEXEC);
bb77af
         if (fd == 2)
bb77af
                 log_perror = 0; /* No sense logging to /dev/null. */
bb77af
         else if (fd != -1)
740675
@@ -743,7 +743,7 @@ main(int argc, char **argv) {
9568cf
 	 * appropriate.
bb77af
 	 */
bb77af
 	if (no_pid_file == ISC_FALSE) {
bb77af
-		i = open(path_dhcpd_pid, O_WRONLY|O_CREAT|O_TRUNC, 0644);
bb77af
+		i = open(path_dhcpd_pid, O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, 0644);
bb77af
 		if (i >= 0) {
bb77af
 			sprintf(pbuf, "%d\n", (int) getpid());
9568cf
 			IGNORE_RET(write(i, pbuf, strlen(pbuf)));
740675
@@ -787,9 +787,9 @@ main(int argc, char **argv) {
e83fb1
                 (void) close(2);
bb77af
 
bb77af
                 /* Reopen them on /dev/null. */
e83fb1
-                (void) open("/dev/null", O_RDWR);
e83fb1
-                (void) open("/dev/null", O_RDWR);
e83fb1
-                (void) open("/dev/null", O_RDWR);
e83fb1
+                (void) open("/dev/null", O_RDWR | O_CLOEXEC);
e83fb1
+                (void) open("/dev/null", O_RDWR | O_CLOEXEC);
e83fb1
+                (void) open("/dev/null", O_RDWR | O_CLOEXEC);
bb77af
                 log_perror = 0; /* No sense logging to /dev/null. */
bb77af
 
bb77af
        		IGNORE_RET (chdir("/"));
740675
diff -up dhcp-4.3.3b1/server/ldap.c.cloexec dhcp-4.3.3b1/server/ldap.c
740675
--- dhcp-4.3.3b1/server/ldap.c.cloexec	2015-07-30 21:03:40.000000000 +0200
740675
+++ dhcp-4.3.3b1/server/ldap.c	2015-08-10 10:46:20.279755493 +0200
740675
@@ -1442,7 +1442,7 @@ ldap_start (void)
bb77af
 
bb77af
   if (ldap_debug_file != NULL && ldap_debug_fd == -1)
bb77af
     {
bb77af
-      if ((ldap_debug_fd = open (ldap_debug_file, O_CREAT | O_TRUNC | O_WRONLY,
bb77af
+      if ((ldap_debug_fd = open (ldap_debug_file, O_CREAT | O_TRUNC | O_WRONLY | O_CLOEXEC,
bb77af
                                  S_IRUSR | S_IWUSR)) < 0)
bb77af
         log_error ("Error opening debug LDAP log file %s: %s", ldap_debug_file,
bb77af
                    strerror (errno));