Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possible Buffer overflow: add length checks to header creation #51

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/est/est_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -1274,6 +1274,11 @@ static int est_client_build_cacerts_header (EST_CTX *ctx, char *hdr)
EST_LOG_WARN("CA Certs header took up the maximum amount in buffer (%d)",
EST_HTTP_REQ_TOTAL_LEN);
}

if(hdr_len > EST_HTTP_HDR_MAX) {
EST_LOG_ERR("CA Certs header is too big (%d) > (%d)", hdr_len, EST_HTTP_HDR_MAX);
return 0;
}

return (hdr_len);
}
Expand Down Expand Up @@ -1307,6 +1312,12 @@ static int est_client_build_csr_header (EST_CTX *ctx, char *hdr)
EST_LOG_WARN("CSR attributes request header took up the maximum amount in buffer (%d)",
EST_HTTP_REQ_TOTAL_LEN);
}

if(hdr_len > EST_HTTP_HDR_MAX) {
EST_LOG_ERR("CSR attributes header is too big (%d) > (%d)", hdr_len, EST_HTTP_HDR_MAX);
return 0;
}

return (hdr_len);
}

Expand Down Expand Up @@ -1435,6 +1446,11 @@ static int est_client_build_enroll_header (EST_CTX *ctx, char *hdr, int pkcs10_l
EST_LOG_WARN("Client enroll request header took up the maximum amount in buffer (%d)",
EST_HTTP_REQ_TOTAL_LEN);
}

if(hdr_len > EST_HTTP_HDR_MAX) {
EST_LOG_ERR("Client enroll request header is too big (%d) > (%d)", hdr_len, EST_HTTP_HDR_MAX);
return 0;
}

return (hdr_len);
}
Expand Down Expand Up @@ -1471,6 +1487,12 @@ static int est_client_build_reenroll_header (EST_CTX *ctx, char *hdr, int pkcs10
EST_LOG_WARN("Client reenroll request header took up the maximum amount in buffer (%d)",
EST_HTTP_REQ_TOTAL_LEN);
}

if(hdr_len > EST_HTTP_HDR_MAX) {
EST_LOG_ERR("Client reenroll request header is too big (%d) > (%d)", hdr_len, EST_HTTP_HDR_MAX);
return 0;
}

return (hdr_len);
}

Expand Down Expand Up @@ -2615,6 +2637,12 @@ static int est_client_send_cacerts_request (EST_CTX *ctx, SSL *ssl,
}

hdr_len = est_client_build_cacerts_header(ctx, http_data);
if(hdr_len == 0) {
EST_LOG_ERR("Unable to build CA Cert header");
free(http_data);
return (EST_ERR_HTTP_CANNOT_BUILD_HEADER);
}

/*
* terminate the HTTP header
*/
Expand Down
12 changes: 6 additions & 6 deletions src/est/est_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,13 @@ int est_handle_cacerts (EST_CTX *ctx, unsigned char *ca_certs, int ca_certs_len,
snprintf(http_hdr, EST_HTTP_HDR_MAX, "%s%s%s%s", EST_HTTP_HDR_200, EST_HTTP_HDR_EOL,
EST_HTTP_HDR_STAT_200, EST_HTTP_HDR_EOL);
hdrlen = strnlen_s(http_hdr, EST_HTTP_HDR_MAX);
snprintf(http_hdr + hdrlen, EST_HTTP_HDR_MAX, "%s: %s%s", EST_HTTP_HDR_CT,
snprintf(http_hdr + hdrlen, EST_HTTP_HDR_MAX - hdrlen, "%s: %s%s", EST_HTTP_HDR_CT,
EST_HTTP_CT_PKCS7, EST_HTTP_HDR_EOL);
hdrlen = strnlen_s(http_hdr, EST_HTTP_HDR_MAX);
snprintf(http_hdr + hdrlen, EST_HTTP_HDR_MAX, "%s: %s%s", EST_HTTP_HDR_CE,
snprintf(http_hdr + hdrlen, EST_HTTP_HDR_MAX - hdrlen, "%s: %s%s", EST_HTTP_HDR_CE,
EST_HTTP_CE_BASE64, EST_HTTP_HDR_EOL);
hdrlen = strnlen_s(http_hdr, EST_HTTP_HDR_MAX);
snprintf(http_hdr + hdrlen, EST_HTTP_HDR_MAX, "%s: %d%s%s", EST_HTTP_HDR_CL,
snprintf(http_hdr + hdrlen, EST_HTTP_HDR_MAX - hdrlen, "%s: %d%s%s", EST_HTTP_HDR_CL,
ca_certs_len, EST_HTTP_HDR_EOL, EST_HTTP_HDR_EOL);
if (!mg_write(http_ctx, http_hdr, strnlen_s(http_hdr, EST_HTTP_HDR_MAX))) {
return (EST_ERR_HTTP_WRITE);
Expand Down Expand Up @@ -1193,13 +1193,13 @@ static EST_ERROR est_handle_simple_enroll (EST_CTX *ctx, void *http_ctx, SSL *ss
snprintf(http_hdr, EST_HTTP_HDR_MAX, "%s%s%s%s", EST_HTTP_HDR_200, EST_HTTP_HDR_EOL,
EST_HTTP_HDR_STAT_200, EST_HTTP_HDR_EOL);
hdrlen = strnlen_s(http_hdr, EST_HTTP_HDR_MAX);
snprintf(http_hdr + hdrlen, EST_HTTP_HDR_MAX, "%s: %s%s", EST_HTTP_HDR_CT,
snprintf(http_hdr + hdrlen, EST_HTTP_HDR_MAX - hdrlen, "%s: %s%s", EST_HTTP_HDR_CT,
EST_HTTP_CT_PKCS7_CO, EST_HTTP_HDR_EOL);
hdrlen = strnlen_s(http_hdr, EST_HTTP_HDR_MAX);
snprintf(http_hdr + hdrlen, EST_HTTP_HDR_MAX, "%s: %s%s", EST_HTTP_HDR_CE,
snprintf(http_hdr + hdrlen, EST_HTTP_HDR_MAX - hdrlen, "%s: %s%s", EST_HTTP_HDR_CE,
EST_HTTP_CE_BASE64, EST_HTTP_HDR_EOL);
hdrlen = strnlen_s(http_hdr, EST_HTTP_HDR_MAX);
snprintf(http_hdr + hdrlen, EST_HTTP_HDR_MAX, "%s: %d%s%s", EST_HTTP_HDR_CL,
snprintf(http_hdr + hdrlen, EST_HTTP_HDR_MAX - hdrlen, "%s: %d%s%s", EST_HTTP_HDR_CL,
cert_len, EST_HTTP_HDR_EOL, EST_HTTP_HDR_EOL);
if (!mg_write(http_ctx, http_hdr, strnlen_s(http_hdr, EST_HTTP_HDR_MAX))) {
free(cert);
Expand Down
6 changes: 3 additions & 3 deletions src/est/est_server_http.c
Original file line number Diff line number Diff line change
Expand Up @@ -1884,13 +1884,13 @@ EST_ERROR est_send_csrattr_data (EST_CTX *ctx, char *csr_data, int csr_len, void
snprintf(http_hdr, EST_HTTP_HDR_MAX, "%s%s%s%s", EST_HTTP_HDR_200, EST_HTTP_HDR_EOL,
EST_HTTP_HDR_STAT_200, EST_HTTP_HDR_EOL);
hdrlen = strnlen_s(http_hdr, EST_HTTP_HDR_MAX);
snprintf(http_hdr + hdrlen, EST_HTTP_HDR_MAX, "%s: %s%s", EST_HTTP_HDR_CT,
snprintf(http_hdr + hdrlen, EST_HTTP_HDR_MAX - hdr_len, "%s: %s%s", EST_HTTP_HDR_CT,
EST_HTTP_CT_CSRATTRS, EST_HTTP_HDR_EOL);
hdrlen = strnlen_s(http_hdr, EST_HTTP_HDR_MAX);
snprintf(http_hdr + hdrlen, EST_HTTP_HDR_MAX, "%s: %s%s", EST_HTTP_HDR_CE,
snprintf(http_hdr + hdrlen, EST_HTTP_HDR_MAX - hdr_len, "%s: %s%s", EST_HTTP_HDR_CE,
EST_HTTP_CE_BASE64, EST_HTTP_HDR_EOL);
hdrlen = strnlen_s(http_hdr, EST_HTTP_HDR_MAX);
snprintf(http_hdr + hdrlen, EST_HTTP_HDR_MAX, "%s: %d%s%s", EST_HTTP_HDR_CL,
snprintf(http_hdr + hdrlen, EST_HTTP_HDR_MAX - hdr_len, "%s: %d%s%s", EST_HTTP_HDR_CL,
csr_len, EST_HTTP_HDR_EOL, EST_HTTP_HDR_EOL);
if (!mg_write(http_ctx, http_hdr, strnlen_s(http_hdr, EST_HTTP_HDR_MAX))) {
free(csr_data);
Expand Down