All of lore.kernel.org
 help / color / mirror / Atom feed
From: Bilal Hashim <AeeSec@126.com>
To: Min Ma <mamin506@gmail.com>, Lizhi Hou <lizhi.hou@amd.com>,
	Oded Gabbay <ogabbay@kernel.org>
Cc: dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org,
	Bilal Hashim <AeeSec@126.com>
Subject: [PATCH] accel/amdxdna: Fix NULL pointer dereference and u32 underflow in aie2_message
Date: Sat, 28 Nov 2026 22:05:36 +0800	[thread overview]
Message-ID: <20261128140537.1325-1-AeeSec@126.com> (raw)

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


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

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-28 14:05 Bilal Hashim [this message]
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
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=20261128140537.1325-1-AeeSec@126.com \
    --to=aeesec@126.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lizhi.hou@amd.com \
    --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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.