Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] accel/amdxdna: double-fetch of command_count and header from
  shared BO
- [Low] accel/amdxdna: missing Fixes trailers for command BO bounds
  checks
--

commit f26ba1eb1e32cf2335021773851c47a44765f56d
Author: Bilal Hashim <[email protected]>

> accel/amdxdna: Validate command BO size and payload in amdxdna_ctx
>
> Command BOs (`AMDXDNA_BO_SHARE`) are mapped into userspace and their
> contents can be modified by userspace at any time before or during
> command execution. Three helpers in `amdxdna_ctx.c` access `struct
> amdxdna_cmd` fields in the mapped BO without complete bounds checks:
>
> 1. `amdxdna_cmd_get_cu_idx()` computes `num_masks = 1 +
>    FIELD_GET(AMDXDNA_CMD_EXTRA_CU_MASK, cmd->header)` (up to 4 `u32`
>    words) and reads `cmd->data[0..num_masks - 1]` without verifying that
>    `struct_size(cmd, data, num_masks) <= abo->mem.size`.
> 2. `amdxdna_cmd_set_error()` calls `amdxdna_cmd_get_payload(abo, NULL)`
>    when `amdxdna_cmd_get_op(abo) == ERT_CMD_CHAIN`. Passing `size = NULL`
>    bypasses the payload length validation in `amdxdna_cmd_get_payload()`,
>    and `amdxdna_cmd_set_error()` then writes `cc->error_index` and reads
>    `cc->data[0]` without verifying that the payload is large enough to
>    hold a `struct amdxdna_cmd_chain` with at least one `data` entry.
>    Furthermore, `memset(cmd->data, 0xff, abo->mem.size - sizeof(*cmd))`
>    assumes `abo->mem.size >= sizeof(*cmd)`.
> 3. `amdxdna_cmd_get_payload()` only validates `count` against
>    `abo->mem.size` when `size != NULL`. If called with `size == NULL`,
>    it returns `&cmd->data[num_masks]` without checking that the offset
>    lies within `abo->mem.size`.
>
> Harden `amdxdna_cmd_get_payload()`, `amdxdna_cmd_get_cu_idx()`, and
> `amdxdna_cmd_set_error()` by validating the command BO size and payload
> length before reading or writing `cmd->data` and `amdxdna_cmd_chain`.
>
> Signed-off-by: Bilal Hashim <[email protected]>

[Severity: Low]
This fixes missing bounds and payload checks in the command helpers.
Should the commit message include Fixes tags for the commits that
introduced them:

  Fixes: aac243092b70 ("accel/amdxdna: Add command execution")
  Fixes: 89ff45359abb ("accel/amdxdna: Fill invalid payload for failed command")

> diff --git a/drivers/accel/amdxdna/amdxdna_ctx.c 
> b/drivers/accel/amdxdna/amdxdna_ctx.c
> index 8601655cecdb5..06c9eabefac38 100644
> --- a/drivers/accel/amdxdna/amdxdna_ctx.c
> +++ b/drivers/accel/amdxdna/amdxdna_ctx.c
[ ... ]
> @@ -168,21 +171,26 @@ int amdxdna_cmd_set_error(struct amdxdna_gem_obj *abo,
>       struct amdxdna_client *client = job->hwctx->client;
>       struct amdxdna_cmd *cmd = amdxdna_gem_vmap(abo);
>       struct amdxdna_cmd_chain *cc = NULL;
> +     u32 payload_len;
>  
> -     if (!cmd)
> +     if (!cmd || abo->mem.size < sizeof(*cmd))
>               return -ENOMEM;
>  
>       cmd->header &= ~AMDXDNA_CMD_STATE;
>       cmd->header |= FIELD_PREP(AMDXDNA_CMD_STATE, error_state);
>  
>       if (amdxdna_cmd_get_op(abo) == ERT_CMD_CHAIN) {
> -             cc = amdxdna_cmd_get_payload(abo, NULL);
> +             cc = amdxdna_cmd_get_payload(abo, &payload_len);
> +             if (!cc || !cc->command_count ||
> +                 payload_len < struct_size(cc, data, cc->command_count))
> +                     return -EINVAL;
> +
>               cc->error_index = (cmd_idx < cc->command_count) ? cmd_idx : 0;
>               abo = amdxdna_gem_get_obj(client, cc->data[0], 
> AMDXDNA_BO_SHARE);

[Severity: Medium]
Can userspace bypass the new struct_size() check by modifying
cc->command_count between reads?

cc points into the userspace-mapped AMDXDNA_BO_SHARE buffer, and
cc->command_count is read three times without READ_ONCE().  With
payload_len == sizeof(*cc), if a concurrent userspace thread changes
cc->command_count from 1 to 0 between the !cc->command_count check and
the struct_size(cc, data, cc->command_count) check, both checks pass and
cc->data[0] is read past payload_len.

cmd->header is likewise read multiple times from the shared BO across
amdxdna_cmd_get_payload(), amdxdna_cmd_get_cu_idx(), and
amdxdna_cmd_set_error(), so snapshotting both cc->command_count (as
get_cmd_bos() in aie4_ctx.c does) and cmd->header into local variables
with READ_ONCE() would avoid inconsistent reads.

-- 
Sashiko AI review ยท 
https://sashiko.dev/#/patchset/[email protected]?part=1

Reply via email to