Blame SOURCES/0006-Use-more-randomness-for-ksu-secondary-cache-names.patch

4be148
From 69c8e20b18577781e17c5959e23514134dfb5755 Mon Sep 17 00:00:00 2001
4be148
From: Nalin Dahyabhai <nalin@redhat.com>
4be148
Date: Thu, 24 Jul 2014 16:43:21 -0400
4be148
Subject: [PATCH 6/7] Use more randomness for ksu secondary cache names
4be148
4be148
When generating a suffix to append to a ccache name that will hold the
4be148
credentials for a ksu-invoked process, instead of using integers
4be148
counting up from 1, use the result of base64-encoding six randomly-
4be148
generated octets.  Tweak the output alphabet just a bit to avoid using
4be148
'+' or '/' in the generated names, the latter of which could really
4be148
confuse things.
4be148
---
4be148
 src/clients/ksu/ccache.c | 27 +++++++++++++++++++++++----
4be148
 src/clients/ksu/ksu.h    |  2 +-
4be148
 src/clients/ksu/main.c   | 16 ++++++++++++----
4be148
 3 files changed, 36 insertions(+), 9 deletions(-)
4be148
4be148
diff --git a/src/clients/ksu/ccache.c b/src/clients/ksu/ccache.c
4be148
index 0f9e042..a0736f2 100644
4be148
--- a/src/clients/ksu/ccache.c
4be148
+++ b/src/clients/ksu/ccache.c
4be148
@@ -27,6 +27,7 @@
4be148
  */
4be148
 
4be148
 #include "ksu.h"
4be148
+#include "k5-base64.h"
4be148
 #include "adm_proto.h"
4be148
 #include <sys/types.h>
4be148
 #include <sys/stat.h>
4be148
@@ -504,10 +505,28 @@ show_credential(context, cred, cc)
4be148
     free(sname);
4be148
 }
4be148
 
4be148
-int gen_sym(){
4be148
-    static int i = 0;
4be148
-    i ++;
4be148
-    return i;
4be148
+/* Create a random string suitable for a filename extension. */
4be148
+krb5_error_code
4be148
+gen_sym(krb5_context context, char **sym_out)
4be148
+{
4be148
+    krb5_error_code retval;
4be148
+    char bytes[6], *p, *sym;
4be148
+    krb5_data data = make_data(bytes, sizeof(bytes));
4be148
+
4be148
+    *sym_out = NULL;
4be148
+    retval = krb5_c_random_make_octets(context, &data);
4be148
+    if (retval)
4be148
+        return retval;
4be148
+    sym = k5_base64_encode(data.data, data.length);
4be148
+    if (sym == NULL)
4be148
+        return ENOMEM;
4be148
+    /* Tweak the output alphabet just a bit. */
4be148
+    while ((p = strchr(sym, '/')) != NULL)
4be148
+        *p = '_';
4be148
+    while ((p = strchr(sym, '+')) != NULL)
4be148
+        *p = '-';
4be148
+    *sym_out = sym;
4be148
+    return 0;
4be148
 }
4be148
 
4be148
 krb5_error_code krb5_ccache_overwrite(context, ccs, cct, primary_principal)
4be148
diff --git a/src/clients/ksu/ksu.h b/src/clients/ksu/ksu.h
4be148
index fbbf217..5ba5ceb 100644
4be148
--- a/src/clients/ksu/ksu.h
4be148
+++ b/src/clients/ksu/ksu.h
4be148
@@ -130,7 +130,7 @@ extern krb5_error_code krb5_get_login_princ
4be148
 extern void show_credential
4be148
 (krb5_context, krb5_creds *, krb5_ccache);
4be148
 
4be148
-extern int gen_sym (void);
4be148
+krb5_error_code gen_sym(krb5_context context, char **sym);
4be148
 
4be148
 extern krb5_error_code krb5_ccache_overwrite
4be148
 (krb5_context, krb5_ccache, krb5_ccache, krb5_principal);
4be148
diff --git a/src/clients/ksu/main.c b/src/clients/ksu/main.c
4be148
index 41a3bf8..47fa820 100644
4be148
--- a/src/clients/ksu/main.c
4be148
+++ b/src/clients/ksu/main.c
4be148
@@ -856,7 +856,7 @@ resolve_target_cache(krb5_context context, krb5_principal princ,
4be148
     krb5_error_code retval;
4be148
     krb5_boolean switchable, reused = FALSE;
4be148
     krb5_ccache ccache = NULL;
4be148
-    char *sep, *ccname = NULL, *target;
4be148
+    char *sep, *ccname = NULL, *sym = NULL, *target;
4be148
 
4be148
     *ccache_out = NULL;
4be148
     *ccache_reused = FALSE;
4be148
@@ -876,12 +876,20 @@ resolve_target_cache(krb5_context context, krb5_principal princ,
4be148
          * the name of a cache that doesn't exist yet. */
4be148
         do {
4be148
             free(ccname);
4be148
-            if (asprintf(&ccname, "%s.%d", target, gen_sym()) < 0) {
4be148
+            retval = gen_sym(context, &sym);
4be148
+            if (retval) {
4be148
+                com_err(prog_name, retval,
4be148
+                        _("while generating part of the target ccache name"));
4be148
+                return retval;
4be148
+            }
4be148
+            if (asprintf(&ccname, "%s.%s", target, sym) < 0) {
4be148
                 retval = ENOMEM;
4be148
-                com_err(prog_name, ENOMEM,
4be148
-                        _("while allocating memory for target ccache name"));
4be148
+                free(sym);
4be148
+                com_err(prog_name, retval, _("while allocating memory for the "
4be148
+                                             "target ccache name"));
4be148
                 goto cleanup;
4be148
             }
4be148
+            free(sym);
4be148
         } while (ks_ccache_name_is_initialized(context, ccname));
4be148
         retval = krb5_cc_resolve(context, ccname, &ccache);
4be148
     } else {
4be148
-- 
4be148
2.0.4
4be148