Skip to contentSkip to conversation

cxl/mem: Force array size of mem_commands[] to CXL_MEM_COMMAND_ID_MAX

PATCH Applied Robert Richter 3 participants 5 messages Mar 24, 2021 – Mar 25, 2021 CXL

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 cxl/mem: Force array size of mem_commands[] to CXL_MEM_COMMAND_ID_MAX

Typically the mem_commands[] array is in sync with 'enum { CXL_CMDS }'. Current code works well.

However, the array size of mem_commands[] may not strictly be the same as CXL_MEM_COMMAND_ID_MAX. E.g. if a new CXL_CMD() is added that is guarded by #ifdefs, the array could be shorter. This could lead then further to an out-of-bounds array access in cxl_validate_cmd_from_user().

Fix this by forcing the array size to CXL_MEM_COMMAND_ID_MAX. This also adds range checks for array items in mem_commands[] at compile time.

---
 drivers/cxl/mem.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/cxl/mem.c +1 -1
index 244cb7d89678..ecfc9ccdba8d 100644
@@ -169,7 +169,7 @@ struct cxl_mem_command {
  * table will be validated against the user's input. For example, if size_in is
  * 0, and the user passed in 1, it is an error.
  */
-static struct cxl_mem_command mem_commands[] = {
+static struct cxl_mem_command mem_commands[CXL_MEM_COMMAND_ID_MAX] = {
 	CXL_CMD(IDENTIFY, 0, 0x43, CXL_CMD_FLAG_FORCE_ENABLE),
 #ifdef CONFIG_CXL_MEM_RAW_COMMANDS
 	CXL_CMD(RAW, ~0, ~0, 0),
Signature · 1 line
2.29.2
#1 lore raw
Quoted 11 lines · Robert Richter

Typically the mem_commands[] array is in sync with 'enum { CXL_CMDS }'. Current code works well.

However, the array size of mem_commands[] may not strictly be the same as CXL_MEM_COMMAND_ID_MAX. E.g. if a new CXL_CMD() is added that is guarded by #ifdefs, the array could be shorter. This could lead then further to an out-of-bounds array access in cxl_validate_cmd_from_user().

Fix this by forcing the array size to CXL_MEM_COMMAND_ID_MAX. This also adds range checks for array items in mem_commands[] at compile time.

Can't we use ARRAY_SIZE?

Ira

Quoted 22 lines
---
 drivers/cxl/mem.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/cxl/mem.c +1 -1
index 244cb7d89678..ecfc9ccdba8d 100644
@@ -169,7 +169,7 @@ struct cxl_mem_command {
  * table will be validated against the user's input. For example, if size_in is
  * 0, and the user passed in 1, it is an error.
  */
-static struct cxl_mem_command mem_commands[] = {
+static struct cxl_mem_command mem_commands[CXL_MEM_COMMAND_ID_MAX] = {
 	CXL_CMD(IDENTIFY, 0, 0x43, CXL_CMD_FLAG_FORCE_ENABLE),
 #ifdef CONFIG_CXL_MEM_RAW_COMMANDS
 	CXL_CMD(RAW, ~0, ~0, 0),
Signature · 1 line
2.29.2
#2 lore raw
Quoted 10 lines · Ira Weiny
Quoted 8 lines · Robert Richter

Typically the mem_commands[] array is in sync with 'enum { CXL_CMDS }'. Current code works well.

However, the array size of mem_commands[] may not strictly be the same as CXL_MEM_COMMAND_ID_MAX. E.g. if a new CXL_CMD() is added that is guarded by #ifdefs, the array could be shorter. This could lead then further to an out-of-bounds array access in cxl_validate_cmd_from_user().

Quoted 5 lines
Quoted 3 lines

Fix this by forcing the array size to CXL_MEM_COMMAND_ID_MAX. This also adds range checks for array items in mem_commands[] at compile time.

Can't we use ARRAY_SIZE?

An ARRAY_SIZE() check in cxl_validate_cmd_from_user() would work too, but it wouldn't give the compiler protection that Robert mentions for going the other way where mem_commands tries to add an entry that is out of bounds relative to CXL_CMDS.

#3 lore raw
Quoted 8 lines · Dan Williams
Quoted 1 line · Ira Weiny

Can't we use ARRAY_SIZE?

An ARRAY_SIZE() check in cxl_validate_cmd_from_user() would work too, but it wouldn't give the compiler protection that Robert mentions for going the other way where mem_commands tries to add an entry that is out of bounds relative to CXL_CMDS.

I was considering that too. Another reason apart from above was to treat 'holes' in the array caused by #ifdefs the same regardless its position in the array. Thus, all should show up as being zeroed instead of cutting those at the end from the array.

Thanks for applying,

-Robert

Applied
#4 lore raw
In reply to Robert Richter
Quoted 8 lines · Robert Richter

Typically the mem_commands[] array is in sync with 'enum { CXL_CMDS }'. Current code works well.

However, the array size of mem_commands[] may not strictly be the same as CXL_MEM_COMMAND_ID_MAX. E.g. if a new CXL_CMD() is added that is guarded by #ifdefs, the array could be shorter. This could lead then further to an out-of-bounds array access in cxl_validate_cmd_from_user().

Good catch.

Quoted 6 lines

Fix this by forcing the array size to CXL_MEM_COMMAND_ID_MAX. This also adds range checks for array items in mem_commands[] at compile time.

Thanks, applied.