Skip to content

Commit 1110a94

Browse files
committed
accel/amdxdna: Fix out-of-bounds memset in command slot handling
The remaining space in a command slot may be smaller than the size of the command header. Clearing the command header with memset() before verifying the available slot space can result in an out-of-bounds write and memory corruption. Fix this by moving the memset() call after the size validation. Fixes: 3d32eb7 ("accel/amdxdna: Fix cu_idx being cleared by memset() during command setup") Reviewed-by: Mario Limonciello (AMD) <superm1@kernel.org> Signed-off-by: Lizhi Hou <lizhi.hou@amd.com> Link: https://patch.msgid.link/20260217185415.1781908-1-lizhi.hou@amd.com
1 parent 07efce5 commit 1110a94

1 file changed

Lines changed: 4 additions & 4 deletions

File tree

drivers/accel/amdxdna/aie2_message.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -699,11 +699,11 @@ aie2_cmdlist_fill_npu_cf(struct amdxdna_gem_obj *cmd_bo, void *slot, size_t *siz
699699
u32 cmd_len;
700700
void *cmd;
701701

702-
memset(npu_slot, 0, sizeof(*npu_slot));
703702
cmd = amdxdna_cmd_get_payload(cmd_bo, &cmd_len);
704703
if (*size < sizeof(*npu_slot) + cmd_len)
705704
return -EINVAL;
706705

706+
memset(npu_slot, 0, sizeof(*npu_slot));
707707
npu_slot->cu_idx = amdxdna_cmd_get_cu_idx(cmd_bo);
708708
if (npu_slot->cu_idx == INVALID_CU_IDX)
709709
return -EINVAL;
@@ -724,7 +724,6 @@ aie2_cmdlist_fill_npu_dpu(struct amdxdna_gem_obj *cmd_bo, void *slot, size_t *si
724724
u32 cmd_len;
725725
u32 arg_sz;
726726

727-
memset(npu_slot, 0, sizeof(*npu_slot));
728727
sn = amdxdna_cmd_get_payload(cmd_bo, &cmd_len);
729728
arg_sz = cmd_len - sizeof(*sn);
730729
if (cmd_len < sizeof(*sn) || arg_sz > MAX_NPU_ARGS_SIZE)
@@ -733,6 +732,7 @@ aie2_cmdlist_fill_npu_dpu(struct amdxdna_gem_obj *cmd_bo, void *slot, size_t *si
733732
if (*size < sizeof(*npu_slot) + arg_sz)
734733
return -EINVAL;
735734

735+
memset(npu_slot, 0, sizeof(*npu_slot));
736736
npu_slot->cu_idx = amdxdna_cmd_get_cu_idx(cmd_bo);
737737
if (npu_slot->cu_idx == INVALID_CU_IDX)
738738
return -EINVAL;
@@ -756,7 +756,6 @@ aie2_cmdlist_fill_npu_preempt(struct amdxdna_gem_obj *cmd_bo, void *slot, size_t
756756
u32 cmd_len;
757757
u32 arg_sz;
758758

759-
memset(npu_slot, 0, sizeof(*npu_slot));
760759
pd = amdxdna_cmd_get_payload(cmd_bo, &cmd_len);
761760
arg_sz = cmd_len - sizeof(*pd);
762761
if (cmd_len < sizeof(*pd) || arg_sz > MAX_NPU_ARGS_SIZE)
@@ -765,6 +764,7 @@ aie2_cmdlist_fill_npu_preempt(struct amdxdna_gem_obj *cmd_bo, void *slot, size_t
765764
if (*size < sizeof(*npu_slot) + arg_sz)
766765
return -EINVAL;
767766

767+
memset(npu_slot, 0, sizeof(*npu_slot));
768768
npu_slot->cu_idx = amdxdna_cmd_get_cu_idx(cmd_bo);
769769
if (npu_slot->cu_idx == INVALID_CU_IDX)
770770
return -EINVAL;
@@ -792,7 +792,6 @@ aie2_cmdlist_fill_npu_elf(struct amdxdna_gem_obj *cmd_bo, void *slot, size_t *si
792792
u32 cmd_len;
793793
u32 arg_sz;
794794

795-
memset(npu_slot, 0, sizeof(*npu_slot));
796795
pd = amdxdna_cmd_get_payload(cmd_bo, &cmd_len);
797796
arg_sz = cmd_len - sizeof(*pd);
798797
if (cmd_len < sizeof(*pd) || arg_sz > MAX_NPU_ARGS_SIZE)
@@ -801,6 +800,7 @@ aie2_cmdlist_fill_npu_elf(struct amdxdna_gem_obj *cmd_bo, void *slot, size_t *si
801800
if (*size < sizeof(*npu_slot) + arg_sz)
802801
return -EINVAL;
803802

803+
memset(npu_slot, 0, sizeof(*npu_slot));
804804
npu_slot->type = EXEC_NPU_TYPE_ELF;
805805
npu_slot->inst_buf_addr = pd->inst_buf;
806806
npu_slot->save_buf_addr = pd->save_buf;

0 commit comments

Comments
 (0)