s3:messages: protect against usage of wrapper tevent_context objects for messaging
authorStefan Metzmacher <metze@samba.org>
Fri, 18 May 2018 14:28:47 +0000 (16:28 +0200)
committerRalph Boehme <slow@samba.org>
Wed, 11 Jul 2018 21:04:23 +0000 (23:04 +0200)
This makes a lot of assumtion easier to understand and the introduction
of wrapper tevent contexts will not change the existing behaviour.

We'll relax this a bit in the next commits.

Signed-off-by: Stefan Metzmacher <metze@samba.org>
Reviewed-by: Ralph Boehme <slow@samba.org>
source3/lib/messages.c
source3/lib/messages_ctdb.c
source3/lib/messages_ctdb_ref.c
source3/lib/messages_dgm.c
source3/lib/messages_dgm_ref.c

index 82a177856f60d0bed089ab589a9d93f7a4f22ee8..1a5ea4659aa41f9fa709717f4e3712a0533bf88a 100644 (file)
@@ -365,6 +365,11 @@ static bool messaging_alert_event_contexts(struct messaging_context *ctx)
                 * alternatively would be to track whether the
                 * immediate has already been scheduled. For
                 * now, avoid that complexity here.
+                *
+                * reg->ev and ctx->event_ctx can't
+                * be wrapper tevent_context pointers
+                * so we don't need to use
+                * tevent_context_same_loop().
                 */
 
                if (reg->ev == ctx->event_ctx) {
@@ -493,6 +498,12 @@ static NTSTATUS messaging_init_internal(TALLOC_CTX *mem_ctx,
 
        sec_init();
 
+       if (tevent_context_is_wrapper(ev)) {
+               /* This is really a programmer error! */
+               DBG_ERR("Should not be used with a wrapper tevent context\n");
+               return NT_STATUS_INVALID_PARAMETER;
+       }
+
        lck_path = lock_path("msg.lock");
        if (lck_path == NULL) {
                return NT_STATUS_NO_MEMORY;
@@ -1023,6 +1034,13 @@ struct tevent_req *messaging_filtered_read_send(
        state->filter = filter;
        state->private_data = private_data;
 
+       if (tevent_context_is_wrapper(ev)) {
+               /* This is really a programmer error! */
+               DBG_ERR("Wrapper tevent context doesn't use main context.\n");
+               tevent_req_error(req, EINVAL);
+               return tevent_req_post(req, ev);
+       }
+
        /*
         * We have to defer the callback here, as we might be called from
         * within a different tevent_context than state->ev
@@ -1343,6 +1361,11 @@ static void messaging_dispatch_rec(struct messaging_context *msg_ctx,
        bool consumed;
        size_t i;
 
+       /*
+        * ev and msg_ctx->event_ctx can't be wrapper tevent_context pointers
+        * so we don't need to use tevent_context_same_loop().
+        */
+
        if (ev == msg_ctx->event_ctx) {
                consumed = messaging_dispatch_classic(msg_ctx, rec);
                if (consumed) {
index d3e2e3f858937fbf7368a6267acd016ea7f4afc7..a1aeb37af19867ef2fa9093d75322a5620aa7b65 100644 (file)
@@ -209,6 +209,14 @@ struct messaging_ctdb_fde *messaging_ctdb_register_tevent_context(
                return NULL;
        }
 
+       if (tevent_context_is_wrapper(ev)) {
+               /*
+                * This is really a programmer error!
+                */
+               DBG_ERR("Should not be used with a wrapper tevent context\n");
+               return NULL;
+       }
+
        fde = talloc(mem_ctx, struct messaging_ctdb_fde);
        if (fde == NULL) {
                return NULL;
index 3570ed8ae4c8c5e8c1446bfccf7215ebe95192ec..47b4b758dac54c80029b12c040050780052ff758 100644 (file)
@@ -52,6 +52,18 @@ void *messaging_ctdb_ref(TALLOC_CTX *mem_ctx, struct tevent_context *ev,
 {
        struct msg_ctdb_ref *result, *tmp_refs;
 
+       if (tevent_context_is_wrapper(ev)) {
+               /*
+                * This is really a programmer error!
+                *
+                * The main/raw tevent context should
+                * have been registered first!
+                */
+               DBG_ERR("Should not be used with a wrapper tevent context\n");
+               *err = EINVAL;
+               return NULL;
+       }
+
        result = talloc(mem_ctx, struct msg_ctdb_ref);
        if (result == NULL) {
                *err = ENOMEM;
index b8878b68b9967378f72741631dd7cd7d841c8e79..1c76615093c0d397e0d62fa550d3db7182b9a801 100644 (file)
@@ -993,6 +993,12 @@ int messaging_dgm_init(struct tevent_context *ev,
                return EEXIST;
        }
 
+       if (tevent_context_is_wrapper(ev)) {
+               /* This is really a programmer error! */
+               DBG_ERR("Should not be used with a wrapper tevent context\n");
+               return EINVAL;
+       }
+
        ctx = talloc_zero(NULL, struct messaging_dgm_context);
        if (ctx == NULL) {
                goto fail_nomem;
@@ -1673,6 +1679,14 @@ struct messaging_dgm_fde *messaging_dgm_register_tevent_context(
                return NULL;
        }
 
+       if (tevent_context_is_wrapper(ev)) {
+               /*
+                * This is really a programmer error!
+                */
+               DBG_ERR("Should not be used with a wrapper tevent context\n");
+               return NULL;
+       }
+
        fde = talloc(mem_ctx, struct messaging_dgm_fde);
        if (fde == NULL) {
                return NULL;
index 470dfbeabc74f8650b4eb38b89835aad58216da5..fd170961746832edff80345934ae771004a6e3b3 100644 (file)
@@ -55,6 +55,18 @@ void *messaging_dgm_ref(TALLOC_CTX *mem_ctx, struct tevent_context *ev,
 {
        struct msg_dgm_ref *result, *tmp_refs;
 
+       if (tevent_context_is_wrapper(ev)) {
+               /*
+                * This is really a programmer error!
+                *
+                * The main/raw tevent context should
+                * have been registered first!
+                */
+               DBG_ERR("Should not be used with a wrapper tevent context\n");
+               *err = EINVAL;
+               return NULL;
+       }
+
        result = talloc(mem_ctx, struct msg_dgm_ref);
        if (result == NULL) {
                *err = ENOMEM;