Skip to content

Commit 54c8366

Browse files
AttilaFueloepbehlendorf
authored andcommitted
ICP: Fix null pointer dereference and use after free
In gcm_mode_decrypt_contiguous_blocks(), if vmem_alloc() fails, bcopy is called with a NULL pointer destination and a length > 0. This results in undefined behavior. Further ctx->gcm_pt_buf is freed but not set to NULL, leading to a potential write after free and a double free due to missing return value handling in crypto_update_uio(). The code as is may write to ctx->gcm_pt_buf in gcm_decrypt_final() and may free ctx->gcm_pt_buf again in aes_decrypt_atomic(). The fix is to slightly rework error handling and check the return value in crypto_update_uio(). Reviewed-by: Brian Behlendorf <[email protected]> Reviewed-by: Tom Caputi <[email protected]> Reviewed-by: Kjeld Schouten <[email protected]> Signed-off-by: Attila Fülöp <[email protected]> Closes #9659
1 parent 7af7286 commit 54c8366

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

module/icp/algs/modes/gcm.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -300,11 +300,13 @@ gcm_mode_decrypt_contiguous_blocks(gcm_ctx_t *ctx, char *data, size_t length,
300300
if (length > 0) {
301301
new_len = ctx->gcm_pt_buf_len + length;
302302
new = vmem_alloc(new_len, ctx->gcm_kmflag);
303+
if (new == NULL) {
304+
vmem_free(ctx->gcm_pt_buf, ctx->gcm_pt_buf_len);
305+
ctx->gcm_pt_buf = NULL;
306+
return (CRYPTO_HOST_MEMORY);
307+
}
303308
bcopy(ctx->gcm_pt_buf, new, ctx->gcm_pt_buf_len);
304309
vmem_free(ctx->gcm_pt_buf, ctx->gcm_pt_buf_len);
305-
if (new == NULL)
306-
return (CRYPTO_HOST_MEMORY);
307-
308310
ctx->gcm_pt_buf = new;
309311
ctx->gcm_pt_buf_len = new_len;
310312
bcopy(data, &ctx->gcm_pt_buf[ctx->gcm_processed_data_len],

module/icp/core/kcf_prov_lib.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,9 +207,12 @@ crypto_update_uio(void *ctx, crypto_data_t *input, crypto_data_t *output,
207207
cur_len = MIN(uiop->uio_iov[vec_idx].iov_len -
208208
offset, length);
209209

210-
(cipher)(ctx, uiop->uio_iov[vec_idx].iov_base + offset,
210+
int rv = (cipher)(ctx, uiop->uio_iov[vec_idx].iov_base + offset,
211211
cur_len, (input == output) ? NULL : output);
212212

213+
if (rv != CRYPTO_SUCCESS) {
214+
return (rv);
215+
}
213216
length -= cur_len;
214217
vec_idx++;
215218
offset = 0;

0 commit comments

Comments
 (0)