Author: abartlet
Date: 2004-09-06 12:14:10 +0000 (Mon, 06 Sep 2004)
New Revision: 53

WebSVN: 
http://websvn.samba.org/websvn/changeset.php?rep=lorikeet&path=/trunk/heimdal/lib&rev=53&nolog=1

Log:
Another attempt at the memory leak and correctness fixes in the
string2key code.

Thanks to lha for his feedback!

Andrew Bartlett

Added:
   trunk/heimdal/lib/roken/memdup.c
Modified:
   trunk/heimdal/lib/hdb/keys.c
   trunk/heimdal/lib/roken/Makefile.am
   trunk/heimdal/lib/roken/roken.h.in


Changeset:
Modified: trunk/heimdal/lib/hdb/keys.c
===================================================================
--- trunk/heimdal/lib/hdb/keys.c        2004-09-06 07:29:38 UTC (rev 52)
+++ trunk/heimdal/lib/hdb/keys.c        2004-09-06 12:14:10 UTC (rev 53)
@@ -81,7 +81,7 @@
     krb5_error_code ret;
     
     /* the 3 DES types must be first */
-    krb5_enctype all_etypes[] = { 
+    static const krb5_enctype all_etypes[] = { 
        ETYPE_DES_CBC_MD5,
        ETYPE_DES_CBC_MD4,
        ETYPE_DES_CBC_CRC,
@@ -111,12 +111,11 @@
            /* XXX there should be a string_to_etypes handling
               special cases like `des' and `all' */
            if(strcmp(buf[i], "des") == 0) {
-               *enctypes = malloc(sizeof(all_etypes[0])*3);
-               memcpy(*enctypes, all_etypes, sizeof(all_etypes[0])*3);
+               *enctypes = memdup(all_etypes, sizeof(all_etypes[0])*3);
                *num_enctypes = 3;
                continue;
            } else if(strcmp(buf[i], "des3") == 0) {
-               *enctypes = malloc(sizeof(**enctypes));
+               *enctypes = memdup(all_etypes, sizeof(**enctypes));
                *enctypes[0] = ETYPE_DES3_CBC_SHA1;
                *num_enctypes = 1;
                continue;
@@ -124,8 +123,7 @@
                krb5_enctype e;
                ret = krb5_string_to_enctype(context, buf[i], &e);
                if (ret == 0) {
-                   *enctypes = malloc(sizeof(**enctypes));
-                   *enctypes[0] = e;
+                   *enctypes = memdup(&e, sizeof(*enctypes[0]));
                    *num_enctypes = 1;
                    continue;
                }
@@ -139,15 +137,13 @@
               interface sucks */
            if(strcmp(buf[i], "pw-salt") == 0) {
                if(*enctypes == NULL) {
-                   *enctypes = malloc(sizeof(all_etypes));
-                   memcpy(*enctypes, all_etypes, sizeof(all_etypes));
+                   *enctypes = memdup(all_etypes, sizeof(all_etypes));
                    *num_enctypes = sizeof(all_etypes)/sizeof(all_etypes[0]);
                }
                salt->salttype = KRB5_PW_SALT;
            } else if(strcmp(buf[i], "afs3-salt") == 0) {
                if(*enctypes == NULL) {
-                   *enctypes = malloc(sizeof(all_etypes[0])*3);
-                   memcpy(*enctypes, all_etypes, sizeof(all_etypes[0])*3);
+                   *enctypes = memdup(all_etypes, sizeof(all_etypes[0])*3);
                    *num_enctypes = 3;
                }
                salt->salttype = KRB5_AFS3_SALT;
@@ -158,6 +154,11 @@
               v4 compat, and a cell name for afs compat */
            salt->saltvalue.data = strdup(buf[i]);
            salt->saltvalue.length = strlen(buf[i]);
+           if (!salt->saltvalue.data) {
+               krb5_set_error_string(context, "out of memory while "
+                                     "parsing salt specifiers");
+               return ENOMEM;
+           }
        }
     }
     
@@ -319,6 +320,7 @@
                ret = add_enctype_to_key_set(&key_set, nkeyset, enctypes[i], 
                                             no_salt ? NULL : &salt);
                if (ret) {
+                       krb5_free_salt(context, salt);
                        free(enctypes);
                        goto out;
                }

Modified: trunk/heimdal/lib/roken/Makefile.am
===================================================================
--- trunk/heimdal/lib/roken/Makefile.am 2004-09-06 07:29:38 UTC (rev 52)
+++ trunk/heimdal/lib/roken/Makefile.am 2004-09-06 12:14:10 UTC (rev 53)
@@ -59,6 +59,7 @@
        k_getpwnam.c            \
        k_getpwuid.c            \
        mini_inetd.c            \
+       memdup.c                \
        net_read.c              \
        net_write.c             \
        parse_bytes.c           \

Added: trunk/heimdal/lib/roken/memdup.c
===================================================================
--- trunk/heimdal/lib/roken/memdup.c    2004-09-06 07:29:38 UTC (rev 52)
+++ trunk/heimdal/lib/roken/memdup.c    2004-09-06 12:14:10 UTC (rev 53)
@@ -0,0 +1,63 @@
+/*
+ * Copyright (c) Andrew Barltett <[EMAIL PROTECTED]> 2004
+ * All rights reserved.
+ * 
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 
+ * 3. Neither the name of the Institute nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ * 
+ * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+RCSID("$Id: memdup.h $");
+#endif
+
+#include "roken.h"
+
+/* 
+ * memdup for systems that doesn't have it 
+ */
+
+#ifdef HAVE_SYS_TYPES_H
+#include <sys/types.h>
+#endif
+
+#ifdef HAVE_STDLIB_H
+#include <stdlib.h>
+#endif
+
+#ifdef HAVE_STRING_H
+#include <string.h>
+#endif
+
+void *memdup(const void *s2, size_t n)
+{
+    void *ret = malloc(n);
+    if (ret) 
+       memcpy(ret, s2, n);
+       
+    return ret;
+}

Modified: trunk/heimdal/lib/roken/roken.h.in
===================================================================
--- trunk/heimdal/lib/roken/roken.h.in  2004-09-06 07:29:38 UTC (rev 52)
+++ trunk/heimdal/lib/roken/roken.h.in  2004-09-06 12:14:10 UTC (rev 53)
@@ -414,6 +414,8 @@
 
 int issuid(void);
 
+void *memdup(const void *ptr, size_t bytes);
+
 #ifndef HAVE_STRUCT_WINSIZE
 struct winsize {
        unsigned short ws_row, ws_col;

Reply via email to