Blame SOURCES/0015-Ticket-49174-nunc-stans-can-not-use-negative-timeout.patch

61f723
From 4f90e73538f1faf101733fcd95392bb77ba9467c Mon Sep 17 00:00:00 2001
61f723
From: William Brown <firstyear@redhat.com>
61f723
Date: Wed, 22 Mar 2017 14:10:11 +1000
61f723
Subject: [PATCH] Ticket 49174 - nunc-stans can not use negative timeout
61f723
61f723
Bug Description:  FreeIPA regularly sets up service accounts with
61f723
an nsIdleTimeout of -1. As a result of an issue with NS and libevent
61f723
this would cause an instant timeout and disconnect of the service
61f723
account.
61f723
61f723
Fix Description:  Correctly check that jobs are registered to NS.
61f723
Add validation to NS for negative timeouts. During the job registration,
61f723
we force the timeout to be a valid value.
61f723
61f723
https://pagure.io/389-ds-base/issue/49174
61f723
61f723
Author: wibrown
61f723
61f723
Review by: mreynolds(Thanks!!!)
61f723
61f723
Signed-off-by: Mark Reynolds <mreynolds@redhat.com>
61f723
---
61f723
 ldap/servers/slapd/daemon.c           | 39 ++++++++++++++++++++++++++++-------
61f723
 src/nunc-stans/ns/ns_event_fw_event.c |  8 -------
61f723
 src/nunc-stans/ns/ns_thrpool.c        | 16 ++++++++++++++
61f723
 src/nunc-stans/test/test_nuncstans.c  | 20 ++++++++++++++++++
61f723
 4 files changed, 68 insertions(+), 15 deletions(-)
61f723
61f723
diff --git a/ldap/servers/slapd/daemon.c b/ldap/servers/slapd/daemon.c
61f723
index e17a858..a4ea4c0 100644
61f723
--- a/ldap/servers/slapd/daemon.c
61f723
+++ b/ldap/servers/slapd/daemon.c
61f723
@@ -1891,15 +1891,32 @@ ns_connection_post_io_or_closing(Connection *conn)
61f723
 			tv.tv_usec = slapd_wakeup_timer * 1000;
61f723
 			conn->c_ns_close_jobs++; /* now 1 active closure job */
61f723
 			connection_acquire_nolock_ext(conn, 1 /* allow acquire even when closing */); /* event framework now has a reference */
61f723
-			ns_add_timeout_job(conn->c_tp, &tv, NS_JOB_TIMER,
61f723
+			PRStatus job_result = ns_add_timeout_job(conn->c_tp, &tv, NS_JOB_TIMER,
61f723
 					   ns_handle_closure, conn, NULL);
61f723
-			slapi_log_err(SLAPI_LOG_CONNS, "ns_connection_post_io_or_closing", "post closure job "
61f723
-				"for conn %" NSPRIu64 " for fd=%d\n", conn->c_connid, conn->c_sd);
61f723
+#ifdef DEBUG
61f723
+			PR_ASSERT(job_result == PR_SUCCESS);
61f723
+#endif
61f723
+			if (job_result != PR_SUCCESS) {
61f723
+				slapi_log_err(SLAPI_LOG_WARNING, "ns_connection_post_io_or_closing", "post closure job "
61f723
+					"for conn %" NSPRIu64 " for fd=%d failed to be added to event queue\n", conn->c_connid, conn->c_sd);
61f723
+			} else {
61f723
+				slapi_log_err(SLAPI_LOG_CONNS, "ns_connection_post_io_or_closing", "post closure job "
61f723
+					"for conn %" NSPRIu64 " for fd=%d\n", conn->c_connid, conn->c_sd);
61f723
+			}
61f723
 			
61f723
 		}
61f723
 	} else {
61f723
 		/* process event normally - wait for I/O until idletimeout */
61f723
-		tv.tv_sec = conn->c_idletimeout;
61f723
+		/* With nunc-stans there is a quirk. When we have idleTimeout of -1
61f723
+		 * which is set on some IPA bind dns for infinite, this causes libevent 
61f723
+		 * to *instantly* timeout. So if we detect < 0, we set 0 to this timeout, to
61f723
+		 * catch all possible times that an admin could set.
61f723
+		 */
61f723
+		if (conn->c_idletimeout < 0) {
61f723
+			tv.tv_sec = 0;
61f723
+		} else {
61f723
+			tv.tv_sec = conn->c_idletimeout;
61f723
+		}
61f723
 		tv.tv_usec = 0;
61f723
 #ifdef DEBUG
61f723
 		PR_ASSERT(0 == connection_acquire_nolock(conn));
61f723
@@ -1913,11 +1930,19 @@ ns_connection_post_io_or_closing(Connection *conn)
61f723
 			return;
61f723
 		}
61f723
 #endif
61f723
-		ns_add_io_timeout_job(conn->c_tp, conn->c_prfd, &tv,
61f723
+		PRStatus job_result = ns_add_io_timeout_job(conn->c_tp, conn->c_prfd, &tv,
61f723
 				      NS_JOB_READ|NS_JOB_PRESERVE_FD,
61f723
 				      ns_handle_pr_read_ready, conn, NULL);
61f723
-		slapi_log_err(SLAPI_LOG_CONNS, "ns_connection_post_io_or_closing", "post I/O job for "
61f723
-			"conn %" NSPRIu64 " for fd=%d\n", conn->c_connid, conn->c_sd);
61f723
+#ifdef DEBUG
61f723
+		PR_ASSERT(job_result == PR_SUCCESS);
61f723
+#endif
61f723
+		if (job_result != PR_SUCCESS) {
61f723
+			slapi_log_err(SLAPI_LOG_WARNING, "ns_connection_post_io_or_closing", "post I/O job for "
61f723
+				"conn %" NSPRIu64 " for fd=%d failed to be added to event queue\n", conn->c_connid, conn->c_sd);
61f723
+		} else {
61f723
+			slapi_log_err(SLAPI_LOG_CONNS, "ns_connection_post_io_or_closing", "post I/O job for "
61f723
+				"conn %" NSPRIu64 " for fd=%d\n", conn->c_connid, conn->c_sd);
61f723
+		}
61f723
 	}
61f723
 #endif
61f723
 }
61f723
diff --git a/src/nunc-stans/ns/ns_event_fw_event.c b/src/nunc-stans/ns/ns_event_fw_event.c
61f723
index 3acbaf7..76936de 100644
61f723
--- a/src/nunc-stans/ns/ns_event_fw_event.c
61f723
+++ b/src/nunc-stans/ns/ns_event_fw_event.c
61f723
@@ -48,7 +48,6 @@ typedef struct event ns_event_fw_sig_t;
61f723
 #include "ns_event_fw.h"
61f723
 #include <syslog.h>
61f723
 
61f723
-
61f723
 static void
61f723
 event_logger_cb(int severity, const char *msg)
61f723
 {
61f723
@@ -248,13 +247,6 @@ ns_event_fw_mod_io(
61f723
     }
61f723
     if (events) {
61f723
         job->ns_event_fw_fd->ev_events = events;
61f723
-
61f723
-#ifdef DEBUG_FSM
61f723
-        /* REALLY make sure that we aren't being re-added */
61f723
-        if (event_pending(job->ns_event_fw_fd, events, tv)) {
61f723
-            abort();
61f723
-        }
61f723
-#endif
61f723
         event_add(job->ns_event_fw_fd, tv);
61f723
     } else {
61f723
         /* setting the job_type to remove IO events will remove it from the event system */
61f723
diff --git a/src/nunc-stans/ns/ns_thrpool.c b/src/nunc-stans/ns/ns_thrpool.c
61f723
index a867b39..9d87384 100644
61f723
--- a/src/nunc-stans/ns/ns_thrpool.c
61f723
+++ b/src/nunc-stans/ns/ns_thrpool.c
61f723
@@ -180,6 +180,14 @@ ns_thrpool_is_event_shutdown(struct ns_thrpool_t *tp)
61f723
     return result;
61f723
 }
61f723
 
61f723
+static int32_t
61f723
+validate_event_timeout(struct timeval *tv) {
61f723
+    if (tv->tv_sec < 0 || tv->tv_usec < 0) {
61f723
+        /* If we get here, you have done something WRONG */
61f723
+        return 1;
61f723
+    }
61f723
+    return 0;
61f723
+}
61f723
 
61f723
 static void
61f723
 job_queue_cleanup(void *arg) {
61f723
@@ -864,6 +872,10 @@ ns_add_timeout_job(ns_thrpool_t *tp, struct timeval *tv, ns_job_type_t job_type,
61f723
         return PR_FAILURE;
61f723
     }
61f723
 
61f723
+    if (validate_event_timeout(tv)) {
61f723
+        return PR_FAILURE;
61f723
+    }
61f723
+
61f723
     /* get an event context for a timer job */
61f723
     _job = alloc_timeout_context(tp, tv, job_type, func, data);
61f723
     if (!_job) {
61f723
@@ -900,6 +912,10 @@ ns_add_io_timeout_job(ns_thrpool_t *tp, PRFileDesc *fd, struct timeval *tv,
61f723
         return PR_FAILURE;
61f723
     }
61f723
 
61f723
+    if (validate_event_timeout(tv)) {
61f723
+        return PR_FAILURE;
61f723
+    }
61f723
+
61f723
     /* Don't allow an accept job to be run outside of the event thread.
61f723
      * We do this so a listener job won't shut down while still processing
61f723
      * current connections in other threads.
61f723
diff --git a/src/nunc-stans/test/test_nuncstans.c b/src/nunc-stans/test/test_nuncstans.c
61f723
index 8eef9e6..2795302 100644
61f723
--- a/src/nunc-stans/test/test_nuncstans.c
61f723
+++ b/src/nunc-stans/test/test_nuncstans.c
61f723
@@ -385,6 +385,23 @@ ns_job_signal_cb_test(void **state)
61f723
     assert_int_equal(ns_job_done(job), 0);
61f723
 }
61f723
 
61f723
+/*
61f723
+ * Test that given a timeout of -1, we fail to create a job.
61f723
+ */
61f723
+
61f723
+static void
61f723
+ns_job_neg_timeout_test(void **state)
61f723
+{
61f723
+    struct ns_thrpool_t *tp = *state;
61f723
+
61f723
+    struct timeval tv = { -1, 0 };
61f723
+
61f723
+    PR_ASSERT(PR_FAILURE == ns_add_io_timeout_job(tp, 0, &tv, NS_JOB_THREAD, ns_init_do_nothing_cb, NULL, NULL));
61f723
+
61f723
+    PR_ASSERT(PR_FAILURE == ns_add_timeout_job(tp, &tv, NS_JOB_THREAD, ns_init_do_nothing_cb, NULL, NULL));
61f723
+
61f723
+}
61f723
+
61f723
 int
61f723
 main(void)
61f723
 {
61f723
@@ -410,6 +427,9 @@ main(void)
61f723
         cmocka_unit_test_setup_teardown(ns_job_signal_cb_test,
61f723
                                         ns_test_setup,
61f723
                                         ns_test_teardown),
61f723
+        cmocka_unit_test_setup_teardown(ns_job_neg_timeout_test,
61f723
+                                        ns_test_setup,
61f723
+                                        ns_test_teardown),
61f723
     };
61f723
     return cmocka_run_group_tests(tests, NULL, NULL);
61f723
 }
61f723
-- 
61f723
2.9.3
61f723