From: Lizhi Hou <lizhi.hou@amd.com>
To: Bilal Hashim <AeeSec@126.com>, Min Ma <mamin506@gmail.com>,
Oded Gabbay <ogabbay@kernel.org>
Cc: <dri-devel@lists.freedesktop.org>, <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH] accel/amdxdna: Validate command BO size and payload in amdxdna_ctx
Date: Mon, 30 Nov 2026 14:20:48 -0800 [thread overview]
Message-ID: <c8160bac-bd77-a532-3b48-f51cd9277910@amd.com> (raw)
In-Reply-To: <20261128140537.1325-2-AeeSec@126.com>
On 11/28/26 06:05, Bilal Hashim wrote:
> 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`.
AMDXDNA_CMD_EXTRA_CU_MASK is GENMASK(11, 10), so num_masks <= 4 and the loop
reads cmd->data[0..3]
> 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)`.
It is true that the payload *length* is not validated on this path. That is
not the same as an out-of-bounds access, which is what matters here.
For ERT_CMD_CHAIN, num_masks is 0, so cc == &cmd->data[0], at offset 4.
struct amdxdna_cmd_chain is 24 bytes plus a u64 flex array, so
cc->error_index
is a write at offset 12 and cc->data[0] is an 8-byte read at offset 28 --
both comfortably inside a >= 4096-byte mapping. There is no OOB read and
no OOB write.
sizeof(*cmd) is 4 and mem.size is page-aligned and non-zero at that point.
No underflow is reachable.
> 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`.
The returned offset is offsetof(struct amdxdna_cmd, data[0]) + num_masks
* 4,
which is at most 20. It is always within mem.size.
Lizhi
>
> 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`.
Lizhi
>
> Signed-off-by: Bilal Hashim <AeeSec@126.com>
> ---
> drivers/accel/amdxdna/amdxdna_ctx.c | 25 ++++++++++++++++++-------
> 1 file changed, 18 insertions(+), 7 deletions(-)
>
> diff --git a/drivers/accel/amdxdna/amdxdna_ctx.c b/drivers/accel/amdxdna/amdxdna_ctx.c
> index 123456789abc..23456789abcd 100644
> --- a/drivers/accel/amdxdna/amdxdna_ctx.c
> +++ b/drivers/accel/amdxdna/amdxdna_ctx.c
> @@ -117,74 +117,82 @@ void *amdxdna_cmd_get_payload(struct amdxdna_gem_obj *abo, u32 *size)
> struct amdxdna_cmd *cmd = amdxdna_gem_vmap(abo);
> u32 num_masks, count;
>
> - if (!cmd)
> + if (!cmd || abo->mem.size < sizeof(*cmd))
> return NULL;
>
> if (amdxdna_cmd_get_op(abo) == ERT_CMD_CHAIN)
> num_masks = 0;
> else
> num_masks = 1 + FIELD_GET(AMDXDNA_CMD_EXTRA_CU_MASK, cmd->header);
>
> - if (size) {
> - count = FIELD_GET(AMDXDNA_CMD_COUNT, cmd->header);
> - if (unlikely(count <= num_masks ||
> - count * sizeof(u32) +
> - offsetof(struct amdxdna_cmd, data[0]) >
> - abo->mem.size)) {
> + count = FIELD_GET(AMDXDNA_CMD_COUNT, cmd->header);
> + if (unlikely(count <= num_masks ||
> + struct_size(cmd, data, count) > abo->mem.size)) {
> + if (size)
> *size = 0;
> - return NULL;
> - }
> - *size = (count - num_masks) * sizeof(u32);
> + return NULL;
> }
> +
> + if (size)
> + *size = (count - num_masks) * sizeof(u32);
> +
> return &cmd->data[num_masks];
> }
>
> u32 amdxdna_cmd_get_cu_idx(struct amdxdna_gem_obj *abo)
> {
> struct amdxdna_cmd *cmd = amdxdna_gem_vmap(abo);
> u32 num_masks, i;
> u32 *cu_mask;
>
> - if (!cmd)
> + if (!cmd || abo->mem.size < sizeof(*cmd))
> return INVALID_CU_IDX;
>
> if (amdxdna_cmd_get_op(abo) == ERT_CMD_CHAIN)
> return INVALID_CU_IDX;
>
> num_masks = 1 + FIELD_GET(AMDXDNA_CMD_EXTRA_CU_MASK, cmd->header);
> + if (unlikely(struct_size(cmd, data, num_masks) > abo->mem.size))
> + return INVALID_CU_IDX;
> +
> cu_mask = cmd->data;
> for (i = 0; i < num_masks; i++) {
> if (cu_mask[i])
> return ffs(cu_mask[i]) - 1;
> }
>
> return INVALID_CU_IDX;
> }
>
> int amdxdna_cmd_set_error(struct amdxdna_gem_obj *abo,
> struct amdxdna_sched_job *job, u32 cmd_idx,
> enum ert_cmd_state error_state,
> void *err_data, size_t size)
> {
> 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);
> if (!abo)
> return -EINVAL;
> cmd = amdxdna_gem_vmap(abo);
> - if (!cmd) {
> + if (!cmd || abo->mem.size < sizeof(*cmd)) {
> amdxdna_gem_put_obj(abo);
> return -ENOMEM;
> }
> }
next prev parent reply other threads:[~2026-11-30 22:20 UTC|newest]
Thread overview: 4+ 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-30 22:20 ` Lizhi Hou [this message]
2026-11-30 21:36 ` [PATCH] accel/amdxdna: Fix NULL pointer dereference and u32 underflow in aie2_message 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=c8160bac-bd77-a532-3b48-f51cd9277910@amd.com \
--to=lizhi.hou@amd.com \
--cc=AeeSec@126.com \
--cc=dri-devel@lists.freedesktop.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mamin506@gmail.com \
--cc=ogabbay@kernel.org \
/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
all inboxes | Powered by JetHome®