1
0
mirror of https://github.com/wolfSSL/wolfssl.git synced 2026-10-24 04:09:07 +03:00

Bound error-message and time-string formatting under WOLF_C89

Under WOLF_C89, XSNPRINTF is sprintf and ignores the size argument.
WOLFSSL_ERROR_LINE formatted the source file name into an 80-byte
buffer and wolfSSL_ERR_print_errors formatted the reason and file into
a 160-byte one, so long __FILE__ paths, as CMake builds produce,
overflowed the stack. GetTimeString wrote its 24-character string
regardless of the length wolfSSL_ASN1_TIME_to_string was given.

Bound the file name and reason with a precision, and format the time
into a local buffer and copy it only when it fits the caller's length.
This commit is contained in:
Tobias Frauenschläger
2026-09-30 10:05:25 +02:00
parent c991775457
commit 5a02fa04d9
3 changed files with 18 additions and 10 deletions

View File

@@ -149,7 +149,7 @@ void wolfSSL_ERR_print_errors_cb (int (*cb)(const char *str, size_t len,
const char* reason = NULL;
int ret;
int line = 0;
char buf[WOLFSSL_MAX_ERROR_SZ * 2];
char buf[WOLFSSL_MAX_ERROR_SZ * 2 + 64];
WOLFSSL_ENTER("wolfSSL_ERR_print_errors");
@@ -164,8 +164,9 @@ void wolfSSL_ERR_print_errors_cb (int (*cb)(const char *str, size_t len,
const char* r = wolfSSL_ERR_reason_error_string(
(unsigned long)(0 - ret));
if (XSNPRINTF(buf, sizeof(buf),
"error:%d:wolfSSL library:%s:%s:%d\n",
ret, r, file, line)
"error:%d:wolfSSL library:%.*s:%.*s:%d\n",
ret, WOLFSSL_MAX_ERROR_SZ - 1, r,
WOLFSSL_MAX_ERROR_SZ - 1, file, line)
>= (int)sizeof(buf))
{
WOLFSSL_MSG("Buffer overrun formatting error message");

View File

@@ -16299,6 +16299,8 @@ int GetTimeString(byte* date, int format, char* buf, int len, int dateLen)
{
struct tm t;
int idx = 0;
char tmp[64];
int tmpLen;
if (!ExtractDate(date, (unsigned char)format, &t, &idx, dateLen)) {
return 0;
@@ -16330,13 +16332,13 @@ int GetTimeString(byte* date, int format, char* buf, int len, int dateLen)
}
idx = 4; /* use idx now for char buffer */
if (XSNPRINTF(buf + idx, (size_t)(len - idx), "%2d %02d:%02d:%02d %d GMT",
t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec, (int)t.tm_year + 1900)
>= len - idx)
{
tmpLen = XSNPRINTF(tmp, sizeof(tmp), "%2d %02d:%02d:%02d %d GMT",
t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec, (int)t.tm_year + 1900);
if ((tmpLen < 0) || (tmpLen >= len - idx)) {
WOLFSSL_MSG("buffer overrun in GetTimeString");
return 0;
}
XMEMCPY(buf + idx, tmp, (size_t)tmpLen + 1);
return 1;
}

View File

@@ -1775,6 +1775,7 @@ void WOLFSSL_ERROR(int error)
"wolfSSL error occurred, error = %d", error);
}
else {
int len;
#if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
/* If running in compatibility mode do not add want read and
want right to error queue */
@@ -1783,9 +1784,13 @@ void WOLFSSL_ERROR(int error)
#endif
if (error < 0)
error = error - (2 * error); /* get absolute value */
(void)XSNPRINTF(buffer, sizeof(buffer),
"wolfSSL error occurred, error = %d line:%u file:%s",
error, line, file);
len = XSNPRINTF(buffer, sizeof(buffer),
"wolfSSL error occurred, error = %d line:%u file:",
error, line);
if ((len > 0) && (len < (int)sizeof(buffer))) {
(void)XSNPRINTF(buffer + len, sizeof(buffer) - (size_t)len,
"%.*s", (int)sizeof(buffer) - 1 - len, file);
}
if (wc_AddErrorNode(error, (int)line, buffer, (char*)file) != 0) {
WOLFSSL_MSG("Error creating logging node");