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

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