From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f48.google.com (mail-wm1-f48.google.com [209.85.128.48]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BB5A041D107 for ; Fri, 7 Aug 2026 10:14:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.48 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786097660; cv=none; b=Rzus6lm1bNc6PQrfdVzJ1k5bUihAsQSZHd7UR7T4Wt7IVCzSYrotVs37jhYIObfbqvd+sd2uwOV1q3o9wxqtlAmffL2ffI7e52LbriAo9pJ2vnSWtiXPitn+JVffN49TQAreBAiLT1qF4YhNGjlZvexAU0SAT3kfysJeVIOwfxo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786097660; c=relaxed/simple; bh=ghBWfrgtRQcEmr9V6zkgDLfR+kSbJqfZ+kj0H7kkEgo=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=R6ui1b8XKhlCbPmaea5oA4T/aCm2p1cjfYtpomK3OfxHkw4uTCvgYcfOugX8Z/BUvDb9K5wGKjhTyjdpkbvfdh965new7r6+/aQBHUdIWZxpJv1uktCGIx+IZ/frfD3BlKmQGGbM58kc8oa48Tjg94EBlkmuIxs4zlf/aJuH61U= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=UuzfEXPq; arc=none smtp.client-ip=209.85.128.48 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="UuzfEXPq" Received: by mail-wm1-f48.google.com with SMTP id 5b1f17b1804b1-4954a9e8490so11031445e9.1 for ; Fri, 07 Aug 2026 03:14:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1786097654; x=1786702454; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=fCmJ7V9mYpgyM9qqojJ7zamTNRxKN8QjnRiKGjxJdDY=; b=UuzfEXPqNe+1n/3aLwVgTMnAQh+OXtRG3CKF5JfylhagK0LUzq9tIu0MA+E9yWxGyz pOdROtjET/6hg8l0E106SsVn+4rDxyGxb7sbjjCCL2cCSr38xwYEev2U/XL0S1RqaHIE Evw1NRiKN6xAlg+li+D7aot3XSn1MhytTfBiVlxMFUQ1pICg663Do5jpYeri2pOwEgJy 0z0iioSkvB3BNnwJuMdIPpEcKM4j5AaYePuZYeze+76plw2t5RvnKvN3ymRFdEZef4BV R4L/g/M7SYSoWEhGosQdiH3I+6AxPWnBNeqdQsZ3N4iltluImCK4NOjp8NAS7+YX48Gt 5bhQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1786097654; x=1786702454; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=fCmJ7V9mYpgyM9qqojJ7zamTNRxKN8QjnRiKGjxJdDY=; b=jTJ9BMeIBu4ZR1N0X2SFESM2FpHgyZ/zzEAOlfdUAQeyWnCydNMA6Ss1YgFGuocv6+ Ec3jyIEHjQ7YxZUHquYEGxAHgM5c0A48wSvtPzmHUNmNbZUmyCGAl6HwogGGIIF9iyFv P3NhAxmmpv+72ikzgeLcTw1UXqJdLyOF8x7ec1YrwfwB9LYPh8wlXyOZCUvsXZnEZckb gwX5VdeVIOBi0X6qgf6psrG0X0m9Pbv7tqofO7isDn5MO3gMDuKoQsbCRkl2RgmW3YC8 gHdGo0uo3kJiAjqDssxFnQ2KmSc8ZNX/ZMr3Vi1qUu6xS0V+s/6fq54061bv80vp6ri+ REBg== X-Forwarded-Encrypted: i=1; AHgh+RpDJz5uV4MGh66xMO1scOe8iAUxhjOIUhaQRaXOXESDb5jLl9Vj+gnSlzyz1oNSjiTZSqALp/xJMeGRYA8=@vger.kernel.org X-Gm-Message-State: AOJu0YxnpnrwhaUigG5aZLuyu8+j5KyS+tiSb8Aim7+Ms+2mciIiooqj MoL0of7xr0aLhAY+2gBxhgbupeZyYHLKZGPJ3UiKTzBpa1OhYhHttULICfSaMUbgUCk= X-Gm-Gg: AR+sD139muaym3jAZO/R32KuWF8jw/CEDPibHHxT6DXli+h+Em0nzuX0Z7+iB1dN0zg pSUDYAA9Tg9FbowetWNzwpJ3OW/h9YCbN4S0VqVxvfdl1YlsbYmLUwkS1eMPuXJvmwK1v2JM1y/ y6MIDw4hCltjVSamH710JHKYvVYTTTLjEEv3XgzwWl3rfzH9vTgZYPWUTOv5KKHHzxcQM2e2ScW BguHd2+KlwJ2EjIBiHjq9jxojsRNPcRthOpiwmzfNbgkzahQVZWTnuF3a5CSHHYfStjQ1kIqMJV rg6aKn0/YqV7nWYhPv8xBM9uhdDNedIj7TQ3bk4XkkRBuJQ4qwz29UL6yr1gASD+99iRIPX6W4X rgKUKOPycSy58TY0cvddoLbo5BhgPnLxXrMvNt+QauI5VkfaEscsU0gSS9ABy7WLUxEt8yAAbZy tMlFjv+ciBpEbLZy8eB8rdPMmVWo4cTi3C8h1rKQJ6Ow0ItzVpRrQhOZgHYxPFPNcCsOAOJ9U5Q vN6vVjy5ath23dXZ0o5jInEjJFPe9CjkelhgPxW4Po= X-Received: by 2002:a05:600c:42d4:b0:499:59fd:dbfc with SMTP id 5b1f17b1804b1-49959fddc5emr61573625e9.1.1786097653412; Fri, 07 Aug 2026 03:14:13 -0700 (PDT) Received: from USERUSE-R7PLK0L.localdomain (IGLD-84-228-188-63.inter.net.il. [84.228.188.63]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4995ea3361esm25946735e9.15.2026.08.07.03.14.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 07 Aug 2026 03:14:12 -0700 (PDT) From: Noam Ben Shimon To: laurent.pinchart@ideasonboard.com, hansg@kernel.org, mchehab@kernel.org Cc: linux-media@vger.kernel.org, linux-kernel@vger.kernel.org, Noam Ben Shimon Subject: [PATCH] media: uvcvideo: Fix integer overflow in frame buffer size calculation Date: Fri, 7 Aug 2026 13:14:33 +0300 Message-Id: <20260807101433.54886-1-noambs2999@gmail.com> X-Mailer: git-send-email 2.34.1 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit In the function uvc_parse_frame(), it recomputes dwMaxVideoFrameBufferSize for uncompressed formats. This helps working around devices that report it wrong: frame->dwMaxVideoFrameBufferSize = format->bpp * frame->wWidth * frame->wHeight / 8; These three arguments originate from the device's own descriptors, and therefore can be decided by it. bpp is a u8 and wWidth and wHeight are u16. The expression is evaluated in int, and the maximum value is 255 * 65535 * 65535 (which is roughly 510 times INT_MAX). A device that declares large dimensions therefore overflows a signed int here. The kernel is built using -fno-strict-overflow, so this wraps rather than being miscompiled, but the wrapped value (which is often negative) is then divided by 8 and stored in a u32 used as a size. Two examples for this (using legal field values): - 32 bpp, 16384x4096: the product is exactly 2^31 and wraps to INT_MIN. After division and conversion to u32 the field has the value 4026531840 rather than 268435456. - 16 bpp, 16384x16384: the product is exactly 2^32 and wraps to 0. The field holds 0, rather than the correct 536870912. I don't think memory corruption is a consequence of this. The value reaches uvc_queue_setup() as the vb2 buffer size, and every copy on the decode path is bounded by buf->length, which uvc_buffer_prepare() gets from vb2_plane_size() rather than this field. What a wrapped value does instead is make the driver describe the stream inconsistently. For an uncompressed format uvc_fixup_video_ctrl() copies it into ctrl->dwMaxVideoFrameSize unconditionally, and that becomes the sizeimage reported by VIDIOC_G_FMT. This is while width, height and bytesperline continue to describe the full frame. This also makes uvc_video_validate_buffer() mark error on all frames, because it is comparing bytesused against the same number. Compute the size in 64-bit, and if the result does not fit in the u32 field then reject the frame descriptor. An uncompressed frame this large is probably not a real device and rejection is consistent with the other checks over malformed-descriptors in this function. Fixes: c0efd232929c ("V4L/DVB (8145a): USB Video Class driver") Signed-off-by: Noam Ben Shimon --- drivers/media/usb/uvc/uvc_driver.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/drivers/media/usb/uvc/uvc_driver.c b/drivers/media/usb/uvc/uvc_driver.c index e289cc71ba98..d319368f9d21 100644 --- a/drivers/media/usb/uvc/uvc_driver.c +++ b/drivers/media/usb/uvc/uvc_driver.c @@ -296,9 +296,21 @@ static int uvc_parse_frame(struct uvc_device *dev, * information. For uncompressed formats this can be fixed by computing * the value from the frame size. */ - if (!(format->flags & UVC_FMT_FLAG_COMPRESSED)) - frame->dwMaxVideoFrameBufferSize = format->bpp * frame->wWidth - * frame->wHeight / 8; + if (!(format->flags & UVC_FMT_FLAG_COMPRESSED)) { + u64 bufsize; + + bufsize = (u64)format->bpp * frame->wWidth * frame->wHeight / 8; + if (bufsize > U32_MAX) { + uvc_dbg(dev, DESCR, + "device %d videostreaming interface %d FRAME %u: computed buffer size overflows\n", + dev->udev->devnum, + alts->desc.bInterfaceNumber, + frame->bFrameIndex); + return -EINVAL; + } + + frame->dwMaxVideoFrameBufferSize = bufsize; + } /* * Clamp the default frame interval to the boundaries. A zero base-commit: f9a2394a23482bfd330911e9c8295b71724feacd -- 2.34.1 I was working on a certain device that had a variation of the Linux kernel. During my work, I had searched for memory mismanagement and misallocation in media drivers. At some point I stumbled into the `uvc_driver.c` and found a flaw that is not a vulnerability, but still a flaw. I figured that it was worth letting you know rather than shrug it off.