Skip to content

Commit d8915d2

Browse files
ribaldagregkh
authored andcommitted
media: uvcvideo: Enforce alignment of frame and interval
[ Upstream commit c8931ef ] Struct uvc_frame and interval (u32*) are packaged together on streaming->formats on a single contiguous allocation. Right now they are allocated right after uvc_format, without taking into consideration their required alignment. This is working fine because both structures have a field with a pointer, but it will stop working when the sizeof() of any of those structs is not a multiple of the sizeof(void*). Enforce that alignment during the allocation. Signed-off-by: Ricardo Ribalda <[email protected]> Reviewed-by: Laurent Pinchart <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Laurent Pinchart <[email protected]> Signed-off-by: Sasha Levin <[email protected]>
1 parent e3a95f2 commit d8915d2

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

drivers/media/usb/uvc/uvc_driver.c

+14-4
Original file line numberDiff line numberDiff line change
@@ -687,16 +687,26 @@ static int uvc_parse_streaming(struct uvc_device *dev,
687687
goto error;
688688
}
689689

690-
size = nformats * sizeof(*format) + nframes * sizeof(*frame)
690+
/*
691+
* Allocate memory for the formats, the frames and the intervals,
692+
* plus any required padding to guarantee that everything has the
693+
* correct alignment.
694+
*/
695+
size = nformats * sizeof(*format);
696+
size = ALIGN(size, __alignof__(*frame)) + nframes * sizeof(*frame);
697+
size = ALIGN(size, __alignof__(*interval))
691698
+ nintervals * sizeof(*interval);
699+
692700
format = kzalloc(size, GFP_KERNEL);
693-
if (format == NULL) {
701+
if (!format) {
694702
ret = -ENOMEM;
695703
goto error;
696704
}
697705

698-
frame = (struct uvc_frame *)&format[nformats];
699-
interval = (u32 *)&frame[nframes];
706+
frame = (void *)format + nformats * sizeof(*format);
707+
frame = PTR_ALIGN(frame, __alignof__(*frame));
708+
interval = (void *)frame + nframes * sizeof(*frame);
709+
interval = PTR_ALIGN(interval, __alignof__(*interval));
700710

701711
streaming->formats = format;
702712
streaming->nformats = 0;

0 commit comments

Comments
 (0)