Blame SOURCES/0124-ldap_child-copy-keytab-into-memory-to-drop-privilege.patch

905b4d
From ebda717383bf29731c3b4d7e809be7c7fae76284 Mon Sep 17 00:00:00 2001
905b4d
From: Sumit Bose <sbose@redhat.com>
905b4d
Date: Mon, 1 Dec 2014 17:24:31 +0100
905b4d
Subject: [PATCH 124/128] ldap_child: copy keytab into memory to drop
905b4d
 privileges earlier
905b4d
905b4d
Reviewed-by: Jakub Hrozek <jhrozek@redhat.com>
905b4d
---
905b4d
 Makefile.am                     |  1 +
905b4d
 src/providers/ldap/ldap_child.c | 64 +++++++++++++++++++++++++++++------------
905b4d
 2 files changed, 46 insertions(+), 19 deletions(-)
905b4d
905b4d
diff --git a/Makefile.am b/Makefile.am
905b4d
index 065992b84ce491b8b6ce1826cb5e88d7e0295176..3d16428856d6cb2e9e190b0df8895ab3f45db39c 100644
905b4d
--- a/Makefile.am
905b4d
+++ b/Makefile.am
905b4d
@@ -2564,6 +2564,7 @@ krb5_child_LDADD = \
905b4d
 
905b4d
 ldap_child_SOURCES = \
905b4d
     src/providers/ldap/ldap_child.c \
905b4d
+    src/providers/krb5/krb5_keytab.c \
905b4d
     src/util/sss_krb5.c \
905b4d
     src/util/atomic_io.c \
905b4d
     src/util/authtok.c \
905b4d
diff --git a/src/providers/ldap/ldap_child.c b/src/providers/ldap/ldap_child.c
905b4d
index a922b181715c5e89301e9f50bdb81723d1ff2a6a..f7f8d1d2eab3f66fe4f7d09e50458b495739c1d2 100644
905b4d
--- a/src/providers/ldap/ldap_child.c
905b4d
+++ b/src/providers/ldap/ldap_child.c
905b4d
@@ -33,6 +33,7 @@
905b4d
 #include "util/sss_krb5.h"
905b4d
 #include "util/child_common.h"
905b4d
 #include "providers/dp_backend.h"
905b4d
+#include "providers/krb5/krb5_common.h"
905b4d
 
905b4d
 static krb5_context krb5_error_ctx;
905b4d
 #define LDAP_CHILD_DEBUG(level, error) KRB5_DEBUG(level, krb5_error_ctx, error)
905b4d
@@ -47,8 +48,9 @@ static const char *__ldap_child_krb5_error_msg;
905b4d
 struct input_buffer {
905b4d
     const char *realm_str;
905b4d
     const char *princ_str;
905b4d
-    const char *keytab_name;
905b4d
+    char *keytab_name;
905b4d
     krb5_deltat lifetime;
905b4d
+    krb5_context context;
905b4d
     uid_t uid;
905b4d
     gid_t gid;
905b4d
 };
905b4d
@@ -246,12 +248,11 @@ static int lc_verify_keytab_ex(const char *principal,
905b4d
 }
905b4d
 
905b4d
 static krb5_error_code ldap_child_get_tgt_sync(TALLOC_CTX *memctx,
905b4d
+                                               krb5_context context,
905b4d
                                                const char *realm_str,
905b4d
                                                const char *princ_str,
905b4d
                                                const char *keytab_name,
905b4d
                                                const krb5_deltat lifetime,
905b4d
-                                               uid_t uid,
905b4d
-                                               gid_t gid,
905b4d
                                                const char **ccname_out,
905b4d
                                                time_t *expire_time_out)
905b4d
 {
905b4d
@@ -262,7 +263,6 @@ static krb5_error_code ldap_child_get_tgt_sync(TALLOC_CTX *memctx,
905b4d
     char *full_princ = NULL;
905b4d
     char *default_realm = NULL;
905b4d
     char *tmp_str = NULL;
905b4d
-    krb5_context context = NULL;
905b4d
     krb5_keytab keytab = NULL;
905b4d
     krb5_ccache ccache = NULL;
905b4d
     krb5_principal kprinc;
905b4d
@@ -278,13 +278,6 @@ static krb5_error_code ldap_child_get_tgt_sync(TALLOC_CTX *memctx,
905b4d
     char *ccname_file;
905b4d
     mode_t old_umask;
905b4d
 
905b4d
-    krberr = krb5_init_context(&context);
905b4d
-    if (krberr) {
905b4d
-        DEBUG(SSSDBG_OP_FAILURE, "Failed to init kerberos context\n");
905b4d
-        return krberr;
905b4d
-    }
905b4d
-    DEBUG(SSSDBG_TRACE_INTERNAL, "Kerberos context initialized\n");
905b4d
-
905b4d
     tmp_ctx = talloc_new(memctx);
905b4d
     if (tmp_ctx == NULL) {
905b4d
         krberr = KRB5KRB_ERR_GENERIC;
905b4d
@@ -440,12 +433,6 @@ static krb5_error_code ldap_child_get_tgt_sync(TALLOC_CTX *memctx,
905b4d
     }
905b4d
     DEBUG(SSSDBG_TRACE_INTERNAL, "credentials initialized\n");
905b4d
 
905b4d
-    krberr = become_user(uid, gid);
905b4d
-    if (krberr != 0) {
905b4d
-        DEBUG(SSSDBG_CRIT_FAILURE, "become_user failed.\n");
905b4d
-        goto done;
905b4d
-    }
905b4d
-
905b4d
     ccname_dummy = talloc_asprintf(tmp_ctx, "FILE:%s", ccname_file_dummy);
905b4d
     ccname = talloc_asprintf(tmp_ctx, "FILE:%s", ccname_file);
905b4d
     if (ccname_dummy == NULL || ccname == NULL) {
905b4d
@@ -558,6 +545,30 @@ static int prepare_response(TALLOC_CTX *mem_ctx,
905b4d
     return EOK;
905b4d
 }
905b4d
 
905b4d
+static krb5_error_code privileged_krb5_setup(struct input_buffer *ibuf)
905b4d
+{
905b4d
+    krb5_error_code kerr;
905b4d
+    char *keytab_name;
905b4d
+
905b4d
+    kerr = krb5_init_context(&ibuf->context);
905b4d
+    if (kerr != 0) {
905b4d
+        DEBUG(SSSDBG_CRIT_FAILURE, "Failed to init kerberos context\n");
905b4d
+        return kerr;
905b4d
+    }
905b4d
+    DEBUG(SSSDBG_TRACE_INTERNAL, "Kerberos context initialized\n");
905b4d
+
905b4d
+    kerr = copy_keytab_into_memory(ibuf, ibuf->context, ibuf->keytab_name,
905b4d
+                                   &keytab_name, NULL);
905b4d
+    if (kerr != 0) {
905b4d
+        DEBUG(SSSDBG_OP_FAILURE, "copy_keytab_into_memory failed.\n");
905b4d
+        return kerr;
905b4d
+    }
905b4d
+    talloc_free(ibuf->keytab_name);
905b4d
+    ibuf->keytab_name = keytab_name;
905b4d
+
905b4d
+    return 0;
905b4d
+}
905b4d
+
905b4d
 int main(int argc, const char *argv[])
905b4d
 {
905b4d
     int ret;
905b4d
@@ -662,11 +673,26 @@ int main(int argc, const char *argv[])
905b4d
         goto fail;
905b4d
     }
905b4d
 
905b4d
+    kerr = privileged_krb5_setup(ibuf);
905b4d
+    if (kerr != EOK) {
905b4d
+        DEBUG(SSSDBG_CRIT_FAILURE, "Privileged Krb5 setup failed.\n");
905b4d
+        goto fail;
905b4d
+    }
905b4d
+    DEBUG(SSSDBG_TRACE_INTERNAL, "Kerberos context initialized\n");
905b4d
+
905b4d
+    kerr = become_user(ibuf->uid, ibuf->gid);
905b4d
+    if (kerr != 0) {
905b4d
+        DEBUG(SSSDBG_CRIT_FAILURE, "become_user failed.\n");
905b4d
+        goto fail;
905b4d
+    }
905b4d
+
905b4d
+    DEBUG(SSSDBG_TRACE_INTERNAL,
905b4d
+          "Running as [%"SPRIuid"][%"SPRIgid"].\n", geteuid(), getegid());
905b4d
+
905b4d
     DEBUG(SSSDBG_TRACE_INTERNAL, "getting TGT sync\n");
905b4d
-    kerr = ldap_child_get_tgt_sync(main_ctx,
905b4d
+    kerr = ldap_child_get_tgt_sync(main_ctx, ibuf->context,
905b4d
                                    ibuf->realm_str, ibuf->princ_str,
905b4d
                                    ibuf->keytab_name, ibuf->lifetime,
905b4d
-                                   ibuf->uid, ibuf->gid,
905b4d
                                    &ccname, &expire_time);
905b4d
     if (kerr != EOK) {
905b4d
         DEBUG(SSSDBG_CRIT_FAILURE, "ldap_child_get_tgt_sync failed.\n");
905b4d
-- 
905b4d
1.9.3
905b4d