Skip to content

Commit d2747b0

Browse files
author
Ivan Zhakov
committed
bcrypt: Destroy BCrypt hash context using BCryptDestroyHash(). We allocate
memory for BCrypt hash context from APR pool, so existing code didn't have memory leak. But in theory BCrypt may have other resources besides memory used by context itself. * subversion/libsvn_subr/checksum_bcrypt.c (bcrypt_ctx_cleanup): New. APR pool cleanup handler to cleanup bcrypt_ctx_t. (bcrypt_ctx_init): Register APR pool cleanup handler. (bcrypt_ctx_reset, bcrypt_checksum): Destroy BCrypt hash context using BCryptDestroyHash(). git-svn-id: https://svn.apache.org/repos/asf/subversion/trunk@1936009 13f79535-47bb-0310-9956-ffa450edef68
1 parent d24a2ab commit d2747b0

1 file changed

Lines changed: 51 additions & 8 deletions

File tree

subversion/libsvn_subr/checksum_bcrypt.c

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,25 @@ typedef struct bcrypt_ctx_t
9393
apr_pool_t *pool;
9494
} bcrypt_ctx_t;
9595

96+
/* A cleanup handler. */
97+
static apr_status_t
98+
bcrypt_ctx_cleanup(void *data)
99+
{
100+
bcrypt_ctx_t *ctx = data;
101+
102+
if (ctx->handle)
103+
{
104+
NTSTATUS status = BCryptDestroyHash(ctx->handle);
105+
106+
if (!BCRYPT_SUCCESS(status))
107+
SVN_ERR_MALFUNCTION_NO_RETURN();
108+
109+
ctx->handle = NULL;
110+
}
111+
112+
return APR_SUCCESS;
113+
}
114+
96115
static svn_error_t *
97116
bcrypt_ctx_init(algorithm_state_t *algorithm,
98117
bcrypt_ctx_t *ctx)
@@ -102,8 +121,12 @@ bcrypt_ctx_init(algorithm_state_t *algorithm,
102121
SVN_ERR(svn_atomic__init_once(&algorithm->initialized, algorithm_init,
103122
algorithm, NULL));
104123

105-
if (! ctx->object_buf)
106-
ctx->object_buf = apr_pcalloc(ctx->pool, algorithm->object_length);
124+
if (!ctx->object_buf)
125+
{
126+
ctx->object_buf = apr_pcalloc(ctx->pool, algorithm->object_length);
127+
apr_pool_cleanup_register(ctx->pool, ctx, bcrypt_ctx_cleanup,
128+
apr_pool_cleanup_null);
129+
}
107130

108131
SVN_ERR(handle_error(BCryptCreateHash(algorithm->alg_handle,
109132
&handle,
@@ -162,8 +185,14 @@ bcrypt_ctx_final(algorithm_state_t *algorithm,
162185
static svn_error_t *
163186
bcrypt_ctx_reset(algorithm_state_t *algorithm, bcrypt_ctx_t *ctx)
164187
{
188+
if (ctx->handle)
189+
{
190+
SVN_ERR(handle_error(BCryptDestroyHash(ctx->handle)));
191+
ctx->handle = NULL;
192+
}
193+
165194
memset(ctx->object_buf, 0, algorithm->object_length);
166-
ctx->handle = NULL;
195+
167196
return SVN_NO_ERROR;
168197
}
169198

@@ -175,6 +204,7 @@ bcrypt_checksum(algorithm_state_t *algorithm,
175204
{
176205
BCRYPT_HANDLE handle;
177206
void *object_buf;
207+
svn_error_t *err;
178208

179209
SVN_ERR(svn_atomic__init_once(&algorithm->initialized, algorithm_init,
180210
algorithm, NULL));
@@ -198,16 +228,29 @@ bcrypt_checksum(algorithm_state_t *algorithm,
198228
else
199229
block = UINT_MAX;
200230

201-
SVN_ERR(handle_error(BCryptHashData(handle, (PUCHAR)data, block,
202-
/* dwFlags */ 0)));
231+
err = handle_error(BCryptHashData(handle, (PUCHAR)data, block,
232+
/* dwFlags */ 0));
233+
if (err)
234+
{
235+
return svn_error_compose_create(
236+
err, handle_error(BCryptDestroyHash(handle)));
237+
}
203238

204239
len -= block;
205240
data += block;
206241
}
207242

208-
SVN_ERR(handle_error(BCryptFinishHash(handle, digest,
209-
algorithm->hash_length,
210-
/* dwFlags */ 0)));
243+
err = handle_error(BCryptFinishHash(handle, digest,
244+
algorithm->hash_length,
245+
/* dwFlags */ 0));
246+
247+
if (err)
248+
{
249+
return svn_error_compose_create(err,
250+
handle_error(BCryptDestroyHash(handle)));
251+
}
252+
253+
SVN_ERR(handle_error(BCryptDestroyHash(handle)));
211254

212255
return SVN_NO_ERROR;
213256
}

0 commit comments

Comments
 (0)