linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: <guangming.cao@mediatek.com>
To: Sumit Semwal <sumit.semwal@linaro.org>, <christian.koenig@amd.com>
Cc: <wsd_upstream@mediatek.com>, <linux-media@vger.kernel.org>,
	<dri-devel@lists.freedesktop.org>,
	<linaro-mm-sig@lists.linaro.org>, <linux-kernel@vger.kernel.org>,
	<linux-arm-kernel@lists.infradead.org>,
	<linux-mediatek@lists.infradead.org>,
	Guangming Cao <Guangming.Cao@mediatek.com>
Subject: [PATCH] dma-buf: add kernel count for dma_buf
Date: Wed, 14 Jul 2021 15:11:44 +0800	[thread overview]
Message-ID: <20210714071144.62126-1-guangming.cao@mediatek.com> (raw)

From: Guangming Cao <Guangming.Cao@mediatek.com>

Add a refcount for kernel to prevent UAF(Use After Free) issue.

We can assume a case like below:
    1. kernel space alloc dma_buf(file count = 1)
    2. kernel use dma_buf to get fd(file count = 1)
    3. userspace use fd to do mapping (file count = 2)
    4. kernel call dma_buf_put (file count = 1)
    5. userpsace close buffer fd(file count = 0)
    6. at this time, buffer is released, but va is valid!!
       So we still can read/write buffer via mmap va,
       it maybe cause memory leak, or kernel exception.
       And also, if we use "ls -ll" to watch corresponding process
           fd link info, it also will cause kernel exception.

Another case:
     Using dma_buf_fd to generate more than 1 fd, because
     dma_buf_fd will not increase file count, thus, when close
     the second fd, it maybe occurs error.

Solution:
    Add a kernel count for dma_buf, and make sure the file count
        of dma_buf.file hold by kernel is 1.

Notes: For this solution, kref couldn't work because kernel ref
       maybe added from 0, but kref don't allow it.

Signed-off-by: Guangming Cao <Guangming.Cao@mediatek.com>
---
 drivers/dma-buf/dma-buf.c | 23 +++++++++++++++++++----
 include/linux/dma-buf.h   |  6 ++++--
 2 files changed, 23 insertions(+), 6 deletions(-)

diff --git a/drivers/dma-buf/dma-buf.c b/drivers/dma-buf/dma-buf.c
index 511fe0d217a0..04ee92aac8b9 100644
--- a/drivers/dma-buf/dma-buf.c
+++ b/drivers/dma-buf/dma-buf.c
@@ -62,6 +62,7 @@ static void dma_buf_release(struct dentry *dentry)
 	if (unlikely(!dmabuf))
 		return;
 
+	WARN_ON(atomic64_read(&dmabuf->kernel_ref));
 	BUG_ON(dmabuf->vmapping_counter);
 
 	/*
@@ -555,6 +556,7 @@ struct dma_buf *dma_buf_export(const struct dma_buf_export_info *exp_info)
 		goto err_module;
 	}
 
+	atomic64_set(&dmabuf->kernel_ref, 1);
 	dmabuf->priv = exp_info->priv;
 	dmabuf->ops = exp_info->ops;
 	dmabuf->size = exp_info->size;
@@ -617,6 +619,9 @@ int dma_buf_fd(struct dma_buf *dmabuf, int flags)
 
 	fd_install(fd, dmabuf->file);
 
+	/* Add file cnt for each new fd */
+	get_file(dmabuf->file);
+
 	return fd;
 }
 EXPORT_SYMBOL_GPL(dma_buf_fd);
@@ -626,12 +631,13 @@ EXPORT_SYMBOL_GPL(dma_buf_fd);
  * @fd:	[in]	fd associated with the struct dma_buf to be returned
  *
  * On success, returns the struct dma_buf associated with an fd; uses
- * file's refcounting done by fget to increase refcount. returns ERR_PTR
- * otherwise.
+ * dmabuf's ref refcounting done by kref_get to increase refcount.
+ * Returns ERR_PTR otherwise.
  */
 struct dma_buf *dma_buf_get(int fd)
 {
 	struct file *file;
+	struct dma_buf *dmabuf;
 
 	file = fget(fd);
 
@@ -643,7 +649,12 @@ struct dma_buf *dma_buf_get(int fd)
 		return ERR_PTR(-EINVAL);
 	}
 
-	return file->private_data;
+	dmabuf = file->private_data;
+	/* replace file count increase as ref increase for kernel user */
+	get_dma_buf(dmabuf);
+	fput(file);
+
+	return dmabuf;
 }
 EXPORT_SYMBOL_GPL(dma_buf_get);
 
@@ -662,7 +673,11 @@ void dma_buf_put(struct dma_buf *dmabuf)
 	if (WARN_ON(!dmabuf || !dmabuf->file))
 		return;
 
-	fput(dmabuf->file);
+	if (WARN_ON(!atomic64_read(&dmabuf->kernel_ref)))
+		return;
+
+	if (!atomic64_dec_return(&dmabuf->kernel_ref))
+		fput(dmabuf->file);
 }
 EXPORT_SYMBOL_GPL(dma_buf_put);
 
diff --git a/include/linux/dma-buf.h b/include/linux/dma-buf.h
index efdc56b9d95f..bc790cb028eb 100644
--- a/include/linux/dma-buf.h
+++ b/include/linux/dma-buf.h
@@ -308,6 +308,7 @@ struct dma_buf_ops {
 struct dma_buf {
 	size_t size;
 	struct file *file;
+	atomic64_t kernel_ref;
 	struct list_head attachments;
 	const struct dma_buf_ops *ops;
 	struct mutex lock;
@@ -436,7 +437,7 @@ struct dma_buf_export_info {
 					 .owner = THIS_MODULE }
 
 /**
- * get_dma_buf - convenience wrapper for get_file.
+ * get_dma_buf - increase a kernel ref of dma-buf
  * @dmabuf:	[in]	pointer to dma_buf
  *
  * Increments the reference count on the dma-buf, needed in case of drivers
@@ -446,7 +447,8 @@ struct dma_buf_export_info {
  */
 static inline void get_dma_buf(struct dma_buf *dmabuf)
 {
-	get_file(dmabuf->file);
+	if (atomic64_inc_return(&dmabuf->kernel_ref) == 1)
+		get_file(dmabuf->file);
 }
 
 /**
-- 
2.17.1


             reply	other threads:[~2021-07-14  7:12 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-07-14  7:11 guangming.cao [this message]
2021-07-14  8:46 ` Christian König
2021-07-14  9:44   ` guangming.cao
2021-07-14 10:43     ` Christian König
2021-07-14 12:03       ` guangming.cao
2021-07-14 12:28         ` Christian König
2021-07-15  6:06           ` guangming.cao

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=20210714071144.62126-1-guangming.cao@mediatek.com \
    --to=guangming.cao@mediatek.com \
    --cc=christian.koenig@amd.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=linaro-mm-sig@lists.linaro.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-media@vger.kernel.org \
    --cc=linux-mediatek@lists.infradead.org \
    --cc=sumit.semwal@linaro.org \
    --cc=wsd_upstream@mediatek.com \
    --subject='Re: [PATCH] dma-buf: add kernel count for dma_buf' \
    /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

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).