Skip to contentSkip to conversation

OOB read and write in mdp_prepare_buffer

PATCH In review [email protected] 5 participants 8 messages Jun 27, 2023 – Jun 29, 2023

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 OOB read and write in mdp_prepare_buffer

From: yqsun1997 <[email protected]>

Because format in struct img_image_buffer max index is IMG_MAX_PLANES ==3, The num_planes max index is 8.so will be OOB like in mdp_prepare_buffer.

static void mdp_prepare_buffer(struct img_image_buffer *b,
struct mdp_frame *frame, struct vb2_buffer *vb)

{
        struct v4l2_pix_format_mplane *pix_mp = &frame->format.fmt.pix_mp;
        unsigned int i;

        b->format.colorformat = frame->mdp_fmt->mdp_color;
        b->format.ycbcr_prof = frame->ycbcr_prof;
        for (i = 0; i < pix_mp->num_planes; ++i) {
                u32 stride = mdp_fmt_get_stride(frame->mdp_fmt,
                        pix_mp->plane_fmt[i].bytesperline, i);

                b->format.plane_fmt[i].stride = stride;  //oob
                ......
---
 drivers/media/platform/mediatek/mdp3/mtk-mdp3-type.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/media/platform/mediatek/mdp3/mtk-mdp3-type.h +1 -1
index ae0396806..e2e991a34 100644
@@ -11,7 +11,7 @@
 
 #define IMG_MAX_HW_INPUTS	3
 #define IMG_MAX_HW_OUTPUTS	4
-#define IMG_MAX_PLANES		3
+#define IMG_MAX_PLANES		8
 #define IMG_MAX_COMPONENTS	20
 
 struct img_crop {
Signature · 1 line
2.39.2
#1 lore raw

Hi,

Quoted 4 lines · On Tue, Jun 27, 2023 at 04:27:31PM +0800, [email protected] wrote:

From: yqsun1997 <[email protected]>

Because format in struct img_image_buffer max index is IMG_MAX_PLANES ==3, The num_planes max index is 8.so will be OOB like in mdp_prepare_buffer.

Similarly as your other patch, could you describe why you need to increase the IMG_MAX_PLANES while I suspect your driver only needs to deal with 3 planes. While the maximum num_planes value that can be given by the user is 8, this has to be first compared to the configured format prior to reaching this function.

Quoted 37 lines

static void mdp_prepare_buffer(struct img_image_buffer *b,
struct mdp_frame *frame, struct vb2_buffer *vb)

{
        struct v4l2_pix_format_mplane *pix_mp = &frame->format.fmt.pix_mp;
        unsigned int i;

        b->format.colorformat = frame->mdp_fmt->mdp_color;
        b->format.ycbcr_prof = frame->ycbcr_prof;
        for (i = 0; i < pix_mp->num_planes; ++i) {
                u32 stride = mdp_fmt_get_stride(frame->mdp_fmt,
                        pix_mp->plane_fmt[i].bytesperline, i);

                b->format.plane_fmt[i].stride = stride;  //oob
                ......
---
 drivers/media/platform/mediatek/mdp3/mtk-mdp3-type.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/media/platform/mediatek/mdp3/mtk-mdp3-type.h +1 -1
index ae0396806..e2e991a34 100644
@@ -11,7 +11,7 @@
 
 #define IMG_MAX_HW_INPUTS	3
 #define IMG_MAX_HW_OUTPUTS	4
-#define IMG_MAX_PLANES		3
+#define IMG_MAX_PLANES		8
 #define IMG_MAX_COMPONENTS	20
 
 struct img_crop {
Signature · 1 line
2.39.2

Regards,
Alain

author
#2 lore raw

Hi,
Because there are many functions using the plane, increasing the max number of the plane is to maximize the solution to all possible oob places.

Quoted 55 lines · Alain Volmat

Hi,

Quoted 4 lines · On Tue, Jun 27, 2023 at 04:27:31PM +0800, [email protected] wrote:

From: yqsun1997 <[email protected]>

Because format in struct img_image_buffer max index is IMG_MAX_PLANES ==3, The num_planes max index is 8.so will be OOB like in mdp_prepare_buffer.

Similarly as your other patch, could you describe why you need to increase the IMG_MAX_PLANES while I suspect your driver only needs to deal with 3 planes. While the maximum num_planes value that can be given by the user is 8, this has to be first compared to the configured format prior to reaching this function.

Quoted 35 lines

static void mdp_prepare_buffer(struct img_image_buffer *b,
struct mdp_frame *frame, struct vb2_buffer *vb)

{
        struct v4l2_pix_format_mplane *pix_mp = &frame->format.fmt.pix_mp;
        unsigned int i;

        b->format.colorformat = frame->mdp_fmt->mdp_color;
        b->format.ycbcr_prof = frame->ycbcr_prof;
        for (i = 0; i < pix_mp->num_planes; ++i) {
                u32 stride = mdp_fmt_get_stride(frame->mdp_fmt,
                        pix_mp->plane_fmt[i].bytesperline, i);

                b->format.plane_fmt[i].stride = stride;  //oob
                ......
---
 drivers/media/platform/mediatek/mdp3/mtk-mdp3-type.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/media/platform/mediatek/mdp3/mtk-mdp3-type.h +1 -2
index ae0396806..e2e991a34 100644
@@ -11,7 +11,7 @@
 
 #define IMG_MAX_HW_INPUTS    3
 #define IMG_MAX_HW_OUTPUTS   4
-#define IMG_MAX_PLANES               3
+#define IMG_MAX_PLANES               8
 #define IMG_MAX_COMPONENTS   20
 
 struct img_crop {
--
Quoted 2 lines

2.39.2

Regards,
Alain

#3 lore raw

Hi,

Quoted 4 lines · sun yq

Hi,
Because there are many functions using the plane, increasing the max number of the plane is to maximize the solution to all possible oob places.

I don't think it is the right approach then. If the HW is only handling 3 planes, there should be no reason to have to allocate for 8 planes. I suspect that this 8 value is coming from the maximum allowed plane number in V4L2 right ?
INHO driver should simply be fixed to ensure that num_plane won't go higher than the real number of plane allocated in the structures. It should be possible to get the num_plane value from the format selected.

Alain

Quoted 57 lines · Alain Volmat
Quoted 55 lines

Hi,

Quoted 4 lines · On Tue, Jun 27, 2023 at 04:27:31PM +0800, [email protected] wrote:

From: yqsun1997 <[email protected]>

Because format in struct img_image_buffer max index is IMG_MAX_PLANES ==3, The num_planes max index is 8.so will be OOB like in mdp_prepare_buffer.

Similarly as your other patch, could you describe why you need to increase the IMG_MAX_PLANES while I suspect your driver only needs to deal with 3 planes. While the maximum num_planes value that can be given by the user is 8, this has to be first compared to the configured format prior to reaching this function.

Quoted 35 lines

static void mdp_prepare_buffer(struct img_image_buffer *b,
struct mdp_frame *frame, struct vb2_buffer *vb)

{
        struct v4l2_pix_format_mplane *pix_mp = &frame->format.fmt.pix_mp;
        unsigned int i;

        b->format.colorformat = frame->mdp_fmt->mdp_color;
        b->format.ycbcr_prof = frame->ycbcr_prof;
        for (i = 0; i < pix_mp->num_planes; ++i) {
                u32 stride = mdp_fmt_get_stride(frame->mdp_fmt,
                        pix_mp->plane_fmt[i].bytesperline, i);

                b->format.plane_fmt[i].stride = stride;  //oob
                ......
---
 drivers/media/platform/mediatek/mdp3/mtk-mdp3-type.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/media/platform/mediatek/mdp3/mtk-mdp3-type.h +1 -2
index ae0396806..e2e991a34 100644
@@ -11,7 +11,7 @@
 
 #define IMG_MAX_HW_INPUTS    3
 #define IMG_MAX_HW_OUTPUTS   4
-#define IMG_MAX_PLANES               3
+#define IMG_MAX_PLANES               8
 #define IMG_MAX_COMPONENTS   20
 
 struct img_crop {
--
Quoted 2 lines

2.39.2

Regards,
Alain

author
#4 lore raw

Hi Alain,

May I ask if you are the person involved in the code?We should listen to the opinions of the code owner.

Quoted 77 lines · Alain Volmat

Hi,

Quoted 4 lines · sun yq

Hi,
Because there are many functions using the plane, increasing the max number of the plane is to maximize the solution to all possible oob places.

I don't think it is the right approach then. If the HW is only handling 3 planes, there should be no reason to have to allocate for 8 planes. I suspect that this 8 value is coming from the maximum allowed plane number in V4L2 right ?
INHO driver should simply be fixed to ensure that num_plane won't go higher than the real number of plane allocated in the structures. It should be possible to get the num_plane value from the format selected.

Alain

Quoted 57 lines · Alain Volmat
Quoted 55 lines

Hi,

Quoted 4 lines · On Tue, Jun 27, 2023 at 04:27:31PM +0800, [email protected] wrote:

From: yqsun1997 <[email protected]>

Because format in struct img_image_buffer max index is IMG_MAX_PLANES ==3, The num_planes max index is 8.so will be OOB like in mdp_prepare_buffer.

Similarly as your other patch, could you describe why you need to increase the IMG_MAX_PLANES while I suspect your driver only needs to deal with 3 planes. While the maximum num_planes value that can be given by the user is 8, this has to be first compared to the configured format prior to reaching this function.

Quoted 35 lines

static void mdp_prepare_buffer(struct img_image_buffer *b,
struct mdp_frame *frame, struct vb2_buffer *vb)

{
        struct v4l2_pix_format_mplane *pix_mp = &frame->format.fmt.pix_mp;
        unsigned int i;

        b->format.colorformat = frame->mdp_fmt->mdp_color;
        b->format.ycbcr_prof = frame->ycbcr_prof;
        for (i = 0; i < pix_mp->num_planes; ++i) {
                u32 stride = mdp_fmt_get_stride(frame->mdp_fmt,
                        pix_mp->plane_fmt[i].bytesperline, i);

                b->format.plane_fmt[i].stride = stride;  //oob
                ......
---
 drivers/media/platform/mediatek/mdp3/mtk-mdp3-type.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/media/platform/mediatek/mdp3/mtk-mdp3-type.h +1 -2
index ae0396806..e2e991a34 100644
@@ -11,7 +11,7 @@
 
 #define IMG_MAX_HW_INPUTS    3
 #define IMG_MAX_HW_OUTPUTS   4
-#define IMG_MAX_PLANES               3
+#define IMG_MAX_PLANES               8
 #define IMG_MAX_COMPONENTS   20
 
 struct img_crop {
--
Quoted 2 lines

2.39.2

Regards,
Alain

#5 lore raw
Quoted 5 lines · sun yq

Hi Alain,

May I ask if you are the person involved in the code?We should listen to the opinions of the code owner.

Please don't top post.

Alain has a valid point. Please describe in detail how you are running into or detecting this problem. Are you running into this for real? Or is this from a static analyzer? Or are you simply eyeballing the issue?

ChenYu

Quoted 79 lines · Alain Volmat
Quoted 77 lines

Hi,

Quoted 4 lines · sun yq

Hi,
Because there are many functions using the plane, increasing the max number of the plane is to maximize the solution to all possible oob places.

I don't think it is the right approach then. If the HW is only handling 3 planes, there should be no reason to have to allocate for 8 planes. I suspect that this 8 value is coming from the maximum allowed plane number in V4L2 right ?
INHO driver should simply be fixed to ensure that num_plane won't go higher than the real number of plane allocated in the structures. It should be possible to get the num_plane value from the format selected.

Alain

Quoted 57 lines · Alain Volmat
Quoted 55 lines

Hi,

Quoted 4 lines · On Tue, Jun 27, 2023 at 04:27:31PM +0800, [email protected] wrote:

From: yqsun1997 <[email protected]>

Because format in struct img_image_buffer max index is IMG_MAX_PLANES ==3, The num_planes max index is 8.so will be OOB like in mdp_prepare_buffer.

Similarly as your other patch, could you describe why you need to increase the IMG_MAX_PLANES while I suspect your driver only needs to deal with 3 planes. While the maximum num_planes value that can be given by the user is 8, this has to be first compared to the configured format prior to reaching this function.

Quoted 35 lines

static void mdp_prepare_buffer(struct img_image_buffer *b,
struct mdp_frame *frame, struct vb2_buffer *vb)

{
        struct v4l2_pix_format_mplane *pix_mp = &frame->format.fmt.pix_mp;
        unsigned int i;

        b->format.colorformat = frame->mdp_fmt->mdp_color;
        b->format.ycbcr_prof = frame->ycbcr_prof;
        for (i = 0; i < pix_mp->num_planes; ++i) {
                u32 stride = mdp_fmt_get_stride(frame->mdp_fmt,
                        pix_mp->plane_fmt[i].bytesperline, i);

                b->format.plane_fmt[i].stride = stride;  //oob
                ......
---
 drivers/media/platform/mediatek/mdp3/mtk-mdp3-type.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/media/platform/mediatek/mdp3/mtk-mdp3-type.h +1 -2
index ae0396806..e2e991a34 100644
@@ -11,7 +11,7 @@
 
 #define IMG_MAX_HW_INPUTS    3
 #define IMG_MAX_HW_OUTPUTS   4
-#define IMG_MAX_PLANES               3
+#define IMG_MAX_PLANES               8
 #define IMG_MAX_COMPONENTS   20
 
 struct img_crop {
--
Quoted 2 lines

2.39.2

Regards,
Alain

#6 lore raw
In reply to [email protected]

Please add a prefix to the subject.

…

Quoted 1 line

The num_planes max index is 8.so will be OOB like in mdp_prepare_buffer.

…

Please choose an imperative change suggestion.

See also:
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst?h=v6.4#n94

Quoted 1 line

I imagine that an other real name would be more appropriate here.

See also:
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst?h=v6.4#n411

Regards,
Markus

#7 lore raw
In reply to [email protected]

Hi!

Quoted 7 lines · On 6/27/23 10:27, [email protected] wrote:

From: yqsun1997 <[email protected]>

Because format in struct img_image_buffer max index is IMG_MAX_PLANES ==3, The num_planes max index is 8.so will be OOB like in mdp_prepare_buffer.

static void mdp_prepare_buffer(struct img_image_buffer *b,
struct mdp_frame *frame, struct vb2_buffer *vb)

Quoted 7 lines
{
         struct v4l2_pix_format_mplane *pix_mp = &frame->format.fmt.pix_mp;
         unsigned int i;

         b->format.colorformat = frame->mdp_fmt->mdp_color;
         b->format.ycbcr_prof = frame->ycbcr_prof;
         for (i = 0; i < pix_mp->num_planes; ++i) {

Unless there is a bug in the driver, pix_mp->num_planes will never exceed 3. Userspace might certainly pass more than 3 planes, but only the first pix_mp->num_planes should ever be used.

If pix_mp->num_planes can ever be more than 3, then that would be the real bug.

Regards,

Hans

Quoted 24 lines
                 u32 stride = mdp_fmt_get_stride(frame->mdp_fmt,
                         pix_mp->plane_fmt[i].bytesperline, i);

                 b->format.plane_fmt[i].stride = stride;  //oob
                 ......
---
  drivers/media/platform/mediatek/mdp3/mtk-mdp3-type.h | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/media/platform/mediatek/mdp3/mtk-mdp3-type.h +1 -1
index ae0396806..e2e991a34 100644
@@ -11,7 +11,7 @@
 
  #define IMG_MAX_HW_INPUTS	3
  #define IMG_MAX_HW_OUTPUTS	4
-#define IMG_MAX_PLANES		3
+#define IMG_MAX_PLANES		8
  #define IMG_MAX_COMPONENTS	20
 
  struct img_crop {