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

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