From nobody Tue Sep 29 04:39:19 2026 Received: from mail-wm1-f51.google.com (mail-wm1-f51.google.com [209.85.128.51]) (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 B3557425CF0 for ; Wed, 12 Aug 2026 10:33:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786530792; cv=none; b=sk0gOIWtfaNrtckOJ8UDrIo0Vz//QUVHFBYDhBNTiT7Syqa3PQZOkxiT9VXhE1xZaydqXCRulyMpI72x1m2kpIJwoefWavq4w5bBGsRKItS2tCQZCaZOu5TJNaZcMhcRAkxZsDp6VCOI8zdmhH/8KiIN+uWemYctIeORu36unMs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786530792; c=relaxed/simple; bh=B8ueUhG5tBGvwqhk81omzoqC2jILrshowUdeHW5IBPs=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=XwdhCKLnW1NJHRLAjsqupMC0gd4uOjhMUQnrno99Zl25MmraRdRzbnNkaGhX0CwWFj3Yn8K7Jyr0UNfWFUBcxsjt9xHY8XxCGqel2JD5aW6cGdLwBDBFX5pSy3SAxQd8XPc2Z0guvjGgt401lYTwgFgAIL8zS1WJVmBCkzTOs/w= 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=DeznSG6m; arc=none smtp.client-ip=209.85.128.51 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="DeznSG6m" Received: by mail-wm1-f51.google.com with SMTP id 5b1f17b1804b1-495635a85d2so5635925e9.0 for ; Wed, 12 Aug 2026 03:33:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1786530788; x=1787135588; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=wfIp5NDU7PWeyYmfA4vACcCezJB2SbD/Br0L9G6eGm0=; b=DeznSG6mv9jKSk4rglFXT9baXJaPccQQoF298H1rMi9eKRZaQLeis2ANhZILTpUubA YrRQA1iY798aj8ds5VR6exSISLPjwmYqP+DG+tz94tcYPkT6mAZUB7kbssHFCdpJrbrS MCUTdS09XJPdK+KBcpxuseoGRZfp6WyADNlhzThMLO6LDm6rgCyk4W8ea05X6tiTbQJT /20IhicXqva+UNL9+RrbCub1KgLYeDadBctjxu5vsBdfR/Hh10fbPnYQDWXd32NatudW 45tce91CO2JKm8bGCPhxwnrZsjbYL5l7db0+QSGVTIj8/8NGwCuK9849LZi7LL1r7ljF LXxQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1786530788; x=1787135588; h=content-transfer-encoding:mime-version:references:in-reply-to :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=wfIp5NDU7PWeyYmfA4vACcCezJB2SbD/Br0L9G6eGm0=; b=MAnmOeSdoVfpkdN4N/VyM18XJ9X1n358QA5NTwO0omEIP1BN8dsyC6pygZAtJ7Dci0 3sor12Za4XpWKVr0OMUHQJUlQ7MfEgK7DTC1qpc5OtP8CJ1FsaJxFYFzVOu/5ml9gJm4 lpLq/rZ1BVnoWuz4HicmEztww407SjeK1RzlbvbZhassSvrn1/Zpeh5UCQJMsIYgE2cd phn62rMCymQSRdOFTKIuXF64Z+lPknUgKL7WyAKXQVL8ImwR+3I7uXrweMHFOpZFBD9e bU5eI4L08G1t89QvpV0A73ZmSOpEWpNP989pz1F9twYwugiDa+J3nSEaK819HrdSAhS/ UVdA== X-Forwarded-Encrypted: i=1; AHgh+RpF7pcOctqp6fmfKa+YylKExKJShecOolVP7z+EIQF9s4SSKl9XntQq3fUokSIV8T3npxhYO7A0Xhi2mTw=@vger.kernel.org X-Gm-Message-State: AOJu0YyKvhmj3DLCnT2JORFOUjo+d4c/4brIax2uc8N7t2vnK06Wrb5z b85QAlWhMlXI5OQER/FieZmCwwQ9sKFZz7JLylM+oUXck8ubMmAWB43foQvoiTeIeojcbA== X-Gm-Gg: AR+sD12LAVFjODNLKqOLHpJR7NnZZ6VAAxXKnGuos22eCn3NSCLcPg6tgMKRUTs/RKq aADqnHIoAzVBYbwKDb3FBo7fEQM1q5I9mpxpZNU0Yk0KkXkzP1F767PtHKfr7KkjHjjWKxtMl8I Hw82iUTEKSROGG9gG7/xMFjwN59b214Py7+M1ap+ExXG95tUohEEOAXd/dZyouC8CdzNIkxMopu dY/o1uI/iYCTWH+BBTLFaRLT4ad62Kfl3g+VgqKcg3GBSpYnu2H4SOSOPN6VpJbpbM0VE7jUof5 EtXi4v6OH7O4glvCM/JJ9aZJOfbM1JmliJxyTKaELb6qGEKMXjDUNEOdiR2kZ275fbyFYrcMhLc /0I7CxpcmIDrJLIJl6vmdcyVknVHvCa+Toca+Nb9osVs/VCHwEEHnrYhwVNZaI5w7S9l2NU3td6 91OKDP5/5L0eIbmSJFUQHUMXx4ZzpZzpHHHcFUMU446XMogTPoW5jstc6IwKpAx8IZ3S33NdFyK tjJLmRHjc0PnJetSWrEpiJqap9TOgKl9u98JLxn7eM= X-Received: by 2002:a05:600c:2e11:b0:498:943:ccc0 with SMTP id 5b1f17b1804b1-4997c0e2ba9mr30739545e9.6.1786530787670; Wed, 12 Aug 2026 03:33:07 -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-4997abf917csm73026875e9.13.2026.08.12.03.33.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 12 Aug 2026 03:33:06 -0700 (PDT) From: Noam Ben Shimon To: laurent.pinchart@ideasonboard.com, hansg@kernel.org, mchehab@kernel.org Cc: ribalda@chromium.org, linux-media@vger.kernel.org, linux-kernel@vger.kernel.org, Noam Ben Shimon , stable@vger.kernel.org Subject: [PATCH v2] media: uvcvideo: Fix integer overflow in frame buffer size calculation Date: Wed, 12 Aug 2026 13:32:51 +0300 Message-Id: <20260812103251.18309-1-noambs2999@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260807101433.54886-1-noambs2999@gmail.com> References: <20260807101433.54886-1-noambs2999@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" In the function uvc_parse_frame(), it recomputes dwMaxVideoFrameBufferSize for uncompressed formats. This helps working around devices that report it wrong: frame->dwMaxVideoFrameBufferSize =3D 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") Cc: stable@vger.kernel.org Signed-off-by: Noam Ben Shimon Reviewed-by: Ricardo Ribalda Reviewed-by: Natasha Klaus --- Changes in v2: - Used a shift instead of division (Ricardo Ribalda) (Thanks!) - Add Cc: stable@vger.kernel.org (Ricardo Ribalda) Compile-tested using W=3D1 and no warnings. Tested with a UVC gadget over dummy_hcd with WSL. A frame descriptor declaring 32 bpp at 40000x40000 (computed size =3D 6400000000, which is above U32_MAX) is then rejected as expected, and the streaming interface is not registered: uvcvideo 1-1:1.0: Found format YUYV little-endian (0x56595559) uvcvideo 1-1:1.0: device 2 videostreaming interface 1 FRAME 1: computed b= uffer size overflows uvcvideo 1-1:1.0: No streaming interface found for terminal 32771. 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. 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..29e23f94751c 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 =3D format->bpp * frame->wWidth - * frame->wHeight / 8; + if (!(format->flags & UVC_FMT_FLAG_COMPRESSED)) { + u64 bufsize; + + bufsize =3D ((u64)format->bpp * frame->wWidth * frame->wHeight) >> 3; + 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 =3D bufsize; + } =20 /* * Clamp the default frame interval to the boundaries. A zero base-commit: f9a2394a23482bfd330911e9c8295b71724feacd --=20 2.34.1