Skip to contentSkip to conversation

media: dvb_vb2: fix possible out of bound access

PATCH Superseded Hangyu Hua 3 participants 5 messages Mar 24, 2022 – May 19, 2022 Media (V4L/DVB)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Hangyu Hua

author
#0 lore raw

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

---
 drivers/media/dvb-core/dvb_vb2.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
drivers/media/dvb-core/dvb_vb2.c +11 -0
index a1bd6d9c9223..f410800b92e7 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(q, 1, "buffer index out of range\n");
+		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(q, 1, "buffer index out of range\n");
+		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
#2 lore raw
Quoted 9 lines · Sergey Senozhatsky
Quoted 6 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.

Hi guys,

Two other patches for this module that I submitted at the same time as this one have been accepted. But this bug looks much worse than the other two. Is this patch forgotten to merge?

Thanks.

#3 lore raw
In reply to Hangyu Hua

Hi Hangyu,

It appears this patch fell through the cracks. It's certainly useful to have.

Quoted 15 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.

---
 drivers/media/dvb-core/dvb_vb2.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
drivers/media/dvb-core/dvb_vb2.c +0 -0
index a1bd6d9c9223..f410800b92e7 100644

drivers/media/dvb-core/dvb_vb2.c int dvb_vb2_reqbufs(struct dvb_vb2_ctx *ctx, struct dmx_requestbuffers *req) · line 360

Quoted 7 lines
drivers/media/dvb-core/dvb_vb2.c +4 -0
@@ -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(q, 1, "buffer index out of range\n");

However, this patch doesn't compile: dprintk in this source doesn't have a 'q' argument!

drivers/media/dvb-core/dvb_vb2.c int dvb_vb2_reqbufs(struct dvb_vb2_ctx *ctx, struct dmx_requestbuffers *req) · line 364

Quoted 4 lines
drivers/media/dvb-core/dvb_vb2.c +2 -0
+		return -EINVAL;
+	}
 	vb2_core_querybuf(&ctx->vb_q, b->index, b);
 	dprintk(3, "[%s] index=%d\n", ctx->name, b->index);

Also, to help debugging it prefixes the warnings with the ctx-name.

Can you post a v2 of this patch?

Regards,

Hans

Quoted 15 lines
drivers/media/dvb-core/dvb_vb2.c +5 -0
 	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(q, 1, "buffer index out of range\n");
+		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,
author
#4 lore raw
Quoted 22 lines · Hans Verkuil

Hi Hangyu,

It appears this patch fell through the cracks. It's certainly useful to have.

Quoted 16 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.

---
  drivers/media/dvb-core/dvb_vb2.c | 11 +++++++++++
  1 file changed, 11 insertions(+)
drivers/media/dvb-core/dvb_vb2.c +0 -0
index a1bd6d9c9223..f410800b92e7 100644
@@ -354,6 +354,12 @@ int dvb_vb2_reqbufs(struct dvb_vb2_ctx *ctx, struct dmx_requestbuffers *req)
 
Quoted 9 lines

drivers/media/dvb-core/dvb_vb2.c int dvb_vb2_reqbufs(struct dvb_vb2_ctx *ctx, struct dmx_requestbuffers *req) · line 360

Quoted 6 lines
drivers/media/dvb-core/dvb_vb2.c +4 -0
  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(q, 1, "buffer index out of range\n");

However, this patch doesn't compile: dprintk in this source doesn't have a 'q' argument!

Oops, i will fix this out.

Quoted 8 lines

drivers/media/dvb-core/dvb_vb2.c int dvb_vb2_reqbufs(struct dvb_vb2_ctx *ctx, struct dmx_requestbuffers *req) · line 364

Quoted 5 lines
drivers/media/dvb-core/dvb_vb2.c +2 -0
+		return -EINVAL;
+	}
  	vb2_core_querybuf(&ctx->vb_q, b->index, b);
  	dprintk(3, "[%s] index=%d\n", ctx->name, b->index);

Also, to help debugging it prefixes the warnings with the ctx-name.

Quoted 5 lines

Can you post a v2 of this patch?

Regards,

Hans

I will send a v2 later.

Thanks,
Hangyu

Quoted 16 lines
Quoted 16 lines
drivers/media/dvb-core/dvb_vb2.c +5 -0
  	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(q, 1, "buffer index out of range\n");
+		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,