* [PATCH 4/5] crypto: ti - Trim scatterlists to correct length in AES
2026-08-27 10:57 [PATCH 0/5] Fix several issues in DTHEv2 driver T Pratham
` (2 preceding siblings ...)
2026-08-27 10:57 ` [PATCH 3/5] crypto: ti - Fix potential memory corruption on highmem pages T Pratham
@ 2026-08-27 10:57 ` T Pratham
2026-08-27 10:57 ` [PATCH 5/5] crypto: ti - Fix use-after-free of dev_data on DTHEv2 driver removal T Pratham
4 siblings, 0 replies; 6+ messages in thread
From: T Pratham @ 2026-08-27 10:57 UTC (permalink / raw)
To: T Pratham, Herbert Xu, David S. Miller
Cc: Sebin Francis, Manorit Chawdhry, Vishal Mahaveer,
Praneeth Bajjuri, Kamlesh Gurudasani, linux-crypto, linux-kernel
AES functions were using src and dst scatterlists directly provided by
the request. This is problematic as it is not guaranteed that the input
scatterlist is exactly the length reqired. This problem was seen in
IPSec use case when the kernel provides the scatterlist which contains
space for plaintext/ciphertext and TAG.
The problem comes when the scatterlist is mapped and sent via DMA. The
K3 UDMA sends/waits for the amount of data equal to the length of
scatterlist mapped. So when the last mapped nent contains some extra
length, the DMA keeps waiting for the extra data and eventually times
out and crashes.
Mitigate this by copying the nents to a local scatterlist, copying only
exactly cryptlen of data. Note that this does not copy the whole data,
but rather only the scatterlist mapping. So it is not as penalising on
performance.
Fixes: 52f641bc63a4 ("crypto: ti - Add driver for DTHE V2 AES Engine (ECB, CBC)")
Signed-off-by: T Pratham <t-pratham@ti.com>
Reviewed-by: Kamlesh Gurudasani <kamlesh@ti.com>
---
drivers/crypto/ti/dthev2-aes.c | 146 ++++++++++++++++-----------------
1 file changed, 69 insertions(+), 77 deletions(-)
diff --git a/drivers/crypto/ti/dthev2-aes.c b/drivers/crypto/ti/dthev2-aes.c
index c025b08c49930..8899b53f032b1 100644
--- a/drivers/crypto/ti/dthev2-aes.c
+++ b/drivers/crypto/ti/dthev2-aes.c
@@ -108,20 +108,6 @@ enum aes_ctrl_mode_masks {
#define POLL_TIMEOUT_INTERVAL HZ
static int dthe_cipher_init_tfm(struct crypto_skcipher *tfm)
-{
- struct dthe_tfm_ctx *ctx = crypto_skcipher_ctx(tfm);
- struct dthe_data *dev_data = dthe_get_dev(ctx);
-
- if (!dev_data)
- return -ENODEV;
-
- ctx->dev_data = dev_data;
- ctx->keylen = 0;
-
- return 0;
-}
-
-static int dthe_cipher_init_tfm_fallback(struct crypto_skcipher *tfm)
{
struct dthe_tfm_ctx *ctx = crypto_skcipher_ctx(tfm);
struct dthe_data *dev_data = dthe_get_dev(ctx);
@@ -155,19 +141,24 @@ static int dthe_aes_setkey(struct crypto_skcipher *tfm, const u8 *key, unsigned
{
struct dthe_tfm_ctx *ctx = crypto_skcipher_ctx(tfm);
- if (keylen != AES_KEYSIZE_128 && keylen != AES_KEYSIZE_192 && keylen != AES_KEYSIZE_256)
- return -EINVAL;
-
ctx->keylen = keylen;
memcpy(ctx->key, key, keylen);
- return 0;
+ crypto_sync_skcipher_clear_flags(ctx->skcipher_fb, CRYPTO_TFM_REQ_MASK);
+ crypto_sync_skcipher_set_flags(ctx->skcipher_fb,
+ crypto_skcipher_get_flags(tfm) &
+ CRYPTO_TFM_REQ_MASK);
+
+ return crypto_sync_skcipher_setkey(ctx->skcipher_fb, key, keylen);
}
static int dthe_aes_ecb_setkey(struct crypto_skcipher *tfm, const u8 *key, unsigned int keylen)
{
struct dthe_tfm_ctx *ctx = crypto_skcipher_ctx(tfm);
+ if (keylen != AES_KEYSIZE_128 && keylen != AES_KEYSIZE_192 && keylen != AES_KEYSIZE_256)
+ return -EINVAL;
+
ctx->aes_mode = DTHE_AES_ECB;
return dthe_aes_setkey(tfm, key, keylen);
@@ -177,6 +168,9 @@ static int dthe_aes_cbc_setkey(struct crypto_skcipher *tfm, const u8 *key, unsig
{
struct dthe_tfm_ctx *ctx = crypto_skcipher_ctx(tfm);
+ if (keylen != AES_KEYSIZE_128 && keylen != AES_KEYSIZE_192 && keylen != AES_KEYSIZE_256)
+ return -EINVAL;
+
ctx->aes_mode = DTHE_AES_CBC;
return dthe_aes_setkey(tfm, key, keylen);
@@ -185,24 +179,19 @@ static int dthe_aes_cbc_setkey(struct crypto_skcipher *tfm, const u8 *key, unsig
static int dthe_aes_ctr_setkey(struct crypto_skcipher *tfm, const u8 *key, unsigned int keylen)
{
struct dthe_tfm_ctx *ctx = crypto_skcipher_ctx(tfm);
- int ret = dthe_aes_setkey(tfm, key, keylen);
- if (ret)
- return ret;
+ if (keylen != AES_KEYSIZE_128 && keylen != AES_KEYSIZE_192 && keylen != AES_KEYSIZE_256)
+ return -EINVAL;
ctx->aes_mode = DTHE_AES_CTR;
- crypto_sync_skcipher_clear_flags(ctx->skcipher_fb, CRYPTO_TFM_REQ_MASK);
- crypto_sync_skcipher_set_flags(ctx->skcipher_fb,
- crypto_skcipher_get_flags(tfm) &
- CRYPTO_TFM_REQ_MASK);
-
- return crypto_sync_skcipher_setkey(ctx->skcipher_fb, key, keylen);
+ return dthe_aes_setkey(tfm, key, keylen);
}
static int dthe_aes_xts_setkey(struct crypto_skcipher *tfm, const u8 *key, unsigned int keylen)
{
struct dthe_tfm_ctx *ctx = crypto_skcipher_ctx(tfm);
+ int ret;
if (keylen != 2 * AES_KEYSIZE_128 &&
keylen != 2 * AES_KEYSIZE_192 &&
@@ -210,15 +199,12 @@ static int dthe_aes_xts_setkey(struct crypto_skcipher *tfm, const u8 *key, unsig
return -EINVAL;
ctx->aes_mode = DTHE_AES_XTS;
- ctx->keylen = keylen / 2;
- memcpy(ctx->key, key, keylen);
-
- crypto_sync_skcipher_clear_flags(ctx->skcipher_fb, CRYPTO_TFM_REQ_MASK);
- crypto_sync_skcipher_set_flags(ctx->skcipher_fb,
- crypto_skcipher_get_flags(tfm) &
- CRYPTO_TFM_REQ_MASK);
+ ret = dthe_aes_setkey(tfm, key, keylen);
+ if (ret)
+ return ret;
- return crypto_sync_skcipher_setkey(ctx->skcipher_fb, key, keylen);
+ ctx->keylen = keylen / 2;
+ return 0;
}
static void dthe_aes_set_ctrl_key(struct dthe_tfm_ctx *ctx,
@@ -341,8 +327,10 @@ static int dthe_aes_run(struct crypto_engine *engine, void *areq)
struct dthe_aes_req_ctx *rctx = skcipher_request_ctx(req);
unsigned int len = req->cryptlen;
+ unsigned int pad_len = 0;
struct scatterlist *src = req->src;
struct scatterlist *dst = req->dst;
+ struct scatterlist *sg;
int src_nents = sg_nents_for_len(src, len);
int dst_nents = sg_nents_for_len(dst, len);
@@ -385,38 +373,38 @@ static int dthe_aes_run(struct crypto_engine *engine, void *areq)
* We need to handle the padding in the driver.
*/
if (ctx->aes_mode == DTHE_AES_CTR && req->cryptlen % AES_BLOCK_SIZE) {
- unsigned int pad_size = AES_BLOCK_SIZE - (req->cryptlen % AES_BLOCK_SIZE);
- u8 *pad_buf = rctx->padding;
- struct scatterlist *sg;
-
- len += pad_size;
+ pad_len = AES_BLOCK_SIZE - (req->cryptlen % AES_BLOCK_SIZE);
+ len += pad_len;
src_nents++;
dst_nents++;
+ }
- src = kmalloc_array(src_nents, sizeof(*src), GFP_ATOMIC);
- if (!src) {
- ret = -ENOMEM;
- goto aes_ctr_src_alloc_err;
- }
-
- sg_init_table(src, src_nents);
- sg = dthe_copy_sg(src, req->src, req->cryptlen);
- memzero_explicit(pad_buf, AES_BLOCK_SIZE);
- sg_set_buf(sg, pad_buf, pad_size);
+ src = kmalloc_array(src_nents, sizeof(*src), GFP_ATOMIC);
+ if (!src) {
+ ret = -ENOMEM;
+ goto aes_src_alloc_err;
+ }
- if (diff_dst) {
- dst = kmalloc_array(dst_nents, sizeof(*dst), GFP_ATOMIC);
- if (!dst) {
- ret = -ENOMEM;
- goto aes_ctr_dst_alloc_err;
- }
+ sg_init_table(src, src_nents);
+ sg = dthe_copy_sg(src, req->src, req->cryptlen);
+ if (pad_len > 0) {
+ memzero_explicit(rctx->padding, AES_BLOCK_SIZE);
+ sg_set_buf(sg, rctx->padding, pad_len);
+ }
- sg_init_table(dst, dst_nents);
- sg = dthe_copy_sg(dst, req->dst, req->cryptlen);
- sg_set_buf(sg, pad_buf, pad_size);
- } else {
- dst = src;
+ if (diff_dst) {
+ dst = kmalloc_array(dst_nents, sizeof(*dst), GFP_ATOMIC);
+ if (!dst) {
+ ret = -ENOMEM;
+ goto aes_dst_alloc_err;
}
+
+ sg_init_table(dst, dst_nents);
+ sg = dthe_copy_sg(dst, req->dst, req->cryptlen);
+ if (pad_len > 0)
+ sg_set_buf(sg, rctx->padding, pad_len);
+ } else {
+ dst = src;
}
tx_dev = dmaengine_get_dma_device(dev_data->dma_aes_tx);
@@ -503,19 +491,19 @@ static int dthe_aes_run(struct crypto_engine *engine, void *areq)
dma_unmap_sg(tx_dev, src, src_nents, src_dir);
aes_map_src_err:
- if (ctx->aes_mode == DTHE_AES_CTR && req->cryptlen % AES_BLOCK_SIZE) {
+ if (ctx->aes_mode == DTHE_AES_CTR && req->cryptlen % AES_BLOCK_SIZE)
memzero_explicit(rctx->padding, AES_BLOCK_SIZE);
- if (diff_dst)
- kfree(dst);
-aes_ctr_dst_alloc_err:
- kfree(src);
-aes_ctr_src_alloc_err:
- /*
- * Fallback to software if ENOMEM
- */
- if (ret == -ENOMEM)
- ret = dthe_aes_do_fallback(req);
- }
+ if (diff_dst)
+ kfree(dst);
+
+aes_dst_alloc_err:
+ kfree(src);
+aes_src_alloc_err:
+ /*
+ * Fallback to software if ENOMEM
+ */
+ if (ret == -ENOMEM)
+ ret = dthe_aes_do_fallback(req);
local_bh_disable();
crypto_finalize_skcipher_request(dev_data->engine, req, ret);
@@ -1215,6 +1203,7 @@ static int dthe_aead_decrypt(struct aead_request *req)
static struct skcipher_engine_alg cipher_algs[] = {
{
.base.init = dthe_cipher_init_tfm,
+ .base.exit = dthe_cipher_exit_tfm,
.base.setkey = dthe_aes_ecb_setkey,
.base.encrypt = dthe_aes_encrypt,
.base.decrypt = dthe_aes_decrypt,
@@ -1226,7 +1215,8 @@ static struct skcipher_engine_alg cipher_algs[] = {
.cra_priority = 299,
.cra_flags = CRYPTO_ALG_TYPE_SKCIPHER |
CRYPTO_ALG_ASYNC |
- CRYPTO_ALG_KERN_DRIVER_ONLY,
+ CRYPTO_ALG_KERN_DRIVER_ONLY |
+ CRYPTO_ALG_NEED_FALLBACK,
.cra_alignmask = AES_BLOCK_SIZE - 1,
.cra_blocksize = AES_BLOCK_SIZE,
.cra_ctxsize = sizeof(struct dthe_tfm_ctx),
@@ -1237,6 +1227,7 @@ static struct skcipher_engine_alg cipher_algs[] = {
}, /* ECB AES */
{
.base.init = dthe_cipher_init_tfm,
+ .base.exit = dthe_cipher_exit_tfm,
.base.setkey = dthe_aes_cbc_setkey,
.base.encrypt = dthe_aes_encrypt,
.base.decrypt = dthe_aes_decrypt,
@@ -1249,7 +1240,8 @@ static struct skcipher_engine_alg cipher_algs[] = {
.cra_priority = 299,
.cra_flags = CRYPTO_ALG_TYPE_SKCIPHER |
CRYPTO_ALG_ASYNC |
- CRYPTO_ALG_KERN_DRIVER_ONLY,
+ CRYPTO_ALG_KERN_DRIVER_ONLY |
+ CRYPTO_ALG_NEED_FALLBACK,
.cra_alignmask = AES_BLOCK_SIZE - 1,
.cra_blocksize = AES_BLOCK_SIZE,
.cra_ctxsize = sizeof(struct dthe_tfm_ctx),
@@ -1259,7 +1251,7 @@ static struct skcipher_engine_alg cipher_algs[] = {
.op.do_one_request = dthe_aes_run,
}, /* CBC AES */
{
- .base.init = dthe_cipher_init_tfm_fallback,
+ .base.init = dthe_cipher_init_tfm,
.base.exit = dthe_cipher_exit_tfm,
.base.setkey = dthe_aes_ctr_setkey,
.base.encrypt = dthe_aes_encrypt,
@@ -1284,7 +1276,7 @@ static struct skcipher_engine_alg cipher_algs[] = {
.op.do_one_request = dthe_aes_run,
}, /* CTR AES */
{
- .base.init = dthe_cipher_init_tfm_fallback,
+ .base.init = dthe_cipher_init_tfm,
.base.exit = dthe_cipher_exit_tfm,
.base.setkey = dthe_aes_xts_setkey,
.base.encrypt = dthe_aes_encrypt,
--
2.34.1
^ permalink raw reply related [flat|nested] 6+ messages in thread* [PATCH 5/5] crypto: ti - Fix use-after-free of dev_data on DTHEv2 driver removal
2026-08-27 10:57 [PATCH 0/5] Fix several issues in DTHEv2 driver T Pratham
` (3 preceding siblings ...)
2026-08-27 10:57 ` [PATCH 4/5] crypto: ti - Trim scatterlists to correct length in AES T Pratham
@ 2026-08-27 10:57 ` T Pratham
4 siblings, 0 replies; 6+ messages in thread
From: T Pratham @ 2026-08-27 10:57 UTC (permalink / raw)
To: T Pratham, Herbert Xu, David S. Miller
Cc: Sebin Francis, Manorit Chawdhry, Vishal Mahaveer,
Praneeth Bajjuri, linux-crypto, linux-kernel
Each *_init_tfm() caches a pointer to the per-instance struct dthe_data
in its transform context (ctx->dev_data), but never takes a reference on
it. If there are tfms in progress when dthe_remove() is called, the devm
allocated dev_data gets freed. Then ctx->dev_data will point to a memory
that has been freed.
Add a refcnt to struct dthe_data, incrementing it atomically in
*_init_tfm() and decrementing atomically in *_exit_tfm().
dthe_remove() now polls this count, with a bounded timeout, so tfms
allocated before removal have a chance to be freed first. If the timeout
expires, it warns and proceeds anyway rather than blocking removal
indefinitely.
Fixes: 52f641bc63a46 ("crypto: ti - Add driver for DTHE V2 AES Engine (ECB, CBC)")
Signed-off-by: T Pratham <t-pratham@ti.com>
---
drivers/crypto/ti/dthev2-aes.c | 4 ++++
drivers/crypto/ti/dthev2-common.c | 22 +++++++++++++++++++++-
drivers/crypto/ti/dthev2-common.h | 5 +++++
3 files changed, 30 insertions(+), 1 deletion(-)
diff --git a/drivers/crypto/ti/dthev2-aes.c b/drivers/crypto/ti/dthev2-aes.c
index 8899b53f032b1..3769fd7da8d0f 100644
--- a/drivers/crypto/ti/dthev2-aes.c
+++ b/drivers/crypto/ti/dthev2-aes.c
@@ -122,6 +122,7 @@ static int dthe_cipher_init_tfm(struct crypto_skcipher *tfm)
ctx->skcipher_fb = crypto_alloc_sync_skcipher(alg_name, 0,
CRYPTO_ALG_NEED_FALLBACK);
if (IS_ERR(ctx->skcipher_fb)) {
+ dthe_put_dev(ctx);
dev_err(dev_data->dev, "fallback driver %s couldn't be loaded\n",
alg_name);
return PTR_ERR(ctx->skcipher_fb);
@@ -135,6 +136,7 @@ static void dthe_cipher_exit_tfm(struct crypto_skcipher *tfm)
struct dthe_tfm_ctx *ctx = crypto_skcipher_ctx(tfm);
crypto_free_sync_skcipher(ctx->skcipher_fb);
+ dthe_put_dev(ctx);
}
static int dthe_aes_setkey(struct crypto_skcipher *tfm, const u8 *key, unsigned int keylen)
@@ -576,6 +578,7 @@ static int dthe_aead_init_tfm(struct crypto_aead *tfm)
ctx->aead_fb = crypto_alloc_sync_aead(alg_name, 0,
CRYPTO_ALG_NEED_FALLBACK);
if (IS_ERR(ctx->aead_fb)) {
+ dthe_put_dev(ctx);
dev_err(dev_data->dev, "fallback driver %s couldn't be loaded\n",
alg_name);
return PTR_ERR(ctx->aead_fb);
@@ -589,6 +592,7 @@ static void dthe_aead_exit_tfm(struct crypto_aead *tfm)
struct dthe_tfm_ctx *ctx = crypto_aead_ctx(tfm);
crypto_free_sync_aead(ctx->aead_fb);
+ dthe_put_dev(ctx);
}
/**
diff --git a/drivers/crypto/ti/dthev2-common.c b/drivers/crypto/ti/dthev2-common.c
index 5ca1576664133..3844faf5fec1f 100644
--- a/drivers/crypto/ti/dthev2-common.c
+++ b/drivers/crypto/ti/dthev2-common.c
@@ -27,6 +27,10 @@
#define DRIVER_NAME "dthev2"
+/* Interval and timeout for polling dthe_data::refcnt on removal */
+#define DTHE_REFCNT_POLL_INTERVAL_US 20000
+#define DTHE_REFCNT_POLL_TIMEOUT_US 1000000
+
static struct dthe_list dthe_dev_list = {
.dev_list = LIST_HEAD_INIT(dthe_dev_list.dev_list),
.lock = __SPIN_LOCK_UNLOCKED(dthe_dev_list.lock),
@@ -41,13 +45,21 @@ struct dthe_data *dthe_get_dev(struct dthe_tfm_ctx *ctx)
spin_lock(&dthe_dev_list.lock);
dev_data = list_first_entry_or_null(&dthe_dev_list.dev_list, struct dthe_data, list);
- if (dev_data)
+ if (dev_data) {
list_move_tail(&dev_data->list, &dthe_dev_list.dev_list);
+ atomic_inc(&dev_data->refcnt);
+ }
spin_unlock(&dthe_dev_list.lock);
return dev_data;
}
+void dthe_put_dev(struct dthe_tfm_ctx *ctx)
+{
+ atomic_dec(&ctx->dev_data->refcnt);
+ ctx->dev_data = NULL;
+}
+
struct scatterlist *dthe_copy_sg(struct scatterlist *dst,
struct scatterlist *src,
int buflen)
@@ -200,9 +212,17 @@ static int dthe_probe(struct platform_device *pdev)
static void dthe_remove(struct platform_device *pdev)
{
struct dthe_data *dev_data = platform_get_drvdata(pdev);
+ int refcnt, ret;
dthe_unregister_algs();
+ ret = readx_poll_timeout(atomic_read, &dev_data->refcnt, refcnt, !refcnt,
+ DTHE_REFCNT_POLL_INTERVAL_US, DTHE_REFCNT_POLL_TIMEOUT_US);
+ if (ret)
+ dev_warn(dev_data->dev,
+ "removing with %d transform context(s) still active\n",
+ refcnt);
+
spin_lock(&dthe_dev_list.lock);
list_del(&dev_data->list);
spin_unlock(&dthe_dev_list.lock);
diff --git a/drivers/crypto/ti/dthev2-common.h b/drivers/crypto/ti/dthev2-common.h
index d4a3b9c18bbc1..8eb27812b8cdf 100644
--- a/drivers/crypto/ti/dthev2-common.h
+++ b/drivers/crypto/ti/dthev2-common.h
@@ -18,6 +18,7 @@
#include <crypto/internal/hash.h>
#include <crypto/internal/skcipher.h>
+#include <linux/atomic.h>
#include <linux/delay.h>
#include <linux/dmaengine.h>
#include <linux/dmapool.h>
@@ -53,6 +54,7 @@ enum dthe_aes_mode {
* @dma_aes_rx: AES Rx DMA Channel
* @dma_aes_tx: AES Tx DMA Channel
* @dma_sha_tx: SHA Tx DMA Channel
+ * @refcnt: Count of transform contexts currently holding a reference to this instance
*/
struct dthe_data {
struct device *dev;
@@ -64,6 +66,8 @@ struct dthe_data {
struct dma_chan *dma_aes_tx;
struct dma_chan *dma_sha_tx;
+
+ atomic_t refcnt;
};
/**
@@ -113,6 +117,7 @@ struct dthe_aes_req_ctx {
/* Struct definitions end */
struct dthe_data *dthe_get_dev(struct dthe_tfm_ctx *ctx);
+void dthe_put_dev(struct dthe_tfm_ctx *ctx);
/**
* dthe_copy_sg - Copy sg entries from src to dst
--
2.34.1
^ permalink raw reply related [flat|nested] 6+ messages in thread