[gcr/gnome-3-6] gck: Fix crash when hashing an unsupported attributes



commit c96aa2795ffa0c3305787c237dc223571b4934b4
Author: Stef Walter <stefw gnome org>
Date:   Thu Mar 21 18:32:07 2013 +0100

    gck: Fix crash when hashing an unsupported attributes
    
    PKCS#11 modules return attributes with a negative length and a
    NULL value, when they don't support the attribute in question.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=696305

 gck/gck-attributes.c            |    6 ++++--
 gck/tests/test-gck-attributes.c |   26 ++++++++++++++++++++++++++
 2 files changed, 30 insertions(+), 2 deletions(-)
---
diff --git a/gck/gck-attributes.c b/gck/gck-attributes.c
index 356d6f1..5b6dad9 100644
--- a/gck/gck-attributes.c
+++ b/gck/gck-attributes.c
@@ -1996,8 +1996,10 @@ gck_attribute_hash (gconstpointer attr)
 
        h ^= _gck_ulong_hash (&a->type);
 
-       for (p = (signed char *)a->value, e = p + a->length; p != e; p++)
-               h = (h << 5) + h + *p;
+       if (a->value) {
+               for (p = (signed char *)a->value, e = p + a->length; p != e; p++)
+                       h = (h << 5) + h + *p;
+       }
 
        return h;
 }
diff --git a/gck/tests/test-gck-attributes.c b/gck/tests/test-gck-attributes.c
index 04ae70e..9a1aff6 100644
--- a/gck/tests/test-gck-attributes.c
+++ b/gck/tests/test-gck-attributes.c
@@ -963,6 +963,31 @@ test_builder_add_attr (void)
 }
 
 static void
+test_attribute_hash (void)
+{
+       guchar *data = (guchar *)"extra attribute";
+       GckAttribute one = { CKA_LABEL, (guchar *)"yay", 3 };
+       GckAttribute null = { CKA_LABEL, (guchar *)NULL, 3 };
+       GckAttribute zero = { CKA_LABEL, (guchar *)NULL, 0 };
+       GckAttribute two = { CKA_VALUE, (guchar *)"yay", 3 };
+       GckAttribute other = { CKA_VALUE, data, 5 };
+       GckAttribute overflow = { CKA_VALUE, data, 5 };
+       GckAttribute content = { CKA_VALUE, (guchar *)"conte", 5 };
+       guint hash;
+
+       hash = gck_attribute_hash (&one);
+       g_assert_cmpuint (hash, !=, 0);
+
+       g_assert_cmpuint (gck_attribute_hash (&one), ==, hash);
+       g_assert_cmpuint (gck_attribute_hash (&two), !=, hash);
+       g_assert_cmpuint (gck_attribute_hash (&other), !=, hash);
+       g_assert_cmpuint (gck_attribute_hash (&overflow), !=, hash);
+       g_assert_cmpuint (gck_attribute_hash (&null), !=, hash);
+       g_assert_cmpuint (gck_attribute_hash (&zero), !=, hash);
+       g_assert_cmpuint (gck_attribute_hash (&content), !=, hash);
+}
+
+static void
 test_attributes_refs (void)
 {
        GckBuilder builder = GCK_BUILDER_INIT;
@@ -1301,6 +1326,7 @@ main (int argc, char **argv)
        g_test_add_func ("/gck/attribute/get_string", test_get_string);
        g_test_add_func ("/gck/attribute/dup_attribute", test_dup_attribute);
        g_test_add_func ("/gck/attribute/copy_attribute", test_copy_attribute);
+       g_test_add_func ("/gck/attribute/hash", test_attribute_hash);
        g_test_add_func ("/gck/builder/blank", test_builder_blank);
        g_test_add_func ("/gck/builder/data", test_build_data);
        g_test_add_func ("/gck/builder/data-invalid", test_build_data_invalid);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]