* [PATCH 0/5] Fix several issues in DTHEv2 driver
@ 2026-08-27 10:57 T Pratham
2026-08-27 10:57 ` [PATCH 1/5] crypto: ti - Use list_first_entry_or_null() in dthe_get_dev() T Pratham
` (4 more replies)
0 siblings, 5 replies; 6+ messages in thread
From: T Pratham @ 2026-08-27 10:57 UTC (permalink / raw)
To: Herbert Xu, David S . Miller
Cc: T Pratham, linux-crypto, linux-kernel, Sebin Francis,
Manorit Chawdhry, Vishal Mahaveer, Praneeth Bajjuri
This series fixes a handful of independent and mostly benign till now
issues in the DTHEv2 driver which were flagged by bots and AI agents.
Except for the fourth patch in this series which was found in an
internal testing.
The Sashiko review on DTHEv2 hashing driver patch series v5 pointed out
some pre-existing issues which needed to be fixed.
https://sashiko.dev/#/patchset/20260807110501.975130-1-t-pratham%40ti.com
One of the issues had already been flagged by kernel test bot and a fix
sent on the list by Mert:
https://lore.kernel.org/all/20260613085858.32580-1-mertsftl@gmail.com/
I included this patch in this series with minor edits. Then the Sashiko
review page for this had more comments:
https://sashiko.dev/#/patchset/20260613085858.32580-1-mertsftl%40gmail.com
Overall this resulted in a bunch of fixes patches which would have
deviated the DTHEv2 hashing driver series had they been included with
it. So I clubbed them together in a new patch series which is this.
Signed-off-by: T Pratham <t-pratham@ti.com>
---
Mert Seftali (1):
crypto: ti - Use list_first_entry_or_null() in dthe_get_dev()
T Pratham (4):
crypto: ti - Fix potential deadlock and allocation bugs in DTHEv2
crypto: ti - Fix potential memory corruption on highmem pages
crypto: ti - Trim scatterlists to correct length in AES
crypto: ti - Fix use-after-free of dev_data on DTHEv2 driver removal
drivers/crypto/ti/dthev2-aes.c | 153 +++++++++++++++---------------
drivers/crypto/ti/dthev2-common.c | 34 +++++--
drivers/crypto/ti/dthev2-common.h | 5 +
3 files changed, 111 insertions(+), 81 deletions(-)
--
2.34.1
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH 1/5] crypto: ti - Use list_first_entry_or_null() in dthe_get_dev()
2026-08-27 10:57 [PATCH 0/5] Fix several issues in DTHEv2 driver T Pratham
@ 2026-08-27 10:57 ` T Pratham
2026-08-27 10:57 ` [PATCH 2/5] crypto: ti - Fix potential deadlock and allocation bugs in DTHEv2 T Pratham
` (3 subsequent siblings)
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: Mert Seftali, Sebin Francis, Manorit Chawdhry, Vishal Mahaveer,
Praneeth Bajjuri, kernel test robot, Dan Carpenter, linux-crypto,
linux-kernel
From: Mert Seftali <mertsftl@gmail.com>
dthe_get_dev() fetches a device from the global device list with
list_first_entry() and then checks the result for NULL. However,
list_first_entry() never returns NULL: on an empty list it returns a
bogus pointer computed from the list head. The NULL check is therefore
dead code, and an empty list would be treated as a valid entry and
moved around as if it were a real device.
Use list_first_entry_or_null() so the existing NULL check works as
intended and an empty list is handled gracefully.
[pratham:]
Add null checks on dev_data in callers of dthe_get_dev().
Fixes: 52f641bc63a4 ("crypto: ti - Add driver for DTHE V2 AES Engine (ECB, CBC)")
Reported-by: kernel test robot <lkp@intel.com>
Reported-by: Dan Carpenter <error27@gmail.com>
Closes: https://lore.kernel.org/r/202606111933.69GGTKxr-lkp@intel.com/
Signed-off-by: Mert Seftali <mertsftl@gmail.com>
Co-developed-by: T Pratham <t-pratham@ti.com>
Signed-off-by: T Pratham <t-pratham@ti.com>
---
drivers/crypto/ti/dthev2-aes.c | 9 +++++++++
drivers/crypto/ti/dthev2-common.c | 2 +-
2 files changed, 10 insertions(+), 1 deletion(-)
diff --git a/drivers/crypto/ti/dthev2-aes.c b/drivers/crypto/ti/dthev2-aes.c
index eb5cd902dfb59..c025b08c49930 100644
--- a/drivers/crypto/ti/dthev2-aes.c
+++ b/drivers/crypto/ti/dthev2-aes.c
@@ -112,6 +112,9 @@ 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;
@@ -124,6 +127,9 @@ static int dthe_cipher_init_tfm_fallback(struct crypto_skcipher *tfm)
struct dthe_data *dev_data = dthe_get_dev(ctx);
const char *alg_name = crypto_tfm_alg_name(crypto_skcipher_tfm(tfm));
+ if (!dev_data)
+ return -ENODEV;
+
ctx->dev_data = dev_data;
ctx->keylen = 0;
@@ -572,6 +578,9 @@ static int dthe_aead_init_tfm(struct crypto_aead *tfm)
struct dthe_tfm_ctx *ctx = crypto_aead_ctx(tfm);
struct dthe_data *dev_data = dthe_get_dev(ctx);
+ if (!dev_data)
+ return -ENODEV;
+
ctx->dev_data = dev_data;
const char *alg_name = crypto_tfm_alg_name(crypto_aead_tfm(tfm));
diff --git a/drivers/crypto/ti/dthev2-common.c b/drivers/crypto/ti/dthev2-common.c
index a2ad79bec105a..cc02449382673 100644
--- a/drivers/crypto/ti/dthev2-common.c
+++ b/drivers/crypto/ti/dthev2-common.c
@@ -40,7 +40,7 @@ struct dthe_data *dthe_get_dev(struct dthe_tfm_ctx *ctx)
return ctx->dev_data;
spin_lock_bh(&dthe_dev_list.lock);
- dev_data = list_first_entry(&dthe_dev_list.dev_list, struct dthe_data, list);
+ dev_data = list_first_entry_or_null(&dthe_dev_list.dev_list, struct dthe_data, list);
if (dev_data)
list_move_tail(&dev_data->list, &dthe_dev_list.dev_list);
spin_unlock_bh(&dthe_dev_list.lock);
--
2.34.1
^ permalink raw reply related [flat|nested] 6+ messages in thread
* [PATCH 2/5] crypto: ti - Fix potential deadlock and allocation bugs in DTHEv2
2026-08-27 10:57 [PATCH 0/5] Fix several issues in DTHEv2 driver T Pratham
2026-08-27 10:57 ` [PATCH 1/5] crypto: ti - Use list_first_entry_or_null() in dthe_get_dev() T Pratham
@ 2026-08-27 10:57 ` T Pratham
2026-08-27 10:57 ` [PATCH 3/5] crypto: ti - Fix potential memory corruption on highmem pages T Pratham
` (2 subsequent siblings)
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
Probe and remove functions use spin_(un)lock for acquiring the
dthe_dev_list.lock. But dthe_get_dev uses the bh variant. Change
spin_(un)lock_bh to spin_(un)lock to avoid potential deadlock when a
softIRQ process tries to acquire the already held lock.
De-register the algorithms before removing the dev_data from linked list
to avoid any allocation during removal.
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-common.c | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/drivers/crypto/ti/dthev2-common.c b/drivers/crypto/ti/dthev2-common.c
index cc02449382673..b315c850f05d6 100644
--- a/drivers/crypto/ti/dthev2-common.c
+++ b/drivers/crypto/ti/dthev2-common.c
@@ -39,11 +39,11 @@ struct dthe_data *dthe_get_dev(struct dthe_tfm_ctx *ctx)
if (ctx->dev_data)
return ctx->dev_data;
- spin_lock_bh(&dthe_dev_list.lock);
+ 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)
list_move_tail(&dev_data->list, &dthe_dev_list.dev_list);
- spin_unlock_bh(&dthe_dev_list.lock);
+ spin_unlock(&dthe_dev_list.lock);
return dev_data;
}
@@ -201,12 +201,12 @@ static void dthe_remove(struct platform_device *pdev)
{
struct dthe_data *dev_data = platform_get_drvdata(pdev);
+ dthe_unregister_algs();
+
spin_lock(&dthe_dev_list.lock);
list_del(&dev_data->list);
spin_unlock(&dthe_dev_list.lock);
- dthe_unregister_algs();
-
crypto_engine_exit(dev_data->engine);
dma_release_channel(dev_data->dma_aes_rx);
--
2.34.1
^ permalink raw reply related [flat|nested] 6+ messages in thread
* [PATCH 3/5] crypto: ti - Fix potential memory corruption on highmem pages
2026-08-27 10:57 [PATCH 0/5] Fix several issues in DTHEv2 driver T Pratham
2026-08-27 10:57 ` [PATCH 1/5] crypto: ti - Use list_first_entry_or_null() in dthe_get_dev() T Pratham
2026-08-27 10:57 ` [PATCH 2/5] crypto: ti - Fix potential deadlock and allocation bugs in DTHEv2 T Pratham
@ 2026-08-27 10:57 ` T Pratham
2026-08-27 10:57 ` [PATCH 4/5] crypto: ti - Trim scatterlists to correct length in AES 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, linux-crypto, linux-kernel
Change sg_set_buf to sg_set_page in DTHEv2 dthe_copy_sg function to
avoid using sg_virt() on scatterlists. For scatterlists containing a
highmem page, sg_virt() yields invalid or null adrdess, causing
potential memory corruption.
Fixes: 35645ca63caa1 ("crypto: ti - Add support for AES-CTR in DTHEv2 driver")
Signed-off-by: T Pratham <t-pratham@ti.com>
---
drivers/crypto/ti/dthev2-common.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/drivers/crypto/ti/dthev2-common.c b/drivers/crypto/ti/dthev2-common.c
index b315c850f05d6..5ca1576664133 100644
--- a/drivers/crypto/ti/dthev2-common.c
+++ b/drivers/crypto/ti/dthev2-common.c
@@ -59,7 +59,7 @@ struct scatterlist *dthe_copy_sg(struct scatterlist *dst,
sglen = from_sg->length;
if (sglen > buflen)
sglen = buflen;
- sg_set_buf(to_sg, sg_virt(from_sg), sglen);
+ sg_set_page(to_sg, sg_page(from_sg), sglen, from_sg->offset);
from_sg = sg_next(from_sg);
to_sg = sg_next(to_sg);
}
--
2.34.1
^ permalink raw reply related [flat|nested] 6+ messages in thread
* [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
end of thread, other threads:[~2026-08-27 10:59 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-08-27 10:57 [PATCH 0/5] Fix several issues in DTHEv2 driver T Pratham
2026-08-27 10:57 ` [PATCH 1/5] crypto: ti - Use list_first_entry_or_null() in dthe_get_dev() T Pratham
2026-08-27 10:57 ` [PATCH 2/5] crypto: ti - Fix potential deadlock and allocation bugs in DTHEv2 T Pratham
2026-08-27 10:57 ` [PATCH 3/5] crypto: ti - Fix potential memory corruption on highmem pages T Pratham
2026-08-27 10:57 ` [PATCH 4/5] crypto: ti - Trim scatterlists to correct length in AES 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
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox