From nobody Fri Jul 24 23:30:11 2026 Received: from mail-yx1-f47.google.com (mail-yx1-f47.google.com [74.125.224.47]) (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 3840923C503 for ; Wed, 22 Jul 2026 04:23:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.224.47 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784694196; cv=none; b=GEqaAtbjGyPvtGJojKcA+NS1/LrJadTdsrA0xosx+9Toli6GNDL7IzCJbvCbwaoUbxU3GGtz+yjgUcHoOSrNTSeXXcVdjg/7ah9R9/aQmhnPMWcl0HBcgZ+8P18lO1hoRfWyEM+ekHx99hvxBAtcVXMymvDSTWqHRagClSWx2Is= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784694196; c=relaxed/simple; bh=Tz8ATHq3AOH0oiRHBDlB0/d5Gvc7fJ2Zya9H84QVcgQ=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=VXG9U+oM10eSeZHSu/tDuzrP08y1b3wBuxK3RDjtejG80ySLNfl5DNicMfxK+GFJscr7Pl6OtKfLSu3zBxLZepmo9aP921Cps6xdxyq1gghPpPZgUui2LoKS9vY1okmQEX2ADSkMpuO8nXUUTeEJtcAheKfLT6LS1KwfzXM6CpM= 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=ihNe+z1K; arc=none smtp.client-ip=74.125.224.47 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="ihNe+z1K" Received: by mail-yx1-f47.google.com with SMTP id 956f58d0204a3-6688dfaed31so611240d50.3 for ; Tue, 21 Jul 2026 21:23:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1784694190; x=1785298990; 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=BVU1uXONejBx60ngAbjZKU1diMFaAoxWI34xUNLIr4k=; b=ihNe+z1KC2yBiwU3FjLI/+98w/HV6PAZD20uzxDED9jQMebOvhGSzIsVORh9BF8E06 GgpEsTZJHCjEYtUpnYYx10uegl60U+eIJXY+6hq37Mx43UjYmT/BjAwflyBDu+pU2tRI +UtTeqRVKkdZr25Nsp9qG3iykGa6BPCZ5YKn/C064wSqowy8U+mUM23ZxehQFLWuT787 kbSxAg97zGa5NquU1MKriElfow4s64ZTjaALLI7yVMyAlu4fJ7b2al5+FvMt6bwowL4u J9EywylXAcXQUWQh3fQGnWAurboc+oKHzudvbsWlGAP2ZtnoWqdQgnI9NWTALp9WWBME /6ZQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1784694190; x=1785298990; 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=BVU1uXONejBx60ngAbjZKU1diMFaAoxWI34xUNLIr4k=; b=cCGc6Ry5OeohGUzOBoRHxHLzvaGlznqFgbtdsruyGbxraFfd5VLjHJnlOD+i9Vm/UO 8vy2GNYRpkIj4uaDvezF91Nqc4qNRYzUA/vbYF+xEvfAp9O8f53t++ewJ4JqsV8vZK8m 5AeEUSpV/beNXdwoUNxInihcKn7wS34IjmIgbh7omlInepIv6ZOv3sYnpEVnG5Qk+WWN HxwkLT7UA8Bg5WAttjGw5YpS8OZ0+fEXE2m9K8lWezfP3dA3EG7WqhxTawR/POJ/2eAB inDdDES1p/bYtbm+hVPXnJExI5PDjv7/a9Hh3/Fh2UgXwhd0GSKFsAkH0HmW5AGihgfe JEOg== X-Forwarded-Encrypted: i=1; AHgh+RouutnK03a6n5uIdhYf2gTH7AQjkkNuHxEr+rdnbRU3iZbU263aWE+bqDTV8iyvL6wZKrmGpXOdEXuoysQ=@vger.kernel.org X-Gm-Message-State: AOJu0YyDGiDdmCSCh7cmoNDfUoYlJNVZbBX9XUJecLzwkUTPmXnX8/8h TrBKj9CoSfxJq3nV/zMP0k0ZZpHBbb2Dvj9XfjYoFZMD41y3w7jOY8/e X-Gm-Gg: AR+sD11I+pUS7hqgqmfz0trQydnSkC1PEi1pRsUeKIIDJh4SL0935xtXyu7phs0qp+b px0Mtxk+bSJ4YG9YbHK9wMk5midSgp1bsKKcyY8fe1P5yTHduSqOxTLSaPI/mfZcuZ62aPQw2zJ Rgbxk2lk5huPCR6VpeTjHXY9msX1bnK2L6Q5DrhOMTN4EYd5mxb2qA1lZ5IZNFg9MPFTol7YYBJ iv2rpbWE/qhW9JSSObtWZFwGUA/MHmsmx77M5Xl+sCntchcZEE67VwxazNEPTsaXYDK+bxesxYF RMKna4+HDoDnAY5imvlfY97DDnBIXPI28HtaHZGo4t8USfMz84YTszevr3siMR61PC685YfDNM9 HrLuZwztyCm4GimFw+U0Sx5JHoqLaF06WyoBUPRmhmULUN3uAdU6CNP2R7CfelGPxB659qlW/e3 fIMHHI3rFQEL1jyfxD+07CqPX3EYRxshcOswt++4KDbfC4vtfQFE4Ewwzq/SvFhf/F6twEx6WeQ iKsAOKDapcaOw== X-Received: by 2002:a53:d057:0:10b0:667:8b90:2eeb with SMTP id 956f58d0204a3-6683bdb71c2mr4845726d50.102.1784694190034; Tue, 21 Jul 2026 21:23:10 -0700 (PDT) Received: from LAPTOP-83ECOPAB.f7a5e5c3-cab1-4810-bdbb-207cdd06de9e.globalsecureaccess.local ([136.55.173.105]) by smtp.gmail.com with ESMTPSA id 956f58d0204a3-668906d9344sm695500d50.12.2026.07.21.21.23.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Jul 2026 21:23:09 -0700 (PDT) From: "Cen Zhang (Microsoft)" To: mchehab@kernel.org, linux-media@vger.kernel.org Cc: kees@kernel.org, linux-kernel@vger.kernel.org, AutonomousCodeSecurity@microsoft.com, tgopinath@linux.microsoft.com, kys@microsoft.com, blbllhy@gmail.com, stable@vger.kernel.org Subject: [PATCH v2] media: v4l2-event: limit number of event subscriptions per file handle Date: Wed, 22 Jul 2026 00:23:08 -0400 Message-ID: <20260722042308.75939-1-blbllhy@gmail.com> X-Mailer: git-send-email 2.53.0 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" v4l2_event_subscribe() allows an unbounded number of event subscriptions per file handle. Since the subscription id field is fully user-controlled (32-bit), an unprivileged user with access to a V4L2 device node can create up to 2^32 distinct subscriptions, each pinning a kernel allocation (~200 bytes). This can exhaust kernel memory, causing an OOM condition and kernel panic. An unprivileged local user can trigger this by issuing repeated VIDIOC_SUBSCRIBE_EVENT ioctls with incrementing id values. The allocated objects reside in kernel slab (not accounted to the process cgroup), so existing memory limits (ulimit, memcg) do not prevent this. Most V4L2 drivers are affected because the framework function v4l2_event_subscribe() enforces no limit, such as uvcvideo (USB webcams) and the vicodec test driver used to reproduce this issue. This leads to: Kernel panic - not syncing: Out of memory: compulsory panic_on_oom is enabled Fix by adding a per-filehandle subscription counter and capping it at 256. Fixes: 6e239399e580 ("[media] v4l2-ctrls: add control events") Cc: stable@vger.kernel.org Reported-by: Autonomous Code Security Link: https://lore.kernel.org/all/20260722004818.72310-1-blbllhy@gmail.com Signed-off-by: Cen Zhang (Microsoft) --- v2: - Add Cc: stable@vger.kernel.org - Add Link: to v1 - Wrap long line in commit message drivers/media/v4l2-core/v4l2-event.c | 14 +++++++++++++- include/media/v4l2-fh.h | 2 ++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/drivers/media/v4l2-core/v4l2-event.c b/drivers/media/v4l2-core= /v4l2-event.c index 9dd2aaa95a67..fcd8ce4addc1 100644 --- a/drivers/media/v4l2-core/v4l2-event.c +++ b/drivers/media/v4l2-core/v4l2-event.c @@ -18,6 +18,9 @@ #include #include =20 +/* Per-filehandle limit on the number of event subscriptions. */ +#define V4L2_MAX_EVENT_SUBSCRIPTIONS 256 + static unsigned int sev_pos(const struct v4l2_subscribed_event *sev, unsig= ned int idx) { idx +=3D sev->first; @@ -218,6 +221,7 @@ static void __v4l2_event_unsubscribe(struct v4l2_subscr= ibed_event *sev) fh->navailable--; } list_del(&sev->list); + fh->nsubscribed--; } =20 int v4l2_event_subscribe(struct v4l2_fh *fh, @@ -251,8 +255,16 @@ int v4l2_event_subscribe(struct v4l2_fh *fh, =20 spin_lock_irqsave(&fh->vdev->fh_lock, flags); found_ev =3D v4l2_event_subscribed(fh, sub->type, sub->id); - if (!found_ev) + if (!found_ev) { + if (fh->nsubscribed >=3D V4L2_MAX_EVENT_SUBSCRIPTIONS) { + spin_unlock_irqrestore(&fh->vdev->fh_lock, flags); + kvfree(sev); + mutex_unlock(&fh->subscribe_lock); + return -ENOSPC; + } list_add(&sev->list, &fh->subscribed); + fh->nsubscribed++; + } spin_unlock_irqrestore(&fh->vdev->fh_lock, flags); =20 if (found_ev) { diff --git a/include/media/v4l2-fh.h b/include/media/v4l2-fh.h index aad4b3689d7e..65a7f31af889 100644 --- a/include/media/v4l2-fh.h +++ b/include/media/v4l2-fh.h @@ -33,6 +33,7 @@ struct v4l2_ctrl_handler; * @subscribe_lock: serialise changes to the subscribed list; guarantee th= at * the add and del event callbacks are orderly called * @subscribed: list of subscribed events + * @nsubscribed: number of subscribed events at @subscribed list * @available: list of events waiting to be dequeued * @navailable: number of available events at @available list * @sequence: event sequence number @@ -49,6 +50,7 @@ struct v4l2_fh { wait_queue_head_t wait; struct mutex subscribe_lock; struct list_head subscribed; + unsigned int nsubscribed; struct list_head available; unsigned int navailable; u32 sequence; --=20 2.53.0