media: videobuf2: make sure bytesused is smaller than the buffer size
Subsystems (9)
v2 cover media: videobuf2: make sure bytesused is smaller than the buffer size
Add a WARN_ON_ONCE in vb2_set_plane_payload if bytesused is bigger than length and clamp the bytesused to lenth.
Also change places where bytesused is set directly with that function. This help find/eliminate possible buffer overflow.
chnages since v2:
* replace WARN_ON with WARN_ON_ONCE
* clamp the bytesused to the buffer length if it is bigger
* add inline doc
Dafna Hirschfeld (2):
media: replace setting of bytesused with vb2_set_plane_payload
media: videobuf2: add WARN_ON if bytesused is bigger than buffer
length
drivers/media/platform/allegro-dvt/allegro-core.c | 2 +- drivers/media/platform/mtk-vcodec/mtk_vcodec_enc.c | 10 +++++----- drivers/media/test-drivers/vicodec/vicodec-core.c | 2 +- drivers/media/usb/go7007/go7007-driver.c | 2 +- drivers/staging/media/meson/vdec/vdec_helpers.c | 10 +++++----- include/media/videobuf2-core.h | 8 +++++++- 6 files changed, 20 insertions(+), 14 deletions(-)
Signature · 1 line
2.17.1
PATCH v2 1/2 media: replace setting of bytesused with vb2_set_plane_payload
In many places the bytesused field of struct vb2_buffer is set directly. Replace that with the function call vb2_set_plane_payload
--- drivers/media/platform/allegro-dvt/allegro-core.c | 2 +- drivers/media/platform/mtk-vcodec/mtk_vcodec_enc.c | 10 +++++----- drivers/media/test-drivers/vicodec/vicodec-core.c | 2 +- drivers/media/usb/go7007/go7007-driver.c | 2 +- drivers/staging/media/meson/vdec/vdec_helpers.c | 10 +++++----- 5 files changed, 13 insertions(+), 13 deletions(-)
drivers/media/platform/allegro-dvt/allegro-core.c +1 -1
@@ -2815,7 +2815,7 @@ static void allegro_buf_queue(struct vb2_buffer *vb)unsigned int i;for (i =0 ; i < vb->num_planes; i++) - vb->planes[i].bytesused =0 ; +vb2_set_plane_payload (vb, i,0 ); vbuf->field =V4L2_FIELD_NONE ; vbuf->sequence = channel->csequence++;
drivers/media/platform/mtk-vcodec/mtk_vcodec_enc.c +5 -5
@@ -965,7 +965,7 @@ static void vb2ops_venc_stop_streaming(struct vb2_queue *q)if (q->type ==V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE ) {while ((dst_buf =v4l2_m2m_dst_buf_remove (ctx->m2m_ctx))) { - dst_buf->vb2_buf.planes[0 ].bytesused =0 ; +vb2_set_plane_payload (&dst_buf->vb2_buf,0 ,0 );v4l2_m2m_buf_done (dst_buf,VB2_BUF_STATE_ERROR ); }/* STREAMOFF on the CAPTURE queue completes any ongoing flush */
@@ -1078,7 +1078,7 @@ static int mtk_venc_encode_header(void *priv) NULL, &bs_buf, &enc_result);if (ret) { - dst_buf->vb2_buf.planes[0 ].bytesused =0 ; +vb2_set_plane_payload (&dst_buf->vb2_buf,0 ,0 ); ctx->state =MTK_STATE_ABORT ;v4l2_m2m_buf_done (dst_buf,VB2_BUF_STATE_ERROR );mtk_v4l2_err ("venc_if_encode failed=%d" , ret);
@@ -1093,7 +1093,7 @@ static int mtk_venc_encode_header(void *priv) } ctx->state =MTK_STATE_HEADER ; - dst_buf->vb2_buf.planes[0 ].bytesused = enc_result.bs_size; +vb2_set_plane_payload (&dst_buf->vb2_buf,0 , enc_result.bs_size);v4l2_m2m_buf_done (dst_buf,VB2_BUF_STATE_DONE );return 0 ;
@@ -1242,12 +1242,12 @@ static void mtk_venc_worker(struct work_struct *work)if (ret) {v4l2_m2m_buf_done (src_buf,VB2_BUF_STATE_ERROR ); - dst_buf->vb2_buf.planes[0 ].bytesused =0 ; +vb2_set_plane_payload (&dst_buf->vb2_buf,0 ,0 );v4l2_m2m_buf_done (dst_buf,VB2_BUF_STATE_ERROR );mtk_v4l2_err ("venc_if_encode failed=%d" , ret); }else {v4l2_m2m_buf_done (src_buf,VB2_BUF_STATE_DONE ); - dst_buf->vb2_buf.planes[0 ].bytesused = enc_result.bs_size; +vb2_set_plane_payload (&dst_buf->vb2_buf,0 , enc_result.bs_size);v4l2_m2m_buf_done (dst_buf,VB2_BUF_STATE_DONE );mtk_v4l2_debug (2 ,"venc_if_encode bs size=%d" , enc_result.bs_size);
drivers/media/test-drivers/vicodec/vicodec-core.c +1 -1
@@ -1443,7 +1443,7 @@ static void vicodec_buf_queue(struct vb2_buffer *vb)unsigned int i;for (i =0 ; i < vb->num_planes; i++) - vb->planes[i].bytesused =0 ; +vb2_set_plane_payload (vb, i,0 ); vbuf->field =V4L2_FIELD_NONE ; vbuf->sequence =
drivers/media/usb/go7007/go7007-driver.c +1 -1
@@ -516,7 +516,7 @@ void go7007_parse_video_stream(struct go7007 *go, u8 *buf, int length)if (vb && vb->vb.vb2_buf.planes[0 ].bytesused >=GO7007_BUF_SIZE -3 ) {v4l2_info (&go->v4l2_dev,"dropping oversized frame\n" ); - vb->vb.vb2_buf.planes[0 ].bytesused =0 ; +vb2_set_plane_payload (&vb->vb.vb2_buf,0 ,0 ); vb->frame_offset =0 ; vb->modet_active =0 ; vb = go->active_buf =NULL ;
drivers/staging/media/meson/vdec/vdec_helpers.c +5 -5
@@ -276,13 +276,13 @@ static void dst_buf_done(struct amvdec_session *sess,switch (sess->pixfmt_cap) {case V4L2_PIX_FMT_NV12M : - vbuf->vb2_buf.planes[0 ].bytesused = output_size; - vbuf->vb2_buf.planes[1 ].bytesused = output_size /2 ; +vb2_set_plane_payload (vbuf->vb2_buf,0 , output_size); +vb2_set_plane_payload (vbuf->vb2_buf,1 , output_size /2 );break ;case V4L2_PIX_FMT_YUV420M : - vbuf->vb2_buf.planes[0 ].bytesused = output_size; - vbuf->vb2_buf.planes[1 ].bytesused = output_size /4 ; - vbuf->vb2_buf.planes[2 ].bytesused = output_size /4 ; +vb2_set_plane_payload (vbuf->vb2_buf,0 , output_size); +vb2_set_plane_payload (vbuf->vb2_buf,1 , output_size /4 ); +vb2_set_plane_payload (vbuf->vb2_buf,2 , output_size /4 );break ; }
Signature · 1 line
2.17.1
kernel test robot
build botRe: [PATCH v2 1/2] media: replace setting of bytesused with vb2_set_plane_payload
I love your patch! Yet something to improve:
Hi Dafna,
I love your patch! Yet something to improve:
[auto build test ERROR on media-tree/master]
[also build test ERROR on v5.16-rc1 next-20211115]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]
url: https://github.com/0day-ci/linux/commits/Dafna-Hirschfeld/media-videobuf2-make-sure-bytesused-is-smaller-than-the-buffer-size/20211111-232720 base: git://linuxtv.org/media_tree.git master config: microblaze-randconfig-r013-20211115 (attached as .config) compiler: microblaze-linux-gcc (GCC) 11.2.0
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
chmod +x ~/bin/make.cross
# https://github.com/0day-ci/linux/commit/6934ed79a3550b00a9f9a6ae14a283a59a10f566
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Dafna-Hirschfeld/media-videobuf2-make-sure-bytesused-is-smaller-than-the-buffer-size/20211111-232720 git checkout 6934ed79a3550b00a9f9a6ae14a283a59a10f566
# save the attached .config to linux build tree
mkdir build_dir
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-11.2.0 make.cross O=build_dir ARCH=microblaze SHELL=/bin/bash drivers/staging/media/meson/vdec/
If you fix the issue, kindly add following tag as appropriate
All errors (new ones prefixed by >>):
drivers/staging/media/meson/vdec/vdec_helpers.c: In function 'dst_buf_done':
>> drivers/staging/media/meson/vdec/vdec_helpers.c:279:43: error: incompatible type for argument 1 of 'vb2_set_plane_payload'
279 | vb2_set_plane_payload(vbuf->vb2_buf, 0, output_size);
| ~~~~^~~~~~~~~
| |
| struct vb2_buffer
In file included from include/media/videobuf2-v4l2.h:16,
from include/media/v4l2-mem2mem.h:16,
from drivers/staging/media/meson/vdec/vdec_helpers.c:8:
include/media/videobuf2-core.h:1155:61: note: expected 'struct vb2_buffer *' but argument is of type 'struct vb2_buffer'
1155 | static inline void vb2_set_plane_payload(struct vb2_buffer *vb,
| ~~~~~~~~~~~~~~~~~~~^~
drivers/staging/media/meson/vdec/vdec_helpers.c:280:43: error: incompatible type for argument 1 of 'vb2_set_plane_payload'
280 | vb2_set_plane_payload(vbuf->vb2_buf, 1, output_size / 2);
| ~~~~^~~~~~~~~
| |
| struct vb2_buffer
In file included from include/media/videobuf2-v4l2.h:16,
from include/media/v4l2-mem2mem.h:16,
from drivers/staging/media/meson/vdec/vdec_helpers.c:8:
include/media/videobuf2-core.h:1155:61: note: expected 'struct vb2_buffer *' but argument is of type 'struct vb2_buffer'
1155 | static inline void vb2_set_plane_payload(struct vb2_buffer *vb,
| ~~~~~~~~~~~~~~~~~~~^~
drivers/staging/media/meson/vdec/vdec_helpers.c:283:43: error: incompatible type for argument 1 of 'vb2_set_plane_payload'
283 | vb2_set_plane_payload(vbuf->vb2_buf, 0, output_size);
| ~~~~^~~~~~~~~
| |
| struct vb2_buffer
In file included from include/media/videobuf2-v4l2.h:16,
from include/media/v4l2-mem2mem.h:16,
from drivers/staging/media/meson/vdec/vdec_helpers.c:8:
include/media/videobuf2-core.h:1155:61: note: expected 'struct vb2_buffer *' but argument is of type 'struct vb2_buffer'
1155 | static inline void vb2_set_plane_payload(struct vb2_buffer *vb,
| ~~~~~~~~~~~~~~~~~~~^~
drivers/staging/media/meson/vdec/vdec_helpers.c:284:43: error: incompatible type for argument 1 of 'vb2_set_plane_payload'
284 | vb2_set_plane_payload(vbuf->vb2_buf, 1, output_size / 4);
| ~~~~^~~~~~~~~
| |
| struct vb2_buffer
In file included from include/media/videobuf2-v4l2.h:16,
from include/media/v4l2-mem2mem.h:16,
from drivers/staging/media/meson/vdec/vdec_helpers.c:8:
include/media/videobuf2-core.h:1155:61: note: expected 'struct vb2_buffer *' but argument is of type 'struct vb2_buffer'
1155 | static inline void vb2_set_plane_payload(struct vb2_buffer *vb,
| ~~~~~~~~~~~~~~~~~~~^~
drivers/staging/media/meson/vdec/vdec_helpers.c:285:43: error: incompatible type for argument 1 of 'vb2_set_plane_payload'
285 | vb2_set_plane_payload(vbuf->vb2_buf, 2, output_size / 4);
| ~~~~^~~~~~~~~
| |
| struct vb2_buffer
In file included from include/media/videobuf2-v4l2.h:16,
from include/media/v4l2-mem2mem.h:16,
from drivers/staging/media/meson/vdec/vdec_helpers.c:8: include/media/videobuf2-core.h:1155:61: note: expected 'struct vb2_buffer *' but argument is of type 'struct vb2_buffer'
1155 | static inline void vb2_set_plane_payload(struct vb2_buffer *vb,
| ~~~~~~~~~~~~~~~~~~~^~
vim +/vb2_set_plane_payload +279 drivers/staging/media/meson/vdec/vdec_helpers.c
268
269 static void dst_buf_done(struct amvdec_session *sess,
270 struct vb2_v4l2_buffer *vbuf,
271 u32 field, u64 timestamp,
272 struct v4l2_timecode timecode, u32 flags)
273 {
274 struct device *dev = sess->core->dev_dec;
275 u32 output_size = amvdec_get_output_size(sess);
276
277 switch (sess->pixfmt_cap) {
278 case V4L2_PIX_FMT_NV12M:
> 279 vb2_set_plane_payload(vbuf->vb2_buf, 0, output_size);
280 vb2_set_plane_payload(vbuf->vb2_buf, 1, output_size / 2);
281 break;
282 case V4L2_PIX_FMT_YUV420M:
283 vb2_set_plane_payload(vbuf->vb2_buf, 0, output_size);
284 vb2_set_plane_payload(vbuf->vb2_buf, 1, output_size / 4);
285 vb2_set_plane_payload(vbuf->vb2_buf, 2, output_size / 4);
286 break;
287 }
288
289 vbuf->vb2_buf.timestamp = timestamp;
290 vbuf->sequence = sess->sequence_cap++;
291 vbuf->flags = flags;
292 vbuf->timecode = timecode;
293
294 if (sess->should_stop &&
295 atomic_read(&sess->esparser_queued_bufs) <= 1) {
296 const struct v4l2_event ev = { .type = V4L2_EVENT_EOS };
297
298 dev_dbg(dev, "Signaling EOS, sequence_cap = %u\n",
299 sess->sequence_cap - 1);
300 v4l2_event_queue_fh(&sess->fh, &ev);
301 vbuf->flags |= V4L2_BUF_FLAG_LAST;
302 } else if (sess->status == STATUS_NEEDS_RESUME) {
303 /* Mark LAST for drained show frames during a source change */
304 vbuf->flags |= V4L2_BUF_FLAG_LAST;
305 sess->sequence_cap = 0;
306 } else if (sess->should_stop)
307 dev_dbg(dev, "should_stop, %u bufs remain\n",
308 atomic_read(&sess->esparser_queued_bufs));
309
310 dev_dbg(dev, "Buffer %u done, ts = %llu, flags = %08X\n",
311 vbuf->vb2_buf.index, timestamp, flags);
312 vbuf->field = field;
313 v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_DONE);
314
315 /* Buffer done probably means the vififo got freed */
316 schedule_work(&sess->esparser_queue_work);
317 }
318
---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org
PATCH v2 2/2 media: videobuf2: add WARN_ON if bytesused is bigger than buffer length
In reply to Dafna HirschfeldIn function vb2_set_plane_payload, report if the
given bytesused is bigger than the buffer size,
and clamp it to the buffer size.
--- include/media/videobuf2-core.h | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
include/media/videobuf2-core.h +7 -1
@@ -1155,8 +1155,14 @@ static inline void *vb2_get_drv_priv(struct vb2_queue *q)static inline void vb2_set_plane_payload (struct vb2_buffer *vb,unsigned int plane_no,unsigned long size) { -if (plane_no < vb->num_planes) +/* +* size must never be larger than the buffer length, so +* warn and clamp to the buffer length if that's the case. +*/ +if (plane_no < vb->num_planes) { +WARN_ON_ONCE (size > vb->planes[plane_no].length); vb->planes[plane_no].bytesused = size; + } }/**
Signature · 1 line
2.17.1
Re: [PATCH v2 2/2] media: videobuf2: add WARN_ON if bytesused is bigger than buffer length
Hi Dafna,
Thank you for the patch.
Quoted 5 lines · Dafna Hirschfeld
In function
vb2_set_plane_payload, report if the
given bytesused is bigger than the buffer size,
and clamp it to the buffer size.Signed-off-by Dafna Hirschfeld <[email protected]>
Quoted 24 lines
--- include/media/videobuf2-core.h | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)include/media/videobuf2-core.h +7
-1@@ -1155,8 +1155,14 @@ static inline void *vb2_get_drv_priv(struct vb2_queue *q)static inline void vb2_set_plane_payload (struct vb2_buffer *vb,unsigned int plane_no,unsigned long size) { -if (plane_no < vb->num_planes) +/* +* size must never be larger than the buffer length, so +* warn and clamp to the buffer length if that's the case. +*/ +if (plane_no < vb->num_planes) { +WARN_ON_ONCE (size > vb->planes[plane_no].length); vb->planes[plane_no].bytesused = size; + } }/**
Signature · 3 lines
Regards, Laurent Pinchart
Re: [PATCH v2 2/2] media: videobuf2: add WARN_ON if bytesused is bigger than buffer length
In reply to Dafna HirschfeldQuoted 14 lines · Dafna Hirschfeld
In function
vb2_set_plane_payload, report if the
given bytesused is bigger than the buffer size,
and clamp it to the buffer size.Signed-off-by Dafna Hirschfeld <[email protected]>--- include/media/videobuf2-core.h | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)include/media/videobuf2-core.h +0
-0
include/media/videobuf2-core.h static inline void *vb2_get_drv_priv(struct vb2_queue *q) · line 1163
Quoted 10 lines
include/media/videobuf2-core.h +6
-1@@ -1155,8 +1155,14 @@ static inline void *vb2_get_drv_priv(struct vb2_queue *q)static inline void vb2_set_plane_payload (struct vb2_buffer *vb,unsigned int plane_no,unsigned long size) { -if (plane_no < vb->num_planes) +/* +* size must never be larger than the buffer length, so +* warn and clamp to the buffer length if that's the case. +*/ +if (plane_no < vb->num_planes) { +WARN_ON_ONCE (size > vb->planes[plane_no].length);
This doesn't clamp size to the buffer length, so this needs to be added.
Also, the subject talks about WARN_ON instead of WARN_ON_ONCE.
Regards,
Hans
Quoted 6 lines
include/media/videobuf2-core.h +1
-0vb->planes[plane_no].bytesused = size; + } }/**