Blame SOURCES/0106-nss-add-a-netgroup-counter-to-struct-nss_enum_index.patch

086f82
From f9859498b52d89bf60dbddd898752f859f4952d3 Mon Sep 17 00:00:00 2001
086f82
From: Sumit Bose <sbose@redhat.com>
086f82
Date: Thu, 15 Mar 2018 12:50:20 +0100
086f82
Subject: [PATCH] nss: add a netgroup counter to struct nss_enum_index
086f82
086f82
Netgroups are not looked up with the help of a single request but by
086f82
calling setnetgrent(), getnetgrent() and endnetgrent() where
086f82
getnetgrent() might be called multiple times depending on the number of
086f82
netgroup elements. Since the caller does not provide a state the state
086f82
has to be maintained by the SSSD nss responder. Besides the netgroup
086f82
name this is mainly the number of elements already returned.
086f82
086f82
This number is used to select the next element to return and currently
086f82
it is assumed that there are not changes to the netgroup while the
086f82
client is requesting the individual elements. But if e.g. the 3 nss
086f82
calls are not used correctly or the netgroup is modified while the
086f82
client is sending getnetgrent() calls the stored number might be out of
086f82
range. To be on the safe side the stored number should be always
086f82
compared with the current number of netgroup elements.
086f82
086f82
Related to https://pagure.io/SSSD/sssd/issue/3679
086f82
086f82
Reviewed-by: Jakub Hrozek <jhrozek@redhat.com>
086f82
(cherry picked from commit 08db22b1b1a2e742edbca92e35087294d963adda)
086f82
086f82
DOWNSTREAM:
086f82
Resolves: rhbz#1579703 - crash in nss_protocol_fill_netgrent. sssd_nss[19234]: segfault at 80 ip 000055612688c2a0 sp 00007ffddf9b9cd0 error 4 in sssd_nss[55612687e000+39000] [rhel-7.5.z]
086f82
---
086f82
 src/db/sysdb.h                         | 3 ++-
086f82
 src/db/sysdb_search.c                  | 5 ++++-
086f82
 src/responder/nss/nss_enum.c           | 3 ++-
086f82
 src/responder/nss/nss_private.h        | 1 +
086f82
 src/responder/nss/nss_protocol_netgr.c | 7 +++++++
086f82
 5 files changed, 16 insertions(+), 3 deletions(-)
086f82
086f82
diff --git a/src/db/sysdb.h b/src/db/sysdb.h
086f82
index fd18ecefed2b2c5f35060fa47fd160a8968e073b..2660314a75a574d7f5625c8672e5261587056d1a 100644
086f82
--- a/src/db/sysdb.h
086f82
+++ b/src/db/sysdb.h
086f82
@@ -1219,7 +1219,8 @@ errno_t sysdb_attrs_to_list(TALLOC_CTX *mem_ctx,
086f82
 
086f82
 errno_t sysdb_netgr_to_entries(TALLOC_CTX *mem_ctx,
086f82
                                struct ldb_result *res,
086f82
-                               struct sysdb_netgroup_ctx ***entries);
086f82
+                               struct sysdb_netgroup_ctx ***entries,
086f82
+                               size_t *netgroup_count);
086f82
 
086f82
 errno_t sysdb_dn_sanitize(TALLOC_CTX *mem_ctx, const char *input,
086f82
                           char **sanitized);
086f82
diff --git a/src/db/sysdb_search.c b/src/db/sysdb_search.c
086f82
index a6a81e23d257331614085403b4dca8ded860600b..9f37cbcd50a778145518c15b6146ad812a5b4fa3 100644
086f82
--- a/src/db/sysdb_search.c
086f82
+++ b/src/db/sysdb_search.c
086f82
@@ -1829,7 +1829,8 @@ done:
086f82
 
086f82
 errno_t sysdb_netgr_to_entries(TALLOC_CTX *mem_ctx,
086f82
                                struct ldb_result *res,
086f82
-                               struct sysdb_netgroup_ctx ***entries)
086f82
+                               struct sysdb_netgroup_ctx ***entries,
086f82
+                               size_t *netgroup_count)
086f82
 {
086f82
     errno_t ret;
086f82
     size_t size = 0;
086f82
@@ -1933,6 +1934,8 @@ errno_t sysdb_netgr_to_entries(TALLOC_CTX *mem_ctx,
086f82
     tmp_entry[c] = NULL;
086f82
 
086f82
     *entries = talloc_steal(mem_ctx, tmp_entry);
086f82
+    *netgroup_count = c;
086f82
+
086f82
     ret = EOK;
086f82
 
086f82
 done:
086f82
diff --git a/src/responder/nss/nss_enum.c b/src/responder/nss/nss_enum.c
086f82
index da844fbced529f606a3e98669fb7b95e0696ce00..b2b22bbae8a373ed3abb47381fabd989d4931690 100644
086f82
--- a/src/responder/nss/nss_enum.c
086f82
+++ b/src/responder/nss/nss_enum.c
086f82
@@ -144,7 +144,8 @@ static void nss_setent_internal_done(struct tevent_req *subreq)
086f82
             /* We need to expand the netgroup into triples and members. */
086f82
             ret = sysdb_netgr_to_entries(state->enum_ctx,
086f82
                                          result[0]->ldb_result,
086f82
-                                         &state->enum_ctx->netgroup);
086f82
+                                         &state->enum_ctx->netgroup,
086f82
+                                         &state->enum_ctx->netgroup_count);
086f82
             if (ret != EOK) {
086f82
                 goto done;
086f82
             }
086f82
diff --git a/src/responder/nss/nss_private.h b/src/responder/nss/nss_private.h
086f82
index 5fc19d26be9adda4d967086e7b239e49a78866ee..aa8d8e9cde0d73e72d3aa4c186f104d6baae411f 100644
086f82
--- a/src/responder/nss/nss_private.h
086f82
+++ b/src/responder/nss/nss_private.h
086f82
@@ -41,6 +41,7 @@ struct nss_enum_index {
086f82
 struct nss_enum_ctx {
086f82
     struct cache_req_result **result;
086f82
     struct sysdb_netgroup_ctx **netgroup;
086f82
+    size_t netgroup_count;
086f82
 
086f82
     /* Ongoing cache request that is constructing enumeration result. */
086f82
     struct tevent_req *ongoing;
086f82
diff --git a/src/responder/nss/nss_protocol_netgr.c b/src/responder/nss/nss_protocol_netgr.c
086f82
index ed04fd25821031554e20e14afebaca9b828a748b..9f27c6b78d47f188dea99600a634a18be2512bfb 100644
086f82
--- a/src/responder/nss/nss_protocol_netgr.c
086f82
+++ b/src/responder/nss/nss_protocol_netgr.c
086f82
@@ -126,6 +126,13 @@ nss_protocol_fill_netgrent(struct nss_ctx *nss_ctx,
086f82
     idx = cmd_ctx->enum_index;
086f82
     entries = cmd_ctx->enum_ctx->netgroup;
086f82
 
086f82
+    if (idx->result > cmd_ctx->enum_ctx->netgroup_count) {
086f82
+        DEBUG(SSSDBG_CRIT_FAILURE,
086f82
+              "Unconsistent state while processing netgroups.\n");
086f82
+        ret = EINVAL;
086f82
+        goto done;
086f82
+    }
086f82
+
086f82
     /* First two fields (length and reserved), filled up later. */
086f82
     ret = sss_packet_grow(packet, 2 * sizeof(uint32_t));
086f82
     if (ret != EOK) {
086f82
-- 
086f82
2.17.0
086f82