public inbox for linux-media@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] media: dvb-core: Fix a possible null-pointer dereference due to data race in dvbdmx_write()
@ 2023-06-26  2:44 Tuo Li
  0 siblings, 0 replies; 3+ messages in thread
From: Tuo Li @ 2023-06-26  2:44 UTC (permalink / raw)
  To: mchehab, yongsuyoo0215, tommaso.merciai, colin.i.king,
	hverkuil-cisco
  Cc: linux-media, linux-kernel, baijiaju1990, Tuo Li, BassCheck

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


^ permalink raw reply related	[flat|nested] 3+ messages in thread
* [PATCH] media: dvb-core: Fix a possible null-pointer dereference due to data race in dvbdmx_write()
@ 2023-11-08  9:13 Tuo Li
  2023-11-09  8:02 ` Dan Carpenter
  0 siblings, 1 reply; 3+ messages in thread
From: Tuo Li @ 2023-11-08  9:13 UTC (permalink / raw)
  To: mchehab, yongsuyoo0215
  Cc: linux-media, linux-kernel, baijiaju1990, Tuo Li, BassCheck

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 | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/drivers/media/dvb-core/dvb_demux.c b/drivers/media/dvb-core/dvb_demux.c
index 7c4d86bfdd6c..791db667e2ed 100644
--- a/drivers/media/dvb-core/dvb_demux.c
+++ b/drivers/media/dvb-core/dvb_demux.c
@@ -1141,8 +1141,11 @@ 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;
+	}
 
 	p = memdup_user(buf, count);
 	if (IS_ERR(p))
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2023-11-09  8:02 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-06-26  2:44 [PATCH] media: dvb-core: Fix a possible null-pointer dereference due to data race in dvbdmx_write() Tuo Li
  -- strict thread matches above, loose matches on Subject: below --
2023-11-08  9:13 Tuo Li
2023-11-09  8:02 ` Dan Carpenter

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox