dpward / rpms / sssd

Forked from rpms/sssd 3 years ago
Clone

Blame SOURCES/0087-cache-Check-for-max_id-min_id-in-cache_req.patch

ced1f5
From 2f712c8fe0ecaa07f7b15ebeae5213978d033278 Mon Sep 17 00:00:00 2001
976a3f
From: amitkuma <amitkuma@redhat.com>
976a3f
Date: Thu, 30 Nov 2017 22:18:39 +0530
ced1f5
Subject: [PATCH 87/87] cache: Check for max_id/min_id in cache_req
976a3f
MIME-Version: 1.0
976a3f
Content-Type: text/plain; charset=UTF-8
976a3f
Content-Transfer-Encoding: 8bit
976a3f
976a3f
The cache_req code doesn't check the min_id/max_id
976a3f
boundaries for requests by ID.
976a3f
Extending the .lookup_fn function in each plugin
976a3f
that searches by ID for a check that returns non-zero
976a3f
if the entry is out of the range and 0 if not.
976a3f
976a3f
Resolves: https://pagure.io/SSSD/sssd/issue/3569
976a3f
976a3f
Reviewed-by: Jakub Hrozek <jhrozek@redhat.com>
976a3f
Reviewed-by: Fabiano FidĂȘncio <fidencio@redhat.com>
976a3f
(cherry picked from commit 2af80640f18966d65cf82106059ce3c060df93bf)
976a3f
---
ced1f5
 src/responder/common/cache_req/cache_req.c         |   1 +
ced1f5
 src/responder/common/cache_req/cache_req_private.h |   3 +
ced1f5
 src/responder/common/cache_req/cache_req_search.c  |   5 +
ced1f5
 .../common/cache_req/plugins/cache_req_common.c    |  11 ++
ced1f5
 .../cache_req/plugins/cache_req_group_by_id.c      |   6 +
ced1f5
 .../cache_req/plugins/cache_req_object_by_id.c     |   6 +
ced1f5
 .../cache_req/plugins/cache_req_user_by_id.c       |   5 +
ced1f5
 src/tests/cmocka/test_responder_cache_req.c        | 127 +++++++++++++++++----
ced1f5
 src/util/util_errors.c                             |   1 +
ced1f5
 src/util/util_errors.h                             |   1 +
ced1f5
 10 files changed, 141 insertions(+), 25 deletions(-)
976a3f
976a3f
diff --git a/src/responder/common/cache_req/cache_req.c b/src/responder/common/cache_req/cache_req.c
ced1f5
index ad9bc040dd999a205713141e6a1512e47b69c45e..134688b0f62c6546763d91468af3f54b73b6073a 100644
976a3f
--- a/src/responder/common/cache_req/cache_req.c
976a3f
+++ b/src/responder/common/cache_req/cache_req.c
ced1f5
@@ -953,6 +953,7 @@ static void cache_req_search_domains_done(struct tevent_req *subreq)
ced1f5
             goto done;
976a3f
         }
976a3f
         break;
976a3f
+    case ERR_ID_OUTSIDE_RANGE:
976a3f
     case ENOENT:
976a3f
         if (state->check_next == false) {
976a3f
             /* Not found. */
976a3f
diff --git a/src/responder/common/cache_req/cache_req_private.h b/src/responder/common/cache_req/cache_req_private.h
ced1f5
index 95f24c0e5b9ab1150591d308c7288c57fe478c5d..9538b9568ca7f77e377cfee67235c8a52ebbe454 100644
976a3f
--- a/src/responder/common/cache_req/cache_req_private.h
976a3f
+++ b/src/responder/common/cache_req/cache_req_private.h
ced1f5
@@ -192,4 +192,7 @@ cache_reg_common_get_acct_domain_recv(TALLOC_CTX *mem_ctx,
ced1f5
                                       struct tevent_req *subreq,
ced1f5
                                       struct cache_req *cr,
ced1f5
                                       char **_domain);
ced1f5
+
976a3f
+errno_t cache_req_idminmax_check(struct cache_req_data *data,
976a3f
+                                 struct sss_domain_info *domain);
976a3f
 #endif /* _CACHE_REQ_PRIVATE_H_ */
976a3f
diff --git a/src/responder/common/cache_req/cache_req_search.c b/src/responder/common/cache_req/cache_req_search.c
ced1f5
index 3365962d473b0982945de2541e44ba86b43a0db5..7423feb6305df87d368bcc10ba28b9b29d57ecf0 100644
976a3f
--- a/src/responder/common/cache_req/cache_req_search.c
976a3f
+++ b/src/responder/common/cache_req/cache_req_search.c
ced1f5
@@ -203,6 +203,11 @@ static errno_t cache_req_search_cache(TALLOC_CTX *mem_ctx,
976a3f
 
976a3f
         *_result = result;
976a3f
         break;
976a3f
+    case ERR_ID_OUTSIDE_RANGE:
976a3f
+        CACHE_REQ_DEBUG(SSSDBG_TRACE_FUNC, cr,
976a3f
+                        "ID [%s] was filtered out\n",
976a3f
+                        cr->debugobj);
976a3f
+        break;
976a3f
     case ENOENT:
976a3f
         CACHE_REQ_DEBUG(SSSDBG_TRACE_FUNC, cr,
976a3f
                         "Object [%s] was not found in cache\n",
976a3f
diff --git a/src/responder/common/cache_req/plugins/cache_req_common.c b/src/responder/common/cache_req/plugins/cache_req_common.c
ced1f5
index 408c91949ceb3ecaf743f270f58f4e3fcfc3ccb1..bb11eaa86a8bca3f9d15afe48dab9921319d184e 100644
976a3f
--- a/src/responder/common/cache_req/plugins/cache_req_common.c
976a3f
+++ b/src/responder/common/cache_req/plugins/cache_req_common.c
976a3f
@@ -26,6 +26,17 @@
976a3f
 #include "providers/data_provider.h"
976a3f
 #include "responder/common/cache_req/cache_req_plugin.h"
976a3f
 
976a3f
+errno_t cache_req_idminmax_check(struct cache_req_data *data,
976a3f
+	                         struct sss_domain_info *domain)
976a3f
+{
976a3f
+   if (((domain->id_min != 0) && (data->id < domain->id_min)) ||
976a3f
+       ((domain->id_max != 0) && (data->id > domain->id_max))) {
976a3f
+        DEBUG(SSSDBG_FUNC_DATA, "id exceeds min/max boundaries\n");
976a3f
+        return ERR_ID_OUTSIDE_RANGE;
976a3f
+   }
976a3f
+   return EOK;
976a3f
+}
976a3f
+
976a3f
 static struct ldb_message *
976a3f
 cache_req_well_known_sid_msg(TALLOC_CTX *mem_ctx,
976a3f
                              const char *sid,
976a3f
diff --git a/src/responder/common/cache_req/plugins/cache_req_group_by_id.c b/src/responder/common/cache_req/plugins/cache_req_group_by_id.c
ced1f5
index ce84b1b4458b447ff6b4b036c6e8fe8f4d7758c8..d178283c33c84e277b83772d04973aa6069af967 100644
976a3f
--- a/src/responder/common/cache_req/plugins/cache_req_group_by_id.c
976a3f
+++ b/src/responder/common/cache_req/plugins/cache_req_group_by_id.c
ced1f5
@@ -81,6 +81,12 @@ cache_req_group_by_id_lookup(TALLOC_CTX *mem_ctx,
976a3f
                              struct sss_domain_info *domain,
976a3f
                              struct ldb_result **_result)
976a3f
 {
976a3f
+    errno_t ret;
976a3f
+
976a3f
+    ret = cache_req_idminmax_check(data, domain);
976a3f
+    if (ret != EOK) {
976a3f
+	return ret;
976a3f
+    }
976a3f
     return sysdb_getgrgid_with_views(mem_ctx, domain, data->id, _result);
976a3f
 }
976a3f
 
976a3f
diff --git a/src/responder/common/cache_req/plugins/cache_req_object_by_id.c b/src/responder/common/cache_req/plugins/cache_req_object_by_id.c
ced1f5
index 1327b480c1b1b68f9826fa229c9b001f2d92b79b..be9488d298885320139ccfcd3c59a83ff088e77d 100644
976a3f
--- a/src/responder/common/cache_req/plugins/cache_req_object_by_id.c
976a3f
+++ b/src/responder/common/cache_req/plugins/cache_req_object_by_id.c
ced1f5
@@ -110,6 +110,12 @@ cache_req_object_by_id_lookup(TALLOC_CTX *mem_ctx,
976a3f
                               struct sss_domain_info *domain,
976a3f
                               struct ldb_result **_result)
976a3f
 {
976a3f
+    errno_t ret;
976a3f
+
976a3f
+    ret = cache_req_idminmax_check(data, domain);
976a3f
+    if (ret != EOK) {
976a3f
+        return ret;
976a3f
+    }
976a3f
     return sysdb_search_object_by_id(mem_ctx, domain, data->id,
976a3f
                                      data->attrs, _result);
976a3f
 }
976a3f
diff --git a/src/responder/common/cache_req/plugins/cache_req_user_by_id.c b/src/responder/common/cache_req/plugins/cache_req_user_by_id.c
ced1f5
index 656fa41af5f39f68c64e241aa97c4eaf3ec57395..151c3e17acf6ef0d958d5a73a36e1c93b9e7a9a9 100644
976a3f
--- a/src/responder/common/cache_req/plugins/cache_req_user_by_id.c
976a3f
+++ b/src/responder/common/cache_req/plugins/cache_req_user_by_id.c
ced1f5
@@ -81,6 +81,11 @@ cache_req_user_by_id_lookup(TALLOC_CTX *mem_ctx,
976a3f
                             struct sss_domain_info *domain,
976a3f
                             struct ldb_result **_result)
976a3f
 {
976a3f
+    errno_t ret;
976a3f
+    ret = cache_req_idminmax_check(data, domain);
976a3f
+    if (ret != EOK) {
976a3f
+        return ret;
976a3f
+    }
976a3f
     return sysdb_getpwuid_with_views(mem_ctx, domain, data->id, _result);
976a3f
 }
976a3f
 
976a3f
diff --git a/src/tests/cmocka/test_responder_cache_req.c b/src/tests/cmocka/test_responder_cache_req.c
ced1f5
index 0ee0070d0c9fbb89020f522b2f7613f1076a8cbb..5f50b27a5ee846c9ccf71e1e661359a07c2e02e8 100644
976a3f
--- a/src/tests/cmocka/test_responder_cache_req.c
976a3f
+++ b/src/tests/cmocka/test_responder_cache_req.c
ced1f5
@@ -59,6 +59,11 @@ struct test_group {
976a3f
                                     test_single_domain_setup, \
976a3f
                                     test_single_domain_teardown)
976a3f
 
976a3f
+#define new_single_domain_id_limit_test(test) \
976a3f
+    cmocka_unit_test_setup_teardown(test_ ## test, \
976a3f
+                                    test_single_domain_id_limits_setup, \
976a3f
+                                    test_single_domain_teardown)
976a3f
+
976a3f
 #define new_multi_domain_test(test) \
976a3f
     cmocka_unit_test_setup_teardown(test_ ## test, \
976a3f
                                     test_multi_domain_setup, \
ced1f5
@@ -521,33 +526,39 @@ __wrap_sss_dp_get_account_send(TALLOC_CTX *mem_ctx,
976a3f
     return test_req_succeed_send(mem_ctx, rctx->ev);
976a3f
 }
976a3f
 
976a3f
+static int test_single_domain_setup_common(void **state,
976a3f
+                                           struct sss_test_conf_param *params)
ced1f5
+{
ced1f5
+    struct cache_req_test_ctx *test_ctx = NULL;
ced1f5
+    errno_t ret;
ced1f5
+
ced1f5
+    assert_true(leak_check_setup());
ced1f5
+
ced1f5
+    test_dom_suite_setup(TESTS_PATH);
ced1f5
+
ced1f5
+    test_ctx = talloc_zero(global_talloc_context, struct cache_req_test_ctx);
ced1f5
+    assert_non_null(test_ctx);
ced1f5
+    *state = test_ctx;
ced1f5
+
ced1f5
+    test_ctx->tctx = create_dom_test_ctx(test_ctx, TESTS_PATH, TEST_CONF_DB,
ced1f5
+                                         TEST_DOM_NAME, TEST_ID_PROVIDER, params);
ced1f5
+    assert_non_null(test_ctx->tctx);
ced1f5
+
ced1f5
+    test_ctx->rctx = mock_rctx(test_ctx, test_ctx->tctx->ev,
ced1f5
+                               test_ctx->tctx->dom, NULL);
ced1f5
+    assert_non_null(test_ctx->rctx);
ced1f5
+
ced1f5
+    ret = sss_ncache_init(test_ctx, 10, 0, &test_ctx->ncache);
ced1f5
+    assert_int_equal(ret, EOK);
ced1f5
+
ced1f5
+    check_leaks_push(test_ctx);
ced1f5
+
ced1f5
+    return 0;
ced1f5
+}
ced1f5
+
ced1f5
 static int test_single_domain_setup(void **state)
976a3f
 {
ced1f5
-    struct cache_req_test_ctx *test_ctx = NULL;
ced1f5
-    errno_t ret;
ced1f5
-
ced1f5
-    assert_true(leak_check_setup());
ced1f5
-
ced1f5
-    test_dom_suite_setup(TESTS_PATH);
ced1f5
-
ced1f5
-    test_ctx = talloc_zero(global_talloc_context, struct cache_req_test_ctx);
ced1f5
-    assert_non_null(test_ctx);
ced1f5
-    *state = test_ctx;
ced1f5
-
ced1f5
-    test_ctx->tctx = create_dom_test_ctx(test_ctx, TESTS_PATH, TEST_CONF_DB,
976a3f
-                                         TEST_DOM_NAME, TEST_ID_PROVIDER, NULL);
ced1f5
-    assert_non_null(test_ctx->tctx);
ced1f5
-
ced1f5
-    test_ctx->rctx = mock_rctx(test_ctx, test_ctx->tctx->ev,
ced1f5
-                               test_ctx->tctx->dom, NULL);
ced1f5
-    assert_non_null(test_ctx->rctx);
ced1f5
-
ced1f5
-    ret = sss_ncache_init(test_ctx, 10, 0, &test_ctx->ncache);
ced1f5
-    assert_int_equal(ret, EOK);
ced1f5
-
ced1f5
-    check_leaks_push(test_ctx);
ced1f5
-
ced1f5
-    return 0;
ced1f5
+    return test_single_domain_setup_common(state, NULL);
976a3f
 }
976a3f
 
976a3f
 static int test_single_domain_teardown(void **state)
ced1f5
@@ -565,6 +576,16 @@ static int test_single_domain_teardown(void **state)
976a3f
     return 0;
976a3f
 }
976a3f
 
976a3f
+static int test_single_domain_id_limits_setup(void **state)
976a3f
+{
976a3f
+    struct sss_test_conf_param params[] = {
976a3f
+        { "min_id", "100" },
976a3f
+        { "max_id", "10000" },
976a3f
+        { NULL, NULL },             /* Sentinel */
976a3f
+    };
976a3f
+    return test_single_domain_setup_common(state, params);
976a3f
+}
976a3f
+
976a3f
 static int test_multi_domain_setup(void **state)
976a3f
 {
976a3f
     struct cache_req_test_ctx *test_ctx = NULL;
ced1f5
@@ -596,6 +617,32 @@ static int test_multi_domain_setup(void **state)
976a3f
     return 0;
976a3f
 }
976a3f
 
976a3f
+void test_user_by_id_below_id_range(void **state)
976a3f
+{
976a3f
+    struct cache_req_test_ctx *test_ctx = NULL;
976a3f
+
976a3f
+    test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
976a3f
+
976a3f
+    /* Test. */
976a3f
+    run_cache_req(test_ctx, cache_req_user_by_id_send,
976a3f
+                  cache_req_user_by_id_test_done, test_ctx->tctx->dom,
976a3f
+                  0, 10, ENOENT);
976a3f
+    assert_false(test_ctx->dp_called);
976a3f
+}
976a3f
+
976a3f
+void test_user_by_id_above_id_range(void **state)
976a3f
+{
976a3f
+    struct cache_req_test_ctx *test_ctx = NULL;
976a3f
+
976a3f
+    test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
976a3f
+
976a3f
+    /* Test. */
976a3f
+    run_cache_req(test_ctx, cache_req_user_by_id_send,
976a3f
+                  cache_req_user_by_id_test_done, test_ctx->tctx->dom,
976a3f
+                  0, 100000, ENOENT);
976a3f
+    assert_false(test_ctx->dp_called);
976a3f
+}
976a3f
+
976a3f
 static int test_multi_domain_teardown(void **state)
976a3f
 {
976a3f
     struct cache_req_test_ctx *test_ctx;
ced1f5
@@ -1332,6 +1379,32 @@ void test_user_by_id_sub_domains_locator_missing_found(void **state)
ced1f5
     talloc_free(tmp_ctx);
976a3f
 }
976a3f
 
976a3f
+void test_group_by_id_below_id_range(void **state)
976a3f
+{
976a3f
+    struct cache_req_test_ctx *test_ctx = NULL;
976a3f
+
976a3f
+    test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
976a3f
+
976a3f
+    /* Test. */
976a3f
+    run_cache_req(test_ctx, cache_req_group_by_id_send,
976a3f
+                  cache_req_group_by_id_test_done, test_ctx->tctx->dom,
976a3f
+                  0, 10, ENOENT);
976a3f
+    assert_false(test_ctx->dp_called);
976a3f
+}
976a3f
+
976a3f
+void test_group_by_id_above_id_range(void **state)
976a3f
+{
976a3f
+    struct cache_req_test_ctx *test_ctx = NULL;
976a3f
+
976a3f
+    test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
976a3f
+
976a3f
+    /* Test. */
976a3f
+    run_cache_req(test_ctx, cache_req_group_by_id_send,
976a3f
+                  cache_req_group_by_id_test_done, test_ctx->tctx->dom,
976a3f
+                  0, 100000, ENOENT);
976a3f
+    assert_false(test_ctx->dp_called);
976a3f
+}
976a3f
+
ced1f5
 void test_user_by_id_sub_domains_locator_missing_notfound(void **state)
976a3f
 {
976a3f
     struct cache_req_test_ctx *test_ctx = NULL;
ced1f5
@@ -3874,6 +3947,8 @@ int main(int argc, const char *argv[])
976a3f
         new_single_domain_test(user_by_id_missing_notfound),
976a3f
         new_multi_domain_test(user_by_id_multiple_domains_found),
976a3f
         new_multi_domain_test(user_by_id_multiple_domains_notfound),
976a3f
+        new_single_domain_id_limit_test(user_by_id_below_id_range),
976a3f
+        new_single_domain_id_limit_test(user_by_id_above_id_range),
976a3f
 
976a3f
         new_single_domain_test(group_by_name_cache_valid),
976a3f
         new_single_domain_test(group_by_name_cache_expired),
ced1f5
@@ -3884,6 +3959,8 @@ int main(int argc, const char *argv[])
976a3f
         new_multi_domain_test(group_by_name_multiple_domains_found),
976a3f
         new_multi_domain_test(group_by_name_multiple_domains_notfound),
976a3f
         new_multi_domain_test(group_by_name_multiple_domains_parse),
976a3f
+        new_single_domain_id_limit_test(group_by_id_below_id_range),
976a3f
+        new_single_domain_id_limit_test(group_by_id_above_id_range),
976a3f
 
976a3f
         new_single_domain_test(group_by_id_cache_valid),
976a3f
         new_single_domain_test(group_by_id_cache_expired),
976a3f
diff --git a/src/util/util_errors.c b/src/util/util_errors.c
ced1f5
index 06c620b40aaa00d6ce58ace3a28449ffbdf8da88..39ce3d7dcf4af4c489a0a9b7768668497cb84ba5 100644
976a3f
--- a/src/util/util_errors.c
976a3f
+++ b/src/util/util_errors.c
ced1f5
@@ -117,6 +117,7 @@ struct err_string error_to_str[] = {
976a3f
     { "Unable to resolve host" }, /* ERR_UNABLE_TO_RESOLVE_HOST */
ced1f5
     { "GetAccountDomain() not supported" }, /* ERR_GET_ACCT_DOM_NOT_SUPPORTED */
ced1f5
     { "The last GetAccountDomain() result is still valid" }, /* ERR_GET_ACCT_DOM_CACHED */
976a3f
+    { "ID is outside the allowed range" }, /* ERR_ID_OUTSIDE_RANGE */
976a3f
     { "ERR_LAST" } /* ERR_LAST */
976a3f
 };
976a3f
 
976a3f
diff --git a/src/util/util_errors.h b/src/util/util_errors.h
ced1f5
index bebd6e198fc0077891a602f80182a993ce3f789b..621a3b116edac45960190684055bcd0692135957 100644
976a3f
--- a/src/util/util_errors.h
976a3f
+++ b/src/util/util_errors.h
ced1f5
@@ -139,6 +139,7 @@ enum sssd_errors {
976a3f
     ERR_UNABLE_TO_RESOLVE_HOST,
ced1f5
     ERR_GET_ACCT_DOM_NOT_SUPPORTED,
ced1f5
     ERR_GET_ACCT_DOM_CACHED,
976a3f
+    ERR_ID_OUTSIDE_RANGE,
976a3f
     ERR_LAST            /* ALWAYS LAST */
976a3f
 };
976a3f
 
976a3f
-- 
976a3f
2.14.3
976a3f