Do not allow set-partition immediate mode
[email protected]
maintainerv5 cover Do not allow set-partition immediate mode
From: Alison Schofield <[email protected]>
Blocking immediate mode in set-partition info triggered a refactoring of the send path of userspace mailbox commands.
The v1 to address the issue was a single patch [1] that inserted a new immediate mode check in the send path where the payload was available for examining. That was not in a validation function.
Revisions after v1 included the refactoring of the send path so that validation work all spawns from cxl_validate_cmd_from_user().
Patches 1-7: Refactor existing code so that all validation work
can spawn from cxl_validate_cmd_from_user().
The movement intends to cleanly rip the work of building a mailbox command away from handle_mailbox_command_from_user() and give it to cxl_validate_cmd_from_user().
Patch 8: Blocks the immediate mode of the set-partition command. Patch 9: Removes CXL_PMEM exclusive commands restriction.
[1]https://lore.kernel.org/linux-cxl/[email protected]/
Changes in v5:
- Put destination params first in function param lists. (Dan)
- Renamed the cxl_to_mbox_cmd() to cxl_mbox_cmd_ctor() and added a deconstruct counterpart cxl_mbox_cmd_dtor() to clean up mbox allocations. (Dan)
- Added Dan's Reviewed-by tag to Patches 1-6.
Changes in v4: (All from Jonathan's review - Thank-you Jonathan)
- Add a blank line after return
- Combine cxl_mem_command and info declaration and init.
- Replace "rc =" with direct returns in cxl_validate_cmd_from_user()
- Don't init and then overwrite out_size
- Rm the 'refactor foreshadowing' goto & don't restore it later
- Wordsmithing in Patch 4 commit log
- Mv error path to be out of line, normal path inline.
- Move kvfree()'s from handle_mbox...() to cxl_send_cmd() Patches 1,2,4,5,6,7,8: Added Jonathan Reviewed-by Tags. Patches 2,7,8,9: No code changes
Changes in v3:
- Split up the 'Centralize the validation...' patch into 6 pieces.
Patch: cxl/mbox: Move cxl_mem_command construction to helper funcs
- Safely initialize the cxl_mem_command structs. (Dan)
- Remove unneeded memcpy (Dan)
Patch: cxl/mbox: Block immediate mode in SET_PARTITION_INFO command
- No Changes
Patch: cxl/pmem: Remove CXL SET_PARTITION_INFO from exclusive_cmds list
- No Changes
Changes in v2:
- Refactor the send path of userspace mbox cmds. (Dan, Ben)
- Patch 3 commit log - update the need to block. (Dan)
- Return -EBUSY (not -EINVAL), when blocking immediate mode. (Ben)
- Remove unneeded cast of void (payload_in). (dan)
- s/u64/__le64 in struct cxl_mbox_set_partition_info. (Dan)
Alison Schofield (9):
cxl/mbox: Move cxl_mem_command construction to helper funcs cxl/mbox: Move raw command warning to raw command validation cxl/mbox: Move build of user mailbox cmd to a helper functions cxl/mbox: Construct a users cxl_mbox_cmd in the validation path cxl/mbox: Remove dependency on cxl_mem_command for a debug msg cxl/mbox: Make handle_mailbox_cmd_from_user() use a mbox param cxl/mbox: Move cxl_mem_command param to a local variable cxl/mbox: Block immediate mode in SET_PARTITION_INFO command cxl/pmem: Remove CXL SET_PARTITION_INFO from exclusive_cmds list
drivers/cxl/core/mbox.c | 339 ++++++++++++++++++++++++---------------- drivers/cxl/cxlmem.h | 7 + drivers/cxl/pmem.c | 1 - 3 files changed, 214 insertions(+), 133 deletions(-)
base-commit: 9b688fc651b9d2b633e8d959454670aba1c39162
Signature · 1 line
2.31.1
[email protected]
maintainerPATCH v5 1/9 cxl/mbox: Move cxl_mem_command construction to helper funcs
From: Alison Schofield <[email protected]>
Sanitizing and constructing a cxl_mem_command from a userspace command is part of the validation process prior to submitting the command to a CXL device. Move this work to helper functions:cxl_to_mem_cmd(), cxl_to_mem_cmd_raw().
This declutters cxl_validate_cmd_from_user() in preparation for adding new validation steps.
--- drivers/cxl/core/mbox.c | 155 +++++++++++++++++++++------------------- 1 file changed, 80 insertions(+), 75 deletions(-)
drivers/cxl/core/mbox.c +80 -75
@@ -207,6 +207,81 @@ static bool cxl_mem_raw_command_allowed(u16 opcode)return true; } +static int cxl_to_mem_cmd_raw (struct cxl_mem_command *mem_cmd, +const struct cxl_send_command *send_cmd, +struct cxl_dev_state *cxlds) +{ +if (send_cmd->raw.rsvd) +return -EINVAL ; + +/* +* Unlike supported commands, the output size of RAW commands +* gets passed along without further checking, so it must be +* validated here. +*/ +if (send_cmd->out.size > cxlds->payload_size) +return -EINVAL ; + +if (!cxl_mem_raw_command_allowed (send_cmd->raw.opcode)) +return -EPERM ; + + *mem_cmd = (struct cxl_mem_command ) { + .info = { + .id =CXL_MEM_COMMAND_ID_RAW , + .size_in = send_cmd->in.size, + .size_out = send_cmd->out.size, + }, + .opcode = send_cmd->raw.opcode + }; + +return 0 ; +} + +static int cxl_to_mem_cmd (struct cxl_mem_command *mem_cmd, +const struct cxl_send_command *send_cmd, +struct cxl_dev_state *cxlds) +{ +struct cxl_mem_command *c = &cxl_mem_commands[send_cmd->id]; +const struct cxl_command_info *info = &c->info; + +if (send_cmd->flags & ~CXL_MEM_COMMAND_FLAG_MASK ) +return -EINVAL ; + +if (send_cmd->rsvd) +return -EINVAL ; + +if (send_cmd->in.rsvd || send_cmd->out.rsvd) +return -EINVAL ; + +/* Check that the command is enabled for hardware */ +if (!test_bit (info->id, cxlds->enabled_cmds)) +return -ENOTTY ; + +/* Check that the command is not claimed for exclusive kernel use */ +if (test_bit (info->id, cxlds->exclusive_cmds)) +return -EBUSY ; + +/* Check the input buffer is the expected size */ +if (info->size_in >=0 && info->size_in != send_cmd->in.size) +return -ENOMEM ; + +/* Check the output buffer is at least large enough */ +if (info->size_out >=0 && send_cmd->out.size < info->size_out) +return -ENOMEM ; + + *mem_cmd = (struct cxl_mem_command ) { + .info = { + .id = info->id, + .flags = info->flags, + .size_in = send_cmd->in.size, + .size_out = send_cmd->out.size, + }, + .opcode = c->opcode + }; + +return 0 ; +} +/** * cxl_validate_cmd_from_user() - Check fields for CXL_MEM_SEND_COMMAND. * @cxlds: The device data for the operation
@@ -230,9 +305,6 @@ static int cxl_validate_cmd_from_user(struct cxl_dev_state *cxlds, const struct cxl_send_command *send_cmd, struct cxl_mem_command *out_cmd) { - const struct cxl_command_info *info; -struct cxl_mem_command *c; - if (send_cmd->id == 0 || send_cmd->id >= CXL_MEM_COMMAND_ID_MAX) return -ENOTTY;
@@ -244,78 +316,11 @@ static int cxl_validate_cmd_from_user(struct cxl_dev_state *cxlds,if (send_cmd->in.size > cxlds->payload_size)return -EINVAL ; -/* -* Checks are bypassed for raw commands but a WARN/taint will occur -* later in the callchain -*/ -if (send_cmd->id ==CXL_MEM_COMMAND_ID_RAW ) { -const struct cxl_mem_command temp = { - .info = { - .id =CXL_MEM_COMMAND_ID_RAW , - .flags =0 , - .size_in = send_cmd->in.size, - .size_out = send_cmd->out.size, - }, - .opcode = send_cmd->raw.opcode - }; - -if (send_cmd->raw.rsvd) -return -EINVAL ; - -/* -* Unlike supported commands, the output size of RAW commands -* gets passed along without further checking, so it must be -* validated here. -*/ -if (send_cmd->out.size > cxlds->payload_size) -return -EINVAL ; - -if (!cxl_mem_raw_command_allowed (send_cmd->raw.opcode)) -return -EPERM ; - -memcpy (out_cmd, &temp,sizeof (temp)); - -return 0 ; - } - -if (send_cmd->flags & ~CXL_MEM_COMMAND_FLAG_MASK ) -return -EINVAL ; - -if (send_cmd->rsvd) -return -EINVAL ; - -if (send_cmd->in.rsvd || send_cmd->out.rsvd) -return -EINVAL ; - -/* Convert user's command into the internal representation */ - c = &cxl_mem_commands[send_cmd->id]; - info = &c->info; - -/* Check that the command is enabled for hardware */ -if (!test_bit (info->id, cxlds->enabled_cmds)) -return -ENOTTY ; - -/* Check that the command is not claimed for exclusive kernel use */ -if (test_bit (info->id, cxlds->exclusive_cmds)) -return -EBUSY ; - -/* Check the input buffer is the expected size */ -if (info->size_in >=0 && info->size_in != send_cmd->in.size) -return -ENOMEM ; - -/* Check the output buffer is at least large enough */ -if (info->size_out >=0 && send_cmd->out.size < info->size_out) -return -ENOMEM ; - -memcpy (out_cmd, c,sizeof (*c)); - out_cmd->info.size_in = send_cmd->in.size; -/* -* XXX: out_cmd->info.size_out will be controlled by the driver, and the -* specified number of bytes @send_cmd->out.size will be copied back out -* to userspace. -*/ - -return 0 ; +/* Sanitize and construct a cxl_mem_command */ +if (send_cmd->id ==CXL_MEM_COMMAND_ID_RAW ) +return cxl_to_mem_cmd_raw (out_cmd, send_cmd, cxlds); +else +return cxl_to_mem_cmd (out_cmd, send_cmd, cxlds); } int cxl_query_cmd(struct cxl_memdev *cxlmd,
Signature · 1 line
2.31.1
[email protected]
maintainerPATCH v5 2/9 cxl/mbox: Move raw command warning to raw command validation
In reply to [email protected]From: Alison Schofield <[email protected]>
This move serves two purposes: 1) Emit the warning in the raw command validation path, and 2) Remove the dependency on the struct cxl_mem_command in handle_mailbox_cmd_from_user() in preparation for a refactor of that function.
--- drivers/cxl/core/mbox.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
drivers/cxl/core/mbox.c +2 -3
@@ -225,6 +225,8 @@ static int cxl_to_mem_cmd_raw(struct cxl_mem_command *mem_cmd,if (!cxl_mem_raw_command_allowed (send_cmd->raw.opcode))return -EPERM ; +dev_WARN_ONCE (cxlds->dev, true,"raw command path used\n" ); + *mem_cmd = (struct cxl_mem_command ) { .info = { .id =CXL_MEM_COMMAND_ID_RAW ,
@@ -417,9 +419,6 @@ static int handle_mailbox_cmd_from_user(struct cxl_dev_state *cxlds, cxl_command_names[cmd->info.id].name, mbox_cmd.opcode, cmd->info.size_in); -dev_WARN_ONCE (dev, cmd->info.id ==CXL_MEM_COMMAND_ID_RAW , -"raw command path used\n" ); - rc = cxlds->mbox_send (cxlds, &mbox_cmd);if (rc) goto out;
Signature · 1 line
2.31.1
[email protected]
maintainerPATCH v5 3/9 cxl/mbox: Move build of user mailbox cmd to a helper functions
In reply to [email protected]From: Alison Schofield <[email protected]>
In preparation for moving the construction of a mailbox command to the validation path, extract the work into a helper functions.
--- drivers/cxl/core/mbox.c | 70 ++++++++++++++++++++++++++--------------- 1 file changed, 45 insertions(+), 25 deletions(-)
drivers/cxl/core/mbox.c +45 -25
@@ -207,6 +207,44 @@ static bool cxl_mem_raw_command_allowed(u16 opcode)return true; } +static int cxl_mbox_cmd_ctor (struct cxl_mbox_cmd *mbox, +struct cxl_dev_state *cxlds,u16 opcode, +size_t in_size,size_t out_size,u64 in_payload) +{ + *mbox = (struct cxl_mbox_cmd ) { + .opcode = opcode, + .size_in = in_size, + }; + +if (in_size) { + mbox->payload_in =vmemdup_user (u64_to_user_ptr (in_payload), + in_size); +if (!mbox->payload_in) +return PTR_ERR (mbox->payload_in); + } + +/* Prepare to handle a full payload for variable sized output */ +if (out_size <0 ) + mbox->size_out = cxlds->payload_size; +else + mbox->size_out = out_size; + +if (mbox->size_out) { + mbox->payload_out =kvzalloc (mbox->size_out,GFP_KERNEL ); +if (!mbox->payload_out) { +kvfree (mbox->payload_in); +return -ENOMEM ; + } + } +return 0 ; +} + +static void cxl_mbox_cmd_dtor (struct cxl_mbox_cmd *mbox) +{ +kvfree (mbox->payload_in); +kvfree (mbox->payload_out); +} +static int cxl_to_mem_cmd_raw (struct cxl_mem_command *mem_cmd,const struct cxl_send_command *send_cmd,struct cxl_dev_state *cxlds)
@@ -390,27 +428,14 @@ static int handle_mailbox_cmd_from_user(struct cxl_dev_state *cxlds, s32 *size_out, u32 *retval) {struct device *dev = cxlds->dev; -struct cxl_mbox_cmd mbox_cmd = { - .opcode = cmd->opcode, - .size_in = cmd->info.size_in, - .size_out = cmd->info.size_out, - }; +struct cxl_mbox_cmd mbox_cmd;int rc; -if (cmd->info.size_out) { - mbox_cmd.payload_out =kvzalloc (cmd->info.size_out,GFP_KERNEL ); -if (!mbox_cmd.payload_out) -return -ENOMEM ; - } - -if (cmd->info.size_in) { - mbox_cmd.payload_in =vmemdup_user (u64_to_user_ptr (in_payload), - cmd->info.size_in); -if (IS_ERR (mbox_cmd.payload_in)) { -kvfree (mbox_cmd.payload_out); -return PTR_ERR (mbox_cmd.payload_in); - } - } + rc =cxl_mbox_cmd_ctor (&mbox_cmd, cxlds, cmd->opcode, + cmd->info.size_in, cmd->info.size_out, + in_payload); +if (rc) +return rc;dev_dbg (dev, "Submitting %s command for user\n"
@@ -442,8 +467,7 @@ static int handle_mailbox_cmd_from_user(struct cxl_dev_state *cxlds, *retval = mbox_cmd.return_code; out: -kvfree (mbox_cmd.payload_in); -kvfree (mbox_cmd.payload_out); +cxl_mbox_cmd_dtor (&mbox_cmd);return rc; }
@@ -464,10 +488,6 @@ int cxl_send_cmd(struct cxl_memdev *cxlmd, struct cxl_send_command __user *s)if (rc)return rc; -/* Prepare to handle a full payload for variable sized output */ -if (c.info.size_out <0 ) - c.info.size_out = cxlds->payload_size; - rc =handle_mailbox_cmd_from_user (cxlds, &c, send.in.payload, send.out.payload, &send.out.size, &send.retval);
Signature · 1 line
2.31.1
Re: [PATCH v5 3/9] cxl/mbox: Move build of user mailbox cmd to a helper functions
Quoted 7 lines · On Wed, 30 Mar 2022 18:27:13 -0700 [email protected] wrote:
From: Alison Schofield <[email protected]>
In preparation for moving the construction of a mailbox command to the validation path, extract the work into a helper functions.
Signed-off-by Alison Schofield <[email protected]>Reviewed-by Dan Williams <[email protected]>
Quoted 108 lines
--- drivers/cxl/core/mbox.c | 70 ++++++++++++++++++++++++++--------------- 1 file changed, 45 insertions(+), 25 deletions(-)drivers/cxl/core/mbox.c +45
-25@@ -207,6 +207,44 @@ static bool cxl_mem_raw_command_allowed(u16 opcode)return true; } +static int cxl_mbox_cmd_ctor (struct cxl_mbox_cmd *mbox, +struct cxl_dev_state *cxlds,u16 opcode, +size_t in_size,size_t out_size,u64 in_payload) +{ + *mbox = (struct cxl_mbox_cmd ) { + .opcode = opcode, + .size_in = in_size, + }; + +if (in_size) { + mbox->payload_in =vmemdup_user (u64_to_user_ptr (in_payload), + in_size); +if (!mbox->payload_in) +return PTR_ERR (mbox->payload_in); + } + +/* Prepare to handle a full payload for variable sized output */ +if (out_size <0 ) + mbox->size_out = cxlds->payload_size; +else + mbox->size_out = out_size; + +if (mbox->size_out) { + mbox->payload_out =kvzalloc (mbox->size_out,GFP_KERNEL ); +if (!mbox->payload_out) { +kvfree (mbox->payload_in); +return -ENOMEM ; + } + } +return 0 ; +} + +static void cxl_mbox_cmd_dtor (struct cxl_mbox_cmd *mbox) +{ +kvfree (mbox->payload_in); +kvfree (mbox->payload_out); +} +static int cxl_to_mem_cmd_raw (struct cxl_mem_command *mem_cmd,const struct cxl_send_command *send_cmd,struct cxl_dev_state *cxlds)@@ -390,27 +428,14 @@ static int handle_mailbox_cmd_from_user(struct cxl_dev_state *cxlds, s32 *size_out, u32 *retval) {struct device *dev = cxlds->dev; -struct cxl_mbox_cmd mbox_cmd = { - .opcode = cmd->opcode, - .size_in = cmd->info.size_in, - .size_out = cmd->info.size_out, - }; +struct cxl_mbox_cmd mbox_cmd;int rc; -if (cmd->info.size_out) { - mbox_cmd.payload_out =kvzalloc (cmd->info.size_out,GFP_KERNEL ); -if (!mbox_cmd.payload_out) -return -ENOMEM ; - } - -if (cmd->info.size_in) { - mbox_cmd.payload_in =vmemdup_user (u64_to_user_ptr (in_payload), - cmd->info.size_in); -if (IS_ERR (mbox_cmd.payload_in)) { -kvfree (mbox_cmd.payload_out); -return PTR_ERR (mbox_cmd.payload_in); - } - } + rc =cxl_mbox_cmd_ctor (&mbox_cmd, cxlds, cmd->opcode, + cmd->info.size_in, cmd->info.size_out, + in_payload); +if (rc) +return rc;dev_dbg (dev, "Submitting %s command for user\n"@@ -442,8 +467,7 @@ static int handle_mailbox_cmd_from_user(struct cxl_dev_state *cxlds, *retval = mbox_cmd.return_code; out: -kvfree (mbox_cmd.payload_in); -kvfree (mbox_cmd.payload_out); +cxl_mbox_cmd_dtor (&mbox_cmd);return rc; }@@ -464,10 +488,6 @@ int cxl_send_cmd(struct cxl_memdev *cxlmd, struct cxl_send_command __user *s)if (rc)return rc; -/* Prepare to handle a full payload for variable sized output */ -if (c.info.size_out <0 ) - c.info.size_out = cxlds->payload_size; - rc =handle_mailbox_cmd_from_user (cxlds, &c, send.in.payload, send.out.payload, &send.out.size, &send.retval);
[email protected]
maintainerPATCH v5 4/9 cxl/mbox: Construct a users cxl_mbox_cmd in the validation path
In reply to [email protected]From: Alison Schofield <[email protected]>
This is a step in refactoring the handling of user space mailbox commands. The intent is to have all the validation work originate in cxl_validate_cmd_from_user().
Move the construction and validation of a mailbox command to the validation path. Continue to pass both the out_cmd and the mbox_cmd until handle_mbox_cmd_from_user() learns how to use a mbox_cmd param.
--- drivers/cxl/core/mbox.c | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-)
drivers/cxl/core/mbox.c +17 -4
@@ -324,6 +324,7 @@ static int cxl_to_mem_cmd(struct cxl_mem_command *mem_cmd,/** * cxl_validate_cmd_from_user() - Check fields for CXL_MEM_SEND_COMMAND. +* @mbox_cmd: Sanitized and populated &struct cxl_mbox_cmd. * @cxlds: The device data for the operation * @send_cmd: &struct cxl_send_command copied in from userspace. * @out_cmd: Sanitized and populated &struct cxl_mem_command.
@@ -341,10 +342,13 @@ static int cxl_to_mem_cmd(struct cxl_mem_command *mem_cmd,* * See handle_mailbox_cmd_from_user() */ -static int cxl_validate_cmd_from_user (struct cxl_dev_state *cxlds, +static int cxl_validate_cmd_from_user (struct cxl_mbox_cmd *mbox_cmd, +struct cxl_dev_state *cxlds,const struct cxl_send_command *send_cmd,struct cxl_mem_command *out_cmd) { +int rc; +if (send_cmd->id ==0 || send_cmd->id >=CXL_MEM_COMMAND_ID_MAX )return -ENOTTY ;
@@ -358,9 +362,17 @@ static int cxl_validate_cmd_from_user(struct cxl_dev_state *cxlds,/* Sanitize and construct a cxl_mem_command */ if (send_cmd->id ==CXL_MEM_COMMAND_ID_RAW ) -return cxl_to_mem_cmd_raw (out_cmd, send_cmd, cxlds); + rc =cxl_to_mem_cmd_raw (out_cmd, send_cmd, cxlds);else -return cxl_to_mem_cmd (out_cmd, send_cmd, cxlds); + rc =cxl_to_mem_cmd (out_cmd, send_cmd, cxlds); + +if (rc) +return rc; + +/* Sanitize and construct a cxl_mbox_cmd */ +return cxl_mbox_cmd_ctor (mbox_cmd, cxlds, out_cmd->opcode, + out_cmd->info.size_in, out_cmd->info.size_out, + send_cmd->in.payload); } int cxl_query_cmd(struct cxl_memdev *cxlmd,
@@ -477,6 +489,7 @@ int cxl_send_cmd(struct cxl_memdev *cxlmd, struct cxl_send_command __user *s)struct device *dev = &cxlmd->dev;struct cxl_send_command send;struct cxl_mem_command c; +struct cxl_mbox_cmd mbox_cmd;int rc;dev_dbg (dev,"Send IOCTL\n" );
@@ -484,7 +497,7 @@ int cxl_send_cmd(struct cxl_memdev *cxlmd, struct cxl_send_command __user *s)if (copy_from_user (&send, s,sizeof (send)))return -EFAULT ; - rc =cxl_validate_cmd_from_user (cxlmd->cxlds, &send, &c); + rc =cxl_validate_cmd_from_user (&mbox_cmd, cxlmd->cxlds, &send, &c);if (rc)return rc;
Signature · 1 line
2.31.1
[email protected]
maintainerPATCH v5 5/9 cxl/mbox: Remove dependency on cxl_mem_command for a debug msg
In reply to [email protected]From: Alison Schofield <[email protected]>
In preparation for removing access to struct cxl_mem_command, change this debug message to use cxl_mbox_cmd fields instead. Retrieve the pretty command name from cxl_mbox_cmd using a new opcode to command name helper.
--- drivers/cxl/core/mbox.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-)
drivers/cxl/core/mbox.c +14 -3
@@ -127,6 +127,17 @@ static struct cxl_mem_command *cxl_mem_find_command(u16 opcode)return NULL ; } +static const char *cxl_mem_opcode_to_name (u16 opcode) +{ +struct cxl_mem_command *c; + + c =cxl_mem_find_command (opcode); +if (!c) +return NULL ; + +return cxl_command_names[c->info.id].name; +} +/** * cxl_mbox_send_cmd() - Send a mailbox command to a device. * @cxlds: The device data for the operation
@@ -452,9 +463,9 @@ static int handle_mailbox_cmd_from_user(struct cxl_dev_state *cxlds,dev_dbg (dev,"Submitting %s command for user\n" "\topcode: %x\n" -"\tsize: %ub\n" , - cxl_command_names[cmd->info.id].name, mbox_cmd.opcode, - cmd->info.size_in); +"\tsize: %zx\n" , +cxl_mem_opcode_to_name (mbox_cmd.opcode), + mbox_cmd.opcode, mbox_cmd.size_in); rc = cxlds->mbox_send (cxlds, &mbox_cmd);if (rc)
Signature · 1 line
2.31.1
[email protected]
maintainerPATCH v5 6/9 cxl/mbox: Make handle_mailbox_cmd_from_user() use a mbox param
In reply to [email protected]From: Alison Schofield <[email protected]>
Previously, handle_mailbox_cmd_from_user(), constructed the mailbox command and dispatched it to the hardware. The construction work has moved to the validation path.
handle_mailbox_cmd_from_user() now expects a fully validated mbox param. Make it's caller, cxl_send_cmd(), deliver it. Update the comments and dereferencing of the new mbox parameter.
--- drivers/cxl/core/mbox.c | 44 ++++++++++++++++------------------------- 1 file changed, 17 insertions(+), 27 deletions(-)
drivers/cxl/core/mbox.c +17 -27
@@ -423,8 +423,7 @@ int cxl_query_cmd(struct cxl_memdev *cxlmd,/** * handle_mailbox_cmd_from_user() - Dispatch a mailbox command for userspace. * @cxlds: The device data for the operation -* @cmd: The validated command. -* @in_payload: Pointer to userspace's input payload. +* @mbox_cmd: The validated mailbox command. * @out_payload: Pointer to userspace's output payload. * @size_out: (Input) Max payload size to copy out. * (Output) Payload size hardware generated.
@@ -439,35 +438,27 @@ int cxl_query_cmd(struct cxl_memdev *cxlmd,* * %-EINTR - Mailbox acquisition interrupted. * * %-EXXX - Transaction level failures. * -* Creates the appropriate mailbox command and dispatches it on behalf of a -* userspace request. The input and output payloads are copied between -* userspace. +* Dispatches a mailbox command on behalf of a userspace request. +* The output payload is copied to userspace. * * See cxl_send_cmd(). */ static int handle_mailbox_cmd_from_user (struct cxl_dev_state *cxlds, -const struct cxl_mem_command *cmd, -u64 in_payload,u64 out_payload, -s32 *size_out,u32 *retval) +struct cxl_mbox_cmd *mbox_cmd, +u64 out_payload,s32 *size_out, +u32 *retval) {struct device *dev = cxlds->dev; -struct cxl_mbox_cmd mbox_cmd;int rc; - rc =cxl_mbox_cmd_ctor (&mbox_cmd, cxlds, cmd->opcode, - cmd->info.size_in, cmd->info.size_out, - in_payload); -if (rc) -return rc; -dev_dbg (dev,"Submitting %s command for user\n" "\topcode: %x\n" "\tsize: %zx\n" , -cxl_mem_opcode_to_name (mbox_cmd.opcode), - mbox_cmd.opcode, mbox_cmd.size_in); +cxl_mem_opcode_to_name (mbox_cmd->opcode), + mbox_cmd->opcode, mbox_cmd->size_in); - rc = cxlds->mbox_send (cxlds, &mbox_cmd); + rc = cxlds->mbox_send (cxlds, mbox_cmd);if (rc) goto out;
@@ -476,21 +467,21 @@ static int handle_mailbox_cmd_from_user(struct cxl_dev_state *cxlds,* to userspace. While the payload may have written more output than * this it will have to be ignored. */ -if (mbox_cmd.size_out) { -dev_WARN_ONCE (dev, mbox_cmd.size_out > *size_out, +if (mbox_cmd->size_out) { +dev_WARN_ONCE (dev, mbox_cmd->size_out > *size_out,"Invalid return size\n" );if (copy_to_user (u64_to_user_ptr (out_payload), - mbox_cmd.payload_out, mbox_cmd.size_out)) { + mbox_cmd->payload_out, mbox_cmd->size_out)) { rc = -EFAULT ; goto out; } } - *size_out = mbox_cmd.size_out; - *retval = mbox_cmd.return_code; + *size_out = mbox_cmd->size_out; + *retval = mbox_cmd->return_code; out: -cxl_mbox_cmd_dtor (&mbox_cmd); +cxl_mbox_cmd_dtor (mbox_cmd);return rc; }
@@ -512,9 +503,8 @@ int cxl_send_cmd(struct cxl_memdev *cxlmd, struct cxl_send_command __user *s)if (rc)return rc; - rc =handle_mailbox_cmd_from_user (cxlds, &c, send.in.payload, - send.out.payload, &send.out.size, - &send.retval); + rc =handle_mailbox_cmd_from_user (cxlds, &mbox_cmd, send.out.payload, + &send.out.size, &send.retval);if (rc)return rc;
Signature · 1 line
2.31.1
[email protected]
maintainerPATCH v5 7/9 cxl/mbox: Move cxl_mem_command param to a local variable
In reply to [email protected]From: Alison Schofield <[email protected]>
cxl_validate_command_from_user() is now the single point of validation for mailbox commands coming from user space. Previously, it returned a a cxl_mem_command, but that was not sufficient when validation of the actual mailbox command became a requirement. Now, it returns a fully validated cxl_mbox_cmd.
Remove the extraneous cxl_mem_command parameter. Define and use a local version only.
--- drivers/cxl/core/mbox.c | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-)
drivers/cxl/core/mbox.c +8 -12
@@ -338,7 +338,6 @@ static int cxl_to_mem_cmd(struct cxl_mem_command *mem_cmd,* @mbox_cmd: Sanitized and populated &struct cxl_mbox_cmd. * @cxlds: The device data for the operation * @send_cmd: &struct cxl_send_command copied in from userspace. -* @out_cmd: Sanitized and populated &struct cxl_mem_command. * * Return: * * %0 - @out_cmd is ready to send.
@@ -348,16 +347,14 @@ static int cxl_to_mem_cmd(struct cxl_mem_command *mem_cmd,* * %-EPERM - Attempted to use a protected command. * * %-EBUSY - Kernel has claimed exclusive access to this opcode * -* The result of this command is a fully validated command in @out_cmd that is +* The result of this command is a fully validated command in @mbox_cmd that is * safe to send to the hardware. -* -* See handle_mailbox_cmd_from_user() */ static int cxl_validate_cmd_from_user (struct cxl_mbox_cmd *mbox_cmd,struct cxl_dev_state *cxlds, -const struct cxl_send_command *send_cmd, -struct cxl_mem_command *out_cmd) +const struct cxl_send_command *send_cmd) { +struct cxl_mem_command mem_cmd;int rc;if (send_cmd->id ==0 || send_cmd->id >=CXL_MEM_COMMAND_ID_MAX )
@@ -373,16 +370,16 @@ static int cxl_validate_cmd_from_user(struct cxl_mbox_cmd *mbox_cmd,/* Sanitize and construct a cxl_mem_command */ if (send_cmd->id ==CXL_MEM_COMMAND_ID_RAW ) - rc =cxl_to_mem_cmd_raw (out_cmd, send_cmd, cxlds); + rc =cxl_to_mem_cmd_raw (&mem_cmd, send_cmd, cxlds);else - rc =cxl_to_mem_cmd (out_cmd, send_cmd, cxlds); + rc =cxl_to_mem_cmd (&mem_cmd, send_cmd, cxlds);if (rc)return rc;/* Sanitize and construct a cxl_mbox_cmd */ -return cxl_mbox_cmd_ctor (mbox_cmd, cxlds, out_cmd->opcode, - out_cmd->info.size_in, out_cmd->info.size_out, +return cxl_mbox_cmd_ctor (mbox_cmd, cxlds, mem_cmd.opcode, + mem_cmd.info.size_in, mem_cmd.info.size_out, send_cmd->in.payload); }
@@ -490,7 +487,6 @@ int cxl_send_cmd(struct cxl_memdev *cxlmd, struct cxl_send_command __user *s)struct cxl_dev_state *cxlds = cxlmd->cxlds;struct device *dev = &cxlmd->dev;struct cxl_send_command send; -struct cxl_mem_command c;struct cxl_mbox_cmd mbox_cmd;int rc;
@@ -499,7 +495,7 @@ int cxl_send_cmd(struct cxl_memdev *cxlmd, struct cxl_send_command __user *s)if (copy_from_user (&send, s,sizeof (send)))return -EFAULT ; - rc =cxl_validate_cmd_from_user (&mbox_cmd, cxlmd->cxlds, &send, &c); + rc =cxl_validate_cmd_from_user (&mbox_cmd, cxlmd->cxlds, &send);if (rc)return rc;
Signature · 1 line
2.31.1
Re: [PATCH v5 7/9] cxl/mbox: Move cxl_mem_command param to a local variable
Quoted 9 lines · On Wed, Mar 30, 2022 at 6:25 PM <[email protected]> wrote:
From: Alison Schofield <[email protected]>
cxl_validate_command_from_user()is now the single point of validation for mailbox commands coming from user space. Previously, it returned a acxl_mem_command, but that was not sufficient when validation of the actual mailbox command became a requirement. Now, it returns a fully validatedcxl_mbox_cmd.
Quoted 5 lines
Remove the extraneous
cxl_mem_commandparameter. Define and use a local version only.Signed-off-by Alison Schofield <[email protected]>Reviewed-by Jonathan Cameron <[email protected]>
Looks good,
[email protected]
maintainerPATCH v5 8/9 cxl/mbox: Block immediate mode in SET_PARTITION_INFO command
In reply to [email protected]From: Alison Schofield <[email protected]>
User space may send the SET_PARTITION_INFO mailbox command using the IOCTL interface. Inspect the input payload and fail if the immediate flag is set.
This is the first instance of the driver inspecting an input payload from user space. Assume there will be more such cases and implement with an extensible helper.
In order for the kernel to react to an immediate partition change it needs to assert that the change will not affect any active decode. At a minimum this requires validating that the device is using HDM decoders instead of the CXL DVSEC for decode, and that none of the active HDM decoders are affected by the partition change. For now, just fail until that support arrives.
--- drivers/cxl/core/mbox.c | 41 +++++++++++++++++++++++++++++++++++++++++ drivers/cxl/cxlmem.h | 7 +++++++ 2 files changed, 48 insertions(+)
drivers/cxl/core/mbox.c +41 -0
@@ -218,6 +218,40 @@ static bool cxl_mem_raw_command_allowed(u16 opcode)return true; } +/** +* cxl_payload_from_user_allowed() - Check contents of in_payload. +* @opcode: The mailbox command opcode. +* @payload_in: Pointer to the input payload passed in from user space. +* +* Return: +* * true - payload_in passes check for @opcode. +* * false - payload_in contains invalid or unsupported values. +* +* The driver may inspect payload contents before sending a mailbox +* command from user space to the device. The intent is to reject +* commands with input payloads that are known to be unsafe. This +* check is not intended to replace the users careful selection of +* mailbox command parameters and makes no guarantee that the user +* command will succeed, nor that it is appropriate. +* +* The specific checks are determined by the opcode. +*/ +static bool cxl_payload_from_user_allowed (u16 opcode,void *payload_in) +{ +switch (opcode) { +case CXL_MBOX_OP_SET_PARTITION_INFO : { +struct cxl_mbox_set_partition_info *pi = payload_in; + +if (pi->flags &&CXL_SET_PARTITION_IMMEDIATE_FLAG ) +return false; +break ; + } +default : +break ; + } +return true; +} +static int cxl_mbox_cmd_ctor (struct cxl_mbox_cmd *mbox,struct cxl_dev_state *cxlds,u16 opcode,size_t in_size,size_t out_size,u64 in_payload)
@@ -232,6 +266,13 @@ static int cxl_mbox_cmd_ctor(struct cxl_mbox_cmd *mbox, in_size);if (!mbox->payload_in)return PTR_ERR (mbox->payload_in); + +if (!cxl_payload_from_user_allowed (opcode, mbox->payload_in)) { +dev_dbg (cxlds->dev,"%s: input payload not allowed\n" , +cxl_mem_opcode_to_name (opcode)); +kvfree (mbox->payload_in); +return -EBUSY ; + } }/* Prepare to handle a full payload for variable sized output */
drivers/cxl/cxlmem.h +7 -0
@@ -264,6 +264,13 @@ struct cxl_mbox_set_lsa {u8 data[]; } __packed; +struct cxl_mbox_set_partition_info { +__le64 volatile_capacity; +u8 flags; +} __packed; + +#define CXL_SET_PARTITION_IMMEDIATE_FLAG BIT(0) +/** * struct cxl_mem_command - Driver representation of a memory device command * @info: Command information as it exists for the UAPI
Signature · 1 line
2.31.1
Re: [PATCH v5 8/9] cxl/mbox: Block immediate mode in SET_PARTITION_INFO command
Quoted 18 lines · On Wed, Mar 30, 2022 at 6:25 PM <[email protected]> wrote:
From: Alison Schofield <[email protected]>
User space may send the
SET_PARTITION_INFOmailbox command using the IOCTL interface. Inspect the input payload and fail if the immediate flag is set.This is the first instance of the driver inspecting an input payload from user space. Assume there will be more such cases and implement with an extensible helper.
In order for the kernel to react to an immediate partition change it needs to assert that the change will not affect any active decode. At a minimum this requires validating that the device is using HDM decoders instead of the CXL DVSEC for decode, and that none of the active HDM decoders are affected by the partition change. For now, just fail until that support arrives.
Quoted 2 lines
Signed-off-by Alison Schofield <[email protected]>Reviewed-by Jonathan Cameron <[email protected]>
Looks good,
[email protected]
maintainerPATCH v5 9/9 cxl/pmem: Remove CXL SET_PARTITION_INFO from exclusive_cmds list
In reply to [email protected]From: Alison Schofield <[email protected]>
With SET_PARTITION_INFO on the exclusive_cmds list for the CXL_PMEM driver, userspace cannot execute a set-partition command without first unbinding the pmem driver from the device.
When userspace requests a partition change to take effect on the next reboot this unbind requirement is unnecessarily restrictive. The driver does not need to enforce an unbind because partitions will not change until the next reboot. Of course, userspace still needs to be aware that changing the size of persistent capacity on the next reboot will result in the loss of data stored. That can happen regardless of whether it is presently bound at the time of issuing the set-partition command.
When userspace requests a partition change to take effect immediately, restrictions are needed. The CXL_MEM driver currently blocks the usage of immediate mode, making the presence of SET_PARTITION_INFO, in this exclusive commands list, redundant.
In the future, when the CXL_MEM driver adds support for immediate changes to device partitions it will ensure that the partition change will not affect any active decode. That means the work will not fall right back here, onto the CXL_PMEM driver.
--- drivers/cxl/pmem.c | 1 - 1 file changed, 1 deletion(-)
drivers/cxl/pmem.c +0 -1
@@ -344,7 +344,6 @@ static __init int cxl_pmem_init(void) {int rc; -set_bit (CXL_MEM_COMMAND_ID_SET_PARTITION_INFO , exclusive_cmds);set_bit (CXL_MEM_COMMAND_ID_SET_SHUTDOWN_STATE , exclusive_cmds);set_bit (CXL_MEM_COMMAND_ID_SET_LSA , exclusive_cmds);
Signature · 1 line
2.31.1
Re: [PATCH v5 9/9] cxl/pmem: Remove CXL SET_PARTITION_INFO from exclusive_cmds list
Quoted 21 lines · On Wed, Mar 30, 2022 at 6:25 PM <[email protected]> wrote:
From: Alison Schofield <[email protected]>
With
SET_PARTITION_INFOon theexclusive_cmdslist for theCXL_PMEMdriver, userspace cannot execute a set-partition command without first unbinding the pmem driver from the device.When userspace requests a partition change to take effect on the next reboot this unbind requirement is unnecessarily restrictive. The driver does not need to enforce an unbind because partitions will not change until the next reboot. Of course, userspace still needs to be aware that changing the size of persistent capacity on the next reboot will result in the loss of data stored. That can happen regardless of whether it is presently bound at the time of issuing the set-partition command.
When userspace requests a partition change to take effect immediately, restrictions are needed. The
CXL_MEMdriver currently blocks the usage of immediate mode, making the presence ofSET_PARTITION_INFO, in this exclusive commands list, redundant.
Quoted 6 lines
In the future, when the
CXL_MEMdriver adds support for immediate changes to device partitions it will ensure that the partition change will not affect any active decode. That means the work will not fall right back here, onto theCXL_PMEMdriver.Signed-off-by Alison Schofield <[email protected]>
Looks good.
Re: [PATCH v5 9/9] cxl/pmem: Remove CXL SET_PARTITION_INFO from exclusive_cmds list
In reply to [email protected]Quoted 20 lines · On Wed, 30 Mar 2022 18:27:19 -0700 [email protected] wrote:
From: Alison Schofield <[email protected]>
With
SET_PARTITION_INFOon theexclusive_cmdslist for theCXL_PMEMdriver, userspace cannot execute a set-partition command without first unbinding the pmem driver from the device.When userspace requests a partition change to take effect on the next reboot this unbind requirement is unnecessarily restrictive. The driver does not need to enforce an unbind because partitions will not change until the next reboot. Of course, userspace still needs to be aware that changing the size of persistent capacity on the next reboot will result in the loss of data stored. That can happen regardless of whether it is presently bound at the time of issuing the set-partition command.
When userspace requests a partition change to take effect immediately, restrictions are needed. The
CXL_MEMdriver currently blocks the usage of immediate mode, making the presence ofSET_PARTITION_INFO, in this exclusive commands list, redundant.
Quoted 6 lines
In the future, when the
CXL_MEMdriver adds support for immediate changes to device partitions it will ensure that the partition change will not affect any active decode. That means the work will not fall right back here, onto theCXL_PMEMdriver.Signed-off-by Alison Schofield <[email protected]>
I gave a an RB on v3... To make it easy for whoever picks this up.
Quoted 16 lines
--- drivers/cxl/pmem.c | 1 - 1 file changed, 1 deletion(-)drivers/cxl/pmem.c +0
-1@@ -344,7 +344,6 @@ static __init int cxl_pmem_init(void) {int rc; -set_bit (CXL_MEM_COMMAND_ID_SET_PARTITION_INFO , exclusive_cmds);set_bit (CXL_MEM_COMMAND_ID_SET_SHUTDOWN_STATE , exclusive_cmds);set_bit (CXL_MEM_COMMAND_ID_SET_LSA , exclusive_cmds);