From: Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
To: Sangsup Lee <k1rh4.lee@gmail.com>
Cc: Amol Maheshwari <amahesh@qti.qualcomm.com>,
Arnd Bergmann <arnd@arndb.de>,
Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
linux-arm-msm@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] misc: fastrpc: Fix a Use after-free-bug by race condition
Date: Tue, 21 Mar 2023 09:27:46 +0000 [thread overview]
Message-ID: <9bfef283-e2ac-b2ba-386c-6833e9cb1283@linaro.org> (raw)
In-Reply-To: <20230216014120.3110-1-k1rh4.lee@gmail.com>
Thanks Sangsup for reporting the issue and sharing the patch,
Sorry, for some reason I missed this patch.
On 16/02/2023 01:41, Sangsup Lee wrote:
> This patch adds mutex_lock for fixing an Use-after-free bug.
> fastrpc_req_munmap_impl can be called concurrently in multi-threded environments.
> The buf which is allocated by list_for_each_safe can be used after another thread frees it.
>
Commit log can be improved here to something like:
fastrcp_munmap takes two steps to unmap the memory, first to find a
matching fastrpc buf in the list and second is to send request to DSP to
unmap it.
There is a potentially window of race between these two operations,
which can lead to user-after-free.
Fix this by adding locking around this two operations.
> Signed-off-by: Sangsup Lee <k1rh4.lee@gmail.com>
> ---
> drivers/misc/fastrpc.c | 7 ++++++-
> 1 file changed, 6 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/misc/fastrpc.c b/drivers/misc/fastrpc.c
> index 5310606113fe..c4b5fa4a50a6 100644
> --- a/drivers/misc/fastrpc.c
> +++ b/drivers/misc/fastrpc.c
> @@ -1806,10 +1806,12 @@ static int fastrpc_req_munmap(struct fastrpc_user *fl, char __user *argp)
> struct fastrpc_buf *buf = NULL, *iter, *b;
> struct fastrpc_req_munmap req;
> struct device *dev = fl->sctx->dev;
> + int err;
>
> if (copy_from_user(&req, argp, sizeof(req)))
> return -EFAULT;
>
> + mutex_lock(&fl->mutex);
> spin_lock(&fl->lock);
> list_for_each_entry_safe(iter, b, &fl->mmaps, node) {
> if ((iter->raddr == req.vaddrout) && (iter->size == req.size)) {
> @@ -1822,10 +1824,13 @@ static int fastrpc_req_munmap(struct fastrpc_user *fl, char __user *argp)
> if (!buf) {
> dev_err(dev, "mmap\t\tpt 0x%09llx [len 0x%08llx] not in list\n",
> req.vaddrout, req.size);
> + mutex_unlock(&fl->mutex);
> return -EINVAL;
> }
>
> - return fastrpc_req_munmap_impl(fl, buf);
> + err = fastrpc_req_munmap_impl(fl, buf);
> + mutex_unlock(&fl->mutex);
> + return err;
How about moving the locking to ioctl:
diff --git a/drivers/misc/fastrpc.c b/drivers/misc/fastrpc.c
index a701132638cf..2f217071a6c3 100644
--- a/drivers/misc/fastrpc.c
+++ b/drivers/misc/fastrpc.c
@@ -2087,7 +2087,9 @@ static long fastrpc_device_ioctl(struct file
*file, unsigned int cmd,
err = fastrpc_req_mmap(fl, argp);
break;
case FASTRPC_IOCTL_MUNMAP:
+ mutex_lock(&fl->mutex);
err = fastrpc_req_munmap(fl, argp);
+ mutex_unlock(&fl->mutex);
break;
case FASTRPC_IOCTL_MEM_MAP:
err = fastrpc_req_mem_map(fl, argp);
thanks,
srini
> }
>
> static int fastrpc_req_mmap(struct fastrpc_user *fl, char __user *argp)
next prev parent reply other threads:[~2023-03-21 9:27 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-02-16 1:41 [PATCH] misc: fastrpc: Fix a Use after-free-bug by race condition Sangsup Lee
2023-02-16 1:53 ` sangsup lee
2023-03-21 9:27 ` Srinivas Kandagatla [this message]
2023-03-22 1:59 ` sangsup lee
2023-03-22 8:40 ` Srinivas Kandagatla
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=9bfef283-e2ac-b2ba-386c-6833e9cb1283@linaro.org \
--to=srinivas.kandagatla@linaro.org \
--cc=amahesh@qti.qualcomm.com \
--cc=arnd@arndb.de \
--cc=gregkh@linuxfoundation.org \
--cc=k1rh4.lee@gmail.com \
--cc=linux-arm-msm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox