190a2a
From 32b7c1d012a0904420edc61bf94be66253a6c84a Mon Sep 17 00:00:00 2001
190a2a
From: Alexander Bokovoy <abokovoy@redhat.com>
190a2a
Date: Thu, 28 May 2015 08:33:51 +0000
190a2a
Subject: [PATCH] ipa-kdb: filter out group membership from MS-PAC for exact
190a2a
 SID matches too
190a2a
190a2a
When incoming SID blacklist contains exact SIDs of users and groups,
190a2a
attempt to filter them out as well, according to [MS-PAC] 4.1.1.2.
190a2a
190a2a
Note that we treat user's SID and primary group RID filtering as violation
190a2a
of the KDC policy because the resulting MS-PAC will have no user SID or
190a2a
primary group and thus will be invalid.
190a2a
190a2a
For group RIDs we filter them out. According to [MS-KILE] 3.3.5.6.3.1
190a2a
it is OK to have empty group RIDs array as GroupCount SHOULD be
190a2a
equal to Groups.MembershipCount returned by SamrGetGroupsForUser
190a2a
[MS-SAMR] 3.1.5.9.1, not MUST, thus it may be empty.
190a2a
190a2a
Part of fix for https://bugzilla.redhat.com/show_bug.cgi?id=1222475
190a2a
190a2a
Reviewed-By: Tomas Babej <tbabej@redhat.com>
190a2a
---
190a2a
 daemons/ipa-kdb/ipa_kdb_mspac.c | 100 +++++++++++++++++++++++++++++++++++++++-
190a2a
 1 file changed, 99 insertions(+), 1 deletion(-)
190a2a
190a2a
diff --git a/daemons/ipa-kdb/ipa_kdb_mspac.c b/daemons/ipa-kdb/ipa_kdb_mspac.c
190a2a
index 74ee2f3fd4b81bd3433c9ff9c77f7434b72e7f4d..3e6024f55071e95c6d40869e31d879baf627a3b9 100644
190a2a
--- a/daemons/ipa-kdb/ipa_kdb_mspac.c
190a2a
+++ b/daemons/ipa-kdb/ipa_kdb_mspac.c
190a2a
@@ -1320,6 +1320,22 @@ static void filter_logon_info_log_message(struct dom_sid *sid)
190a2a
     }
190a2a
 }
190a2a
 
190a2a
+static void filter_logon_info_log_message_rid(struct dom_sid *sid, uint32_t rid)
190a2a
+{
190a2a
+    char *domstr = NULL;
190a2a
+
190a2a
+    domstr = dom_sid_string(NULL, sid);
190a2a
+    if (domstr) {
190a2a
+        krb5_klog_syslog(LOG_ERR, "PAC filtering issue: SID [%s-%d] is not allowed "
190a2a
+                                  "from a trusted source and will be excluded.", domstr, rid);
190a2a
+        talloc_free(domstr);
190a2a
+    } else {
190a2a
+        krb5_klog_syslog(LOG_ERR, "PAC filtering issue: SID is not allowed "
190a2a
+                                  "from a trusted source and will be excluded."
190a2a
+                                  "Unable to allocate memory to display SID.");
190a2a
+    }
190a2a
+}
190a2a
+
190a2a
 static krb5_error_code filter_logon_info(krb5_context context,
190a2a
                                          TALLOC_CTX *memctx,
190a2a
                                          krb5_data realm,
190a2a
@@ -1331,9 +1347,21 @@ static krb5_error_code filter_logon_info(krb5_context context,
190a2a
      * attempt at getting us to sign fake credentials with the help of a
190a2a
      * compromised trusted realm */
190a2a
 
190a2a
+    /* NOTE: there are two outcomes from filtering:
190a2a
+     * REJECT TICKET -- ticket is rejected if domain SID of
190a2a
+     *                  the principal with MS-PAC is filtered out or
190a2a
+     *                  its primary group RID is filtered out
190a2a
+     *
190a2a
+     * REMOVE SID    -- SIDs are removed from the list of SIDs associated
190a2a
+     *                  with the principal if they are filtered out
190a2a
+     *                  This applies also to secondary RIDs of the principal
190a2a
+     *                  if domain_sid-<secondary RID> is filtered out
190a2a
+     */
190a2a
+
190a2a
     struct ipadb_context *ipactx;
190a2a
     struct ipadb_adtrusts *domain;
190a2a
-    int i, j, k, count;
190a2a
+    int i, j, k, l, count;
190a2a
+    uint32_t rid;
190a2a
     bool result;
190a2a
     char *domstr = NULL;
190a2a
 
190a2a
@@ -1380,6 +1408,76 @@ static krb5_error_code filter_logon_info(krb5_context context,
190a2a
         }
190a2a
     }
190a2a
 
190a2a
+    /* Check if this user's SIDs membership is filtered too */
190a2a
+    for(k = 0; k < domain->len_sid_blacklist_incoming; k++) {
190a2a
+        /* Short-circuit if there are no RIDs. This may happen if we filtered everything already.
190a2a
+         * In normal situation there would be at least primary gid as RID in the RIDs array
190a2a
+         * but if we filtered out the primary RID, this MS-PAC is invalid */
190a2a
+        count = info->info->info3.base.groups.count;
190a2a
+        result = dom_sid_is_prefix(info->info->info3.base.domain_sid,
190a2a
+                                   &domain->sid_blacklist_incoming[k]);
190a2a
+        if (result) {
190a2a
+            i = 0;
190a2a
+            j = 0;
190a2a
+            if (domain->sid_blacklist_incoming[k].num_auths - info->info->info3.base.domain_sid->num_auths != 1) {
190a2a
+                krb5_klog_syslog(LOG_ERR, "Incoming SID blacklist element matching domain [%s with SID %s] "
190a2a
+                                          "has more than one RID component. Invalid check skipped.",
190a2a
+                                 domain->domain_name, domain->domain_sid);
190a2a
+                break;
190a2a
+            }
190a2a
+            rid = domain->sid_blacklist_incoming[k].sub_auths[domain->sid_blacklist_incoming[k].num_auths - 1];
190a2a
+            if (rid == info->info->info3.base.rid) {
190a2a
+                filter_logon_info_log_message_rid(info->info->info3.base.domain_sid, rid);
190a2a
+                /* Actual user's SID is filtered out */
190a2a
+                return KRB5KDC_ERR_POLICY;
190a2a
+            }
190a2a
+            if (rid == info->info->info3.base.primary_gid) {
190a2a
+                /* User's primary group SID is filtered out */
190a2a
+                return KRB5KDC_ERR_POLICY;
190a2a
+            }
190a2a
+            if (count == 0) {
190a2a
+                /* Having checked actual user's SID and primary group SID, and having no other RIDs,
190a2a
+                 * skip checks below and continue to next blacklist element */
190a2a
+                continue;
190a2a
+            }
190a2a
+
190a2a
+            do {
190a2a
+                if (rid == info->info->info3.base.groups.rids[i].rid) {
190a2a
+                    filter_logon_info_log_message_rid(info->info->info3.base.domain_sid, rid);
190a2a
+                    /* If this is just a non-primary RID, we simply remove it from the array of RIDs */
190a2a
+                    l = count - i - j - 1;
190a2a
+                    if (l != 0) {
190a2a
+                         memmove(info->info->info3.base.groups.rids+i,
190a2a
+                                 info->info->info3.base.groups.rids+i+1,
190a2a
+                                 sizeof(struct samr_RidWithAttribute)*l);
190a2a
+                    }
190a2a
+                    j++;
190a2a
+                } else {
190a2a
+                    i++;
190a2a
+                }
190a2a
+            } while ((i + j) < count);
190a2a
+
190a2a
+            if (j != 0) {
190a2a
+                count = count-j;
190a2a
+                if (count == 0) {
190a2a
+                    /* All RIDs were filtered out. Unusual but MS-KILE 3.3.5.6.3.1 says SHOULD, not MUST for GroupCount */
190a2a
+                    info->info->info3.base.groups.count = 0;
190a2a
+                    talloc_free(info->info->info3.base.groups.rids);
190a2a
+                    info->info->info3.base.groups.rids = NULL;
190a2a
+                } else {
190a2a
+                    info->info->info3.base.groups.rids = talloc_realloc(memctx,
190a2a
+                                                                        info->info->info3.base.groups.rids,
190a2a
+                                                                        struct samr_RidWithAttribute, count);
190a2a
+                    if (!info->info->info3.base.groups.rids) {
190a2a
+                        info->info->info3.base.groups.count = 0;
190a2a
+                        return ENOMEM;
190a2a
+                    }
190a2a
+                    info->info->info3.base.groups.count = count;
190a2a
+                }
190a2a
+            }
190a2a
+        }
190a2a
+    }
190a2a
+
190a2a
     /* According to MS-KILE 25.0, info->info->info3.sids may be non zero, so check
190a2a
      * should include different possibilities into account
190a2a
      * */
190a2a
-- 
190a2a
2.1.0
190a2a