linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] cxl/mem: Force array size of mem_commands[] to CXL_MEM_COMMAND_ID_MAX
@ 2021-03-24 14:16 Robert Richter
  2021-03-24 18:42 ` Ira Weiny
  2021-03-24 18:54 ` Dan Williams
  0 siblings, 2 replies; 5+ messages in thread
From: Robert Richter @ 2021-03-24 14:16 UTC (permalink / raw)
  To: Alison Schofield, Vishal Verma, Ira Weiny, Ben Widawsky, Dan Williams
  Cc: Robert Richter, linux-cxl, linux-kernel

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.

Signed-off-by: Robert Richter <rrichter@amd.com>
---
 drivers/cxl/mem.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/cxl/mem.c b/drivers/cxl/mem.c
index 244cb7d89678..ecfc9ccdba8d 100644
--- a/drivers/cxl/mem.c
+++ b/drivers/cxl/mem.c
@@ -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),
-- 
2.29.2


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] cxl/mem: Force array size of mem_commands[] to CXL_MEM_COMMAND_ID_MAX
  2021-03-24 14:16 [PATCH] cxl/mem: Force array size of mem_commands[] to CXL_MEM_COMMAND_ID_MAX Robert Richter
@ 2021-03-24 18:42 ` Ira Weiny
  2021-03-24 19:08   ` Dan Williams
  2021-03-24 18:54 ` Dan Williams
  1 sibling, 1 reply; 5+ messages in thread
From: Ira Weiny @ 2021-03-24 18:42 UTC (permalink / raw)
  To: Robert Richter
  Cc: Alison Schofield, Vishal Verma, Ben Widawsky, Dan Williams,
	linux-cxl, linux-kernel

On Wed, Mar 24, 2021 at 03:16:35PM +0100, Robert Richter wrote:
> 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

> 
> Signed-off-by: Robert Richter <rrichter@amd.com>
> ---
>  drivers/cxl/mem.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/cxl/mem.c b/drivers/cxl/mem.c
> index 244cb7d89678..ecfc9ccdba8d 100644
> --- a/drivers/cxl/mem.c
> +++ b/drivers/cxl/mem.c
> @@ -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),
> -- 
> 2.29.2
> 

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] cxl/mem: Force array size of mem_commands[] to CXL_MEM_COMMAND_ID_MAX
  2021-03-24 14:16 [PATCH] cxl/mem: Force array size of mem_commands[] to CXL_MEM_COMMAND_ID_MAX Robert Richter
  2021-03-24 18:42 ` Ira Weiny
@ 2021-03-24 18:54 ` Dan Williams
  1 sibling, 0 replies; 5+ messages in thread
From: Dan Williams @ 2021-03-24 18:54 UTC (permalink / raw)
  To: Robert Richter
  Cc: Alison Schofield, Vishal Verma, Ira Weiny, Ben Widawsky,
	linux-cxl, Linux Kernel Mailing List

On Wed, Mar 24, 2021 at 7:17 AM Robert Richter <rrichter@amd.com> wrote:
>
> 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.

>
> 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.
>
> Signed-off-by: Robert Richter <rrichter@amd.com>

Thanks, applied.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] cxl/mem: Force array size of mem_commands[] to CXL_MEM_COMMAND_ID_MAX
  2021-03-24 18:42 ` Ira Weiny
@ 2021-03-24 19:08   ` Dan Williams
  2021-03-25  8:12     ` Robert Richter
  0 siblings, 1 reply; 5+ messages in thread
From: Dan Williams @ 2021-03-24 19:08 UTC (permalink / raw)
  To: Ira Weiny
  Cc: Robert Richter, Alison Schofield, Vishal Verma, Ben Widawsky,
	linux-cxl, Linux Kernel Mailing List

On Wed, Mar 24, 2021 at 11:43 AM Ira Weiny <ira.weiny@intel.com> wrote:
>
> On Wed, Mar 24, 2021 at 03:16:35PM +0100, Robert Richter wrote:
> > 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?

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.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] cxl/mem: Force array size of mem_commands[] to CXL_MEM_COMMAND_ID_MAX
  2021-03-24 19:08   ` Dan Williams
@ 2021-03-25  8:12     ` Robert Richter
  0 siblings, 0 replies; 5+ messages in thread
From: Robert Richter @ 2021-03-25  8:12 UTC (permalink / raw)
  To: Dan Williams
  Cc: Ira Weiny, Alison Schofield, Vishal Verma, Ben Widawsky,
	linux-cxl, Linux Kernel Mailing List

On 24.03.21 12:08:20, Dan Williams wrote:
> On Wed, Mar 24, 2021 at 11:43 AM Ira Weiny <ira.weiny@intel.com> wrote:

> > 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

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2021-03-25  8:13 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-03-24 14:16 [PATCH] cxl/mem: Force array size of mem_commands[] to CXL_MEM_COMMAND_ID_MAX Robert Richter
2021-03-24 18:42 ` Ira Weiny
2021-03-24 19:08   ` Dan Williams
2021-03-25  8:12     ` Robert Richter
2021-03-24 18:54 ` Dan Williams

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).