* [PATCH] mailbox: mailbox-test: fix a locking issue in mbox_test_message_write()
@ 2023-05-05 9:22 Dan Carpenter
2023-05-10 13:35 ` Lee Jones
2023-05-31 8:52 ` Lee Jones
0 siblings, 2 replies; 3+ messages in thread
From: Dan Carpenter @ 2023-05-05 9:22 UTC (permalink / raw)
To: Lee Jones; +Cc: Jassi Brar, linux-kernel, kernel-janitors
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: 2d1e952a2b8e ("mailbox: mailbox-test: Fix potential double-free in mbox_test_message_write()")
Signed-off-by: Dan Carpenter <dan.carpenter@linaro.org>
---
drivers/mailbox/mailbox-test.c | 10 ++++++----
1 file changed, 6 insertions(+), 4 deletions(-)
diff --git a/drivers/mailbox/mailbox-test.c b/drivers/mailbox/mailbox-test.c
index c4a705c30331..fc6a12a51b40 100644
--- a/drivers/mailbox/mailbox-test.c
+++ b/drivers/mailbox/mailbox-test.c
@@ -98,6 +98,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;
@@ -113,12 +114,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;
--
2.39.2
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH] mailbox: mailbox-test: fix a locking issue in mbox_test_message_write()
2023-05-05 9:22 [PATCH] mailbox: mailbox-test: fix a locking issue in mbox_test_message_write() Dan Carpenter
@ 2023-05-10 13:35 ` Lee Jones
2023-05-31 8:52 ` Lee Jones
1 sibling, 0 replies; 3+ messages in thread
From: Lee Jones @ 2023-05-10 13:35 UTC (permalink / raw)
To: Dan Carpenter; +Cc: Jassi Brar, linux-kernel, kernel-janitors
Good catch, thanks Dan.
On Fri, 05 May 2023, Dan Carpenter wrote:
> 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: 2d1e952a2b8e ("mailbox: mailbox-test: Fix potential double-free in mbox_test_message_write()")
> Signed-off-by: Dan Carpenter <dan.carpenter@linaro.org>
> ---
> drivers/mailbox/mailbox-test.c | 10 ++++++----
> 1 file changed, 6 insertions(+), 4 deletions(-)
Reviewed-by: Lee Jones <lee@kernel.org>
> diff --git a/drivers/mailbox/mailbox-test.c b/drivers/mailbox/mailbox-test.c
> index c4a705c30331..fc6a12a51b40 100644
> --- a/drivers/mailbox/mailbox-test.c
> +++ b/drivers/mailbox/mailbox-test.c
> @@ -98,6 +98,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;
>
> @@ -113,12 +114,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;
> --
> 2.39.2
>
--
Lee Jones [李琼斯]
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] mailbox: mailbox-test: fix a locking issue in mbox_test_message_write()
2023-05-05 9:22 [PATCH] mailbox: mailbox-test: fix a locking issue in mbox_test_message_write() Dan Carpenter
2023-05-10 13:35 ` Lee Jones
@ 2023-05-31 8:52 ` Lee Jones
1 sibling, 0 replies; 3+ messages in thread
From: Lee Jones @ 2023-05-31 8:52 UTC (permalink / raw)
To: Dan Carpenter; +Cc: Jassi Brar, linux-kernel, kernel-janitors
Jassi,
On Fri, 05 May 2023, Dan Carpenter wrote:
> 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: 2d1e952a2b8e ("mailbox: mailbox-test: Fix potential double-free in mbox_test_message_write()")
When are you planning on sending this to the -rcs?
> Signed-off-by: Dan Carpenter <dan.carpenter@linaro.org>
> ---
> drivers/mailbox/mailbox-test.c | 10 ++++++----
> 1 file changed, 6 insertions(+), 4 deletions(-)
>
> diff --git a/drivers/mailbox/mailbox-test.c b/drivers/mailbox/mailbox-test.c
> index c4a705c30331..fc6a12a51b40 100644
> --- a/drivers/mailbox/mailbox-test.c
> +++ b/drivers/mailbox/mailbox-test.c
> @@ -98,6 +98,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;
>
> @@ -113,12 +114,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;
> --
> 2.39.2
>
--
Lee Jones [李琼斯]
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2023-05-31 8:53 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-05-05 9:22 [PATCH] mailbox: mailbox-test: fix a locking issue in mbox_test_message_write() Dan Carpenter
2023-05-10 13:35 ` Lee Jones
2023-05-31 8:52 ` Lee Jones
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).