Skip to contentSkip to conversation

accel/amdxdna: Fix out-of-bounds memset in command slot handling

PATCH In review Lizhi Hou 2 participants 2 messages Feb 17

Subsystems (2)
lore
Share & tools
lore

Lizhi Hou

author maintainer
#0 lore raw

PATCH 1/1 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 3d32eb7a5ecf ("accel/amdxdna: Fix cu_idx being cleared by memset() during command setup")
---
 drivers/accel/amdxdna/aie2_message.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/accel/amdxdna/aie2_message.c +4 -4
index 7d7dcfeaf794..8fbbc3280468 100644
@@ -694,11 +694,11 @@ aie2_cmdlist_fill_npu_cf(struct amdxdna_gem_obj *cmd_bo, void *slot, size_t *siz
 	u32 cmd_len;
 	void *cmd;
 
-	memset(npu_slot, 0, sizeof(*npu_slot));
 	cmd = amdxdna_cmd_get_payload(cmd_bo, &cmd_len);
 	if (*size < sizeof(*npu_slot) + cmd_len)
 		return -EINVAL;
 
+	memset(npu_slot, 0, sizeof(*npu_slot));
 	npu_slot->cu_idx = amdxdna_cmd_get_cu_idx(cmd_bo);
 	if (npu_slot->cu_idx == INVALID_CU_IDX)
 		return -EINVAL;
@@ -719,7 +719,6 @@ aie2_cmdlist_fill_npu_dpu(struct amdxdna_gem_obj *cmd_bo, void *slot, size_t *si
 	u32 cmd_len;
 	u32 arg_sz;
 
-	memset(npu_slot, 0, sizeof(*npu_slot));
 	sn = amdxdna_cmd_get_payload(cmd_bo, &cmd_len);
 	arg_sz = cmd_len - sizeof(*sn);
 	if (cmd_len < sizeof(*sn) || arg_sz > MAX_NPU_ARGS_SIZE)
@@ -728,6 +727,7 @@ aie2_cmdlist_fill_npu_dpu(struct amdxdna_gem_obj *cmd_bo, void *slot, size_t *si
 	if (*size < sizeof(*npu_slot) + arg_sz)
 		return -EINVAL;
 
+	memset(npu_slot, 0, sizeof(*npu_slot));
 	npu_slot->cu_idx = amdxdna_cmd_get_cu_idx(cmd_bo);
 	if (npu_slot->cu_idx == INVALID_CU_IDX)
 		return -EINVAL;
@@ -751,7 +751,6 @@ aie2_cmdlist_fill_npu_preempt(struct amdxdna_gem_obj *cmd_bo, void *slot, size_t
 	u32 cmd_len;
 	u32 arg_sz;
 
-	memset(npu_slot, 0, sizeof(*npu_slot));
 	pd = amdxdna_cmd_get_payload(cmd_bo, &cmd_len);
 	arg_sz = cmd_len - sizeof(*pd);
 	if (cmd_len < sizeof(*pd) || arg_sz > MAX_NPU_ARGS_SIZE)
@@ -760,6 +759,7 @@ aie2_cmdlist_fill_npu_preempt(struct amdxdna_gem_obj *cmd_bo, void *slot, size_t
 	if (*size < sizeof(*npu_slot) + arg_sz)
 		return -EINVAL;
 
+	memset(npu_slot, 0, sizeof(*npu_slot));
 	npu_slot->cu_idx = amdxdna_cmd_get_cu_idx(cmd_bo);
 	if (npu_slot->cu_idx == INVALID_CU_IDX)
 		return -EINVAL;
@@ -787,7 +787,6 @@ aie2_cmdlist_fill_npu_elf(struct amdxdna_gem_obj *cmd_bo, void *slot, size_t *si
 	u32 cmd_len;
 	u32 arg_sz;
 
-	memset(npu_slot, 0, sizeof(*npu_slot));
 	pd = amdxdna_cmd_get_payload(cmd_bo, &cmd_len);
 	arg_sz = cmd_len - sizeof(*pd);
 	if (cmd_len < sizeof(*pd) || arg_sz > MAX_NPU_ARGS_SIZE)
@@ -796,6 +795,7 @@ aie2_cmdlist_fill_npu_elf(struct amdxdna_gem_obj *cmd_bo, void *slot, size_t *si
 	if (*size < sizeof(*npu_slot) + arg_sz)
 		return -EINVAL;
 
+	memset(npu_slot, 0, sizeof(*npu_slot));
 	npu_slot->type = EXEC_NPU_TYPE_ELF;
 	npu_slot->inst_buf_addr = pd->inst_buf;
 	npu_slot->save_buf_addr = pd->save_buf;
Signature · 1 line
2.34.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 9 lines · Lizhi Hou

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 3d32eb7a5ecf ("accel/amdxdna: Fix cu_idx being cleared by memset() during command setup")
Quoted 69 lines
---
  drivers/accel/amdxdna/aie2_message.c | 8 ++++----
  1 file changed, 4 insertions(+), 4 deletions(-)
drivers/accel/amdxdna/aie2_message.c +4 -4
index 7d7dcfeaf794..8fbbc3280468 100644
@@ -694,11 +694,11 @@ aie2_cmdlist_fill_npu_cf(struct amdxdna_gem_obj *cmd_bo, void *slot, size_t *siz
  	u32 cmd_len;
  	void *cmd;
 
-	memset(npu_slot, 0, sizeof(*npu_slot));
  	cmd = amdxdna_cmd_get_payload(cmd_bo, &cmd_len);
  	if (*size < sizeof(*npu_slot) + cmd_len)
  		return -EINVAL;
 
+	memset(npu_slot, 0, sizeof(*npu_slot));
  	npu_slot->cu_idx = amdxdna_cmd_get_cu_idx(cmd_bo);
  	if (npu_slot->cu_idx == INVALID_CU_IDX)
  		return -EINVAL;
@@ -719,7 +719,6 @@ aie2_cmdlist_fill_npu_dpu(struct amdxdna_gem_obj *cmd_bo, void *slot, size_t *si
  	u32 cmd_len;
  	u32 arg_sz;
 
-	memset(npu_slot, 0, sizeof(*npu_slot));
  	sn = amdxdna_cmd_get_payload(cmd_bo, &cmd_len);
  	arg_sz = cmd_len - sizeof(*sn);
  	if (cmd_len < sizeof(*sn) || arg_sz > MAX_NPU_ARGS_SIZE)
@@ -728,6 +727,7 @@ aie2_cmdlist_fill_npu_dpu(struct amdxdna_gem_obj *cmd_bo, void *slot, size_t *si
  	if (*size < sizeof(*npu_slot) + arg_sz)
  		return -EINVAL;
 
+	memset(npu_slot, 0, sizeof(*npu_slot));
  	npu_slot->cu_idx = amdxdna_cmd_get_cu_idx(cmd_bo);
  	if (npu_slot->cu_idx == INVALID_CU_IDX)
  		return -EINVAL;
@@ -751,7 +751,6 @@ aie2_cmdlist_fill_npu_preempt(struct amdxdna_gem_obj *cmd_bo, void *slot, size_t
  	u32 cmd_len;
  	u32 arg_sz;
 
-	memset(npu_slot, 0, sizeof(*npu_slot));
  	pd = amdxdna_cmd_get_payload(cmd_bo, &cmd_len);
  	arg_sz = cmd_len - sizeof(*pd);
  	if (cmd_len < sizeof(*pd) || arg_sz > MAX_NPU_ARGS_SIZE)
@@ -760,6 +759,7 @@ aie2_cmdlist_fill_npu_preempt(struct amdxdna_gem_obj *cmd_bo, void *slot, size_t
  	if (*size < sizeof(*npu_slot) + arg_sz)
  		return -EINVAL;
 
+	memset(npu_slot, 0, sizeof(*npu_slot));
  	npu_slot->cu_idx = amdxdna_cmd_get_cu_idx(cmd_bo);
  	if (npu_slot->cu_idx == INVALID_CU_IDX)
  		return -EINVAL;
@@ -787,7 +787,6 @@ aie2_cmdlist_fill_npu_elf(struct amdxdna_gem_obj *cmd_bo, void *slot, size_t *si
  	u32 cmd_len;
  	u32 arg_sz;
 
-	memset(npu_slot, 0, sizeof(*npu_slot));
  	pd = amdxdna_cmd_get_payload(cmd_bo, &cmd_len);
  	arg_sz = cmd_len - sizeof(*pd);
  	if (cmd_len < sizeof(*pd) || arg_sz > MAX_NPU_ARGS_SIZE)
@@ -796,6 +795,7 @@ aie2_cmdlist_fill_npu_elf(struct amdxdna_gem_obj *cmd_bo, void *slot, size_t *si
  	if (*size < sizeof(*npu_slot) + arg_sz)
  		return -EINVAL;
 
+	memset(npu_slot, 0, sizeof(*npu_slot));
  	npu_slot->type = EXEC_NPU_TYPE_ELF;
  	npu_slot->inst_buf_addr = pd->inst_buf;
  	npu_slot->save_buf_addr = pd->save_buf;