linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] media: vidtv: Fix a wild pointer dereference in vidtv_channel_pmt_match_sections()
@ 2021-11-30 16:39 Zhou Qingyang
  2021-12-02 13:42 ` Hans Verkuil
  0 siblings, 1 reply; 3+ messages in thread
From: Zhou Qingyang @ 2021-11-30 16:39 UTC (permalink / raw)
  To: zhou1615
  Cc: kjlu, Daniel W. S. Almeida, Mauro Carvalho Chehab, linux-media,
	linux-kernel

In vidtv_channel_pmt_match_sections(), vidtv_psi_pmt_stream_init() is
assigned to tail and &tail->descriptor is used in
vidtv_psi_desc_assign(). There is a dereference of &tail->descriptor
in vidtv_psi_desc_assign(), which could lead to a wild pointer
dereference onfailure of vidtv_psi_pmt_stream_init().

Fix this bug by adding a check of tail.

This bug was found by a static analyzer. The analysis employs
differential checking to identify inconsistent security operations
(e.g., checks or kfrees) between two code paths and confirms that the
inconsistent operations are not recovered in the current function or
the callers, so they constitute bugs.

Note that, as a bug found by static analysis, it can be a false
positive or hard to trigger. Multiple researchers have cross-reviewed
the bug.

Builds with CONFIG_DVB_VIDTV=m show no new warnings,
and our static analyzer no longer warns about this code.

Fixes: f90cf6079bf6 ("media: vidtv: add a bridge driver")
Signed-off-by: Zhou Qingyang <zhou1615@umn.edu>
---
 drivers/media/test-drivers/vidtv/vidtv_channel.c | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/drivers/media/test-drivers/vidtv/vidtv_channel.c b/drivers/media/test-drivers/vidtv/vidtv_channel.c
index 7838e6272712..f2faa5504642 100644
--- a/drivers/media/test-drivers/vidtv/vidtv_channel.c
+++ b/drivers/media/test-drivers/vidtv/vidtv_channel.c
@@ -318,6 +318,10 @@ vidtv_channel_pmt_match_sections(struct vidtv_channel *channels,
 	struct vidtv_psi_table_pmt_stream *s = NULL;
 	struct vidtv_channel *cur_chnl = channels;
 	struct vidtv_psi_desc *desc = NULL;
+	struct vidtv_mux *m = container_of(&channels,
+					struct vidtv_mux,
+					channels);
+
 	u16 e_pid; /* elementary stream pid */
 	u16 curr_id;
 	u32 j;
@@ -341,6 +345,13 @@ vidtv_channel_pmt_match_sections(struct vidtv_channel *channels,
 					tail = vidtv_psi_pmt_stream_init(tail,
 									 s->type,
 									 e_pid);
+
+					if (!tail) {
+						vidtv_psi_pmt_stream_destroy(head);
+						dev_warn_ratelimited(m->dev,
+							"No enough memory for vidtv_psi_pmt_stream_init");
+						return;
+					}

 					if (!head)
 						head = tail;
-- 
2.25.1


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

* Re: [PATCH] media: vidtv: Fix a wild pointer dereference in vidtv_channel_pmt_match_sections()
  2021-11-30 16:39 [PATCH] media: vidtv: Fix a wild pointer dereference in vidtv_channel_pmt_match_sections() Zhou Qingyang
@ 2021-12-02 13:42 ` Hans Verkuil
  2021-12-02 17:07   ` Daniel Almeida
  0 siblings, 1 reply; 3+ messages in thread
From: Hans Verkuil @ 2021-12-02 13:42 UTC (permalink / raw)
  To: Zhou Qingyang
  Cc: kjlu, Daniel W. S. Almeida, Mauro Carvalho Chehab, linux-media,
	linux-kernel

On 30/11/2021 17:39, Zhou Qingyang wrote:
> In vidtv_channel_pmt_match_sections(), vidtv_psi_pmt_stream_init() is
> assigned to tail and &tail->descriptor is used in
> vidtv_psi_desc_assign(). There is a dereference of &tail->descriptor
> in vidtv_psi_desc_assign(), which could lead to a wild pointer
> dereference onfailure of vidtv_psi_pmt_stream_init().

onfailure -> on failure

> 
> Fix this bug by adding a check of tail.
> 
> This bug was found by a static analyzer. The analysis employs
> differential checking to identify inconsistent security operations
> (e.g., checks or kfrees) between two code paths and confirms that the
> inconsistent operations are not recovered in the current function or
> the callers, so they constitute bugs.
> 
> Note that, as a bug found by static analysis, it can be a false
> positive or hard to trigger. Multiple researchers have cross-reviewed
> the bug.
> 
> Builds with CONFIG_DVB_VIDTV=m show no new warnings,
> and our static analyzer no longer warns about this code.
> 
> Fixes: f90cf6079bf6 ("media: vidtv: add a bridge driver")
> Signed-off-by: Zhou Qingyang <zhou1615@umn.edu>
> ---
>  drivers/media/test-drivers/vidtv/vidtv_channel.c | 11 +++++++++++
>  1 file changed, 11 insertions(+)
> 
> diff --git a/drivers/media/test-drivers/vidtv/vidtv_channel.c b/drivers/media/test-drivers/vidtv/vidtv_channel.c
> index 7838e6272712..f2faa5504642 100644
> --- a/drivers/media/test-drivers/vidtv/vidtv_channel.c
> +++ b/drivers/media/test-drivers/vidtv/vidtv_channel.c
> @@ -318,6 +318,10 @@ vidtv_channel_pmt_match_sections(struct vidtv_channel *channels,
>  	struct vidtv_psi_table_pmt_stream *s = NULL;
>  	struct vidtv_channel *cur_chnl = channels;
>  	struct vidtv_psi_desc *desc = NULL;
> +	struct vidtv_mux *m = container_of(&channels,
> +					struct vidtv_mux,
> +					channels);
> +
>  	u16 e_pid; /* elementary stream pid */
>  	u16 curr_id;
>  	u32 j;
> @@ -341,6 +345,13 @@ vidtv_channel_pmt_match_sections(struct vidtv_channel *channels,
>  					tail = vidtv_psi_pmt_stream_init(tail,
>  									 s->type,
>  									 e_pid);
> +
> +					if (!tail) {
> +						vidtv_psi_pmt_stream_destroy(head);

I honestly can't tell if this is the right thing to do.

Daniel, can you take a look at this?

> +						dev_warn_ratelimited(m->dev,
> +							"No enough memory for vidtv_psi_pmt_stream_init");

No -> Not
Add newline at the end of the string.

> +						return;
> +					}
> 
>  					if (!head)
>  						head = tail;
> 

Regards,

	Hans

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

* Re: [PATCH] media: vidtv: Fix a wild pointer dereference in vidtv_channel_pmt_match_sections()
  2021-12-02 13:42 ` Hans Verkuil
@ 2021-12-02 17:07   ` Daniel Almeida
  0 siblings, 0 replies; 3+ messages in thread
From: Daniel Almeida @ 2021-12-02 17:07 UTC (permalink / raw)
  To: Hans Verkuil, Zhou Qingyang
  Cc: kjlu, Mauro Carvalho Chehab, linux-media, linux-kernel

Hi Zhou Qingyang, thanks for the patch.

Yes this is a real issue.

> +
> +					if (!tail) {
> +						vidtv_psi_pmt_stream_destroy(head);
> +						dev_warn_ratelimited(m->dev,
> +							"No enough memory for vidtv_psi_pmt_stream_init");
> +						return;
> +					}

Yes this is the right thing to do, as vidtv_psi_pmt_stream_destroy will 
free the stream chain and all descriptors associated with it, if any.

By then aborting out of this function, we will have a PMT section 
without any streams. This is OK if the allocation failed.

Reviewed-by Daniel Almeida <daniel.almeida@collabora.com>

Thanks,

-- Daniel

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

end of thread, other threads:[~2021-12-02 17:07 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-11-30 16:39 [PATCH] media: vidtv: Fix a wild pointer dereference in vidtv_channel_pmt_match_sections() Zhou Qingyang
2021-12-02 13:42 ` Hans Verkuil
2021-12-02 17:07   ` Daniel Almeida

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