[PATCH v2] media: exynos4-is: fimc: Fix reading uninitialized plane sizes

Lin Huiyi xinghui79 at qq.com
Tue Dec 1 07:21:56 PST 2026


fimc_get_sensor_frame_desc() takes a 'try' argument that selects between
set_frame_desc() and get_frame_desc(), so a function named after the
"get" operation also performs the "set" one and two very different
contracts end up in a single place:

 - set_frame_desc() lets the sensor adjust the descriptor to its
   capabilities.  s5c73m3_oif_set_frame_desc(), the only implementation
   in the tree, sets entry[0].length to a fixed value and derives
   entry[1].length from the value passed in (max_t(fd->entry[1].length,
   S5C73M3_EMBEDDED_DATA_MAXLEN)), so the caller has to provide
   initialised plane sizes.

 - get_frame_desc() fills the descriptor in completely, so the incoming
   plane sizes are irrelevant.

set_frame_desc() is therefore the operation computing the layout for a
requested size, which both TRY_FMT and S_FMT need, while
get_frame_desc() only reads back what the sensor stored.  The old code
called the first one for TRY_FMT and the second one for S_FMT, so an
application that skipped TRY_FMT was handed back the zero-initialised
descriptor and had no plane sizes to size its buffers from.

fimc_pipeline_validate() additionally calls the helper with try == false
and passes an uninitialised stack array, yet the pre-fill loop runs
unconditionally and reads plane_fmt[].sizeimage before the sensor had any
chance to fill it in.  That is an uninitialised stack read.

Split the helper into fimc_set_sensor_frame_desc() and
fimc_get_sensor_frame_desc(), drop the 'try' argument from both, and move
the common validation into fimc_update_plane_sizes().  Only the set
variant pre-fills the descriptor and it is now used for both TRY_FMT and
S_FMT, while the get variant hands the sensor an empty descriptor and is
left with fimc_pipeline_validate().

Changes since v1:
- Split the helper into separate set and get functions instead of
  choosing the operation with the 'try' argument, as suggested by Hans
  Verkuil.  TRY_FMT and S_FMT now both use the set variant, since that is
  the one computing the layout; the previous version kept the original
  set/get assignment.

Suggested-by: Hans Verkuil <hverkuil+cisco at kernel.org>
Signed-off-by: Lin Huiyi <xinghui79 at qq.com>
---
Hi Hans,

thanks for the review.  I split the helper the way you suggested:
fimc_set_sensor_frame_desc() only calls set_frame_desc() and pre-fills
the descriptor, fimc_get_sensor_frame_desc() only calls
get_frame_desc() and hands the sensor an empty descriptor.  The common
validation moved into fimc_update_plane_sizes(), and both functions lost
the 'try' argument.

For "call the correct function": TRY_FMT and S_FMT both use
fimc_set_sensor_frame_desc() now.  set_frame_desc() is the operation that
computes the layout from the requested size -- s5c73m3_oif_set_frame_desc()
doesn't touch the hardware, it stores the result in its own state.
get_frame_desc() only reads that back, and it returns the zero-initialised
descriptor when S_FMT is called without a preceding TRY_FMT, leaving the
application without plane sizes to allocate buffers from.  So
get_frame_desc() is kept for fimc_pipeline_validate(), where reading back
is exactly what is wanted.

I'm aware this changes S_FMT behaviour when TRY_FMT was skipped, so I'd
rather flag it explicitly than do it silently.

Testing: I have no Exynos4 hardware, so this is compile-tested only
(COMPILE_TEST=y, W=1) plus reading the set and get implementations in
the tree.  Testing on real hardware would be very welcome.
 .../samsung/exynos4-is/fimc-capture.c         | 89 +++++++++++++------
 1 file changed, 64 insertions(+), 25 deletions(-)

diff --git a/drivers/media/platform/samsung/exynos4-is/fimc-capture.c b/drivers/media/platform/samsung/exynos4-is/fimc-capture.c
index 8d7e7b1905..8c08875d99 100644
--- a/drivers/media/platform/samsung/exynos4-is/fimc-capture.c
+++ b/drivers/media/platform/samsung/exynos4-is/fimc-capture.c
@@ -841,18 +841,48 @@ static int fimc_pipeline_try_format(struct fimc_ctx *ctx,
 	return 0;
 }
 
+/*
+ * Validate the frame descriptor returned by the sensor and copy the plane
+ * sizes back to the caller.
+ */
+static int fimc_update_plane_sizes(struct v4l2_subdev *sensor,
+				   struct v4l2_plane_pix_format *plane_fmt,
+				   unsigned int num_planes,
+				   struct v4l2_mbus_frame_desc *fd)
+{
+	int i;
+
+	if (num_planes != fd->num_entries)
+		return -EINVAL;
+
+	for (i = 0; i < num_planes; i++)
+		plane_fmt[i].sizeimage = fd->entry[i].length;
+
+	if (fd->entry[0].length > FIMC_MAX_JPEG_BUF_SIZE) {
+		v4l2_err(sensor->v4l2_dev, "Unsupported buffer size: %u\n",
+			 fd->entry[0].length);
+
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
 /**
- * fimc_get_sensor_frame_desc - query the sensor for media bus frame parameters
+ * fimc_set_sensor_frame_desc - negotiate the media bus frame parameters
  * @sensor: pointer to the sensor subdev
- * @plane_fmt: provides plane sizes corresponding to the frame layout entries
+ * @plane_fmt: plane sizes the frame layout is derived from, updated in place
  * @num_planes: number of planes
- * @try: true to set the frame parameters, false to query only
+ *
+ * The sensor driver may adjust the descriptor to its capabilities, and it may
+ * use the plane sizes passed in to derive the layout, so @plane_fmt has to be
+ * initialised by the caller.
  *
  * This function is used by this driver only for compressed/blob data formats.
  */
-static int fimc_get_sensor_frame_desc(struct v4l2_subdev *sensor,
+static int fimc_set_sensor_frame_desc(struct v4l2_subdev *sensor,
 				      struct v4l2_plane_pix_format *plane_fmt,
-				      unsigned int num_planes, bool try)
+				      unsigned int num_planes)
 {
 	struct v4l2_mbus_frame_desc fd = { };
 	int i, ret;
@@ -862,28 +892,38 @@ static int fimc_get_sensor_frame_desc(struct v4l2_subdev *sensor,
 		fd.entry[i].length = plane_fmt[i].sizeimage;
 
 	pad = sensor->entity.num_pads - 1;
-	if (try)
-		ret = v4l2_subdev_call(sensor, pad, set_frame_desc, pad, &fd);
-	else
-		ret = v4l2_subdev_call(sensor, pad, get_frame_desc, pad, &fd);
-
+	ret = v4l2_subdev_call(sensor, pad, set_frame_desc, pad, &fd);
 	if (ret < 0)
 		return ret;
 
-	if (num_planes != fd.num_entries)
-		return -EINVAL;
-
-	for (i = 0; i < num_planes; i++)
-		plane_fmt[i].sizeimage = fd.entry[i].length;
+	return fimc_update_plane_sizes(sensor, plane_fmt, num_planes, &fd);
+}
 
-	if (fd.entry[0].length > FIMC_MAX_JPEG_BUF_SIZE) {
-		v4l2_err(sensor->v4l2_dev,  "Unsupported buffer size: %u\n",
-			 fd.entry[0].length);
+/**
+ * fimc_get_sensor_frame_desc - read back the media bus frame parameters
+ * @sensor: pointer to the sensor subdev
+ * @plane_fmt: plane sizes filled in by the sensor, output only
+ * @num_planes: number of planes
+ *
+ * Unlike fimc_set_sensor_frame_desc(), the descriptor is filled in completely
+ * by the sensor, so @plane_fmt does not have to be initialised by the caller.
+ *
+ * This function is used by this driver only for compressed/blob data formats.
+ */
+static int fimc_get_sensor_frame_desc(struct v4l2_subdev *sensor,
+				      struct v4l2_plane_pix_format *plane_fmt,
+				      unsigned int num_planes)
+{
+	struct v4l2_mbus_frame_desc fd = { };
+	int ret;
+	int pad;
 
-		return -EINVAL;
-	}
+	pad = sensor->entity.num_pads - 1;
+	ret = v4l2_subdev_call(sensor, pad, get_frame_desc, pad, &fd);
+	if (ret < 0)
+		return ret;
 
-	return 0;
+	return fimc_update_plane_sizes(sensor, plane_fmt, num_planes, &fd);
 }
 
 static int fimc_cap_g_fmt_mplane(struct file *file, void *fh,
@@ -970,8 +1010,8 @@ static int __video_try_or_set_format(struct fimc_dev *fimc,
 
 		sensor = __fimc_md_get_subdev(ve->pipe, IDX_SENSOR);
 		if (sensor)
-			fimc_get_sensor_frame_desc(sensor, pix->plane_fmt,
-						   (*out_fmt)->memplanes, try);
+			fimc_set_sensor_frame_desc(sensor, pix->plane_fmt,
+						   (*out_fmt)->memplanes);
 		else
 			ret = -EPIPE;
 
@@ -1161,8 +1201,7 @@ static int fimc_pipeline_validate(struct fimc_dev *fimc)
 			unsigned int i;
 
 			ret = fimc_get_sensor_frame_desc(sd, plane_fmt,
-							 frame->fmt->memplanes,
-							 false);
+							 frame->fmt->memplanes);
 			if (ret < 0)
 				return -EPIPE;
 
-- 
2.56.1




More information about the linux-arm-kernel mailing list