Skip to content

Commit 0fa6bea

Browse files
Wenwen Wanggregkh
authored andcommitted
crypto: cavium/nitrox - fix a DMA pool free failure
commit 7172122 upstream. In crypto_alloc_context(), a DMA pool is allocated through dma_pool_alloc() to hold the crypto context. The meta data of the DMA pool, including the pool used for the allocation 'ndev->ctx_pool' and the base address of the DMA pool used by the device 'dma', are then stored to the beginning of the pool. These meta data are eventually used in crypto_free_context() to free the DMA pool through dma_pool_free(). However, given that the DMA pool can also be accessed by the device, a malicious device can modify these meta data, especially when the device is controlled to deploy an attack. This can cause an unexpected DMA pool free failure. To avoid the above issue, this patch introduces a new structure crypto_ctx_hdr and a new field chdr in the structure nitrox_crypto_ctx hold the meta data information of the DMA pool after the allocation. Note that the original structure ctx_hdr is not changed to ensure the compatibility. Cc: <stable@vger.kernel.org> Signed-off-by: Wenwen Wang <wang6495@umn.edu> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent d095e1b commit 0fa6bea

3 files changed

Lines changed: 31 additions & 10 deletions

File tree

‎drivers/crypto/cavium/nitrox/nitrox_algs.c‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,20 +73,22 @@ static int flexi_aes_keylen(int keylen)
7373
static int nitrox_skcipher_init(struct crypto_skcipher *tfm)
7474
{
7575
struct nitrox_crypto_ctx *nctx = crypto_skcipher_ctx(tfm);
76-
void *fctx;
76+
struct crypto_ctx_hdr *chdr;
7777

7878
/* get the first device */
7979
nctx->ndev = nitrox_get_first_device();
8080
if (!nctx->ndev)
8181
return -ENODEV;
8282

8383
/* allocate nitrox crypto context */
84-
fctx = crypto_alloc_context(nctx->ndev);
85-
if (!fctx) {
84+
chdr = crypto_alloc_context(nctx->ndev);
85+
if (!chdr) {
8686
nitrox_put_device(nctx->ndev);
8787
return -ENOMEM;
8888
}
89-
nctx->u.ctx_handle = (uintptr_t)fctx;
89+
nctx->chdr = chdr;
90+
nctx->u.ctx_handle = (uintptr_t)((u8 *)chdr->vaddr +
91+
sizeof(struct ctx_hdr));
9092
crypto_skcipher_set_reqsize(tfm, crypto_skcipher_reqsize(tfm) +
9193
sizeof(struct nitrox_kcrypt_request));
9294
return 0;
@@ -102,7 +104,7 @@ static void nitrox_skcipher_exit(struct crypto_skcipher *tfm)
102104

103105
memset(&fctx->crypto, 0, sizeof(struct crypto_keys));
104106
memset(&fctx->auth, 0, sizeof(struct auth_keys));
105-
crypto_free_context((void *)fctx);
107+
crypto_free_context((void *)nctx->chdr);
106108
}
107109
nitrox_put_device(nctx->ndev);
108110

‎drivers/crypto/cavium/nitrox/nitrox_lib.c‎

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -146,20 +146,31 @@ static void destroy_crypto_dma_pool(struct nitrox_device *ndev)
146146
void *crypto_alloc_context(struct nitrox_device *ndev)
147147
{
148148
struct ctx_hdr *ctx;
149+
struct crypto_ctx_hdr *chdr;
149150
void *vaddr;
150151
dma_addr_t dma;
151152

153+
chdr = kmalloc(sizeof(*chdr), GFP_KERNEL);
154+
if (!chdr)
155+
return NULL;
156+
152157
vaddr = dma_pool_alloc(ndev->ctx_pool, (GFP_KERNEL | __GFP_ZERO), &dma);
153-
if (!vaddr)
158+
if (!vaddr) {
159+
kfree(chdr);
154160
return NULL;
161+
}
155162

156163
/* fill meta data */
157164
ctx = vaddr;
158165
ctx->pool = ndev->ctx_pool;
159166
ctx->dma = dma;
160167
ctx->ctx_dma = dma + sizeof(struct ctx_hdr);
161168

162-
return ((u8 *)vaddr + sizeof(struct ctx_hdr));
169+
chdr->pool = ndev->ctx_pool;
170+
chdr->dma = dma;
171+
chdr->vaddr = vaddr;
172+
173+
return chdr;
163174
}
164175

165176
/**
@@ -168,13 +179,14 @@ void *crypto_alloc_context(struct nitrox_device *ndev)
168179
*/
169180
void crypto_free_context(void *ctx)
170181
{
171-
struct ctx_hdr *ctxp;
182+
struct crypto_ctx_hdr *ctxp;
172183

173184
if (!ctx)
174185
return;
175186

176-
ctxp = (struct ctx_hdr *)((u8 *)ctx - sizeof(struct ctx_hdr));
177-
dma_pool_free(ctxp->pool, ctxp, ctxp->dma);
187+
ctxp = ctx;
188+
dma_pool_free(ctxp->pool, ctxp->vaddr, ctxp->dma);
189+
kfree(ctxp);
178190
}
179191

180192
/**

‎drivers/crypto/cavium/nitrox/nitrox_req.h‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,12 +181,19 @@ struct flexi_crypto_context {
181181
struct auth_keys auth;
182182
};
183183

184+
struct crypto_ctx_hdr {
185+
struct dma_pool *pool;
186+
dma_addr_t dma;
187+
void *vaddr;
188+
};
189+
184190
struct nitrox_crypto_ctx {
185191
struct nitrox_device *ndev;
186192
union {
187193
u64 ctx_handle;
188194
struct flexi_crypto_context *fctx;
189195
} u;
196+
struct crypto_ctx_hdr *chdr;
190197
};
191198

192199
struct nitrox_kcrypt_request {

0 commit comments

Comments
 (0)