Blame SOURCES/0010-nss-check-if-groups-are-filtered-during-initgroups.patch

8ad293
From c87b2208b9a58c12eeceb5b8ccf9c34dcd835b8d Mon Sep 17 00:00:00 2001
8ad293
From: Sumit Bose <sbose@redhat.com>
8ad293
Date: Tue, 17 Nov 2020 12:59:23 +0100
8ad293
Subject: [PATCH] nss: check if groups are filtered during initgroups
8ad293
MIME-Version: 1.0
8ad293
Content-Type: text/plain; charset=UTF-8
8ad293
Content-Transfer-Encoding: 8bit
8ad293
8ad293
If groups are filtered, i.e. SSSD should not handle them, they should
8ad293
not appear in the group list returned by an initgroups request.
8ad293
8ad293
Resolves: https://github.com/SSSD/sssd/issues/5403
8ad293
8ad293
Reviewed-by: Pavel Březina <pbrezina@redhat.com>
8ad293
---
8ad293
 src/responder/nss/nss_protocol_grent.c | 35 ++++++++++++++++++++++++++
8ad293
 src/tests/intg/test_ldap.py            | 12 +++++++++
8ad293
 2 files changed, 47 insertions(+)
8ad293
8ad293
diff --git a/src/responder/nss/nss_protocol_grent.c b/src/responder/nss/nss_protocol_grent.c
8ad293
index 8f1d3fe81..135b392f7 100644
8ad293
--- a/src/responder/nss/nss_protocol_grent.c
8ad293
+++ b/src/responder/nss/nss_protocol_grent.c
8ad293
@@ -326,6 +326,34 @@ done:
8ad293
     return EOK;
8ad293
 }
8ad293
 
8ad293
+static bool is_group_filtered(struct sss_nc_ctx *ncache,
8ad293
+                              struct sss_domain_info *domain,
8ad293
+                              const char *grp_name, gid_t gid)
8ad293
+{
8ad293
+    int ret;
8ad293
+
8ad293
+    if (grp_name == NULL) {
8ad293
+        DEBUG(SSSDBG_CRIT_FAILURE,
8ad293
+              "Group with gid [%"SPRIgid"] has no name, this should never "
8ad293
+              "happen, trying to continue without.\n", gid);
8ad293
+    } else {
8ad293
+        ret = sss_ncache_check_group(ncache, domain, grp_name);
8ad293
+        if (ret == EEXIST) {
8ad293
+            DEBUG(SSSDBG_TRACE_FUNC, "Group [%s] is filtered out! "
8ad293
+                                     "(negative cache)", grp_name);
8ad293
+            return true;
8ad293
+        }
8ad293
+    }
8ad293
+    ret = sss_ncache_check_gid(ncache, domain, gid);
8ad293
+    if (ret == EEXIST) {
8ad293
+        DEBUG(SSSDBG_TRACE_FUNC, "Group [%"SPRIgid"] is filtered out! "
8ad293
+                                 "(negative cache)", gid);
8ad293
+        return true;
8ad293
+    }
8ad293
+
8ad293
+    return false;
8ad293
+}
8ad293
+
8ad293
 errno_t
8ad293
 nss_protocol_fill_initgr(struct nss_ctx *nss_ctx,
8ad293
                          struct nss_cmd_ctx *cmd_ctx,
8ad293
@@ -344,6 +372,7 @@ nss_protocol_fill_initgr(struct nss_ctx *nss_ctx,
8ad293
     size_t body_len;
8ad293
     size_t rp;
8ad293
     gid_t gid;
8ad293
+    const char *grp_name;
8ad293
     gid_t orig_gid;
8ad293
     errno_t ret;
8ad293
     int i;
8ad293
@@ -392,6 +421,8 @@ nss_protocol_fill_initgr(struct nss_ctx *nss_ctx,
8ad293
         gid = sss_view_ldb_msg_find_attr_as_uint64(domain, msg, SYSDB_GIDNUM,
8ad293
                                                    0);
8ad293
         posix = ldb_msg_find_attr_as_string(msg, SYSDB_POSIX, NULL);
8ad293
+        grp_name = sss_view_ldb_msg_find_attr_as_string(domain, msg, SYSDB_NAME,
8ad293
+                                                        NULL);
8ad293
 
8ad293
         if (gid == 0) {
8ad293
             if (posix != NULL && strcmp(posix, "FALSE") == 0) {
8ad293
@@ -404,6 +435,10 @@ nss_protocol_fill_initgr(struct nss_ctx *nss_ctx,
8ad293
             }
8ad293
         }
8ad293
 
8ad293
+        if (is_group_filtered(nss_ctx->rctx->ncache, domain, grp_name, gid)) {
8ad293
+            continue;
8ad293
+        }
8ad293
+
8ad293
         SAFEALIGN_COPY_UINT32(&body[rp], &gid, &rp);
8ad293
         num_results++;
8ad293
 
8ad293
diff --git a/src/tests/intg/test_ldap.py b/src/tests/intg/test_ldap.py
8ad293
index 194d7d9cc..6a78c960f 100644
8ad293
--- a/src/tests/intg/test_ldap.py
8ad293
+++ b/src/tests/intg/test_ldap.py
8ad293
@@ -1190,6 +1190,18 @@ def test_nss_filters(ldap_conn, sanity_nss_filter):
8ad293
     with pytest.raises(KeyError):
8ad293
         grp.getgrgid(14)
8ad293
 
8ad293
+    # test initgroups - user1 is member of group_two_one_user_groups (2019)
8ad293
+    # which is filtered out
8ad293
+    (res, errno, gids) = sssd_id.call_sssd_initgroups("user1", 2001)
8ad293
+    assert res == sssd_id.NssReturnCode.SUCCESS
8ad293
+
8ad293
+    user_with_group_ids = [2001, 2012, 2015, 2017, 2018]
8ad293
+    assert sorted(gids) == sorted(user_with_group_ids), \
8ad293
+        "result: %s\n expected %s" % (
8ad293
+            ", ".join(["%s" % s for s in sorted(gids)]),
8ad293
+            ", ".join(["%s" % s for s in sorted(user_with_group_ids)])
8ad293
+        )
8ad293
+
8ad293
 
8ad293
 @pytest.fixture
8ad293
 def sanity_nss_filter_cached(request, ldap_conn):
8ad293
-- 
8ad293
2.21.3
8ad293