d40_validate_conf() checks the raw dev_type against num_log_chans,
but d40_allocate_channel() derives the lookup_log_chans index as either
2 * dev_type or 2 * dev_type + 1.

Validate the dev_type against the derived logical channel index limit so
channel allocation cannot write past lookup_log_chans.

Reported-by: [email protected]
Closes: 
https://lore.kernel.org/dmaengine/[email protected]/
Assisted-by: Codex:gpt-5-5
Signed-off-by: Linus Walleij <[email protected]>
---
 drivers/dma/ste_dma40.c | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)

diff --git a/drivers/dma/ste_dma40.c b/drivers/dma/ste_dma40.c
index 4e840846466b..7c777e410c10 100644
--- a/drivers/dma/ste_dma40.c
+++ b/drivers/dma/ste_dma40.c
@@ -1737,15 +1737,26 @@ static int d40_validate_conf(struct d40_chan *d40c,
 {
        int res = 0;
        bool is_log = conf->mode == STEDMA40_MODE_LOGICAL;
+       bool invalid_dev_type = conf->dev_type < 0;
 
        if (!conf->dir) {
                chan_err(d40c, "Invalid direction.\n");
                res = -EINVAL;
        }
 
-       if ((is_log && conf->dev_type > d40c->base->num_log_chans)  ||
-           (!is_log && conf->dev_type > d40c->base->num_phy_chans) ||
-           (conf->dev_type < 0)) {
+       if (!invalid_dev_type && is_log) {
+               int max_dev_type;
+
+               if (conf->dir == DMA_DEV_TO_MEM)
+                       max_dev_type = DIV_ROUND_UP(d40c->base->num_log_chans, 
2);
+               else
+                       max_dev_type = d40c->base->num_log_chans / 2;
+
+               invalid_dev_type = conf->dev_type >= max_dev_type;
+       }
+
+       if (invalid_dev_type ||
+           (!is_log && conf->dev_type > d40c->base->num_phy_chans)) {
                chan_err(d40c, "Invalid device type (%d)\n", conf->dev_type);
                res = -EINVAL;
        }

-- 
2.55.0


Reply via email to