linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Tuo Li <islituo@gmail.com>
To: mchehab@kernel.org, yongsuyoo0215@gmail.com,
	tommaso.merciai@amarulasolutions.com, colin.i.king@gmail.com,
	hverkuil-cisco@xs4all.nl
Cc: linux-media@vger.kernel.org, linux-kernel@vger.kernel.org,
	baijiaju1990@outlook.com, Tuo Li <islituo@gmail.com>,
	BassCheck <bass@buaa.edu.cn>
Subject: [PATCH] media: dvb-core: Fix a possible null-pointer dereference due to data race in dvbdmx_write()
Date: Mon, 26 Jun 2023 10:44:29 +0800	[thread overview]
Message-ID: <20230626024429.994136-1-islituo@gmail.com> (raw)

The struct field dmx_demux.frontend is often protected by the lock 
dvb_demux.mutex when is accessed. Here is an example in 
dvbdmx_connect_frontend():

  mutex_lock(&dvbdemux->mutex);

  demux->frontend = frontend;
  mutex_unlock(&dvbdemux->mutex);

However, the variable demux->frontend is accessed without holding the lock
dvbdemux->mutex in dvbdmx_write():

  if ((!demux->frontend) || (demux->frontend->source != DMX_MEMORY_FE))

In my opinion, this may be a harmful race, because if demux->fontend is set
to NULL right after the first condition is checked, a null-pointer 
dereference can occur when accessing the field demux->frontend->source.

To fix this possible null-pointer dereference caused by data race, a lock
and unlock pair is added when accessing the variable demux->frontend.

Reported-by: BassCheck <bass@buaa.edu.cn>
Signed-off-by: Tuo Li <islituo@gmail.com>
---
 drivers/media/dvb-core/dvb_demux.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/drivers/media/dvb-core/dvb_demux.c b/drivers/media/dvb-core/dvb_demux.c
index 7c4d86bfdd6c..d26738e3310a 100644
--- a/drivers/media/dvb-core/dvb_demux.c
+++ b/drivers/media/dvb-core/dvb_demux.c
@@ -1140,9 +1140,13 @@ static int dvbdmx_write(struct dmx_demux *demux, const char __user *buf, size_t
 {
 	struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
 	void *p;
-
-	if ((!demux->frontend) || (demux->frontend->source != DMX_MEMORY_FE))
+	
+	mutex_lock(&dvbdemux->mutex);
+	if ((!demux->frontend) || (demux->frontend->source != DMX_MEMORY_FE)) {
+		mutex_unlock(&dvbdemux->mutex);
 		return -EINVAL;
+	}
+	mutex_unlock(&dvbdemux->mutex);
 
 	p = memdup_user(buf, count);
 	if (IS_ERR(p))
-- 
2.34.1


             reply	other threads:[~2023-06-26  2:45 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-26  2:44 Tuo Li [this message]
2023-11-08  9:13 [PATCH] media: dvb-core: Fix a possible null-pointer dereference due to data race in dvbdmx_write() Tuo Li
2023-11-09  8:02 ` Dan Carpenter

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=20230626024429.994136-1-islituo@gmail.com \
    --to=islituo@gmail.com \
    --cc=baijiaju1990@outlook.com \
    --cc=bass@buaa.edu.cn \
    --cc=colin.i.king@gmail.com \
    --cc=hverkuil-cisco@xs4all.nl \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-media@vger.kernel.org \
    --cc=mchehab@kernel.org \
    --cc=tommaso.merciai@amarulasolutions.com \
    --cc=yongsuyoo0215@gmail.com \
    /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).