intel / QAT_Engine

Intel QuickAssist Technology( QAT) OpenSSL Engine (an OpenSSL Plug-In Engine) which provides cryptographic acceleration for both hardware and optimized software using Intel QuickAssist Technology enabled Intel platforms. https://developer.intel.com/quickassist
BSD 3-Clause "New" or "Revised" License
406 stars 128 forks source link

qat_hw_sha3.c: resource leak in qat_sha3_session_data_init #294

Open ColinIanKing opened 9 months ago

ColinIanKing commented 9 months ago

in qat_hw_sha3.c, in function qat_sha3_session_data_init there is a leak of session_data when the call to OPENSSL_zalloc fails:

   session_data = OPENSSL_zalloc(sizeof(CpaCySymSessionSetupData));
    if (NULL == session_data) {
        WARN("session setup data Malloc failure\n");
        QATerr(QAT_F_QAT_SHA3_SESSION_DATA_INIT, QAT_R_SSD_MALLOC_FAILURE);
        return 0;
    }

    /* Set priority and operation of this session */
    session_data->sessionPriority = CPA_CY_PRIORITY_HIGH;
    session_data->symOperation = CPA_CY_SYM_OP_HASH;

    /* Set the hash mode and the length of the digest */
#ifdef QAT_OPENSSL_PROVIDER
    session_data->hashSetupData.hashAlgorithm = qat_get_hash_alg_data(ctx->md_type);
#else
    session_data->hashSetupData.hashAlgorithm = qat_get_hash_alg_data(EVP_MD_CTX_type(ctx));
#endif
    session_data->hashSetupData.hashMode = CPA_CY_SYM_HASH_MODE_PLAIN;
    session_data->hashSetupData.digestResultLenInBytes = sha3_ctx->md_size;
    session_data->hashSetupData.authModeSetupData.authKey = NULL;
    session_data->hashSetupData.nestedModeSetupData.pInnerPrefixData = NULL;
    session_data->hashSetupData.nestedModeSetupData.pOuterPrefixData = NULL;

    /* Tag follows immediately after the region to hash */
    session_data->digestIsAppended = CPA_FALSE;

    /* digestVerify is not required to be set.*/ 
    session_data->verifyDigest = CPA_FALSE;

    pOpData = OPENSSL_zalloc(sizeof(template_opData));
    if (pOpData == NULL) {
        WARN("memory allocation failed for symopData struct.\n");
        QATerr(QAT_F_QAT_SHA3_SESSION_DATA_INIT, ERR_R_MALLOC_FAILURE);
        OPENSSL_free(sha3_ctx->session_data);
        return 0;
    }

^^ the case where pOpData == NULL will return without free'ing session_data

bjayanax commented 9 months ago

@ColinIanKing Thanks for the help. We will check and fix it in upcoming release.