* [PATCH] accel/amdxdna: Fix NULL pointer dereference and u32 underflow in aie2_message
@ 2026-11-28 14:05 Bilal Hashim
2026-11-28 14:05 ` [PATCH] accel/amdxdna: Validate command BO size and payload in amdxdna_ctx Bilal Hashim
2026-11-30 21:36 ` [PATCH] accel/amdxdna: Fix NULL pointer dereference and u32 underflow in aie2_message Lizhi Hou
0 siblings, 2 replies; 4+ messages in thread
From: Bilal Hashim @ 2026-11-28 14:05 UTC (permalink / raw)
To: Min Ma, Lizhi Hou, Oded Gabbay; +Cc: dri-devel, linux-kernel, Bilal Hashim
amdxdna_cmd_get_payload() returns NULL and sets *size = 0 when the
user-supplied command header in the shared command BO has an invalid
AMDXDNA_CMD_COUNT (for example, count <= num_masks or exceeding the BO
size), or if amdxdna_gem_vmap() fails.
However, the eight command payload helpers in aie2_message.c do not
check the return value of amdxdna_cmd_get_payload() for NULL before
dereferencing it or passing it to memcpy():
1. In aie2_init_exec_dpu_req():
- `if (cmd_len - sizeof(*sn) > sizeof(dpu_req->payload))` performs an
unsigned 32-bit subtraction (`u32 cmd_len - sizeof(*sn)`). If
`cmd_len < sizeof(*sn)` (for instance, `cmd_len == 4`), on 64-bit
architectures `sizeof(*sn)` is `size_t`, so `(size_t)cmd_len -
sizeof(*sn)` wraps around and is rejected, unless `cmd_len` is 0
(when `sn == NULL`) or if `sn` is dereferenced when `cmd_len` is
valid for the check on 32-bit platforms. More importantly, when
`cmd_len < sizeof(*sn)` or `sn == NULL`, `cmd_len < sizeof(*sn)`
is not explicitly checked first.
2. In aie2_init_exec_cu_req(), aie2_cmdlist_fill_cf(), and
aie2_cmdlist_fill_npu_cf():
- When `amdxdna_cmd_get_payload()` returns `NULL` with `cmd_len == 0`,
the size check (`cmd_len > ...` or `*size < sizeof(...) + cmd_len`)
passes, and if `amdxdna_cmd_get_cu_idx()` succeeds (because the CU
mask word in the command BO is non-zero), `cmd` (`NULL`) is passed
to `memcpy()`, or in future/non-zero paths dereferenced directly.
3. Similarly, in aie2_cmdlist_fill_dpu(), aie2_cmdlist_fill_npu_dpu(),
aie2_cmdlist_fill_npu_preempt(), and aie2_cmdlist_fill_npu_elf(),
checking `!sn` / `!pd` right after `amdxdna_cmd_get_payload()` makes
the error handling explicit and consistent with
aie2_cmdlist_multi_execbuf().
Add explicit NULL checks after every amdxdna_cmd_get_payload() call and
check `cmd_len < sizeof(*sn)` in aie2_init_exec_dpu_req() before
subtracting `sizeof(*sn)`.
Signed-off-by: Bilal Hashim <AeeSec@126.com>
---
drivers/accel/amdxdna/aie2_message.c | 18 ++++++++++++++++--
1 file changed, 16 insertions(+), 2 deletions(-)
diff --git a/drivers/accel/amdxdna/aie2_message.c b/drivers/accel/amdxdna/aie2_message.c
index 8fa123456789..9ab234567890 100644
--- a/drivers/accel/amdxdna/aie2_message.c
+++ b/drivers/accel/amdxdna/aie2_message.c
@@ -556,7 +556,7 @@ static int aie2_init_exec_cu_req(struct amdxdna_gem_obj *cmd_bo, void *req,
void *cmd;
cmd = amdxdna_cmd_get_payload(cmd_bo, &cmd_len);
- if (cmd_len > sizeof(cu_req->payload))
+ if (!cmd || cmd_len > sizeof(cu_req->payload))
return -EINVAL;
cu_req->cu_idx = amdxdna_cmd_get_cu_idx(cmd_bo);
@@ -578,7 +578,7 @@ static int aie2_init_exec_dpu_req(struct amdxdna_gem_obj *cmd_bo, void *req,
u32 cmd_len;
sn = amdxdna_cmd_get_payload(cmd_bo, &cmd_len);
- if (cmd_len - sizeof(*sn) > sizeof(dpu_req->payload))
+ if (!sn || cmd_len < sizeof(*sn) || cmd_len - sizeof(*sn) > sizeof(dpu_req->payload))
return -EINVAL;
dpu_req->cu_idx = amdxdna_cmd_get_cu_idx(cmd_bo);
@@ -623,6 +623,8 @@ aie2_cmdlist_fill_cf(struct amdxdna_gem_obj *cmd_bo, void *slot, size_t *size)
void *cmd;
cmd = amdxdna_cmd_get_payload(cmd_bo, &cmd_len);
+ if (!cmd)
+ return -EINVAL;
if (*size < sizeof(*cf_slot) + cmd_len)
return -EINVAL;
@@ -646,6 +648,8 @@ aie2_cmdlist_fill_dpu(struct amdxdna_gem_obj *cmd_bo, void *slot, size_t *size)
u32 arg_sz;
sn = amdxdna_cmd_get_payload(cmd_bo, &cmd_len);
+ if (!sn)
+ return -EINVAL;
arg_sz = cmd_len - sizeof(*sn);
if (cmd_len < sizeof(*sn) || arg_sz > MAX_DPU_ARGS_SIZE)
return -EINVAL;
@@ -706,6 +710,8 @@ aie2_cmdlist_fill_npu_cf(struct amdxdna_gem_obj *cmd_bo, void *slot, size_t *siz
void *cmd;
cmd = amdxdna_cmd_get_payload(cmd_bo, &cmd_len);
+ if (!cmd)
+ return -EINVAL;
if (*size < sizeof(*npu_slot) + cmd_len)
return -EINVAL;
@@ -731,6 +737,8 @@ aie2_cmdlist_fill_npu_dpu(struct amdxdna_gem_obj *cmd_bo, void *slot, size_t *si
u32 arg_sz;
sn = amdxdna_cmd_get_payload(cmd_bo, &cmd_len);
+ if (!sn)
+ return -EINVAL;
arg_sz = cmd_len - sizeof(*sn);
if (cmd_len < sizeof(*sn) || arg_sz > MAX_NPU_ARGS_SIZE)
return -EINVAL;
@@ -763,6 +771,8 @@ aie2_cmdlist_fill_npu_preempt(struct amdxdna_gem_obj *cmd_bo, void *slot, size_t
u32 arg_sz;
pd = amdxdna_cmd_get_payload(cmd_bo, &cmd_len);
+ if (!pd)
+ return -EINVAL;
arg_sz = cmd_len - sizeof(*pd);
if (cmd_len < sizeof(*pd) || arg_sz > MAX_NPU_ARGS_SIZE)
return -EINVAL;
@@ -799,6 +809,8 @@ aie2_cmdlist_fill_npu_elf(struct amdxdna_gem_obj *cmd_bo, void *slot, size_t *si
u32 arg_sz;
pd = amdxdna_cmd_get_payload(cmd_bo, &cmd_len);
+ if (!pd)
+ return -EINVAL;
arg_sz = cmd_len - sizeof(*pd);
if (cmd_len < sizeof(*pd) || arg_sz > MAX_NPU_ARGS_SIZE)
return -EINVAL;
--
2.43.0
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH] accel/amdxdna: Validate command BO size and payload in amdxdna_ctx
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 ` Bilal Hashim
2026-11-30 22:20 ` Lizhi Hou
2026-11-30 21:36 ` [PATCH] accel/amdxdna: Fix NULL pointer dereference and u32 underflow in aie2_message Lizhi Hou
1 sibling, 1 reply; 4+ messages in thread
From: Bilal Hashim @ 2026-11-28 14:05 UTC (permalink / raw)
To: Min Ma, Lizhi Hou, Oded Gabbay; +Cc: dri-devel, linux-kernel, Bilal Hashim
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>
---
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;
}
}
--
2.43.0
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] accel/amdxdna: Fix NULL pointer dereference and u32 underflow in aie2_message
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 21:36 ` Lizhi Hou
1 sibling, 0 replies; 4+ messages in thread
From: Lizhi Hou @ 2026-11-30 21:36 UTC (permalink / raw)
To: Bilal Hashim, Min Ma, Oded Gabbay; +Cc: dri-devel, linux-kernel
On 11/28/26 06:05, Bilal Hashim wrote:
> amdxdna_cmd_get_payload() returns NULL and sets *size = 0 when the
> user-supplied command header in the shared command BO has an invalid
> AMDXDNA_CMD_COUNT (for example, count <= num_masks or exceeding the BO
> size), or if amdxdna_gem_vmap() fails.
>
> However, the eight command payload helpers in aie2_message.c do not
> check the return value of amdxdna_cmd_get_payload() for NULL before
> dereferencing it or passing it to memcpy():
>
> 1. In aie2_init_exec_dpu_req():
> - `if (cmd_len - sizeof(*sn) > sizeof(dpu_req->payload))` performs an
> unsigned 32-bit subtraction (`u32 cmd_len - sizeof(*sn)`). If
> `cmd_len < sizeof(*sn)` (for instance, `cmd_len == 4`), on 64-bit
> architectures `sizeof(*sn)` is `size_t`, so `(size_t)cmd_len -
> sizeof(*sn)` wraps around and is rejected, unless `cmd_len` is 0
> (when `sn == NULL`) or if `sn` is dereferenced when `cmd_len` is
> valid for the check on 32-bit platforms. More importantly, when
> `cmd_len < sizeof(*sn)` or `sn == NULL`, `cmd_len < sizeof(*sn)`
> is not explicitly checked first.
> 2. In aie2_init_exec_cu_req(), aie2_cmdlist_fill_cf(), and
> aie2_cmdlist_fill_npu_cf():
> - When `amdxdna_cmd_get_payload()` returns `NULL` with `cmd_len == 0`,
> the size check (`cmd_len > ...` or `*size < sizeof(...) + cmd_len`)
> passes, and if `amdxdna_cmd_get_cu_idx()` succeeds (because the CU
> mask word in the command BO is non-zero), `cmd` (`NULL`) is passed
> to `memcpy()`, or in future/non-zero paths dereferenced directly.
> 3. Similarly, in aie2_cmdlist_fill_dpu(), aie2_cmdlist_fill_npu_dpu(),
> aie2_cmdlist_fill_npu_preempt(), and aie2_cmdlist_fill_npu_elf(),
> checking `!sn` / `!pd` right after `amdxdna_cmd_get_payload()` makes
> the error handling explicit and consistent with
> aie2_cmdlist_multi_execbuf().
>
> Add explicit NULL checks after every amdxdna_cmd_get_payload() call and
> check `cmd_len < sizeof(*sn)` in aie2_init_exec_dpu_req() before
> subtracting `sizeof(*sn)`.
The commit message doesn't match the code. All eight call sites are reached
via aie2_init_exec_req() / aie2_cmdlist_fill_slot(), both of which call
amdxdna_cmd_get_op() first that vmaps the BO and returns ERT_INVALID_CMD
on failure, so the vmap in amdxdna_cmd_get_payload() always hits the cached
abo->mem.kva. The only NULL return left is the bad-AMDXDNA_CMD_COUNT path,
which sets *size = 0, and every site that dereferences the pointer already
rejects cmd_len == 0 via cmd_len < sizeof(*sn).
The cmd_len - sizeof(*sn) subtraction promotes to size_t and wraps to a huge
value, which makes the check reject the command. That's the safe direction,
not an underflow bug.
Lizhi
>
> Signed-off-by: Bilal Hashim <AeeSec@126.com>
> ---
> drivers/accel/amdxdna/aie2_message.c | 18 ++++++++++++++++--
> 1 file changed, 16 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/accel/amdxdna/aie2_message.c b/drivers/accel/amdxdna/aie2_message.c
> index 8fa123456789..9ab234567890 100644
> --- a/drivers/accel/amdxdna/aie2_message.c
> +++ b/drivers/accel/amdxdna/aie2_message.c
> @@ -556,7 +556,7 @@ static int aie2_init_exec_cu_req(struct amdxdna_gem_obj *cmd_bo, void *req,
> void *cmd;
>
> cmd = amdxdna_cmd_get_payload(cmd_bo, &cmd_len);
> - if (cmd_len > sizeof(cu_req->payload))
> + if (!cmd || cmd_len > sizeof(cu_req->payload))
> return -EINVAL;
>
> cu_req->cu_idx = amdxdna_cmd_get_cu_idx(cmd_bo);
> @@ -578,7 +578,7 @@ static int aie2_init_exec_dpu_req(struct amdxdna_gem_obj *cmd_bo, void *req,
> u32 cmd_len;
>
> sn = amdxdna_cmd_get_payload(cmd_bo, &cmd_len);
> - if (cmd_len - sizeof(*sn) > sizeof(dpu_req->payload))
> + if (!sn || cmd_len < sizeof(*sn) || cmd_len - sizeof(*sn) > sizeof(dpu_req->payload))
> return -EINVAL;
>
> dpu_req->cu_idx = amdxdna_cmd_get_cu_idx(cmd_bo);
> @@ -623,6 +623,8 @@ aie2_cmdlist_fill_cf(struct amdxdna_gem_obj *cmd_bo, void *slot, size_t *size)
> void *cmd;
>
> cmd = amdxdna_cmd_get_payload(cmd_bo, &cmd_len);
> + if (!cmd)
> + return -EINVAL;
> if (*size < sizeof(*cf_slot) + cmd_len)
> return -EINVAL;
>
> @@ -646,6 +648,8 @@ aie2_cmdlist_fill_dpu(struct amdxdna_gem_obj *cmd_bo, void *slot, size_t *size)
> u32 arg_sz;
>
> sn = amdxdna_cmd_get_payload(cmd_bo, &cmd_len);
> + if (!sn)
> + return -EINVAL;
> arg_sz = cmd_len - sizeof(*sn);
> if (cmd_len < sizeof(*sn) || arg_sz > MAX_DPU_ARGS_SIZE)
> return -EINVAL;
> @@ -706,6 +710,8 @@ aie2_cmdlist_fill_npu_cf(struct amdxdna_gem_obj *cmd_bo, void *slot, size_t *siz
> void *cmd;
>
> cmd = amdxdna_cmd_get_payload(cmd_bo, &cmd_len);
> + if (!cmd)
> + return -EINVAL;
> if (*size < sizeof(*npu_slot) + cmd_len)
> return -EINVAL;
>
> @@ -731,6 +737,8 @@ aie2_cmdlist_fill_npu_dpu(struct amdxdna_gem_obj *cmd_bo, void *slot, size_t *si
> u32 arg_sz;
>
> sn = amdxdna_cmd_get_payload(cmd_bo, &cmd_len);
> + if (!sn)
> + return -EINVAL;
> arg_sz = cmd_len - sizeof(*sn);
> if (cmd_len < sizeof(*sn) || arg_sz > MAX_NPU_ARGS_SIZE)
> return -EINVAL;
> @@ -763,6 +771,8 @@ aie2_cmdlist_fill_npu_preempt(struct amdxdna_gem_obj *cmd_bo, void *slot, size_t
> u32 arg_sz;
>
> pd = amdxdna_cmd_get_payload(cmd_bo, &cmd_len);
> + if (!pd)
> + return -EINVAL;
> arg_sz = cmd_len - sizeof(*pd);
> if (cmd_len < sizeof(*pd) || arg_sz > MAX_NPU_ARGS_SIZE)
> return -EINVAL;
> @@ -799,6 +809,8 @@ aie2_cmdlist_fill_npu_elf(struct amdxdna_gem_obj *cmd_bo, void *slot, size_t *si
> u32 arg_sz;
>
> pd = amdxdna_cmd_get_payload(cmd_bo, &cmd_len);
> + if (!pd)
> + return -EINVAL;
> arg_sz = cmd_len - sizeof(*pd);
> if (cmd_len < sizeof(*pd) || arg_sz > MAX_NPU_ARGS_SIZE)
> return -EINVAL;
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] accel/amdxdna: Validate command BO size and payload in amdxdna_ctx
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
0 siblings, 0 replies; 4+ messages in thread
From: Lizhi Hou @ 2026-11-30 22:20 UTC (permalink / raw)
To: Bilal Hashim, Min Ma, Oded Gabbay; +Cc: dri-devel, linux-kernel
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;
> }
> }
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-11-30 22:20 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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
2026-11-30 21:36 ` [PATCH] accel/amdxdna: Fix NULL pointer dereference and u32 underflow in aie2_message Lizhi Hou
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®