Skip to content

Commit

Permalink
Fix a memory leak
Browse files Browse the repository at this point in the history
  • Loading branch information
madeye committed Feb 17, 2017
1 parent 97ac602 commit d434cd2
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 24 deletions.
8 changes: 4 additions & 4 deletions src/aead.c
Original file line number Diff line number Diff line change
Expand Up @@ -365,16 +365,16 @@ aead_ctx_init(cipher_t *cipher, cipher_ctx_t *cipher_ctx, int enc)
void
aead_ctx_release(cipher_ctx_t *cipher_ctx)
{
if (cipher_ctx->cipher->method >= CHACHA20POLY1305IETF) {
return;
}

if (cipher_ctx->chunk != NULL) {
bfree(cipher_ctx->chunk);
ss_free(cipher_ctx->chunk);
cipher_ctx->chunk = NULL;
}

if (cipher_ctx->cipher->method >= CHACHA20POLY1305IETF) {
return;
}

mbedtls_cipher_free(cipher_ctx->evp);
ss_free(cipher_ctx->evp);
}
Expand Down
4 changes: 2 additions & 2 deletions src/local.c
Original file line number Diff line number Diff line change
Expand Up @@ -1014,8 +1014,8 @@ new_server(int fd)
server->recv_ctx->server = server;
server->send_ctx->server = server;

server->e_ctx = ss_malloc(sizeof(cipher_ctx_t));
server->d_ctx = ss_malloc(sizeof(cipher_ctx_t));
server->e_ctx = ss_align(sizeof(cipher_ctx_t));
server->d_ctx = ss_align(sizeof(cipher_ctx_t));
crypto->ctx_init(crypto->cipher, server->e_ctx, 1);
crypto->ctx_init(crypto->cipher, server->d_ctx, 0);

Expand Down
4 changes: 2 additions & 2 deletions src/redir.c
Original file line number Diff line number Diff line change
Expand Up @@ -618,8 +618,8 @@ new_server(int fd)
server->hostname = NULL;
server->hostname_len = 0;

server->e_ctx = ss_malloc(sizeof(cipher_ctx_t));
server->d_ctx = ss_malloc(sizeof(cipher_ctx_t));
server->e_ctx = ss_align(sizeof(cipher_ctx_t));
server->d_ctx = ss_align(sizeof(cipher_ctx_t));
crypto->ctx_init(crypto->cipher, server->e_ctx, 1);
crypto->ctx_init(crypto->cipher, server->d_ctx, 0);

Expand Down
4 changes: 2 additions & 2 deletions src/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -1201,8 +1201,8 @@ new_server(int fd, listen_ctx_t *listener)
server->listen_ctx = listener;
server->remote = NULL;

server->e_ctx = ss_malloc(sizeof(cipher_ctx_t));
server->d_ctx = ss_malloc(sizeof(cipher_ctx_t));
server->e_ctx = ss_align(sizeof(cipher_ctx_t));
server->d_ctx = ss_align(sizeof(cipher_ctx_t));
crypto->ctx_init(crypto->cipher, server->e_ctx, 1);
crypto->ctx_init(crypto->cipher, server->d_ctx, 0);

Expand Down
18 changes: 6 additions & 12 deletions src/stream.c
Original file line number Diff line number Diff line change
Expand Up @@ -216,13 +216,18 @@ stream_cipher_ctx_init(cipher_ctx_t *ctx, int method, int enc)
}

void
stream_cipher_ctx_release(cipher_ctx_t *cipher_ctx)
stream_ctx_release(cipher_ctx_t *cipher_ctx)
{
if (cipher_ctx->chunk != NULL) {
bfree(cipher_ctx->chunk);
ss_free(cipher_ctx->chunk);
cipher_ctx->chunk = NULL;
}

if (cipher_ctx->cipher->method >= SALSA20) {
return;
}

mbedtls_cipher_free(cipher_ctx->evp);
ss_free(cipher_ctx->evp);
}
Expand Down Expand Up @@ -574,17 +579,6 @@ stream_ctx_init(cipher_t *cipher, cipher_ctx_t *cipher_ctx, int enc)
}
}

void
stream_ctx_release(cipher_ctx_t *cipher_ctx)
{
if (cipher_ctx->cipher->method >= SALSA20) {
return;
}

mbedtls_cipher_free(cipher_ctx->evp);
ss_free(cipher_ctx->evp);
}

cipher_t *
stream_key_init(int method, const char *pass, const char *key)
{
Expand Down
4 changes: 2 additions & 2 deletions src/tunnel.c
Original file line number Diff line number Diff line change
Expand Up @@ -566,8 +566,8 @@ new_server(int fd)
server->send_ctx->server = server;
server->send_ctx->connected = 0;

server->e_ctx = ss_malloc(sizeof(cipher_ctx_t));
server->d_ctx = ss_malloc(sizeof(cipher_ctx_t));
server->e_ctx = ss_align(sizeof(cipher_ctx_t));
server->d_ctx = ss_align(sizeof(cipher_ctx_t));
crypto->ctx_init(crypto->cipher, server->e_ctx, 1);
crypto->ctx_init(crypto->cipher, server->d_ctx, 0);

Expand Down
13 changes: 13 additions & 0 deletions src/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,19 @@ ss_malloc(size_t size)
return tmp;
}

void *
ss_align(size_t size)
{
int err;
void *tmp;
err = posix_memalign(&tmp, sizeof(void *), size);
if (err) {
return ss_malloc(size);
} else {
return tmp;
}
}

void *
ss_realloc(void *ptr, size_t new_size)
{
Expand Down
1 change: 1 addition & 0 deletions src/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ int set_nofile(int nofile);
#endif

void *ss_malloc(size_t size);
void *ss_align(size_t size);
void *ss_realloc(void *ptr, size_t new_size);

#define ss_free(ptr) \
Expand Down

0 comments on commit d434cd2

Please sign in to comment.