From 7f2afc20e1b6397c364a98d1be006377c95e4665 Mon Sep 17 00:00:00 2001 From: Stefan Metzmacher Date: Fri, 18 May 2018 16:28:47 +0200 Subject: s3:messages: protect against usage of wrapper tevent_context objects for messaging 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 Reviewed-by: Ralph Boehme --- source3/lib/messages.c | 23 +++++++++++++++++++++++ source3/lib/messages_ctdb.c | 8 ++++++++ source3/lib/messages_ctdb_ref.c | 12 ++++++++++++ source3/lib/messages_dgm.c | 14 ++++++++++++++ source3/lib/messages_dgm_ref.c | 12 ++++++++++++ 5 files changed, 69 insertions(+) (limited to 'source3') diff --git a/source3/lib/messages.c b/source3/lib/messages.c index 82a177856f6..1a5ea4659aa 100644 --- a/source3/lib/messages.c +++ b/source3/lib/messages.c @@ -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) { diff --git a/source3/lib/messages_ctdb.c b/source3/lib/messages_ctdb.c index d3e2e3f8589..a1aeb37af19 100644 --- a/source3/lib/messages_ctdb.c +++ b/source3/lib/messages_ctdb.c @@ -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; diff --git a/source3/lib/messages_ctdb_ref.c b/source3/lib/messages_ctdb_ref.c index 3570ed8ae4c..47b4b758dac 100644 --- a/source3/lib/messages_ctdb_ref.c +++ b/source3/lib/messages_ctdb_ref.c @@ -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; diff --git a/source3/lib/messages_dgm.c b/source3/lib/messages_dgm.c index b8878b68b99..1c76615093c 100644 --- a/source3/lib/messages_dgm.c +++ b/source3/lib/messages_dgm.c @@ -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; diff --git a/source3/lib/messages_dgm_ref.c b/source3/lib/messages_dgm_ref.c index 470dfbeabc7..fd170961746 100644 --- a/source3/lib/messages_dgm_ref.c +++ b/source3/lib/messages_dgm_ref.c @@ -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; -- cgit v1.2.1