Skip to content

Commit

Permalink
crypto_wrappers: fix stack overflow caused by quote v5 cert too large
Browse files Browse the repository at this point in the history
Signed-off-by: Kun Lai <[email protected]>
  • Loading branch information
imlk0 committed May 30, 2024
1 parent e061a09 commit 475604c
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 18 deletions.
22 changes: 11 additions & 11 deletions api/librats_get_attestation_certificate.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ rats_attester_err_t librats_get_attestation_certificate(
bool attester_initialized = false;
bool crypto_wrapper_initialized = false;

rats_cert_info_t cert_info = { 0 };

if (!privkey || !privkey_len || (*privkey && !*privkey_len) || !certificate_out ||
!certificate_size_out || (!custom_claims && custom_claims_length))
return RATS_ATTESTER_ERR_INVALID_PARAMETER;
Expand Down Expand Up @@ -177,10 +179,10 @@ rats_attester_err_t librats_get_attestation_certificate(
RATS_DEBUG("endorsements buffer size: %zu\n", endorsements_buffer_size);

/* Prepare cert info for cert generation */
rats_cert_info_t cert_info = {
cert_info = (rats_cert_info_t) {
.subject = subject_name,
.cert_len = 0,
.cert_buf = { 0 },
.cert_bufer = NULL,
.cert_bufer_size = 0,
.evidence_buffer = evidence_buffer,
.evidence_buffer_size = evidence_buffer_size,
.endorsements_buffer = endorsements_buffer,
Expand All @@ -196,14 +198,10 @@ rats_attester_err_t librats_get_attestation_certificate(
goto err;
}

uint8_t *t = (uint8_t *)malloc(cert_info.cert_len);
if (!t) {
ret = RATS_ATTESTER_ERR_NO_MEM;
goto err;
}
memcpy(t, cert_info.cert_buf, cert_info.cert_len);
*certificate_out = t;
*certificate_size_out = cert_info.cert_len;
*certificate_out = cert_info.cert_bufer;
*certificate_size_out = cert_info.cert_bufer_size;
cert_info.cert_bufer = NULL;
cert_info.cert_bufer_size = 0;

ret = RATS_ATTESTER_ERR_NONE;
err:
Expand All @@ -215,6 +213,8 @@ rats_attester_err_t librats_get_attestation_certificate(
ctx.attester->opts->cleanup(ctx.attester) != RATS_ATTESTER_ERR_NONE) {
RATS_ERR("failed to clean up attester\n");
}
if (cert_info.cert_bufer)
free(cert_info.cert_bufer);
if (evidence_buffer)
free(evidence_buffer);
if (endorsements_buffer)
Expand Down
21 changes: 19 additions & 2 deletions crypto_wrappers/openssl/gen_cert.c
Original file line number Diff line number Diff line change
Expand Up @@ -204,12 +204,26 @@ crypto_wrapper_err_t openssl_gen_cert(crypto_wrapper_ctx_t *ctx, rats_hash_algo_
if (!X509_sign(cert, octx->privkey, EVP_sha256()))
goto err;

unsigned char *der = cert_info->cert_buf;
len = i2d_X509(cert, NULL);
if (len < 0) {
RATS_DEBUG("openssl failed to serialize X.509 cert\n");
goto err;
}

u_int8_t *cert_buffer = malloc(len);
if (cert_buffer == NULL) {
ret = CRYPTO_WRAPPER_ERR_NO_MEM;
goto err;
}

unsigned char *der = cert_buffer;
len = i2d_X509(cert, &der);
if (len < 0)
goto err;

cert_info->cert_len = len;
cert_info->cert_bufer = cert_buffer;
cert_info->cert_bufer_size = len;
cert_buffer = NULL;

RATS_DEBUG("self-signing certificate generated\n");

Expand All @@ -219,6 +233,9 @@ crypto_wrapper_err_t openssl_gen_cert(crypto_wrapper_ctx_t *ctx, rats_hash_algo_
if (ret != CRYPTO_WRAPPER_ERR_NONE)
RATS_DEBUG("failed to generate certificate %d\n", ret);

if (cert_buffer)
free(cert_buffer);

if (cert)
X509_free(cert);

Expand Down
5 changes: 3 additions & 2 deletions include/librats/cert.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#define _RATS_CERT_H

#include <stdint.h>
#include <stddef.h>

typedef struct {
const char *organization;
Expand All @@ -17,8 +18,8 @@ typedef struct {

typedef struct {
rats_cert_subject_t subject;
unsigned int cert_len;
uint8_t cert_buf[8192];
uint8_t *cert_bufer /* out */;
size_t cert_bufer_size /* out */;
uint8_t *evidence_buffer;
size_t evidence_buffer_size;
uint8_t *endorsements_buffer;
Expand Down
9 changes: 6 additions & 3 deletions verifiers/sev-snp/verify_evidence.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,13 @@ rats_verifier_err_t sev_snp_verify_evidence(rats_verifier_ctx_t *ctx,
return RATS_VERIFIER_ERR_INVALID;
#else
memset(evidence->snp.vcek, 0, VECK_MAX_SIZE);
err = sev_snp_get_vcek_der(report->chip_id, sizeof(report->chip_id),
&report->current_tcb, &evidence->snp);
if (err != RATS_ATTESTER_ERR_NONE)
rats_attester_err_t attester_err =
sev_snp_get_vcek_der(report->chip_id, sizeof(report->chip_id),
&report->current_tcb, &evidence->snp);
if (attester_err != RATS_ATTESTER_ERR_NONE) {
err = (rats_verifier_err_t)attester_err;
return err;
}
#endif
}

Expand Down

0 comments on commit 475604c

Please sign in to comment.