9af0d9
From 0987e0e137854285d4022f5a910e7923d4e663fd Mon Sep 17 00:00:00 2001
9af0d9
From: Simo Sorce <simo@redhat.com>
9af0d9
Date: Thu, 27 Aug 2020 17:01:39 -0400
9af0d9
Subject: [PATCH] Return static oids for naming functions
9af0d9
9af0d9
gss_display_name and gss_inquire_name reteurn "static" oids, that are
9af0d9
generally not freed by callers, so make sure to match and return actual
9af0d9
static OIDs exported by GSSAPI.
9af0d9
9af0d9
Also remove gpm_equal_oids() and use the library provided gss_oid_equal
9af0d9
function instead.
9af0d9
9af0d9
Signed-off-by: Simo Sorce <simo@redhat.com>
9af0d9
Reviewed-by: Robbie Harwood <rharwood@redhat.com>
9af0d9
(cherry picked from commit 6ea8391257e687dfb3981b634c06cf7a55008eb0)
9af0d9
(cherry picked from commit 41cb9683627d6c3b136a4b48e1b1842619132f16)
9af0d9
---
9af0d9
 src/client/gpm_import_and_canon_name.c | 28 ++++++++++++++++++++++++--
9af0d9
 src/client/gpm_indicate_mechs.c        | 24 +++++-----------------
9af0d9
 src/client/gssapi_gpm.h                |  1 +
9af0d9
 3 files changed, 32 insertions(+), 21 deletions(-)
9af0d9
9af0d9
diff --git a/src/client/gpm_import_and_canon_name.c b/src/client/gpm_import_and_canon_name.c
9af0d9
index 70149a3..88b8d7c 100644
9af0d9
--- a/src/client/gpm_import_and_canon_name.c
9af0d9
+++ b/src/client/gpm_import_and_canon_name.c
9af0d9
@@ -2,6 +2,26 @@
9af0d9
 
9af0d9
 #include "gssapi_gpm.h"
9af0d9
 
9af0d9
+static int gpm_name_oid_to_static(gss_OID name_type, gss_OID *name_static)
9af0d9
+{
9af0d9
+#define ret_static(b) \
9af0d9
+    if (gss_oid_equal(name_type, b)) { \
9af0d9
+        *name_static = b; \
9af0d9
+        return 0; \
9af0d9
+    }
9af0d9
+    ret_static(GSS_C_NT_USER_NAME);
9af0d9
+    ret_static(GSS_C_NT_MACHINE_UID_NAME);
9af0d9
+    ret_static(GSS_C_NT_STRING_UID_NAME);
9af0d9
+    ret_static(GSS_C_NT_HOSTBASED_SERVICE_X);
9af0d9
+    ret_static(GSS_C_NT_HOSTBASED_SERVICE);
9af0d9
+    ret_static(GSS_C_NT_ANONYMOUS);
9af0d9
+    ret_static(GSS_C_NT_EXPORT_NAME);
9af0d9
+    ret_static(GSS_C_NT_COMPOSITE_EXPORT);
9af0d9
+    ret_static(GSS_KRB5_NT_PRINCIPAL_NAME);
9af0d9
+    ret_static(gss_nt_krb5_name);
9af0d9
+    return ENOENT;
9af0d9
+}
9af0d9
+
9af0d9
 OM_uint32 gpm_display_name(OM_uint32 *minor_status,
9af0d9
                            gssx_name *in_name,
9af0d9
                            gss_buffer_t output_name_buffer,
9af0d9
@@ -57,7 +77,9 @@ OM_uint32 gpm_display_name(OM_uint32 *minor_status,
9af0d9
     }
9af0d9
 
9af0d9
     if (output_name_type) {
9af0d9
-        ret = gp_conv_gssx_to_oid_alloc(&in_name->name_type, output_name_type);
9af0d9
+        gss_OID_desc oid;
9af0d9
+        gp_conv_gssx_to_oid(&in_name->name_type, &oid;;
9af0d9
+        ret = gpm_name_oid_to_static(&oid, output_name_type);
9af0d9
         if (ret) {
9af0d9
             gss_release_buffer(&discard, output_name_buffer);
9af0d9
             ret_min = ret;
9af0d9
@@ -285,7 +307,9 @@ OM_uint32 gpm_inquire_name(OM_uint32 *minor_status,
9af0d9
     }
9af0d9
 
9af0d9
     if (MN_mech != NULL) {
9af0d9
-        ret = gp_conv_gssx_to_oid_alloc(&name->name_type, MN_mech);
9af0d9
+        gss_OID_desc oid;
9af0d9
+        gp_conv_gssx_to_oid(&name->name_type, &oid;;
9af0d9
+        ret = gpm_name_oid_to_static(&oid, MN_mech);
9af0d9
         if (ret) {
9af0d9
             *minor_status = ret;
9af0d9
             return GSS_S_FAILURE;
9af0d9
diff --git a/src/client/gpm_indicate_mechs.c b/src/client/gpm_indicate_mechs.c
9af0d9
index 86c7de3..4041dcd 100644
9af0d9
--- a/src/client/gpm_indicate_mechs.c
9af0d9
+++ b/src/client/gpm_indicate_mechs.c
9af0d9
@@ -95,20 +95,6 @@ static uint32_t gpm_copy_gss_buffer(uint32_t *minor_status,
9af0d9
     return GSS_S_COMPLETE;
9af0d9
 }
9af0d9
 
9af0d9
-static bool gpm_equal_oids(gss_const_OID a, gss_const_OID b)
9af0d9
-{
9af0d9
-    int ret;
9af0d9
-
9af0d9
-    if (a->length == b->length) {
9af0d9
-        ret = memcmp(a->elements, b->elements, a->length);
9af0d9
-        if (ret == 0) {
9af0d9
-            return true;
9af0d9
-        }
9af0d9
-    }
9af0d9
-
9af0d9
-    return false;
9af0d9
-}
9af0d9
-
9af0d9
 static void gpmint_indicate_mechs(void)
9af0d9
 {
9af0d9
     union gp_rpc_arg uarg;
9af0d9
@@ -313,7 +299,7 @@ int gpm_mech_to_static(gss_OID mech_type, gss_OID *mech_static)
9af0d9
 
9af0d9
     *mech_static = GSS_C_NO_OID;
9af0d9
     for (size_t i = 0; i < global_mechs.mech_set->count; i++) {
9af0d9
-        if (gpm_equal_oids(&global_mechs.mech_set->elements[i], mech_type)) {
9af0d9
+        if (gss_oid_equal(&global_mechs.mech_set->elements[i], mech_type)) {
9af0d9
             *mech_static = &global_mechs.mech_set->elements[i];
9af0d9
             return 0;
9af0d9
         }
9af0d9
@@ -383,7 +369,7 @@ OM_uint32 gpm_inquire_names_for_mech(OM_uint32 *minor_status,
9af0d9
     }
9af0d9
 
9af0d9
     for (unsigned i = 0; i < global_mechs.info_len; i++) {
9af0d9
-        if (!gpm_equal_oids(global_mechs.info[i].mech, mech_type)) {
9af0d9
+        if (!gss_oid_equal(global_mechs.info[i].mech, mech_type)) {
9af0d9
             continue;
9af0d9
         }
9af0d9
         ret_maj = gpm_copy_gss_OID_set(&ret_min,
9af0d9
@@ -481,7 +467,7 @@ OM_uint32 gpm_inquire_attrs_for_mech(OM_uint32 *minor_status,
9af0d9
     }
9af0d9
 
9af0d9
     for (unsigned i = 0; i < global_mechs.info_len; i++) {
9af0d9
-        if (!gpm_equal_oids(global_mechs.info[i].mech, mech)) {
9af0d9
+        if (!gss_oid_equal(global_mechs.info[i].mech, mech)) {
9af0d9
             continue;
9af0d9
         }
9af0d9
 
9af0d9
@@ -540,7 +526,7 @@ OM_uint32 gpm_inquire_saslname_for_mech(OM_uint32 *minor_status,
9af0d9
     }
9af0d9
 
9af0d9
     for (unsigned i = 0; i < global_mechs.info_len; i++) {
9af0d9
-        if (!gpm_equal_oids(global_mechs.info[i].mech, desired_mech)) {
9af0d9
+        if (!gss_oid_equal(global_mechs.info[i].mech, desired_mech)) {
9af0d9
             continue;
9af0d9
         }
9af0d9
         ret_maj = gpm_copy_gss_buffer(&ret_min,
9af0d9
@@ -598,7 +584,7 @@ OM_uint32 gpm_display_mech_attr(OM_uint32 *minor_status,
9af0d9
     }
9af0d9
 
9af0d9
     for (unsigned i = 0; i < global_mechs.desc_len; i++) {
9af0d9
-        if (!gpm_equal_oids(global_mechs.desc[i].attr, mech_attr)) {
9af0d9
+        if (!gss_oid_equal(global_mechs.desc[i].attr, mech_attr)) {
9af0d9
             continue;
9af0d9
         }
9af0d9
         ret_maj = gpm_copy_gss_buffer(&ret_min,
9af0d9
diff --git a/src/client/gssapi_gpm.h b/src/client/gssapi_gpm.h
9af0d9
index b7ba04b..bdf12e1 100644
9af0d9
--- a/src/client/gssapi_gpm.h
9af0d9
+++ b/src/client/gssapi_gpm.h
9af0d9
@@ -10,6 +10,7 @@
9af0d9
 #include <string.h>
9af0d9
 #include <gssapi/gssapi.h>
9af0d9
 #include <gssapi/gssapi_ext.h>
9af0d9
+#include <gssapi/gssapi_krb5.h>
9af0d9
 #include "rpcgen/gp_rpc.h"
9af0d9
 #include "rpcgen/gss_proxy.h"
9af0d9
 #include "src/gp_common.h"