Blame SOURCES/0049-nss-use-enumeration-context-as-talloc-parent-for-cac.patch

0d602d
From 720a423a0119f23606c6029c3f48be98841ca910 Mon Sep 17 00:00:00 2001
0d602d
From: =?UTF-8?q?Pavel=20B=C5=99ezina?= <pbrezina@redhat.com>
0d602d
Date: Tue, 30 Oct 2018 13:21:28 +0100
0d602d
Subject: [PATCH] nss: use enumeration context as talloc parent for cache req
0d602d
 result
0d602d
0d602d
Otherwise we end up with memory leak since the result is never freed.
0d602d
0d602d
We need to convert nctx->*ent structures into talloc pointer so
0d602d
we can use enum_ctx as parent.
0d602d
0d602d
Resolves:
0d602d
https://pagure.io/SSSD/sssd/issue/3870
0d602d
0d602d
Reviewed-by: Jakub Hrozek <jhrozek@redhat.com>
0d602d
(cherry picked from commit 406b731ddfbeb62623640cc37a7adc76af0a4b22)
0d602d
---
0d602d
 src/responder/nss/nss_cmd.c     | 12 ++++++------
0d602d
 src/responder/nss/nss_enum.c    |  2 +-
0d602d
 src/responder/nss/nss_private.h |  6 +++---
0d602d
 src/responder/nss/nsssrv.c      | 21 +++++++++++++++++++++
0d602d
 4 files changed, 31 insertions(+), 10 deletions(-)
0d602d
0d602d
diff --git a/src/responder/nss/nss_cmd.c b/src/responder/nss/nss_cmd.c
0d602d
index 9ee6ca805..25e663ed5 100644
0d602d
--- a/src/responder/nss/nss_cmd.c
0d602d
+++ b/src/responder/nss/nss_cmd.c
0d602d
@@ -942,7 +942,7 @@ static errno_t nss_cmd_setpwent(struct cli_ctx *cli_ctx)
0d602d
 
0d602d
     nss_ctx = talloc_get_type(cli_ctx->rctx->pvt_ctx, struct nss_ctx);
0d602d
 
0d602d
-    return nss_setent(cli_ctx, CACHE_REQ_ENUM_USERS, &nss_ctx->pwent);
0d602d
+    return nss_setent(cli_ctx, CACHE_REQ_ENUM_USERS, nss_ctx->pwent);
0d602d
 }
0d602d
 
0d602d
 static errno_t nss_cmd_getpwent(struct cli_ctx *cli_ctx)
0d602d
@@ -955,7 +955,7 @@ static errno_t nss_cmd_getpwent(struct cli_ctx *cli_ctx)
0d602d
 
0d602d
     return nss_getent(cli_ctx, CACHE_REQ_ENUM_USERS,
0d602d
                       &state_ctx->pwent, nss_protocol_fill_pwent,
0d602d
-                      &nss_ctx->pwent);
0d602d
+                      nss_ctx->pwent);
0d602d
 }
0d602d
 
0d602d
 static errno_t nss_cmd_endpwent(struct cli_ctx *cli_ctx)
0d602d
@@ -998,7 +998,7 @@ static errno_t nss_cmd_setgrent(struct cli_ctx *cli_ctx)
0d602d
 
0d602d
     nss_ctx = talloc_get_type(cli_ctx->rctx->pvt_ctx, struct nss_ctx);
0d602d
 
0d602d
-    return nss_setent(cli_ctx, CACHE_REQ_ENUM_GROUPS, &nss_ctx->grent);
0d602d
+    return nss_setent(cli_ctx, CACHE_REQ_ENUM_GROUPS, nss_ctx->grent);
0d602d
 }
0d602d
 
0d602d
 static errno_t nss_cmd_getgrent(struct cli_ctx *cli_ctx)
0d602d
@@ -1011,7 +1011,7 @@ static errno_t nss_cmd_getgrent(struct cli_ctx *cli_ctx)
0d602d
 
0d602d
     return nss_getent(cli_ctx, CACHE_REQ_ENUM_GROUPS,
0d602d
                       &state_ctx->grent, nss_protocol_fill_grent,
0d602d
-                      &nss_ctx->grent);
0d602d
+                      nss_ctx->grent);
0d602d
 }
0d602d
 
0d602d
 static errno_t nss_cmd_endgrent(struct cli_ctx *cli_ctx)
0d602d
@@ -1093,7 +1093,7 @@ static errno_t nss_cmd_setservent(struct cli_ctx *cli_ctx)
0d602d
 
0d602d
     nss_ctx = talloc_get_type(cli_ctx->rctx->pvt_ctx, struct nss_ctx);
0d602d
 
0d602d
-    return nss_setent(cli_ctx, CACHE_REQ_ENUM_SVC, &nss_ctx->svcent);
0d602d
+    return nss_setent(cli_ctx, CACHE_REQ_ENUM_SVC, nss_ctx->svcent);
0d602d
 }
0d602d
 
0d602d
 static errno_t nss_cmd_getservent(struct cli_ctx *cli_ctx)
0d602d
@@ -1106,7 +1106,7 @@ static errno_t nss_cmd_getservent(struct cli_ctx *cli_ctx)
0d602d
 
0d602d
     return nss_getent(cli_ctx, CACHE_REQ_ENUM_SVC,
0d602d
                       &state_ctx->svcent, nss_protocol_fill_svcent,
0d602d
-                      &nss_ctx->svcent);
0d602d
+                      nss_ctx->svcent);
0d602d
 }
0d602d
 
0d602d
 static errno_t nss_cmd_endservent(struct cli_ctx *cli_ctx)
0d602d
diff --git a/src/responder/nss/nss_enum.c b/src/responder/nss/nss_enum.c
0d602d
index a45b65233..9588943c9 100644
0d602d
--- a/src/responder/nss/nss_enum.c
0d602d
+++ b/src/responder/nss/nss_enum.c
0d602d
@@ -138,7 +138,7 @@ static void nss_setent_internal_done(struct tevent_req *subreq)
0d602d
     switch (ret) {
0d602d
     case EOK:
0d602d
         talloc_zfree(state->enum_ctx->result);
0d602d
-        state->enum_ctx->result = talloc_steal(state->nss_ctx, result);
0d602d
+        state->enum_ctx->result = talloc_steal(state->enum_ctx, result);
0d602d
 
0d602d
         if (state->type == CACHE_REQ_NETGROUP_BY_NAME) {
0d602d
             /* We need to expand the netgroup into triples and members. */
0d602d
diff --git a/src/responder/nss/nss_private.h b/src/responder/nss/nss_private.h
0d602d
index aa8d8e9cd..cd0d35517 100644
0d602d
--- a/src/responder/nss/nss_private.h
0d602d
+++ b/src/responder/nss/nss_private.h
0d602d
@@ -78,9 +78,9 @@ struct nss_ctx {
0d602d
     const char **extra_attributes;
0d602d
 
0d602d
     /* Enumeration. */
0d602d
-    struct nss_enum_ctx pwent;
0d602d
-    struct nss_enum_ctx grent;
0d602d
-    struct nss_enum_ctx svcent;
0d602d
+    struct nss_enum_ctx *pwent;
0d602d
+    struct nss_enum_ctx *grent;
0d602d
+    struct nss_enum_ctx *svcent;
0d602d
     hash_table_t *netgrent;
0d602d
 
0d602d
     /* Memory cache. */
0d602d
diff --git a/src/responder/nss/nsssrv.c b/src/responder/nss/nsssrv.c
0d602d
index 004e6c1a1..d6c5a08a9 100644
0d602d
--- a/src/responder/nss/nsssrv.c
0d602d
+++ b/src/responder/nss/nsssrv.c
0d602d
@@ -378,6 +378,27 @@ int nss_process_init(TALLOC_CTX *mem_ctx,
0d602d
         goto fail;
0d602d
     }
0d602d
 
0d602d
+    nctx->pwent = talloc_zero(nctx, struct nss_enum_ctx);
0d602d
+    if (nctx->pwent == NULL) {
0d602d
+        DEBUG(SSSDBG_FATAL_FAILURE, "Unable to initialize pwent context!\n");
0d602d
+        ret = ENOMEM;
0d602d
+        goto fail;
0d602d
+    }
0d602d
+
0d602d
+    nctx->grent = talloc_zero(nctx, struct nss_enum_ctx);
0d602d
+    if (nctx->grent == NULL) {
0d602d
+        DEBUG(SSSDBG_FATAL_FAILURE, "Unable to initialize grent context!\n");
0d602d
+        ret = ENOMEM;
0d602d
+        goto fail;
0d602d
+    }
0d602d
+
0d602d
+    nctx->svcent = talloc_zero(nctx, struct nss_enum_ctx);
0d602d
+    if (nctx->svcent == NULL) {
0d602d
+        DEBUG(SSSDBG_FATAL_FAILURE, "Unable to initialize svcent context!\n");
0d602d
+        ret = ENOMEM;
0d602d
+        goto fail;
0d602d
+    }
0d602d
+
0d602d
     nctx->netgrent = sss_ptr_hash_create(nctx, NULL, NULL);
0d602d
     if (nctx->netgrent == NULL) {
0d602d
         DEBUG(SSSDBG_FATAL_FAILURE, "Unable to initialize netgroups table!\n");
0d602d
-- 
0d602d
2.19.1
0d602d