CVE-2019-16058: Improper Restriction of Operations within the Bounds of a Memory Buffer (CWE-119)
value: An issue was discovered in the pam_p11 component 0.2.0 and 0.3.0 for OpenSC. If a smart card creates a signature with a length longer than 256 bytes, this triggers a buffer overflow. This may be the case for RSA keys with 4096 bits depending on the signature scheme.
CWE description: The product performs operations on a memory buffer, but it reads from or writes to a memory location outside the buffer's intended boundary. This may result in read or write operations on unexpected memory locations that could be linked to other variables, data structures, or internal program data.
CVSS v3 base score: 7.5
Published: 2019-09-06T18:15Z
Repository: https://github.com/OpenSC/pam_p11
Language: C
Files changed: src/pam_p11.c
Commit date: 2019-08-26 13:54:09 +0200
Commit message: Use EVP_PKEY_size() to allocate correct size of signature buffer. (#18)

Do not use fixed buffer size for signature, EVP_SignFinal() requires
buffer for signature at least  EVP_PKEY_size(pkey) bytes in size.

Fixes crash when using 4K RSA signatures (https://github.com/OpenSC/pam_p11/issues/16, https://github.com/OpenSC/pam_p11/issues/15)
Diff: diff --git a/src/pam_p11.c b/src/pam_p11.c
index 8bec820..ff1a829 100644
--- a/src/pam_p11.c
+++ b/src/pam_p11.c
@@ -29,10 +29,11 @@
 #include <fcntl.h>
 #include <unistd.h>
 #include <openssl/crypto.h>
 #include <libp11.h>
 #include <regex.h>
+#include <stdlib.h>
 
 /* openssl deprecated API emulation */
 #ifndef HAVE_EVP_MD_CTX_NEW
 #define EVP_MD_CTX_new()	EVP_MD_CTX_create()
 #endif
@@ -632,17 +633,26 @@ static int randomize(pam_handle_t *pamh, unsigned char *r, unsigned int r_len)
 
 static int key_verify(pam_handle_t *pamh, int flags, PKCS11_KEY *authkey)
 {
 	int ok = 0;
 	unsigned char challenge[30];
-	unsigned char signature[256];
-	unsigned int siglen = sizeof signature;
+	unsigned char *signature = NULL;
+	unsigned int siglen;
 	const EVP_MD *md = EVP_sha1();
 	EVP_MD_CTX *md_ctx = EVP_MD_CTX_new();
 	EVP_PKEY *privkey = PKCS11_get_private_key(authkey);
 	EVP_PKEY *pubkey = PKCS11_get_public_key(authkey);
 
+	if (NULL == privkey)
+		goto err;
+	siglen = EVP_PKEY_size(privkey);
+	if (siglen <= 0)
+		goto err;
+	signature = malloc(siglen);
+	if (NULL == signature)
+		goto err;
+
 	/* Verify a SHA-1 hash of random data, signed by the key.
 	 *
 	 * Note that this will not work keys that aren't eligible for signing.
 	 * Unfortunately, libp11 currently has no way of checking
 	 * C_GetAttributeValue(CKA_SIGN), see
@@ -665,10 +675,11 @@ static int key_verify(pam_handle_t *pamh, int flags, PKCS11_KEY *authkey)
 		goto err;
 	}
 	ok = 1;
 
 err:
+	free(signature);
 	if (NULL != pubkey)
 		EVP_PKEY_free(pubkey);
 	if (NULL != privkey)
 		EVP_PKEY_free(privkey);
 	if (NULL != md_ctx) {

Vulnerable code: 	unsigned char signature[256];
	unsigned int siglen = sizeof signature;
Fixed code: #include <stdlib.h>
	unsigned char *signature = NULL;
	unsigned int siglen;
	if (NULL == privkey)
		goto err;
	siglen = EVP_PKEY_size(privkey);
	if (siglen <= 0)
		goto err;
	signature = malloc(siglen);
	if (NULL == signature)
		goto err;

	free(signature);