]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
Fix race condition in cleanup part of dns_dt_create()
authorOndřej Surý <ondrej@sury.org>
Fri, 23 Nov 2018 21:05:08 +0000 (22:05 +0100)
committerOndřej Surý <ondrej@sury.org>
Tue, 15 Jan 2019 08:43:10 +0000 (09:43 +0100)
(cherry picked from commit 482dd7eed379382d627e81822bad10e876501dc4)

lib/dns/dnstap.c

index bc15b23107d92f0a5511f3dbb858afa797468316..6ff4e57ce0b420f64d3728f9740f8b65fa3ea5fb 100644 (file)
@@ -216,26 +216,30 @@ dns_dt_create2(isc_mem_t *mctx, dns_dtmode_t mode, const char *path,
        generation++;
 
        env = isc_mem_get(mctx, sizeof(dns_dtenv_t));
-       if (env == NULL)
-               CHECK(ISC_R_NOMEMORY);
 
        memset(env, 0, sizeof(dns_dtenv_t));
 
+       env->magic = DTENV_MAGIC;
+       isc_mem_attach(mctx, &env->mctx);
+       env->reopen_task = reopen_task;
+       isc_mutex_init(&env->reopen_lock);
+       env->reopen_queued = false;
+       env->path = isc_mem_strdup(mctx, path);
+
        CHECK(isc_refcount_init(&env->refcount, 1));
        CHECK(isc_stats_create(mctx, &env->stats, dns_dnstapcounter_max));
-       env->path = isc_mem_strdup(mctx, path);
-       if (env->path == NULL)
-               CHECK(ISC_R_NOMEMORY);
 
        fwopt = fstrm_writer_options_init();
-       if (fwopt == NULL)
+       if (fwopt == NULL) {
                CHECK(ISC_R_NOMEMORY);
+       }
 
        res = fstrm_writer_options_add_content_type(fwopt,
                                            DNSTAP_CONTENT_TYPE,
                                            sizeof(DNSTAP_CONTENT_TYPE) - 1);
-       if (res != fstrm_res_success)
+       if (res != fstrm_res_success) {
                CHECK(ISC_R_FAILURE);
+       }
 
        if (mode == dns_dtmode_file) {
                ffwopt = fstrm_file_options_init();
@@ -250,11 +254,13 @@ dns_dt_create2(isc_mem_t *mctx, dns_dtmode_t mode, const char *path,
                                                                  env->path);
                        fw = fstrm_unix_writer_init(fuwopt, fwopt);
                }
-       } else
+       } else {
                CHECK(ISC_R_FAILURE);
+       }
 
-       if (fw == NULL)
+       if (fw == NULL) {
                CHECK(ISC_R_FAILURE);
+       }
 
        env->iothr = fstrm_iothr_init(*foptp, &fw);
        if (env->iothr == NULL) {
@@ -270,13 +276,6 @@ dns_dt_create2(isc_mem_t *mctx, dns_dtmode_t mode, const char *path,
        env->fopt = *foptp;
        *foptp = NULL;
 
-       env->reopen_task = reopen_task;
-       isc_mutex_init(&env->reopen_lock);
-       env->reopen_queued = false;
-
-       isc_mem_attach(mctx, &env->mctx);
-
-       env->magic = DTENV_MAGIC;
        *envp = env;
 
  cleanup:
@@ -290,16 +289,12 @@ dns_dt_create2(isc_mem_t *mctx, dns_dtmode_t mode, const char *path,
                fstrm_writer_options_destroy(&fwopt);
 
        if (result != ISC_R_SUCCESS) {
-               if (env != NULL) {
-                       isc_mutex_destroy(&env->reopen_lock);
-                       if (env->mctx != NULL)
-                               isc_mem_detach(&env->mctx);
-                       if (env->path != NULL)
-                               isc_mem_free(mctx, env->path);
-                       if (env->stats != NULL)
-                               isc_stats_detach(&env->stats);
-                       isc_mem_put(mctx, env, sizeof(dns_dtenv_t));
+               isc_mutex_destroy(&env->reopen_lock);
+               isc_mem_free(mctx, env->path);
+               if (env->stats != NULL) {
+                       isc_stats_detach(&env->stats);
                }
+               isc_mem_putanddetach(&env->mctx, env, sizeof(dns_dtenv_t));
        }
 
        return (result);