The command-stream validator consumes 32-bit words, so reject empty or
unaligned streams at that boundary. This also prevents QSIZE from
receiving an unvalidated trailing byte.

Check that the second word of a CMD1 command is within the declared
stream before reading it from userspace. A word-aligned stream can still
end with the first word of a two-word command. The old ordering read
beyond the supplied size and could return -EFAULT instead of rejecting
the truncated command with -EINVAL.

Fixes: 5a5e9c0228e6 ("accel: Add Arm Ethos-U NPU driver")
Cc: [email protected]
Signed-off-by: Rob Herring (Arm) <[email protected]>
---
v5:
 - Also check length before get_user() access

v4:
 - New patch
---
 drivers/accel/ethosu/ethosu_gem.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/drivers/accel/ethosu/ethosu_gem.c 
b/drivers/accel/ethosu/ethosu_gem.c
index ff89cfdb4772..fc31372a2c08 100644
--- a/drivers/accel/ethosu/ethosu_gem.c
+++ b/drivers/accel/ethosu/ethosu_gem.c
@@ -977,6 +977,8 @@ static int ethosu_gem_cmdstream_copy_and_validate(struct 
drm_device *ddev,
 
        if (!info)
                return -ENOMEM;
+       if (!size || size & 3)
+               return -EINVAL;
        info->cmd_size = size;
 
        cmd_state_init(&st);
@@ -997,12 +999,11 @@ static int ethosu_gem_cmdstream_copy_and_validate(struct 
drm_device *ddev,
                param = cmds[0] >> 16;
 
                if (cmd & NPU_CMD_CTRL_CMD1) {
+                       if (++i >= size / 4)
+                               return -EINVAL;
                        if (get_user(cmds[1], ucmds++))
                                return -EFAULT;
 
-                       i++;
-                       if (i >= size / 4)
-                               return -EINVAL;
                        bocmds[i] = cmds[1];
                        addr = cmd_to_addr(cmds);
                }

-- 
2.53.0

Reply via email to