From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A9B7518E021; Tue, 10 Sep 2024 09:59:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725962398; cv=none; b=mxV/v2fltq42A3gmlVIqexN0imwOGFrMX87Kz3xcfnQ2vYr39vqIBFjTrBtSZ1iHLogwATUu0BQyrnj5PfpOmfuD3ruouHdhc5n1LTOsxQcHLcdxp8CsE7AHyfy1GoCnA967/PZVyk1tC0sv8Dp0TmYxAxgcyaaQ1JSpwGR3Rj4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725962398; c=relaxed/simple; bh=2MNBayd7k/krtv4P9T6ziOF6hv01GZIEk8QTClsV35U=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ZONI4dB5n+aqISM6ODOr2rVnOznD2GMBnowhdt5WFScCAZIOlFPi0pMPjwQuIoAT9vls092/SZFdbzsuzjXfqInL2dRh4xHqKBGucBtX+0OGMbAT03BB7TgPKPT+IGu6TRjGjSNcm/N4vw8gL1PMZtjthytQrp13G4yjAKD3ltk= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b=FIcVK0Mn; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b="FIcVK0Mn" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2EE87C4CEC3; Tue, 10 Sep 2024 09:59:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1725962398; bh=2MNBayd7k/krtv4P9T6ziOF6hv01GZIEk8QTClsV35U=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=FIcVK0Mnhzundg1rS56SCDXDfqi0fFlRInYL0PmTF49uSBvjMjy4yH5IZjwH9E+KT uUn7dNl8NPRwCUBbTF43Pjvtd/YNKuYGaA9jeHR44RCfnAJhGZLS8XvAGdLgQjCVbn R+HPvMbhHy/+twR8hieL7Iem+149lAHtCEsN3xso= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Ricardo Ribalda , Laurent Pinchart , Sasha Levin Subject: [PATCH 5.4 020/121] media: uvcvideo: Enforce alignment of frame and interval Date: Tue, 10 Sep 2024 11:31:35 +0200 Message-ID: <20240910092546.690404259@linuxfoundation.org> X-Mailer: git-send-email 2.46.0 In-Reply-To: <20240910092545.737864202@linuxfoundation.org> References: <20240910092545.737864202@linuxfoundation.org> User-Agent: quilt/0.67 X-stable: review X-Patchwork-Hint: ignore Precedence: bulk X-Mailing-List: stable@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 5.4-stable review patch. If anyone has any objections, please let me know. ------------------ From: Ricardo Ribalda [ Upstream commit c8931ef55bd325052ec496f242aea7f6de47dc9c ] 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 Reviewed-by: Laurent Pinchart Link: https://lore.kernel.org/r/20240404-uvc-align-v2-1-9e104b0ecfbd@chromium.org Signed-off-by: Laurent Pinchart Signed-off-by: Sasha Levin --- drivers/media/usb/uvc/uvc_driver.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/drivers/media/usb/uvc/uvc_driver.c b/drivers/media/usb/uvc/uvc_driver.c index 0caa57a6782a..6d1a7e02da51 100644 --- a/drivers/media/usb/uvc/uvc_driver.c +++ b/drivers/media/usb/uvc/uvc_driver.c @@ -887,16 +887,26 @@ static int uvc_parse_streaming(struct uvc_device *dev, goto error; } - size = nformats * sizeof(*format) + nframes * sizeof(*frame) + /* + * Allocate memory for the formats, the frames and the intervals, + * plus any required padding to guarantee that everything has the + * correct alignment. + */ + size = nformats * sizeof(*format); + size = ALIGN(size, __alignof__(*frame)) + nframes * sizeof(*frame); + size = ALIGN(size, __alignof__(*interval)) + nintervals * sizeof(*interval); + format = kzalloc(size, GFP_KERNEL); - if (format == NULL) { + if (!format) { ret = -ENOMEM; goto error; } - frame = (struct uvc_frame *)&format[nformats]; - interval = (u32 *)&frame[nframes]; + frame = (void *)format + nformats * sizeof(*format); + frame = PTR_ALIGN(frame, __alignof__(*frame)); + interval = (void *)frame + nframes * sizeof(*frame); + interval = PTR_ALIGN(interval, __alignof__(*interval)); streaming->format = format; streaming->nformats = nformats; -- 2.43.0