Skip to content

Commit

Permalink
mailbox: mailbox-test: fix a locking issue in mbox_test_message_write()
Browse files Browse the repository at this point in the history
[ Upstream commit 8fe72b7 ]

There was a bug where this code forgot to unlock the tdev->mutex if the
kzalloc() failed.  Fix this issue, by moving the allocation outside the
lock.

Fixes: 2d1e952 ("mailbox: mailbox-test: Fix potential double-free in mbox_test_message_write()")
Signed-off-by: Dan Carpenter <dan.carpenter@linaro.org>
Reviewed-by: Lee Jones <lee@kernel.org>
Signed-off-by: Jassi Brar <jaswinder.singh@linaro.org>
Signed-off-by: Sasha Levin <sashal@kernel.org>
  • Loading branch information
Dan Carpenter authored and gregkh committed Jun 9, 2023
1 parent 4124000 commit 7d233f9
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions drivers/mailbox/mailbox-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ static ssize_t mbox_test_message_write(struct file *filp,
size_t count, loff_t *ppos)
{
struct mbox_test_device *tdev = filp->private_data;
char *message;
void *data;
int ret;

Expand All @@ -112,12 +113,13 @@ static ssize_t mbox_test_message_write(struct file *filp,
return -EINVAL;
}

mutex_lock(&tdev->mutex);

tdev->message = kzalloc(MBOX_MAX_MSG_LEN, GFP_KERNEL);
if (!tdev->message)
message = kzalloc(MBOX_MAX_MSG_LEN, GFP_KERNEL);
if (!message)
return -ENOMEM;

mutex_lock(&tdev->mutex);

tdev->message = message;
ret = copy_from_user(tdev->message, userbuf, count);
if (ret) {
ret = -EFAULT;
Expand Down

0 comments on commit 7d233f9

Please sign in to comment.