From: Eva Crystal <0xiviel@gmail.com>
To: Min Ma <mamin506@gmail.com>, Lizhi Hou <lizhi.hou@amd.com>
Cc: dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org,
Eva Crystal <0xiviel@gmail.com>
Subject: [PATCH 2/4] accel/amdxdna: bound the command error payload length
Date: Sat, 12 Sep 2026 20:10:10 +1200 [thread overview]
Message-ID: <20260912081012.2274075-3-0xiviel@gmail.com> (raw)
In-Reply-To: <20260912081012.2274075-1-0xiviel@gmail.com>
amdxdna_cmd_set_error() computes the length of the region it scribbles
over from the BO size, without a floor:
memset(cmd->data, 0xff, abo->mem.size - sizeof(*cmd));
if (err_data)
memcpy(cmd->data, err_data, min(size, abo->mem.size - sizeof(*cmd)));
abo->mem.size is a size_t and sizeof(struct amdxdna_cmd) is 4 - the
struct is a u32 header followed by a flexible array. A BO smaller than
four bytes therefore turns both lengths into a value near SIZE_MAX, and
the min() in the memcpy offers no protection because the underflowed
value is the larger operand.
No such BO can reach this function today. Command BOs are created by
drm_gem_shmem_create(), which PAGE_ALIGN()s the size, so mem.size is
either 0 or at least PAGE_SIZE. Zero is reachable - PAGE_ALIGN() wraps
for sizes above ULLONG_MAX - PAGE_SIZE + 1, and nothing rejects it on
the share-BO path - but a zero-sized BO cannot be vmap()ed, because
vmap() refuses a zero-page mapping, so amdxdna_gem_vmap() returns NULL
and the !cmd test above rejects the BO before the subtraction. This is
not a fix for a reachable bug.
That leaves an unguarded size_t subtraction feeding a memset() length,
whose safety depends on a property of a different allocator and on
vmap()'s behaviour for a zero-page request. Compute the length once,
reject a BO too small to hold the header, and use the result for both
the memset() and the memcpy() bound.
Signed-off-by: Eva Crystal <0xiviel@gmail.com>
---
drivers/accel/amdxdna/amdxdna_ctx.c | 12 ++++++++++--
1 file changed, 10 insertions(+), 2 deletions(-)
diff --git a/drivers/accel/amdxdna/amdxdna_ctx.c b/drivers/accel/amdxdna/amdxdna_ctx.c
index 163b5fc..c24bf1c 100644
--- a/drivers/accel/amdxdna/amdxdna_ctx.c
+++ b/drivers/accel/amdxdna/amdxdna_ctx.c
@@ -152,6 +152,7 @@ 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;
+ size_t data_size;
if (!cmd)
return -ENOMEM;
@@ -173,9 +174,16 @@ int amdxdna_cmd_set_error(struct amdxdna_gem_obj *abo,
return -ENOMEM;
}
- memset(cmd->data, 0xff, abo->mem.size - sizeof(*cmd));
+ if (abo->mem.size < sizeof(*cmd)) {
+ if (cc)
+ amdxdna_gem_put_obj(abo);
+ return -EINVAL;
+ }
+ data_size = abo->mem.size - sizeof(*cmd);
+
+ memset(cmd->data, 0xff, data_size);
if (err_data)
- memcpy(cmd->data, err_data, min(size, abo->mem.size - sizeof(*cmd)));
+ memcpy(cmd->data, err_data, min(size, data_size));
if (cc)
amdxdna_gem_put_obj(abo);
--
2.53.0
next prev parent reply other threads:[~2026-09-12 8:10 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-12 8:10 [PATCH 0/4] accel/amdxdna: harden command BO payload validation Eva Crystal
2026-09-12 8:10 ` [PATCH 1/4] accel/amdxdna: validate the command payload regardless of the size argument Eva Crystal
2026-09-12 8:10 ` Eva Crystal [this message]
2026-09-12 8:10 ` [PATCH 3/4] accel/amdxdna: release the chained command BO when vmap fails Eva Crystal
2026-09-12 8:10 ` [PATCH 4/4] accel/amdxdna: check the command payload before using it in the exec requests Eva Crystal
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=20260912081012.2274075-3-0xiviel@gmail.com \
--to=0xiviel@gmail.com \
--cc=dri-devel@lists.freedesktop.org \
--cc=linux-kernel@vger.kernel.org \
--cc=lizhi.hou@amd.com \
--cc=mamin506@gmail.com \
/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®