Skip to content

Commit 15e05d4

Browse files
committed
bcrypt: Allocate buffer for checksum context object in a pool.
* subversion/libsvn_subr/checksum_bcrypt.c (bcrypt_ctx_t): Store a pointer to the object buffer to reuse later and the pool where it can be allocated in delayed initialization. (bcrypt_ctx_cleanup): Remove, because we no longer need to cleanup the buffer manually -- it is allocated in a pool. (bcrypt_ctx_init): Create the object buffer if it's NULL and supply it to BCryptCreateHash(). (bcrypt_ctx_reset): memset object_buf with all-zeros instead of cleaning it up via BCryptDestroyHash(). It's not mandatory to reset the memory but a it's just a security consideration. (svn_checksum__md5_ctx_create, svn_checksum__sha1_ctx_create): Don't setup pool cleanup handler. Initialize bcrypt_ctx.pool field. git-svn-id: https://svn.apache.org/repos/asf/subversion/trunk@1931094 13f79535-47bb-0310-9956-ffa450edef68
1 parent d3fab50 commit 15e05d4

File tree

1 file changed

+9
-25
lines changed

1 file changed

+9
-25
lines changed

subversion/libsvn_subr/checksum_bcrypt.c

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -89,25 +89,10 @@ algorithm_init(void *baton, apr_pool_t *null_pool)
8989
typedef struct bcrypt_ctx_t
9090
{
9191
BCRYPT_HASH_HANDLE handle;
92+
void *object_buf;
93+
apr_pool_t *pool;
9294
} bcrypt_ctx_t;
9395

94-
/* A cleanup handler. */
95-
static apr_status_t
96-
bcrypt_ctx_cleanup(void *data)
97-
{
98-
bcrypt_ctx_t *ctx = (bcrypt_ctx_t *)data;
99-
100-
if (ctx->handle)
101-
{
102-
NTSTATUS status = BCryptDestroyHash(ctx->handle);
103-
104-
if (! BCRYPT_SUCCESS(status))
105-
SVN_ERR_MALFUNCTION_NO_RETURN();
106-
}
107-
108-
return APR_SUCCESS;
109-
}
110-
11196
static svn_error_t *
11297
bcrypt_ctx_init(algorithm_state_t *algorithm,
11398
bcrypt_ctx_t *ctx)
@@ -117,9 +102,12 @@ bcrypt_ctx_init(algorithm_state_t *algorithm,
117102
SVN_ERR(svn_atomic__init_once(&algorithm->initialized, algorithm_init,
118103
algorithm, NULL));
119104

105+
if (! ctx->object_buf)
106+
ctx->object_buf = apr_pcalloc(ctx->pool, algorithm->object_length);
107+
120108
SVN_ERR(handle_error(BCryptCreateHash(algorithm->alg_handle,
121109
&handle,
122-
NULL, 0,
110+
ctx->object_buf, algorithm->object_length,
123111
/* pbSecret */ NULL,
124112
/* cbSecret */ 0,
125113
/* dwFlags */ 0)));
@@ -166,7 +154,7 @@ bcrypt_ctx_final(algorithm_state_t *algorithm,
166154
static svn_error_t *
167155
bcrypt_ctx_reset(algorithm_state_t *algorithm, bcrypt_ctx_t *ctx)
168156
{
169-
bcrypt_ctx_cleanup(ctx);
157+
memset(ctx->object_buf, 0, algorithm->object_length);
170158
ctx->handle = NULL;
171159
return SVN_NO_ERROR;
172160
}
@@ -221,9 +209,7 @@ svn_checksum__md5_ctx_t *
221209
svn_checksum__md5_ctx_create(apr_pool_t *pool)
222210
{
223211
svn_checksum__md5_ctx_t *ctx = apr_pcalloc(pool, sizeof(*ctx));
224-
225-
apr_pool_cleanup_register(pool, &ctx->bcrypt_ctx, bcrypt_ctx_cleanup, NULL);
226-
212+
ctx->bcrypt_ctx.pool = pool;
227213
return ctx;
228214
}
229215

@@ -269,9 +255,7 @@ svn_checksum__sha1_ctx_t *
269255
svn_checksum__sha1_ctx_create(apr_pool_t *pool)
270256
{
271257
svn_checksum__sha1_ctx_t *ctx = apr_pcalloc(pool, sizeof(*ctx));
272-
273-
apr_pool_cleanup_register(pool, &ctx->bcrypt_ctx, bcrypt_ctx_cleanup, NULL);
274-
258+
ctx->bcrypt_ctx.pool = pool;
275259
return ctx;
276260
}
277261

0 commit comments

Comments
 (0)