Iterate over EC_GROUP's poly array in a safe way
authorRich Salz <rsalz@openssl.org>
Wed, 22 Feb 2017 18:11:08 +0000 (13:11 -0500)
committerRich Salz <rsalz@openssl.org>
Wed, 22 Feb 2017 18:13:03 +0000 (13:13 -0500)
Prevent that memory beyond the last element is accessed if every element
of group->poly[] is non-zero

Reviewed-by: Richard Levitte <levitte@openssl.org>
Reviewed-by: Rich Salz <rsalz@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/2689)

crypto/ec/ec_asn1.c

index 4f4d1edf0ed3607899eb723480cf1565ee6a4000..b6b13d3c10690623f390ffcc983f9caa246383bb 100644 (file)
 
 int EC_GROUP_get_basis_type(const EC_GROUP *group)
 {
-    int i = 0;
+    int i;
 
     if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) !=
         NID_X9_62_characteristic_two_field)
         /* everything else is currently not supported */
         return 0;
 
-    while (group->poly[i] != 0)
-        i++;
+    /* Find the last non-zero element of group->poly[] */
+    for (i = 0;
+         i < (int)OSSL_NELEM(group->poly) & group->poly[i] != 0;
+         i++)
+        continue;
 
     if (i == 4)
         return NID_X9_62_ppBasis;