Skip to content

Commit 3bacc10

Browse files
Maciej Matraszekmchehab
authored andcommitted
[media] v4l2-common: fix overflow in v4l_bound_align_image()
Fix clamp_align() used in v4l_bound_align_image() to prevent overflow when passed large value like UINT32_MAX. In the current implementation: clamp_align(UINT32_MAX, 8, 8192, 3) returns 8, because in line: x = (x + (1 << (align - 1))) & mask; x overflows to (-1 + 4) & 0x7 = 3, while expected value is 8192. v4l_bound_align_image() is heavily used in VIDIOC_S_FMT and VIDIOC_SUBDEV_S_FMT ioctls handlers, and documentation of the latter explicitly states that: "The modified format should be as close as possible to the original request." -- http://linuxtv.org/downloads/v4l-dvb-apis/vidioc-subdev-g-fmt.html Thus one would expect, that passing UINT32_MAX as format width and height will result in setting maximum possible resolution for the device. Particularly, when the driver doesn't support VIDIOC_ENUM_FRAMESIZES ioctl, which is common in the codebase. Fixes changeset: b0d3159 Signed-off-by: Maciej Matraszek <m.matraszek@samsung.com> Acked-by: Sakari Ailus <sakari.ailus@linux.intel.com> Cc: <stable@vger.kernel.org> Signed-off-by: Mauro Carvalho Chehab <mchehab@osg.samsung.com>
1 parent 43c3ea3 commit 3bacc10

1 file changed

Lines changed: 3 additions & 6 deletions

File tree

‎drivers/media/v4l2-core/v4l2-common.c‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -435,16 +435,13 @@ static unsigned int clamp_align(unsigned int x, unsigned int min,
435435
/* Bits that must be zero to be aligned */
436436
unsigned int mask = ~((1 << align) - 1);
437437

438+
/* Clamp to aligned min and max */
439+
x = clamp(x, (min + ~mask) & mask, max & mask);
440+
438441
/* Round to nearest aligned value */
439442
if (align)
440443
x = (x + (1 << (align - 1))) & mask;
441444

442-
/* Clamp to aligned value of min and max */
443-
if (x < min)
444-
x = (min + ~mask) & mask;
445-
else if (x > max)
446-
x = max & mask;
447-
448445
return x;
449446
}
450447

0 commit comments

Comments
 (0)