linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Javier Martinez Canillas <javierm@redhat.com>
To: linux-kernel@vger.kernel.org
Cc: Maxime Ripard <maxime@cerno.tech>,
	Thomas Zimmermann <tzimmermann@suse.de>,
	Javier Martinez Canillas <javierm@redhat.com>,
	Alex Deucher <alexander.deucher@amd.com>,
	Changcheng Deng <deng.changcheng@zte.com.cn>,
	Daniel Vetter <daniel@ffwll.ch>, Helge Deller <deller@gmx.de>,
	Sam Ravnborg <sam@ravnborg.org>,
	Xiyu Yang <xiyuyang19@fudan.edu.cn>,
	Zhen Lei <thunder.leizhen@huawei.com>,
	dri-devel@lists.freedesktop.org, linux-fbdev@vger.kernel.org
Subject: [PATCH 1/2] fbdev: Check in file_fb_info() if the fb_info was already been freed
Date: Mon,  2 May 2022 15:09:43 +0200	[thread overview]
Message-ID: <20220502130944.363776-2-javierm@redhat.com> (raw)
In-Reply-To: <20220502130944.363776-1-javierm@redhat.com>

If real driver probes, the fbdev core kicks out all drivers that are using
a framebuffer that were provided by the system firmware. But it could be a
user-space process still has a file descriptor for the fbdev device node.

This can lead to a NULL pointer dereference, if the framebuffer device is
unregistered and associated data freed, but later in the .release callback
is attempted to access its struct fb_info.

To prevent this, make file_fb_info() to also check the fb_info reference
counter and just return NULL if this equals zero. Since that means it has
already been freed.

Signed-off-by: Javier Martinez Canillas <javierm@redhat.com>
---

 drivers/video/fbdev/core/fbmem.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/drivers/video/fbdev/core/fbmem.c b/drivers/video/fbdev/core/fbmem.c
index 84427470367b..20d8929df79f 100644
--- a/drivers/video/fbdev/core/fbmem.c
+++ b/drivers/video/fbdev/core/fbmem.c
@@ -751,8 +751,13 @@ static struct fb_info *file_fb_info(struct file *file)
 	int fbidx = iminor(inode);
 	struct fb_info *info = registered_fb[fbidx];
 
-	if (info != file->private_data)
-		info = NULL;
+	if (!info)
+		return NULL;
+
+	/* check that the fb_info has not changed or was already freed */
+	if (info != file->private_data || refcount_read(&info->count) == 0)
+		return NULL;
+
 	return info;
 }
 
-- 
2.35.1


  reply	other threads:[~2022-05-02 13:10 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-05-02 13:09 [PATCH 0/2] fbdev: Fix a NULL pointer dereference in fb_release() Javier Martinez Canillas
2022-05-02 13:09 ` Javier Martinez Canillas [this message]
2022-05-02 13:26   ` [PATCH 1/2] fbdev: Check in file_fb_info() if the fb_info was already been freed Thomas Zimmermann
2022-05-02 13:36     ` Javier Martinez Canillas
2022-05-02 13:09 ` [PATCH 2/2] fbdev: Make fb_release() return -ENODEV if fbdev was unregistered Javier Martinez Canillas
2022-05-02 13:20   ` Thomas Zimmermann
2022-05-02 13:39     ` Javier Martinez Canillas
2022-05-04  9:47   ` Daniel Vetter
2022-05-04 10:09     ` Javier Martinez Canillas
2022-05-04 10:15       ` Daniel Vetter

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=20220502130944.363776-2-javierm@redhat.com \
    --to=javierm@redhat.com \
    --cc=alexander.deucher@amd.com \
    --cc=daniel@ffwll.ch \
    --cc=deller@gmx.de \
    --cc=deng.changcheng@zte.com.cn \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=linux-fbdev@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=maxime@cerno.tech \
    --cc=sam@ravnborg.org \
    --cc=thunder.leizhen@huawei.com \
    --cc=tzimmermann@suse.de \
    --cc=xiyuyang19@fudan.edu.cn \
    /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;
as well as URLs for NNTP newsgroup(s).