Skip to contentSkip to conversation

media: dvb_vb2: fix possible out of bound access

PATCH In review Hangyu Hua 2 participants 4 messages May 19, 2022 – Sep 27, 2022 Media (V4L/DVB)

lore
Share & tools
lore

Hangyu Hua

author
#0 lore raw

PATCH v2 1/1 media: dvb_vb2: fix possible out of bound access

vb2_core_qbuf and vb2_core_querybuf don't check the range of b->index controlled by the user.

Fix this by adding range checking code before using them.

Fixes 57868acc369a ("media: videobuf2: Add new uAPI for DVB streaming I/O")

---

v2:
1. fix inappropriate use of dprintk.
2. add "fixes" tag

 drivers/media/dvb-core/dvb_vb2.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
drivers/media/dvb-core/dvb_vb2.c +11 -0
index a1bd6d9c9223..909df82fed33 100644
@@ -354,6 +354,12 @@ int dvb_vb2_reqbufs(struct dvb_vb2_ctx *ctx, struct dmx_requestbuffers *req)
 
 int dvb_vb2_querybuf(struct dvb_vb2_ctx *ctx, struct dmx_buffer *b)
 {
+	struct vb2_queue *q = &ctx->vb_q;
+
+	if (b->index >= q->num_buffers) {
+		dprintk(1, "[%s] buffer index out of range\n", ctx->name);
+		return -EINVAL;
+	}
 	vb2_core_querybuf(&ctx->vb_q, b->index, b);
 	dprintk(3, "[%s] index=%d\n", ctx->name, b->index);
 	return 0;
@@ -378,8 +384,13 @@ int dvb_vb2_expbuf(struct dvb_vb2_ctx *ctx, struct dmx_exportbuffer *exp)
 
 int dvb_vb2_qbuf(struct dvb_vb2_ctx *ctx, struct dmx_buffer *b)
 {
+	struct vb2_queue *q = &ctx->vb_q;
 	int ret;
 
+	if (b->index >= q->num_buffers) {
+		dprintk(1, "[%s] buffer index out of range\n", ctx->name);
+		return -EINVAL;
+	}
 	ret = vb2_core_qbuf(&ctx->vb_q, b->index, b, NULL);
 	if (ret) {
 		dprintk(1, "[%s] index=%d errno=%d\n", ctx->name,
Signature · 1 line
2.25.1
author
#1 lore raw
Quoted 38 lines · Hangyu Hua

vb2_core_qbuf and vb2_core_querybuf don't check the range of b->index controlled by the user.

Fix this by adding range checking code before using them.

Fixes 57868acc369a ("media: videobuf2: Add new uAPI for DVB streaming I/O")

---

v2:
1. fix inappropriate use of dprintk.
2. add "fixes" tag

  drivers/media/dvb-core/dvb_vb2.c | 11 +++++++++++
  1 file changed, 11 insertions(+)
drivers/media/dvb-core/dvb_vb2.c +6 -0
index a1bd6d9c9223..909df82fed33 100644
@@ -354,6 +354,12 @@ int dvb_vb2_reqbufs(struct dvb_vb2_ctx *ctx, struct dmx_requestbuffers *req)
 
  int dvb_vb2_querybuf(struct dvb_vb2_ctx *ctx, struct dmx_buffer *b)
  {
+	struct vb2_queue *q = &ctx->vb_q;
+
+	if (b->index >= q->num_buffers) {
+		dprintk(1, "[%s] buffer index out of range\n", ctx->name);
+		return -EINVAL;
+	}
  	vb2_core_querybuf(&ctx->vb_q, b->index, b);
  	dprintk(3, "[%s] index=%d\n", ctx->name, b->index);
  	return 0;
@@ -378,8 +384,13 @@ int dvb_vb2_expbuf(struct dvb_vb2_ctx *ctx, struct dmx_exportbuffer *exp)
 
  int dvb_vb2_qbuf(struct dvb_vb2_ctx *ctx, struct dmx_buffer *b)
  {

drivers/media/dvb-core/dvb_vb2.c int dvb_vb2_expbuf(struct dvb_vb2_ctx *ctx, struct dmx_exportbuffer *exp) · line 396

Quoted 10 lines
drivers/media/dvb-core/dvb_vb2.c +5 -0
+	struct vb2_queue *q = &ctx->vb_q;
  	int ret;
 
+	if (b->index >= q->num_buffers) {
+		dprintk(1, "[%s] buffer index out of range\n", ctx->name);
+		return -EINVAL;
+	}
  	ret = vb2_core_qbuf(&ctx->vb_q, b->index, b, NULL);
  	if (ret) {
  		dprintk(1, "[%s] index=%d errno=%d\n", ctx->name,

Hi guys,

Looks like this patch was forgotten to to merge into master branch. This bug still in:
https://git.linuxtv.org/media_tree.git/tree/drivers/media/dvb-core/dvb_vb2.c#n355
and
https://git.linuxtv.org/media_tree.git/tree/drivers/media/dvb-core/dvb_vb2.c#n379

Thanks,
Hangyu

#2 lore raw
Quoted 49 lines · Hangyu Hua
Quoted 35 lines · Hangyu Hua

vb2_core_qbuf and vb2_core_querybuf don't check the range of b->index controlled by the user.

Fix this by adding range checking code before using them.

Fixes 57868acc369a ("media: videobuf2: Add new uAPI for DVB streaming I/O")

---

v2:
1. fix inappropriate use of dprintk.
2. add "fixes" tag

  drivers/media/dvb-core/dvb_vb2.c | 11 +++++++++++

  1 file changed, 11 insertions(+)
drivers/media/dvb-core/dvb_vb2.c +6 -0
index a1bd6d9c9223..909df82fed33 100644
@@ -354,6 +354,12 @@ int dvb_vb2_reqbufs(struct dvb_vb2_ctx *ctx, struct dmx_requestbuffers *req)
     int dvb_vb2_querybuf(struct dvb_vb2_ctx *ctx, struct dmx_buffer *b)
   {
+    struct vb2_queue *q = &ctx->vb_q;
+
+    if (b->index >= q->num_buffers) {
+        dprintk(1, "[%s] buffer index out of range\n", ctx->name);
+        return -EINVAL;
+    }
       vb2_core_querybuf(&ctx->vb_q, b->index, b);
       dprintk(3, "[%s] index=%d\n", ctx->name, b->index);
       return 0;
@@ -378,8 +384,13 @@ int dvb_vb2_expbuf(struct dvb_vb2_ctx *ctx, struct dmx_exportbuffer *exp)
     int dvb_vb2_qbuf(struct dvb_vb2_ctx *ctx, struct dmx_buffer *b)

drivers/media/dvb-core/dvb_vb2.c int dvb_vb2_expbuf(struct dvb_vb2_ctx *ctx, struct dmx_exportbuffer *exp) · line 394

Quoted 10 lines
drivers/media/dvb-core/dvb_vb2.c +4 -0
   {
+    struct vb2_queue *q = &ctx->vb_q;
       int ret;
   +    if (b->index >= q->num_buffers) {
+        dprintk(1, "[%s] buffer index out of range\n", ctx->name);
+        return -EINVAL;
+    }
       ret = vb2_core_qbuf(&ctx->vb_q, b->index, b, NULL);
       if (ret) {
           dprintk(1, "[%s] index=%d errno=%d\n", ctx->name,

Hi guys,

Quoted 7 lines

Looks like this patch was forgotten to to merge into master branch. This bug still in:
https://git.linuxtv.org/media_tree.git/tree/drivers/media/dvb-core/dvb_vb2.c#n355
and
https://git.linuxtv.org/media_tree.git/tree/drivers/media/dvb-core/dvb_vb2.c#n379

Thanks,
Hangyu

That's weird, it was part of this pull request:

https://patchwork.linuxtv.org/project/linux-media/patch/[email protected]/

But none of the patches in that PR ever made it to upstream. Something went very wrong with that PR.

I'm preparing a new pull request.

Thank you very much for notifying me!

Regards,

Hans

#3 lore raw

Hi Hangyu,

Quoted 60 lines · Hans Verkuil
Quoted 49 lines · Hangyu Hua
Quoted 35 lines · Hangyu Hua

vb2_core_qbuf and vb2_core_querybuf don't check the range of b->index controlled by the user.

Fix this by adding range checking code before using them.

Fixes 57868acc369a ("media: videobuf2: Add new uAPI for DVB streaming I/O")

---

v2:
1. fix inappropriate use of dprintk.
2. add "fixes" tag

  drivers/media/dvb-core/dvb_vb2.c | 11 +++++++++++

  1 file changed, 11 insertions(+)
drivers/media/dvb-core/dvb_vb2.c +6 -0
index a1bd6d9c9223..909df82fed33 100644
@@ -354,6 +354,12 @@ int dvb_vb2_reqbufs(struct dvb_vb2_ctx *ctx, struct dmx_requestbuffers *req)
     int dvb_vb2_querybuf(struct dvb_vb2_ctx *ctx, struct dmx_buffer *b)
   {
+    struct vb2_queue *q = &ctx->vb_q;
+
+    if (b->index >= q->num_buffers) {
+        dprintk(1, "[%s] buffer index out of range\n", ctx->name);
+        return -EINVAL;
+    }
       vb2_core_querybuf(&ctx->vb_q, b->index, b);
       dprintk(3, "[%s] index=%d\n", ctx->name, b->index);
       return 0;
@@ -378,8 +384,13 @@ int dvb_vb2_expbuf(struct dvb_vb2_ctx *ctx, struct dmx_exportbuffer *exp)
     int dvb_vb2_qbuf(struct dvb_vb2_ctx *ctx, struct dmx_buffer *b)

drivers/media/dvb-core/dvb_vb2.c int dvb_vb2_expbuf(struct dvb_vb2_ctx *ctx, struct dmx_exportbuffer *exp) · line 394

Quoted 10 lines
drivers/media/dvb-core/dvb_vb2.c +4 -0
   {
+    struct vb2_queue *q = &ctx->vb_q;
       int ret;
   +    if (b->index >= q->num_buffers) {
+        dprintk(1, "[%s] buffer index out of range\n", ctx->name);
+        return -EINVAL;
+    }
       ret = vb2_core_qbuf(&ctx->vb_q, b->index, b, NULL);
       if (ret) {
           dprintk(1, "[%s] index=%d errno=%d\n", ctx->name,

Hi guys,

Quoted 7 lines

Looks like this patch was forgotten to to merge into master branch. This bug still in:
https://git.linuxtv.org/media_tree.git/tree/drivers/media/dvb-core/dvb_vb2.c#n355
and
https://git.linuxtv.org/media_tree.git/tree/drivers/media/dvb-core/dvb_vb2.c#n379

Thanks,
Hangyu

That's weird, it was part of this pull request:

Quoted 8 lines

https://patchwork.linuxtv.org/project/linux-media/patch/[email protected]/

But none of the patches in that PR ever made it to upstream. Something went very wrong with that PR.

I'm preparing a new pull request.

Thank you very much for notifying me!

Mauro discovered that a small number of patches were never pushed to our git trees, and so indeed got lost. They have now been merged. This patch has been really unlucky:
it took a long time for the original patch to be reviewed, and then the PR itself fell through the cracks :-(

Again, thank you very much for double checking this!

Regards,

Hans