[PATCH v3 04/12] crypto: ti - Fix use-after-free of dev_data on DTHEv2 driver removal
From: T Pratham <t-pratham@ti.com>
Date: 2026-09-10 10:43:21
Also in:
lkml
Subsystem:
crypto api, the rest, ti data transform and hashing engine (dthe) v2 crypto driver · Maintainers:
Herbert Xu, "David S. Miller", Linus Torvalds, T Pratham
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.
Move dthe_data to req_ctx structs of algorithms, and store the device
pointer in tfm_ctx. Add a refcnt to struct dthe_data, which atomically
counts the number of requests enqueued in the crypto engine queue which
reference the dthe_data object.
A waitqueue waits on this atomic counter to get back to zero in
dthe_remove() before doing the driver teardown.
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 | 74 +++++++++++++++++++++----------
drivers/crypto/ti/dthev2-common.c | 39 +++++++++++++---
drivers/crypto/ti/dthev2-common.h | 52 ++++++++++++++++++++--
3 files changed, 133 insertions(+), 32 deletions(-)
diff --git a/drivers/crypto/ti/dthev2-aes.c b/drivers/crypto/ti/dthev2-aes.c
index 4fdd24ee91637..6a8fbe67ef6cd 100644
--- a/drivers/crypto/ti/dthev2-aes.c
+++ b/drivers/crypto/ti/dthev2-aes.c@@ -110,12 +110,11 @@ enum aes_ctrl_mode_masks { 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) + ctx->dev = dthe_get_dev(); + if (!dev) return -ENODEV; - ctx->dev_data = dev_data; ctx->keylen = 0; return 0;
@@ -124,20 +123,19 @@ static int dthe_cipher_init_tfm(struct crypto_skcipher *tfm) 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); const char *alg_name = crypto_tfm_alg_name(crypto_skcipher_tfm(tfm)); - if (!dev_data) + ctx->dev = dthe_get_dev(); + if (!ctx->dev) return -ENODEV; - ctx->dev_data = dev_data; ctx->keylen = 0; - ctx->skcipher_fb = crypto_alloc_sync_skcipher(alg_name, 0, CRYPTO_ALG_NEED_FALLBACK); if (IS_ERR(ctx->skcipher_fb)) { - dev_err(dev_data->dev, "fallback driver %s couldn't be loaded\n", + dev_err(ctx->dev, "fallback driver %s couldn't be loaded\n", alg_name); + dthe_put_dev(ctx->dev); return PTR_ERR(ctx->skcipher_fb); }
@@ -149,6 +147,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->dev); } static int dthe_aes_setkey(struct crypto_skcipher *tfm, const u8 *key, unsigned int keylen)
@@ -225,7 +224,7 @@ static void dthe_aes_set_ctrl_key(struct dthe_tfm_ctx *ctx, struct dthe_aes_req_ctx *rctx, u32 *iv_in) { - struct dthe_data *dev_data = dthe_get_dev(ctx); + struct dthe_data *dev_data = rctx->dev_data; void __iomem *aes_base_reg = dev_data->regs + DTHE_P_AES_BASE; u32 ctrl_val = 0;
@@ -337,8 +336,8 @@ static int dthe_aes_run(struct crypto_engine *engine, void *areq) { struct skcipher_request *req = container_of(areq, struct skcipher_request, base); struct dthe_tfm_ctx *ctx = crypto_skcipher_ctx(crypto_skcipher_reqtfm(req)); - struct dthe_data *dev_data = dthe_get_dev(ctx); struct dthe_aes_req_ctx *rctx = skcipher_request_ctx(req); + struct dthe_data *dev_data = rctx->dev_data; unsigned int len = req->cryptlen; struct scatterlist *src = req->src;
@@ -520,14 +519,17 @@ static int dthe_aes_run(struct crypto_engine *engine, void *areq) local_bh_disable(); crypto_finalize_skcipher_request(dev_data->engine, req, ret); local_bh_enable(); + dthe_put_drvdata(dev_data); return 0; } static int dthe_aes_crypt(struct skcipher_request *req) { struct dthe_tfm_ctx *ctx = crypto_skcipher_ctx(crypto_skcipher_reqtfm(req)); - struct dthe_data *dev_data = dthe_get_dev(ctx); + struct dthe_aes_req_ctx *rctx = skcipher_request_ctx(req); + struct dthe_data *dev_data; struct crypto_engine *engine; + int ret; /* * If data is not a multiple of AES_BLOCK_SIZE:
@@ -553,8 +555,18 @@ static int dthe_aes_crypt(struct skcipher_request *req) return 0; } + dev_data = dthe_get_drvdata(ctx->dev); + if (!dev_data) + return -ENODEV; + + rctx->dev_data = dev_data; + engine = dev_data->engine; - return crypto_transfer_skcipher_request_to_engine(engine, req); + ret = crypto_transfer_skcipher_request_to_engine(engine, req); + if (ret != -EINPROGRESS && ret != -EBUSY) + dthe_put_drvdata(dev_data); + + return ret; } static int dthe_aes_encrypt(struct skcipher_request *req)
@@ -576,19 +588,18 @@ static int dthe_aes_decrypt(struct skcipher_request *req) 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); const char *alg_name = crypto_tfm_alg_name(crypto_aead_tfm(tfm)); - if (!dev_data) + ctx->dev = dthe_get_dev(); + if (!ctx->dev) return -ENODEV; - ctx->dev_data = dev_data; - ctx->aead_fb = crypto_alloc_sync_aead(alg_name, 0, CRYPTO_ALG_NEED_FALLBACK); if (IS_ERR(ctx->aead_fb)) { - dev_err(dev_data->dev, "fallback driver %s couldn't be loaded\n", + dev_err(ctx->dev, "fallback driver %s couldn't be loaded\n", alg_name); + dthe_put_dev(ctx->dev); return PTR_ERR(ctx->aead_fb); }
@@ -600,6 +611,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->dev); } /**
@@ -711,9 +723,9 @@ static struct scatterlist *dthe_aead_prep_crypt(struct scatterlist *sg, return crypt_sg; } -static int dthe_aead_read_tag(struct dthe_tfm_ctx *ctx, u32 *tag) +static int dthe_aead_read_tag(struct dthe_aes_req_ctx *rctx, u32 *tag) { - struct dthe_data *dev_data = dthe_get_dev(ctx); + struct dthe_data *dev_data = rctx->dev_data; void __iomem *aes_base_reg = dev_data->regs + DTHE_P_AES_BASE; u32 val; int ret;
@@ -734,11 +746,12 @@ static int dthe_aead_read_tag(struct dthe_tfm_ctx *ctx, u32 *tag) static int dthe_aead_enc_get_tag(struct aead_request *req) { struct dthe_tfm_ctx *ctx = crypto_aead_ctx(crypto_aead_reqtfm(req)); + struct dthe_aes_req_ctx *rctx = aead_request_ctx(req); u32 tag[AES_BLOCK_WORDS]; int nents; int ret; - ret = dthe_aead_read_tag(ctx, tag); + ret = dthe_aead_read_tag(rctx, tag); if (ret) return ret;
@@ -753,12 +766,13 @@ static int dthe_aead_enc_get_tag(struct aead_request *req) static int dthe_aead_dec_verify_tag(struct aead_request *req) { struct dthe_tfm_ctx *ctx = crypto_aead_ctx(crypto_aead_reqtfm(req)); + struct dthe_aes_req_ctx *rctx = aead_request_ctx(req); u32 tag_out[AES_BLOCK_WORDS]; u32 tag_in[AES_BLOCK_WORDS]; int nents; int ret; - ret = dthe_aead_read_tag(ctx, tag_out); + ret = dthe_aead_read_tag(rctx, tag_out); if (ret) return ret;
@@ -859,7 +873,7 @@ static int dthe_aead_run(struct crypto_engine *engine, void *areq) struct aead_request *req = container_of(areq, struct aead_request, base); struct dthe_tfm_ctx *ctx = crypto_aead_ctx(crypto_aead_reqtfm(req)); struct dthe_aes_req_ctx *rctx = aead_request_ctx(req); - struct dthe_data *dev_data = dthe_get_dev(ctx); + struct dthe_data *dev_data = rctx->dev_data; unsigned int cryptlen = req->cryptlen; unsigned int assoclen = req->assoclen;
@@ -1130,6 +1144,7 @@ static int dthe_aead_run(struct crypto_engine *engine, void *areq) local_bh_disable(); crypto_finalize_aead_request(engine, req, ret); local_bh_enable(); + dthe_put_drvdata(dev_data); return 0; }
@@ -1137,10 +1152,11 @@ static int dthe_aead_crypt(struct aead_request *req) { struct dthe_tfm_ctx *ctx = crypto_aead_ctx(crypto_aead_reqtfm(req)); struct dthe_aes_req_ctx *rctx = aead_request_ctx(req); - struct dthe_data *dev_data = dthe_get_dev(ctx); + struct dthe_data *dev_data; struct crypto_engine *engine; unsigned int cryptlen = req->cryptlen; bool is_zero_ctr = true; + int ret; /* In decryption, last authsize bytes are the TAG */ if (!rctx->enc)
@@ -1191,8 +1207,18 @@ static int dthe_aead_crypt(struct aead_request *req) (ctx->aes_mode == DTHE_AES_CCM && !is_zero_ctr)) return dthe_aead_do_fallback(req); + dev_data = dthe_get_drvdata(ctx->dev); + if (!dev_data) + return -ENODEV; + + rctx->dev_data = dev_data; + engine = dev_data->engine; - return crypto_transfer_aead_request_to_engine(engine, req); + ret = crypto_transfer_aead_request_to_engine(engine, req); + if (ret != -EINPROGRESS && ret != -EBUSY) + dthe_put_drvdata(dev_data); + + return ret; } static int dthe_aead_encrypt(struct aead_request *req)
diff --git a/drivers/crypto/ti/dthev2-common.c b/drivers/crypto/ti/dthev2-common.c
index 8628187a32e18..6b88ad72c48d9 100644
--- a/drivers/crypto/ti/dthev2-common.c
+++ b/drivers/crypto/ti/dthev2-common.c@@ -32,22 +32,44 @@ static struct dthe_list dthe_dev_list = { .lock = __SPIN_LOCK_UNLOCKED(dthe_dev_list.lock), }; -struct dthe_data *dthe_get_dev(struct dthe_tfm_ctx *ctx) +struct device *dthe_get_dev(void) { struct dthe_data *dev_data; - - if (ctx->dev_data) - return ctx->dev_data; + struct device *dev = NULL; 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); + dev = get_device(dev_data->dev); + } spin_unlock(&dthe_dev_list.lock); + return dev; +} + +void dthe_put_dev(struct device *dev) +{ + put_device(dev); +} + +struct dthe_data *dthe_get_drvdata(struct device *dev) +{ + struct dthe_data *dev_data; + + dev_data = dev_get_drvdata(dev); + if (dev_data) + atomic_inc(&dev_data->req_refcnt); + return dev_data; } +void dthe_put_drvdata(struct dthe_data *dev_data) +{ + if (atomic_dec_and_test(&dev_data->req_refcnt)) + wake_up(&dev_data->drain_wq); +} + struct scatterlist *dthe_copy_sg(struct scatterlist *dst, struct scatterlist *src, unsigned int buflen)
@@ -153,6 +175,9 @@ static int dthe_probe(struct platform_device *pdev) if (IS_ERR(dev_data->regs)) return PTR_ERR(dev_data->regs); + atomic_set(&dev_data->req_refcnt, 0); + init_waitqueue_head(&dev_data->drain_wq); + platform_set_drvdata(pdev, dev_data); spin_lock(&dthe_dev_list.lock);
@@ -207,6 +232,10 @@ static void dthe_remove(struct platform_device *pdev) dthe_unregister_algs(); + wait_event(dev_data->drain_wq, !atomic_read(&dev_data->req_refcnt)); + + platform_set_drvdata(pdev, NULL); + crypto_engine_exit(dev_data->engine); dma_release_channel(dev_data->dma_aes_rx);
diff --git a/drivers/crypto/ti/dthev2-common.h b/drivers/crypto/ti/dthev2-common.h
index 75d9a097650da..a827817e8119a 100644
--- a/drivers/crypto/ti/dthev2-common.h
+++ b/drivers/crypto/ti/dthev2-common.h@@ -18,12 +18,14 @@ #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> #include <linux/dma-mapping.h> #include <linux/io.h> #include <linux/scatterlist.h> +#include <linux/wait.h> #define DTHE_REG_SIZE 4 #define DTHE_DMA_TIMEOUT_MS 2000
@@ -53,6 +55,8 @@ 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 + * @req_refcnt: Count of requests currently using this instance. + * @drain_wq: Waited on by dthe_remove() until @req_refcnt reaches zero. */ struct dthe_data { struct device *dev;
@@ -64,6 +68,9 @@ struct dthe_data { struct dma_chan *dma_aes_tx; struct dma_chan *dma_sha_tx; + + atomic_t req_refcnt; + wait_queue_head_t drain_wq; }; /**
@@ -78,7 +85,7 @@ struct dthe_list { /** * struct dthe_tfm_ctx - Transform ctx struct containing ctx for all sub-components of DTHE V2 - * @dev_data: Device data struct pointer + * @dev: Device this transform is bound to. * @keylen: AES key length * @authsize: Authentication size for modes with authentication * @key: AES key
@@ -87,7 +94,7 @@ struct dthe_list { * @skcipher_fb: Fallback crypto skcipher handle for AES-XTS mode */ struct dthe_tfm_ctx { - struct dthe_data *dev_data; + struct device *dev; unsigned int keylen; unsigned int authsize; u32 key[DTHE_MAX_KEYSIZE / sizeof(u32)];
@@ -103,16 +110,55 @@ struct dthe_tfm_ctx { * @enc: flag indicating encryption or decryption operation * @padding: padding buffer for handling unaligned data * @aes_compl: Completion variable for use in manual completion in case of DMA callback failure + * @dev_data: Device data struct pointer */ struct dthe_aes_req_ctx { int enc; u8 padding[2 * AES_BLOCK_SIZE]; struct completion aes_compl; + struct dthe_data *dev_data; }; /* Struct definitions end */ -struct dthe_data *dthe_get_dev(struct dthe_tfm_ctx *ctx); +/** + * dthe_get_dev - Get the device pointer after increasing its reference count + * + * Description: + * Picks a device instance and gets the associated device pointer through get_device() + * to maintain its reference count by the kernel itself. Callers must ensure to call + * dthe_put_dev() at exit to decrease the refcnt. + */ +struct device *dthe_get_dev(void); + +/** + * dthe_put_dev - Decrease the reference count of the device + * @dev: Device pointer to be released + * + * Description: + * Decreases the reference count of the device pointer obtained through dthe_get_dev() + * by calling put_device(). + */ +void dthe_put_dev(struct device *dev); + +/** + * dthe_get_drvdata - Get a reference counted device driver data pointer + * @dev: Device pointer + * + * Description: + * Returns the instance's driver data if it is still bound, with @req_refcnt + * incremented, or NULL if it has been removed. Every req accepted for + * processing must call this and balance it with exactly one + * dthe_put_drvdata() when done. + */ +struct dthe_data *dthe_get_drvdata(struct device *dev); + +/** + * dthe_put_drvdata - Decrease the refcnt of the device driver data acquired through + * dthe_get_drvdata() + * @dev_data: Driver data previously returned by dthe_get_drvdata() + */ +void dthe_put_drvdata(struct dthe_data *dev_data); /** * dthe_copy_sg - Copy sg entries from src to dst
--
2.34.1