Skip to contentSkip to conversation

opensc: Fix CVE CVE-2023-5992

PATCH New Nikhil 1 participant 1 message Aug 6, 2024

lore
Share & tools
lore

Nikhil

author
#0 lore raw

PATCH 1/1 opensc: Fix CVE CVE-2023-5992

A vulnerability was found in OpenSC where PKCS#1
encryption padding removal is not implemented as
side-channel resistant. This issue may result in
the potential leak of private data.

Links: https://github.com/OpenSC/OpenSC/pull/2948/commits
https://github.com/OpenSC/OpenSC/pull/3016

Add the above commit to fix the below error
Error:
| ../../../git/src/pkcs11/mechanism.c: In function 'sc_pkcs11_decrypt':
| ../../../git/src/pkcs11/mechanism.c:1093:24: error: 'rv' undeclared (first use in this function)

The below commits were not added as they are part
of unittests
https://github.com/OpenSC/OpenSC/pull/2948/commits/708ce24c6b9f92a9f39bb7accda601b2891304a3
https://github.com/OpenSC/OpenSC/pull/3016/commits/7309b37eb82496e7324671283a39f9291be09830

---
 ...lement-the-C_DecryptFinalize-and-C_D.patch | 110 ++++++
 .../opensc/opensc/CVE-2023-5992-1.patch       | 357 ++++++++++++++++++
 .../opensc/opensc/CVE-2023-5992-2.patch       |  39 ++
 .../opensc/opensc/CVE-2023-5992-3.patch       | 105 ++++++
 .../opensc/opensc/CVE-2023-5992-4.patch       |  47 +++
 .../opensc/opensc/CVE-2023-5992-5.patch       | 115 ++++++
 .../opensc/opensc/CVE-2023-5992-6.patch       |  47 +++
 .../opensc/opensc/CVE-2023-5992-7.patch       |  33 ++
 .../opensc/opensc/CVE-2023-5992-8.patch       |  71 ++++
 .../recipes-support/opensc/opensc_0.22.0.bb   |   9 +
 10 files changed, 933 insertions(+)
 create mode 100644 meta-oe/recipes-support/opensc/opensc/0001-Preparing-to-implement-the-C_DecryptFinalize-and-C_D.patch
 create mode 100644 meta-oe/recipes-support/opensc/opensc/CVE-2023-5992-1.patch
 create mode 100644 meta-oe/recipes-support/opensc/opensc/CVE-2023-5992-2.patch
 create mode 100644 meta-oe/recipes-support/opensc/opensc/CVE-2023-5992-3.patch
 create mode 100644 meta-oe/recipes-support/opensc/opensc/CVE-2023-5992-4.patch
 create mode 100644 meta-oe/recipes-support/opensc/opensc/CVE-2023-5992-5.patch
 create mode 100644 meta-oe/recipes-support/opensc/opensc/CVE-2023-5992-6.patch
 create mode 100644 meta-oe/recipes-support/opensc/opensc/CVE-2023-5992-7.patch
 create mode 100644 meta-oe/recipes-support/opensc/opensc/CVE-2023-5992-8.patch
meta-oe/recipes-support/opensc/opensc/0001-Preparing-to-implement-the-C_DecryptFinalize-and-C_D.patch new file +110 -0
new file mode 100644
index 000000000..619ab522e
@@ -0,0 +1,110 @@
+From bb9bd627462af4d61943a33d6e54308e103618f0 Mon Sep 17 00:00:00 2001
+From: Peter Popovec <[email protected]>
+Date: Mon, 20 Dec 2021 08:43:49 +0100
+Subject: [PATCH] Preparing to implement the C_DecryptFinalize and
+ C_DecryptUpdate functions.
+
+The C_Decrypt() and pkcs15_prkey_decrypt() functions have been changed to
+allow pkcs15_prkey_decrypt() to be called from C_DecryptInit(),
+C_DecryptUpdate() and C_DecryptFinalize().
+
+CVE: CVE-2023-5992
+Upstream-Status: Backport [https://github.com/xhanulik/OpenSC/commit/bb9bd627462af4d61943a33d6e54308e103618f0]
+Signed-off-by: nikhil r <[email protected]>
+Comment: Refreshed hunk to match latest kirkstone
+---
+ src/pkcs11/framework-pkcs15.c | 15 +++++++++++++
+ src/pkcs11/mechanism.c        | 42 ++++++++++++++++++++++++++++++-----
+ 2 files changed, 51 insertions(+), 6 deletions(-)
+
+diff --git a/src/pkcs11/framework-pkcs15.c b/src/pkcs11/framework-pkcs15.c
+index 688cd84fdc..18f94271ed 100644
+--- a/src/pkcs11/framework-pkcs15.c
++++ b/src/pkcs11/framework-pkcs15.c
+@@ -4345,6 +4345,21 @@ pkcs15_prkey_decrypt(struct sc_pkcs11_se
+       int rv, flags = 0, prkey_has_path = 0;
+       CK_ULONG mask, good, rv_pkcs11;
+
++      if (pulDataLen == NULL) {
++              /* This is call from the C_DecyptInit function */
++              sc_log(context, "C_DecryptInit...");
++              return CKR_OK;
++      }
++      if (pEncryptedData == NULL && ulEncryptedDataLen == 0) {
++              /* This is call from the C_DecryptFinalize function */
++              sc_log(context, "C_DecryptFinalize...");
++              *pulDataLen = 0;
++              return CKR_OK;
++      }
++      /* DecryptUpdate: we assume this code is called only once per session, either
++       * from the C_Decrypt function or from an application using the C_DecryptUpdate call
++       */
++
+       sc_log(context, "Initiating decryption.");
+
+       if (!p11card)
+diff --git a/src/pkcs11/mechanism.c b/src/pkcs11/mechanism.c
+index 3035e8a763..a81b58fea6 100644
+--- a/src/pkcs11/mechanism.c
++++ b/src/pkcs11/mechanism.c
+@@ -1072,9 +1072,11 @@ sc_pkcs11_decrypt_init(sc_pkcs11_operati
+                       LOG_FUNC_RETURN(context, (int) rv);
+               }
+       }
+-
+       operation->priv_data = data;
+-      return CKR_OK;
++
++      /* The last parameter is NULL - this is call to INIT code in underlying functions */
++      return key->ops->decrypt(operation->session,
++                      key, &operation->mechanism, NULL, 0, NULL, NULL);
+ }
+
+ static CK_RV
+@@ -1084,14 +1086,42 @@ sc_pkcs11_decrypt(sc_pkcs11_operation_t
+ {
+       struct signature_data *data;
+       struct sc_pkcs11_object *key;
++      CK_RV rv;
++      CK_ULONG ulDataLen, ulLastDataLen;
++
++      /* PKCS#11: If pBuf is not NULL_PTR, then *pulBufLen must contain the size in bytes.. */
++      if (pData && !pulDataLen)
++              return CKR_ARGUMENTS_BAD;
++
++      ulDataLen = pulDataLen ? *pulDataLen : 0;
++      ulLastDataLen = ulDataLen;
+
+       data = (struct signature_data*) operation->priv_data;
+
+       key = data->key;
+-      return key->ops->decrypt(operation->session,
+-                              key, &operation->mechanism,
+-                              pEncryptedData, ulEncryptedDataLen,
+-                              pData, pulDataLen);
++
++      /* Decrypt */
++      rv = key->ops->decrypt(operation->session, key, &operation->mechanism,
++                      pEncryptedData, ulEncryptedDataLen, pData, &ulDataLen);
++
++      if (pulDataLen)
++              *pulDataLen = ulDataLen;
++
++      if (rv != CKR_OK)
++              return rv;
++
++      /* recalculate buffer space */
++      if (ulDataLen <= ulLastDataLen)
++              ulLastDataLen -= ulDataLen;
++      else
++              ulLastDataLen = 0;
++
++      /* DecryptFinalize */
++      rv = key->ops->decrypt(operation->session, key, &operation->mechanism,
++                      NULL, 0, pData + ulDataLen, &ulLastDataLen);
++      if (pulDataLen)
++              *pulDataLen = ulDataLen + ulLastDataLen;
++      return rv;
+ }
+
+ static CK_RV
meta-oe/recipes-support/opensc/opensc/CVE-2023-5992-1.patch new file +357 -0

357 lines not on this page · raw message

meta-oe/recipes-support/opensc/opensc/CVE-2023-5992-2.patch new file +39 -0
new file mode 100644
index 000000000..1d8bc1259
@@ -0,0 +1,39 @@
+From 1a6a18c51a1d6239850b61d19aed6424afd4912f Mon Sep 17 00:00:00 2001
+From: =?UTF-8?q?Veronika=20Hanul=C3=ADkov=C3=A1?= <[email protected]>
+Date: Thu, 16 Nov 2023 10:38:12 +0100
+Subject: [PATCH] pkcs15-sec: Remove logging after PKCS#1 v1.5 depadding
+
+To prevent Marvin attack on RSA PKCS#1 v1.5 padding
+when logging the return value, signaling the padding error.
+
+CVE: CVE-2023-5992
+Upstream-Status: Backport [https://github.com/OpenSC/OpenSC/pull/2948/commits/1a6a18c51a1d6239850b61d19aed6424afd4912f]
+Signed-off-by: nikhil r <[email protected]>
+Comment: Refreshed hunk to match latest kirkstone
+---
+ src/libopensc/pkcs15-sec.c | 9 +++++----
+ 1 file changed, 5 insertions(+), 4 deletions(-)
+
+diff --git a/src/libopensc/pkcs15-sec.c b/src/libopensc/pkcs15-sec.c
+index a019af460f..f7ee819d65 100644
+--- a/src/libopensc/pkcs15-sec.c
++++ b/src/libopensc/pkcs15-sec.c
+@@ -308,13 +308,14 @@ int sc_pkcs15_decipher(struct sc_pkcs15_
+
+       /* Strip any padding */
+       if (pad_flags & SC_ALGORITHM_RSA_PAD_PKCS1) {
+-              int s = r;
+-              int key_size = alg_info->key_length;
++              unsigned int s = r;
++              unsigned int key_size = (unsigned int)alg_info->key_length;
+               r = sc_pkcs1_strip_02_padding_constant_time(ctx, key_size / 8, out, s, out, &s);
+-              LOG_TEST_RET(ctx, r, "Invalid PKCS#1 padding");
++              /* for keeping PKCS#1 v1.5 depadding constant-time, do not log error here */
+       }
+
+-      LOG_FUNC_RETURN(ctx, r);
++      /* do not log error code to prevent side channel attack */
++      return r;
+ }
+
+ /* derive one key from another. RSA can use decipher, so this is for only ECDH
meta-oe/recipes-support/opensc/opensc/CVE-2023-5992-3.patch new file +105 -0

105 lines not on this page · raw message

meta-oe/recipes-support/opensc/opensc/CVE-2023-5992-4.patch new file +47 -0

47 lines not on this page · raw message

meta-oe/recipes-support/opensc/opensc/CVE-2023-5992-5.patch new file +115 -0

115 lines not on this page · raw message

meta-oe/recipes-support/opensc/opensc/CVE-2023-5992-6.patch new file +47 -0

47 lines not on this page · raw message

meta-oe/recipes-support/opensc/opensc/CVE-2023-5992-7.patch new file +33 -0
new file mode 100644
index 000000000..7b41abc0e
@@ -0,0 +1,33 @@
+From aa6bf2b65fe432e8f234a132268b79ea54d5d74b Mon Sep 17 00:00:00 2001
+From: =?UTF-8?q?Veronika=20Hanul=C3=ADkov=C3=A1?= <[email protected]>
+Date: Fri, 24 Nov 2023 21:00:23 +0100
+Subject: [PATCH] misc: Compare return value constant-time
+
+CVE: CVE-2023-5992
+Upstream-Status: Backport [https://github.com/OpenSC/OpenSC/pull/2948/commits/aa6bf2b65fe432e8f234a132268b79ea54d5d74b]
+Signed-off-by: nikhil r <[email protected]>
+---
+ src/pkcs11/misc.c | 3 ++-
+ 1 file changed, 2 insertions(+), 1 deletion(-)
+
+diff --git a/src/pkcs11/misc.c b/src/pkcs11/misc.c
+index 5ca1176b1d..1d893d6181 100644
+--- a/src/pkcs11/misc.c
++++ b/src/pkcs11/misc.c
+@@ -23,6 +23,7 @@
+ #include <stdlib.h>
+ #include <string.h>
+
++#include "common/constant-time.h"
+ #include "sc-pkcs11.h"
+
+ #define DUMP_TEMPLATE_MAX     32
+@@ -174,7 +175,7 @@ CK_RV reset_login_state(struct sc_pkcs11_slot *slot, CK_RV rv)
+                       slot->p11card->framework->logout(slot);
+               }
+
+-              if (rv == CKR_USER_NOT_LOGGED_IN) {
++              if (constant_time_eq_s(rv, CKR_USER_NOT_LOGGED_IN)) {
+                       slot->login_user = -1;
+                       pop_all_login_states(slot);
+               }
meta-oe/recipes-support/opensc/opensc/CVE-2023-5992-8.patch new file +71 -0

71 lines not on this page · raw message

meta-oe/recipes-support/opensc/opensc_0.22.0.bb +9 -0
index 770c2d686..16b3ec292 100644
@@ -23,6 +23,15 @@ SRC_URI = "git://github.com/OpenSC/OpenSC;branch=master;protocol=https \
            file://CVE-2023-40661-5.patch \
            file://CVE-2023-40661-6.patch \
            file://CVE-2023-40661-7.patch \
+           file://CVE-2023-5992-1.patch \
+           file://CVE-2023-5992-2.patch \
+           file://CVE-2023-5992-3.patch \
+           file://0001-Preparing-to-implement-the-C_DecryptFinalize-and-C_D.patch \
+           file://CVE-2023-5992-4.patch \
+           file://CVE-2023-5992-5.patch \
+           file://CVE-2023-5992-6.patch \
+           file://CVE-2023-5992-7.patch \
+           file://CVE-2023-5992-8.patch \
           "
 
 # CVE-2021-34193 is a duplicate CVE covering the 5 individual

--
2.25.1

This message contains information that may be privileged or confidential and is the property of the KPIT Technologies Ltd. It is intended only for the person to whom it is addressed. If you are not the intended recipient, you are not authorized to read, print, retain copy, disseminate, distribute, or use this message or any part thereof. If you receive this message in error, please notify the sender immediately and delete all copies of this message. KPIT Technologies Ltd. does not accept any liability for virus infected mails.