From nobody Tue Jun 16 17:20:55 2026 Received: from mail-pf1-f175.google.com (mail-pf1-f175.google.com [209.85.210.175]) (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 0F56D369236 for ; Thu, 30 Apr 2026 09:10:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.175 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777540216; cv=none; b=UE4CUHlr4UZ81MGywFU1SyZv87Ein9AzPWZH3VjblM6t2aZC/ew+KeZfqJ79WpkVvkk/+aiF75Lsfx4yuhERd/2vgbNSl7THHvQeTA4BLJCXjxHKpnDv7QWuwy/gFGw7ohD2TGnVkVKBOQi0AqNSWB4LV9gO3Tz6et6UFm7sS6I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777540216; c=relaxed/simple; bh=4VajGRHwqqW1OW3StTaeSoxELJbW+gxNIgrIVHaeZ2U=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=n20VvqixPGoKDpj4a2lSrKryLyDn3S90S+WQJG48pOW7lMcELu30fG+cvXGNLiY2UR2KSHX1/9FrrRKL1nwG925m8gBhxnxplFTxLhQ95GWlFucGXeMj6DvrQV+5gBirWXr2gOlcjkI/Ns21lkAPqNlfxUVLN7gHkic3v2l6OVs= 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=m90HewGv; arc=none smtp.client-ip=209.85.210.175 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="m90HewGv" Received: by mail-pf1-f175.google.com with SMTP id d2e1a72fcca58-82f69adeecbso310391b3a.2 for ; Thu, 30 Apr 2026 02:10:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1777540214; x=1778145014; 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; bh=g24cpwlhdgkpjmh/f0yPDDS7asQqXWzHOuNgFla5f/U=; b=m90HewGv100uIRFDG0BMEYLOBXlfnPy3bR7cIVIjwh9f6mQD5Z+y3tAzDtP7R25Sdj R1KAJ9A6kOpOKuK6SFhD3Rm5y8koI86Jboleu633njtz/U20b7p0iwB88O1j2w4YfVE9 9TShH8PCAqLWTCzILqL34MGmX8Ul+WX2hStbqWqjEOJObYYJEwoTavDz7xq7yLzcse1u RIGSJXLq4X7/HIBY/Vzf2NkjaBMT4vDL2VE2QVQxpMqPWRWuSffR3mAPvaxe0kCxYlAR JIehC7DfAn4askQ6LwQ3wabVM2OqnwOOIdlccEsCHoX95Y9OGGVulNqG3n+Tsq/zbbmy v9rw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1777540214; x=1778145014; 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; bh=g24cpwlhdgkpjmh/f0yPDDS7asQqXWzHOuNgFla5f/U=; b=DjZfIzgGOgGnpYUXrGxbQ7I1m2UvU8o87B0diAEfxHi6h9gRFpqND/Bx5gfJ/vjR+F Gh55DsU9wywT4M3Z5tO1Iyrgg+4zzVzQjWR5Mi/WDTb+hXjMZV6wvLm3t64TQ5ME9wkl zR/XGyKw14KhdfNfc65PMj0/+5vrw3GZLT07GWJBoOtwBpeWe3ylfEMNdETWXhE3bBO/ ApKJU7jEtPHGeVfImH1XO1o2sK6qDMlOol1fpL4BG5yjMqlyfNPLxqG7kkB2dOu8TbHY 9ikrJAsNYS7jQwT8nMTEr02SFAvtJ6OwFoZrsMVcQDhQaGZzWbiOhiX0kXiqicEmmg0f Dpqw== X-Forwarded-Encrypted: i=1; AFNElJ9Rp9fDivz6Apog13uj6iePEid2tPd1N3VcHW2sspJbM1y8zt4uprUv+ArdWtGeKpl6mZmb67GCe4daxRw=@vger.kernel.org X-Gm-Message-State: AOJu0YzfiWno5bD0N9nw6d+Jjer9WoJ07nHXoi5MayIWg026zxGoB3/e PX50qp2EdEybU69ubyIHOiEEaLS8Duqdn2cX2ZXefdwXGM1XjLtvT8/h X-Gm-Gg: AeBDievkSzyh/02zHknHNbmuv+eMC/b51Ryy6AA6NE8jFzeI0j39rVWE0m5tGjyUz+L B9r9Q3RpEAu2i3Bs/y7gUtUsgqq+6h+VoiPj0p2F+bL/VlW7N/Mb9x6VVAxQ1De1veptIyTIEYt teY9WkapHi1hm7DpqPTYSVv8t/kee/0wsX1/BypaQ5lC6rwrXAivsh10vWB9cqgcGJhc037I8ar 86fsmBN6d9zWt4JLk6zDzlL0xP/HGTWB5+nOnZwZVdeVJ1kIhbmKlUzZroUemIQb+VInTUggeGr Gn7olDp5uUEAMjzjnXoAzeGJ0o1DfrJNijETNMI8Os9QtdjklHBw12HGINWNRWsLtk9W4Wm2zqr G3x815Zws9URL9U5t4JOGU3GUa9j1aIyxZizF5UcB7VqS8Il4PEUZ16CIf882naY2s3fED9LOKv KRBdAmYNWWnlEgma1R X-Received: by 2002:a05:6a00:3021:b0:82f:5a77:10e8 with SMTP id d2e1a72fcca58-834fe0bd639mr2256806b3a.20.1777540214429; Thu, 30 Apr 2026 02:10:14 -0700 (PDT) Received: from lgs.. ([2001:250:5800:1000::5a26]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-834ed80df96sm4449025b3a.54.2026.04.30.02.10.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Apr 2026 02:10:13 -0700 (PDT) From: Guangshuo Li To: Corentin Labbe , Mauro Carvalho Chehab , Hans Verkuil , mjpeg-users@lists.sourceforge.net, linux-media@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Guangshuo Li Subject: [PATCH] media: zoran: fix video_device double free Date: Thu, 30 Apr 2026 17:08:01 +0800 Message-ID: <20260430090801.455012-1-lgs201920130244@gmail.com> X-Mailer: git-send-email 2.43.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" zoran allocates zr->video_dev with video_device_alloc() and installs zoran_vdev_release() as the video_device release callback. That callback frees the video_device. After video_register_device() succeeds, the video_device lifetime is managed by the V4L2 core. video_unregister_device() eventually invokes the registered release callback, so the explicit kfree() after video_unregister_device() frees the same object again. There is also a registration failure path with the same risk. If device_register() fails inside video_register_device(), the V4L2 core calls put_device(&vdev->dev), which may run v4l2_device_release() and invoke zoran_vdev_release(). Control then returns to zoran_init_video_devices(), which frees zr->video_dev again. Fix the successful unregister path by dropping the extra kfree(). For registration failures, temporarily use video_device_release_empty while calling video_register_device(), so that any release callback run from the V4L2 core's registration failure path does not free the zoran-owned video_device. zoran_init_video_devices() then remains responsible for freeing the allocation exactly once. This issue was found by a static analysis tool I am developing. Fixes: 82e3a496eb56 ("media: staging: media: zoran: move videodev alloc") Signed-off-by: Guangshuo Li --- drivers/media/pci/zoran/zoran_card.c | 29 +++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/drivers/media/pci/zoran/zoran_card.c b/drivers/media/pci/zoran= /zoran_card.c index d81facf735d9..e7268718f137 100644 --- a/drivers/media/pci/zoran/zoran_card.c +++ b/drivers/media/pci/zoran/zoran_card.c @@ -863,10 +863,12 @@ int zoran_check_jpg_settings(struct zoran *zr, =20 static int zoran_init_video_device(struct zoran *zr, struct video_device *= video_dev, int dir) { + void (*release)(struct video_device *vdev); int err; =20 /* Now add the template and register the device unit. */ *video_dev =3D zoran_template; + release =3D video_dev->release; video_dev->v4l2_dev =3D &zr->v4l2_dev; video_dev->lock =3D &zr->lock; video_dev->device_caps =3D V4L2_CAP_STREAMING | dir; @@ -875,17 +877,36 @@ static int zoran_init_video_device(struct zoran *zr, = struct video_device *video_ video_dev->vfl_dir =3D VFL_DIR_RX; zoran_queue_init(zr, &zr->vq, V4L2_BUF_TYPE_VIDEO_CAPTURE); =20 + /* + * zr->video_dev is allocated by zoran_init_video_devices() and the + * caller frees it again if registration fails. If device_register() + * fails inside video_register_device(), the V4L2 core may still run + * vdev->release() from its put_device() cleanup path. Use an empty + * release callback while registering, so that this failure path cannot + * free zr->video_dev before control returns to the caller. + * + * Restore zoran_vdev_release() after successful registration, since + * the V4L2 core owns the video_device lifetime from that point on. + */ + video_dev->release =3D video_device_release_empty; + err =3D video_register_device(video_dev, VFL_TYPE_VIDEO, video_nr[zr->id]= ); - if (err < 0) + if (err < 0) { + video_dev->release =3D release; return err; + } + + video_dev->release =3D release; video_set_drvdata(video_dev, zr); return 0; } =20 static void zoran_exit_video_devices(struct zoran *zr) { + if (!zr->video_dev) + return; video_unregister_device(zr->video_dev); - kfree(zr->video_dev); + zr->video_dev =3D NULL; } =20 static int zoran_init_video_devices(struct zoran *zr) @@ -897,8 +918,10 @@ static int zoran_init_video_devices(struct zoran *zr) return -ENOMEM; =20 err =3D zoran_init_video_device(zr, zr->video_dev, V4L2_CAP_VIDEO_CAPTURE= ); - if (err) + if (err) { kfree(zr->video_dev); + zr->video_dev =3D NULL; + } return err; } =20 --=20 2.43.0