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

20240718-BIO_DGRAM-memory-leak #7760

Merged
merged 1 commit into from
Jul 19, 2024
Merged
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
254 changes: 137 additions & 117 deletions src/bio.c

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions src/ocsp.c
Original file line number Diff line number Diff line change
Expand Up @@ -952,18 +952,18 @@ OcspResponse* wolfSSL_d2i_OCSP_RESPONSE_bio(WOLFSSL_BIO* bio,
long fcur;
long flen;

if (bio->ptr == NULL)
if (bio->ptr.fh == NULL)
return NULL;

fcur = XFTELL((XFILE)bio->ptr);
fcur = XFTELL(bio->ptr.fh);
if (fcur < 0)
return NULL;
if(XFSEEK((XFILE)bio->ptr, 0, SEEK_END) != 0)
if(XFSEEK(bio->ptr.fh, 0, SEEK_END) != 0)
return NULL;
flen = XFTELL((XFILE)bio->ptr);
flen = XFTELL(bio->ptr.fh);
if (flen < 0)
return NULL;
if (XFSEEK((XFILE)bio->ptr, fcur, SEEK_SET) != 0)
if (XFSEEK(bio->ptr.fh, fcur, SEEK_SET) != 0)
return NULL;

/* check calculated length */
Expand Down
6 changes: 4 additions & 2 deletions src/wolfio.c
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,8 @@ int BioReceive(WOLFSSL* ssl, char* buf, int sz, void* ctx)
return WOLFSSL_CBIO_ERR_CONN_CLOSE;
}
#ifdef USE_WOLFSSL_IO
recvd = TranslateIoReturnCode(recvd, ssl->biord->num, SOCKET_RECEIVING);
recvd = TranslateIoReturnCode(recvd, ssl->biord->num.fd,
SOCKET_RECEIVING);
#endif
return recvd;
}
Expand Down Expand Up @@ -346,7 +347,8 @@ int BioSend(WOLFSSL* ssl, char *buf, int sz, void *ctx)
if (sent <= 0) {
if (ssl->biowr->type == WOLFSSL_BIO_SOCKET) {
#ifdef USE_WOLFSSL_IO
sent = TranslateIoReturnCode(sent, ssl->biowr->num, SOCKET_SENDING);
sent = TranslateIoReturnCode(sent, ssl->biowr->num.fd,
SOCKET_SENDING);
#endif
return sent;
}
Expand Down
8 changes: 4 additions & 4 deletions tests/api.c
Original file line number Diff line number Diff line change
Expand Up @@ -601,14 +601,14 @@ static int testDevId = INVALID_DEVID;
static int wolfssl_bio_s_fixed_mem_write(WOLFSSL_BIO* bio, const char* data,
int len)
{
if ((bio == NULL) || (bio->ptr == NULL) || (data == NULL)) {
if ((bio == NULL) || (bio->ptr.mem_buf_data == NULL) || (data == NULL)) {
len = 0;
}
else {
if (bio->wrSz - bio->wrIdx < len) {
len = bio->wrSz - bio->wrIdx;
}
XMEMCPY((char*)bio->ptr + bio->wrIdx, data, len);
XMEMCPY(bio->ptr.mem_buf_data + bio->wrIdx, data, len);
bio->wrIdx += len;
}

Expand All @@ -617,14 +617,14 @@ static int wolfssl_bio_s_fixed_mem_write(WOLFSSL_BIO* bio, const char* data,

static int wolfssl_bio_s_fixed_mem_read(WOLFSSL_BIO* bio, char* data, int len)
{
if ((bio == NULL) || (bio->ptr == NULL) || (data == NULL)) {
if ((bio == NULL) || (bio->ptr.mem_buf_data == NULL) || (data == NULL)) {
len = 0;
}
else {
if (bio->wrSz - bio->rdIdx < len) {
len = bio->wrSz - bio->rdIdx;
}
XMEMCPY(data, (char*)bio->ptr + bio->rdIdx, len);
XMEMCPY(data, bio->ptr.mem_buf_data + bio->rdIdx, len);
bio->rdIdx += len;
}

Expand Down
16 changes: 14 additions & 2 deletions wolfssl/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -2753,7 +2753,16 @@ struct WOLFSSL_BIO {
WOLFSSL_BIO* next; /* next in chain */
WOLFSSL_BIO* pair; /* BIO paired with */
void* heap; /* user heap hint */
void* ptr; /* WOLFSSL, file descriptor, MD, or mem buf */
union {
byte* mem_buf_data;
#ifndef WOLFCRYPT_ONLY
WOLFSSL* ssl;
WOLFSSL_EVP_MD_CTX* md_ctx;
#endif
#ifndef NO_FILESYSTEM
XFILE fh;
#endif
} ptr;
void* usrCtx; /* user set pointer */
char* ip; /* IP address for wolfIO_TcpConnect */
word16 port; /* Port for wolfIO_TcpConnect */
Expand All @@ -2764,7 +2773,10 @@ struct WOLFSSL_BIO {
int wrIdx; /* current index for write buffer */
int rdIdx; /* current read index */
int readRq; /* read request */
int num; /* socket num or length */
union {
SOCKET_T fd;
size_t length;
} num;
int eof; /* eof flag */
int flags;
byte type; /* method type */
Expand Down
19 changes: 19 additions & 0 deletions wolfssl/wolfcrypt/wc_port.h
Original file line number Diff line number Diff line change
Expand Up @@ -859,6 +859,25 @@ WOLFSSL_ABI WOLFSSL_API int wolfCrypt_Cleanup(void);
#define XSPRINTF sprintf
#endif

#ifdef USE_WINDOWS_API
#ifndef SOCKET_T
#ifdef __MINGW64__
typedef size_t SOCKET_T;
#else
typedef unsigned int SOCKET_T;
#endif
#endif
#ifndef SOCKET_INVALID
#define SOCKET_INVALID INVALID_SOCKET
#endif
#else
#ifndef SOCKET_T
typedef int SOCKET_T;
#endif
#ifndef SOCKET_INVALID
#define SOCKET_INVALID (-1)
#endif
#endif

/* MIN/MAX MACRO SECTION */
/* Windows API defines its own min() macro. */
Expand Down
16 changes: 0 additions & 16 deletions wolfssl/wolfio.h
Original file line number Diff line number Diff line change
Expand Up @@ -404,22 +404,6 @@
#endif
#endif

#ifdef USE_WINDOWS_API
#if defined(__MINGW64__)
typedef size_t SOCKET_T;
#else
typedef unsigned int SOCKET_T;
#endif
#ifndef SOCKET_INVALID
#define SOCKET_INVALID INVALID_SOCKET
#endif
#else
typedef int SOCKET_T;
#ifndef SOCKET_INVALID
#define SOCKET_INVALID (-1)
#endif
#endif

#ifndef WOLFSSL_NO_SOCK
#ifndef XSOCKLENT
#ifdef USE_WINDOWS_API
Expand Down