Commit 2309fab9fa for openssl.org

commit 2309fab9fac46eb0b7a7f7160591e8abe6164017
Author: b84330808 <b84330808@gmail.com>
Date:   Wed Nov 4 15:52:36 2026 +0000

    x509, ocsp: avoid lookup cursor overflow at INT_MAX

    The lookup functions that take a "last position" cursor search
    the entries after it and return -1 (or NULL with *idx set to -1)
    when there are none.  They all compute lastpos + 1 before comparing
    it with the number of entries, which overflows a signed int when
    the caller passes INT_MAX:

    - X509_NAME_get_index_by_OBJ() clamps negative values before
      the increment, so the wrapped index INT_MIN reaches
      sk_X509_NAME_ENTRY_value(), which returns NULL, and the NULL entry
      is dereferenced.
    - X509v3_get_ext_by_OBJ(), X509v3_get_ext_by_critical(),
      and X509at_get_attr_by_OBJ() clamp after the increment, so the search
      silently restarts at index 0 and returns the first match.
    - X509V3_get_d2i() does the same with *idx + 1 and returns the first
      matching extension with *idx = 0.
    - OCSP_resp_find() increments last in the same way and dereferences
      the NULL single response.

    The X509_*, X509_CRL_*, X509_REVOKED_*, X509_REQ_*, OCSP_*, TS_*,
    CMS_*_get_attr_by_*() and EVP_PKEY_get_attr_by_*() cursor functions
    and the *_get_ext_d2i() family are thin wrappers around these.

    Return "not found" as soon as the cursor is at or past the last entry.
    The entry count is an int, so lastpos + 1 cannot overflow after that
    check.  The result for every other cursor value is unchanged.

    Add tests for the x509 functions to x509_internal_test
    and for OCSP_resp_find() to ocspapitest.

    Resolves: https://github.com/openssl/openssl/issues/33498
    Assisted-by: Claude Code:claude-opus-5-5
    Reviewed-by: Tomas Mraz <tomas@openssl.foundation>
    Reviewed-by: Eugene Syromiatnikov <esyr@openssl.org>
    Merge-date: Sat Nov 21 06:40:44 2026
    Merged-from: https://github.com/openssl/openssl/pull/33548

diff --git a/crypto/ocsp/ocsp_cl.c b/crypto/ocsp/ocsp_cl.c
index 14537be9d8..5467bdef26 100644
--- a/crypto/ocsp/ocsp_cl.c
+++ b/crypto/ocsp/ocsp_cl.c
@@ -215,18 +215,22 @@ int OCSP_resp_get1_id(const OCSP_BASICRESP *bs,
 /* Look single response matching a given certificate ID */
 int OCSP_resp_find(OCSP_BASICRESP *bs, OCSP_CERTID *id, int last)
 {
-    int i;
+    int i, n;
     STACK_OF(OCSP_SINGLERESP) *sresp;
     OCSP_SINGLERESP *single;

     if (bs == NULL)
         return -1;
+    sresp = bs->tbsResponseData.responses;
+    n = sk_OCSP_SINGLERESP_num(sresp);
+    /* Nothing can follow last; this also keeps last + 1 in range */
+    if (last >= n)
+        return -1;
     if (last < 0)
         last = 0;
     else
         last++;
-    sresp = bs->tbsResponseData.responses;
-    for (i = last; i < sk_OCSP_SINGLERESP_num(sresp); i++) {
+    for (i = last; i < n; i++) {
         single = sk_OCSP_SINGLERESP_value(sresp, i);
         if (!OCSP_id_cmp(id, single->certId))
             return i;
diff --git a/crypto/x509/v3_lib.c b/crypto/x509/v3_lib.c
index a4b46d0924..a6c9986638 100644
--- a/crypto/x509/v3_lib.c
+++ b/crypto/x509/v3_lib.c
@@ -202,7 +202,7 @@ void *X509V3_EXT_d2i(const X509_EXTENSION *ext)
 void *X509V3_get_d2i(const STACK_OF(X509_EXTENSION) *x, int nid, int *crit,
     int *idx)
 {
-    int lastpos, i;
+    int lastpos, i, n;
     X509_EXTENSION *ex, *found_ex = NULL;

     if (!x) {
@@ -212,13 +212,14 @@ void *X509V3_get_d2i(const STACK_OF(X509_EXTENSION) *x, int nid, int *crit,
             *crit = -1;
         return NULL;
     }
-    if (idx)
-        lastpos = *idx + 1;
-    else
+    n = sk_X509_EXTENSION_num(x);
+    if (idx == NULL || *idx < 0)
         lastpos = 0;
-    if (lastpos < 0)
-        lastpos = 0;
-    for (i = lastpos; i < sk_X509_EXTENSION_num(x); i++) {
+    else if (*idx >= n)
+        lastpos = n; /* Nothing can follow *idx; also avoids *idx + 1 overflow */
+    else
+        lastpos = *idx + 1;
+    for (i = lastpos; i < n; i++) {
         ex = sk_X509_EXTENSION_value(x, i);
         if (OBJ_obj2nid(X509_EXTENSION_get_object(ex)) == nid) {
             if (idx) {
diff --git a/crypto/x509/x509_att.c b/crypto/x509/x509_att.c
index ae4b71c05b..9f428e1fc0 100644
--- a/crypto/x509/x509_att.c
+++ b/crypto/x509/x509_att.c
@@ -41,10 +41,13 @@ int X509at_get_attr_by_OBJ(const STACK_OF(X509_ATTRIBUTE) *sk,

     if (sk == NULL)
         return -1;
+    n = sk_X509_ATTRIBUTE_num(sk);
+    /* Nothing can follow lastpos; this also keeps lastpos + 1 in range */
+    if (lastpos >= n)
+        return -1;
     lastpos++;
     if (lastpos < 0)
         lastpos = 0;
-    n = sk_X509_ATTRIBUTE_num(sk);
     for (; lastpos < n; lastpos++) {
         ex = sk_X509_ATTRIBUTE_value(sk, lastpos);
         if (OBJ_cmp(ex->object, obj) == 0)
diff --git a/crypto/x509/x509_v3.c b/crypto/x509/x509_v3.c
index d3fc114163..f8bb3f1eaa 100644
--- a/crypto/x509/x509_v3.c
+++ b/crypto/x509/x509_v3.c
@@ -48,10 +48,13 @@ int X509v3_get_ext_by_OBJ(const STACK_OF(X509_EXTENSION) *sk,

     if (sk == NULL)
         return -1;
+    n = sk_X509_EXTENSION_num(sk);
+    /* Nothing can follow lastpos; this also keeps lastpos + 1 in range */
+    if (lastpos >= n)
+        return -1;
     lastpos++;
     if (lastpos < 0)
         lastpos = 0;
-    n = sk_X509_EXTENSION_num(sk);
     for (; lastpos < n; lastpos++) {
         ex = sk_X509_EXTENSION_value(sk, lastpos);
         if (OBJ_cmp(ex->object, obj) == 0)
@@ -68,10 +71,13 @@ int X509v3_get_ext_by_critical(const STACK_OF(X509_EXTENSION) *sk, int crit,

     if (sk == NULL)
         return -1;
+    n = sk_X509_EXTENSION_num(sk);
+    /* Nothing can follow lastpos; this also keeps lastpos + 1 in range */
+    if (lastpos >= n)
+        return -1;
     lastpos++;
     if (lastpos < 0)
         lastpos = 0;
-    n = sk_X509_EXTENSION_num(sk);
     for (; lastpos < n; lastpos++) {
         ex = sk_X509_EXTENSION_value(sk, lastpos);
         c = X509_EXTENSION_get_critical(ex);
diff --git a/crypto/x509/x509name.c b/crypto/x509/x509name.c
index 1cb9ad00f4..7e24048ef7 100644
--- a/crypto/x509/x509name.c
+++ b/crypto/x509/x509name.c
@@ -87,6 +87,9 @@ int X509_NAME_get_index_by_OBJ(const X509_NAME *name, const ASN1_OBJECT *obj,
         lastpos = -1;
     sk = name->entries;
     n = sk_X509_NAME_ENTRY_num(sk);
+    /* Nothing can follow lastpos; this also keeps lastpos + 1 in range */
+    if (lastpos >= n)
+        return -1;
     for (lastpos++; lastpos < n; lastpos++) {
         ne = sk_X509_NAME_ENTRY_value(sk, lastpos);
         if (OBJ_cmp(ne->object, obj) == 0)
diff --git a/test/ocspapitest.c b/test/ocspapitest.c
index 2444b85b05..1ac1d5293a 100644
--- a/test/ocspapitest.c
+++ b/test/ocspapitest.c
@@ -7,6 +7,7 @@
  * https://www.openssl.org/source/license.html
  */

+#include <limits.h>
 #include <string.h>

 #include <openssl/opensslconf.h>
@@ -114,6 +115,32 @@ err:
     return bs_out;
 }

+/*
+ * OCSP_resp_find() searches after |last|.  With last == INT_MAX there is
+ * nothing to find, and stepping past it must not overflow.
+ */
+static int test_resp_find_last_int_max(void)
+{
+    OCSP_BASICRESP *bs = NULL;
+    OCSP_CERTID *cid = NULL;
+    int ret = 0;
+
+    if (!TEST_ptr(bs = make_dummy_resp())
+        || !TEST_ptr(cid = OCSP_CERTID_dup(OCSP_SINGLERESP_get0_id(OCSP_resp_get0(bs, 0)))))
+        goto err;
+
+    if (!TEST_int_eq(OCSP_resp_find(bs, cid, -1), 0)
+        || !TEST_int_eq(OCSP_resp_find(bs, cid, 0), -1)
+        || !TEST_int_eq(OCSP_resp_find(bs, cid, INT_MAX), -1))
+        goto err;
+
+    ret = 1;
+err:
+    OCSP_CERTID_free(cid);
+    OCSP_BASICRESP_free(bs);
+    return ret;
+}
+
 static int test_resp_signer(void)
 {
     OCSP_BASICRESP *bs = NULL;
@@ -274,6 +301,7 @@ int setup_tests(void)
         || !TEST_ptr(cacertstr = test_get_argument(4)))
         return 0;
 #ifndef OPENSSL_NO_OCSP
+    ADD_TEST(test_resp_find_last_int_max);
     ADD_TEST(test_resp_signer);
     ADD_ALL_TESTS(test_access_description, 3);
     ADD_TEST(test_ocsp_url_svcloc_new);
diff --git a/test/x509_internal_test.c b/test/x509_internal_test.c
index b2798cd626..ffdd69716c 100644
--- a/test/x509_internal_test.c
+++ b/test/x509_internal_test.c
@@ -9,6 +9,7 @@

 /* Internal tests for the x509 and x509v3 modules */

+#include <limits.h>
 #include <stdio.h>
 #include <string.h>

@@ -288,6 +289,68 @@ err:
     return ret;
 }

+/*
+ * The lookup functions that take a |lastpos| cursor search the entries
+ * after it.  With lastpos == INT_MAX nothing can follow, so they must report
+ * "not found" rather than overflow when stepping past it.
+ */
+static int test_lookup_lastpos_int_max(void)
+{
+    X509 *x = NULL;
+    X509_NAME *name = NULL;
+    X509_REQ *req = NULL;
+    BASIC_CONSTRAINTS *bc = NULL;
+    int crit = 0, idx = INT_MAX, ret = 0;
+
+    if (!TEST_ptr(x = X509_new())
+        || !TEST_ptr(name = X509_NAME_new())
+        || !TEST_ptr(req = X509_REQ_new())
+        || !TEST_ptr(bc = BASIC_CONSTRAINTS_new())
+        || !TEST_true(X509_add1_ext_i2d(x, NID_basic_constraints, bc, 1, 0))
+        || !TEST_true(X509_NAME_add_entry_by_NID(name, NID_commonName,
+            MBSTRING_ASC, (const unsigned char *)"test", -1, -1, 0))
+        || !TEST_true(X509_REQ_add1_attr_by_NID(req,
+            NID_pkcs9_challengePassword, MBSTRING_ASC,
+            (const unsigned char *)"pw", -1)))
+        goto err;
+    BASIC_CONSTRAINTS_free(bc);
+    bc = NULL;
+
+    /* Searching from the start finds the single entry */
+    if (!TEST_int_eq(X509_NAME_get_index_by_NID(name, NID_commonName, -1), 0)
+        || !TEST_int_eq(X509_get_ext_by_NID(x, NID_basic_constraints, -1), 0)
+        || !TEST_int_eq(X509_get_ext_by_critical(x, 1, -1), 0)
+        || !TEST_int_eq(X509_REQ_get_attr_by_NID(req,
+                            NID_pkcs9_challengePassword, -1),
+            0))
+        goto err;
+
+    /* Nothing follows INT_MAX */
+    if (!TEST_int_eq(X509_NAME_get_index_by_NID(name, NID_commonName, INT_MAX),
+            -1)
+        || !TEST_int_eq(X509_get_ext_by_NID(x, NID_basic_constraints, INT_MAX),
+            -1)
+        || !TEST_int_eq(X509_get_ext_by_critical(x, 1, INT_MAX), -1)
+        || !TEST_int_eq(X509_REQ_get_attr_by_NID(req,
+                            NID_pkcs9_challengePassword, INT_MAX),
+            -1))
+        goto err;
+
+    bc = X509_get_ext_d2i(x, NID_basic_constraints, &crit, &idx);
+    if (!TEST_ptr_null(bc)
+        || !TEST_int_eq(crit, -1)
+        || !TEST_int_eq(idx, -1))
+        goto err;
+
+    ret = 1;
+err:
+    BASIC_CONSTRAINTS_free(bc);
+    X509_REQ_free(req);
+    X509_NAME_free(name);
+    X509_free(x);
+    return ret;
+}
+
 /*
  * A failed encoding save discards the cached encoding, and the item is
  * encoded from its fields afterwards.
@@ -1490,6 +1553,7 @@ int setup_tests(void)
     ADD_ALL_TESTS(test_a2i_ipaddress, OSSL_NELEM(a2i_ipaddress_tests));
     ADD_ALL_TESTS(test_ipaddr_to_asc, OSSL_NELEM(ipaddr_to_asc_tests));
     ADD_TEST(test_crl_add_ext_modifies);
+    ADD_TEST(test_lookup_lastpos_int_max);
     ADD_TEST(test_enc_save_failure);
     ADD_TEST(tests_X509_PURPOSE);
     ADD_TEST(tests_X509_check_time);