dri-devel Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Bilal Hashim" <AeeSec@126.com>
Cc: dri-devel@lists.freedesktop.org
Subject: Re: [PATCH] accel/amdxdna: Validate command BO size and payload in amdxdna_ctx
Date: Sat, 28 Nov 2026 14:24:35 +0000	[thread overview]
Message-ID: <sashiko-outbox-238771@kernel.org> (raw)
In-Reply-To: <20261128140537.1325-2-AeeSec@126.com>

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 <AeeSec@126.com>

> 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 <AeeSec@126.com>

[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/20261128140537.1325-2-AeeSec@126.com?part=1

  reply	other threads:[~2026-11-28 14:24 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-28 14:05 [PATCH] accel/amdxdna: Fix NULL pointer dereference and u32 underflow in aie2_message Bilal Hashim
2026-11-28 14:05 ` [PATCH] accel/amdxdna: Validate command BO size and payload in amdxdna_ctx Bilal Hashim
2026-11-28 14:24   ` sashiko-bot [this message]
2026-11-30 22:20   ` Lizhi Hou
2026-11-28 14:13 ` [PATCH] accel/amdxdna: Fix NULL pointer dereference and u32 underflow in aie2_message sashiko-bot
2026-11-30 21:36 ` Lizhi Hou

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-238771@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=AeeSec@126.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox