When submitting a command for userspace, input and output payload bounce
buffers are allocated. For a given command, both input and output
buffers may exist and so when allocation of the input buffer fails, the
output buffer must be freed. As far as I can tell, userspace can't
easily exploit the leak to OOM a machine unless the machine was already
near OOM state.

This bug was introduced in v5 of the patch and did not exist in prior
revisions.

While here, adjust the variable 'j' found in patch review by Konrad.

Cc: Al Viro <[email protected]>
Reported-by: Konrad Rzeszutek Wilk <[email protected]>
Signed-off-by: Ben Widawsky <[email protected]>
Reviewed-by: Dan Williams <[email protected]> (v2)
Reviewed-by: Jonathan Cameron <[email protected]>
---
 drivers/cxl/mem.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/drivers/cxl/mem.c b/drivers/cxl/mem.c
index df895bcca63a..626fd7066f4f 100644
--- a/drivers/cxl/mem.c
+++ b/drivers/cxl/mem.c
@@ -514,8 +514,10 @@ static int handle_mailbox_cmd_from_user(struct cxl_mem 
*cxlm,
        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))
+               if (IS_ERR(mbox_cmd.payload_in)) {
+                       kvfree(mbox_cmd.payload_out);
                        return PTR_ERR(mbox_cmd.payload_in);
+               }
        }
 
        rc = cxl_mem_mbox_get(cxlm);
@@ -696,7 +698,7 @@ static int cxl_query_cmd(struct cxl_memdev *cxlmd,
        struct device *dev = &cxlmd->dev;
        struct cxl_mem_command *cmd;
        u32 n_commands;
-       int j = 0;
+       int cmds = 0;
 
        dev_dbg(dev, "Query IOCTL\n");
 
@@ -714,10 +716,10 @@ static int cxl_query_cmd(struct cxl_memdev *cxlmd,
        cxl_for_each_cmd(cmd) {
                const struct cxl_command_info *info = &cmd->info;
 
-               if (copy_to_user(&q->commands[j++], info, sizeof(*info)))
+               if (copy_to_user(&q->commands[cmds++], info, sizeof(*info)))
                        return -EFAULT;
 
-               if (j == n_commands)
+               if (cmds == n_commands)
                        break;
        }
 
-- 
2.30.1
_______________________________________________
Linux-nvdimm mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to