Received: by 2002:a05:6500:2018:b0:1fb:9675:f89d with SMTP id t24csp318040lqh; Fri, 31 May 2024 02:11:47 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCWQnrTRmr8Aa1XSlvHsaGDm6pzuN1XMQb1xlpS7BOC3VRhLVPJVqdoNoAgJ37CAEimEyIYhwkcMSOvyATEtyKV5+JrDYivmqdAUMgLHQg== X-Google-Smtp-Source: AGHT+IGTd8R1igQSIs6JCVAJ0NS1fecgqw9HyM5WNeQddS+7fEvYNZlFK6j3jEADXwWxn1ld7pjE X-Received: by 2002:ae9:e317:0:b0:793:ec5:d8cd with SMTP id af79cd13be357-794f5cc83b2mr118819485a.61.1717146707294; Fri, 31 May 2024 02:11:47 -0700 (PDT) Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id af79cd13be357-794f318a431si171738185a.431.2024.05.31.02.11.47 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 31 May 2024 02:11:47 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-196552-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@163.com header.s=s110527 header.b=fHP4FBZx; arc=fail (body hash mismatch); spf=pass (google.com: domain of linux-kernel+bounces-196552-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-196552-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=163.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 0959B1C20D57 for ; Fri, 31 May 2024 09:11:47 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 62A1B156652; Fri, 31 May 2024 09:11:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=163.com header.i=@163.com header.b="fHP4FBZx" Received: from m16.mail.163.com (m16.mail.163.com [220.197.31.5]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 0027514F98; Fri, 31 May 2024 09:11:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=220.197.31.5 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717146684; cv=none; b=Ai6dIfCeTtyi1iSADhvuskTTmjHimeMsia+ksyPxZC9GGvnEC7kjxjraVWcI7dz+eL7pklMYPtKgNiuEoxA9DIy3jv15E5r5hCkeeF6h8QV7O7fYLtXR5FQBegrQ8jPb02n5aV7JaRmiOfRilmw4H0uCeqgpCGBDumttNtcfuro= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717146684; c=relaxed/simple; bh=mZP/brlklpO5LkphHGOq9s+S2cL8tohoUqi2RukSRpo=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=osESY37qjwB1NMWxou9jr0BRIRPBKcwnfyUykK/ankBeYitC20JswknvltmoIi5A4CMOtRowovcelYDSywz6wtWSU8mb/UcE2B0OcY8Pp2FJFjV9IrGTsHQpx3+Bvx/vChNMb4C0z8UlMaZYpFMxOPlll6zgcNZm+TXtQMlbcKk= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=163.com; spf=pass smtp.mailfrom=163.com; dkim=pass (1024-bit key) header.d=163.com header.i=@163.com header.b=fHP4FBZx; arc=none smtp.client-ip=220.197.31.5 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=163.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=163.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=163.com; s=s110527; h=From:Subject:Date:Message-Id:MIME-Version; bh=OpnQM xVNdJhAJ0JA1aQabqRhD8QhN2Egrh/1v4UTVLc=; b=fHP4FBZxVcERj6ztdMA4V Fwer9qUGhagKRm1oNtNeT8R0i9drBqM9+vggAqOa8ngQoRNuOcs5CMJkm9HPGPks LlSAB63qGGVpqxJ/NIl8wB020kKIh4uPOjypuQkmmsrWon5TXtjA5yPY2JCzV+Rw pKgo5fdTPunaXYPKTvNqhU= Received: from localhost.localdomain (unknown [111.48.69.246]) by gzga-smtp-mta-g3-0 (Coremail) with SMTP id _____wD3f6oalFlm3XSvFg--.25893S2; Fri, 31 May 2024 17:10:50 +0800 (CST) From: chizhiling@163.com To: mchehab@kernel.org Cc: linux-media@vger.kernel.org, linux-kernel@vger.kernel.org, Chi Zhiling Subject: [PATCH] media: xc2028: avoid use-after-free in load_firmware_cb() Date: Fri, 31 May 2024 17:10:26 +0800 Message-Id: <20240531091026.3094284-1-chizhiling@163.com> X-Mailer: git-send-email 2.27.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CM-TRANSID:_____wD3f6oalFlm3XSvFg--.25893S2 X-Coremail-Antispam: 1Uf129KBjvJXoWxXF47ZFyrCFyfKw1ktr1rCrg_yoWrAry3pF sxZFWfCrW8Jry3Jr47Jr4UJr1FqrW5Aa10kr4xA34agr43WrZ8t34UtFWUXr1UWr45Aa47 JF13JrWFkF4qyw7anT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x07UgZ2fUUUUU= X-CM-SenderInfo: hfkl6xxlol0wi6rwjhhfrp/1tbiTwHvnWXAmIwwpAAAsZ From: Chi Zhiling syzkaller reported use-after-free in load_firmware_cb() [1]. The reason is because the module allocated a struct tuner in tuner_probe(), and then the module initialization failed, the struct tuner was released. If a worker which created during module initialization accesses this struct later, it will cause use-after-free. The process is as follows: task-6504 worker_thread tuner_probe <= alloc dvb_frontend [2] .. request_firmware_nowait <= create a worker for request_firmware_work_func .. tuner_remove <= free dvb_frontend .. request_firmware_work_func <= the firmware is ready load_firmware_cb <= but now the dvb_frontend has been freed. To fix the issue, Check the dvd_frontend in load_firmware_cb(), if it is null, report a warning and just return. [1]: ================================================================== BUG: KASAN: use-after-free in load_firmware_cb+0x1310/0x17a0 Read of size 8 at addr ffff8000d7ca2308 by task kworker/2:3/6504 Call trace: load_firmware_cb+0x1310/0x17a0 request_firmware_work_func+0x128/0x220 process_one_work+0x770/0x1824 worker_thread+0x488/0xea0 kthread+0x300/0x430 ret_from_fork+0x10/0x20 Allocated by task 6504: kzalloc tuner_probe+0xb0/0x1430 i2c_device_probe+0x92c/0xaf0 really_probe+0x678/0xcd0 driver_probe_device+0x280/0x370 __device_attach_driver+0x220/0x330 bus_for_each_drv+0x134/0x1c0 __device_attach+0x1f4/0x410 device_initial_probe+0x20/0x30 bus_probe_device+0x184/0x200 device_add+0x924/0x12c0 device_register+0x24/0x30 i2c_new_device+0x4e0/0xc44 v4l2_i2c_new_subdev_board+0xbc/0x290 v4l2_i2c_new_subdev+0xc8/0x104 em28xx_v4l2_init+0x1dd0/0x3770 Freed by task 6504: kfree+0x238/0x4e4 tuner_remove+0x144/0x1c0 i2c_device_remove+0xc8/0x290 __device_release_driver+0x314/0x5fc device_release_driver+0x30/0x44 bus_remove_device+0x244/0x490 device_del+0x350/0x900 device_unregister+0x28/0xd0 i2c_unregister_device+0x174/0x1d0 v4l2_device_unregister+0x224/0x380 em28xx_v4l2_init+0x1d90/0x3770 The buggy address belongs to the object at ffff8000d7ca2000 which belongs to the cache kmalloc-2k of size 2048 The buggy address is located 776 bytes inside of 2048-byte region [ffff8000d7ca2000, ffff8000d7ca2800) The buggy address belongs to the page: page:ffff7fe00035f280 count:1 mapcount:0 mapping:ffff8000c001f000 index:0x0 flags: 0x7ff800000000100(slab) raw: 07ff800000000100 ffff7fe00049d880 0000000300000003 ffff8000c001f000 raw: 0000000000000000 0000000080100010 00000001ffffffff 0000000000000000 page dumped because: kasan: bad access detected Memory state around the buggy address: ffff8000d7ca2200: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb ffff8000d7ca2280: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb >ffff8000d7ca2300: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb ^ ffff8000d7ca2380: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb ffff8000d7ca2400: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb ================================================================== [2] Actually, it is allocated for struct tuner, and dvb_frontend is inside. Signed-off-by: Chi Zhiling --- drivers/media/tuners/xc2028.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/drivers/media/tuners/xc2028.c b/drivers/media/tuners/xc2028.c index 5a967edceca9..352b8a3679b7 100644 --- a/drivers/media/tuners/xc2028.c +++ b/drivers/media/tuners/xc2028.c @@ -1361,9 +1361,16 @@ static void load_firmware_cb(const struct firmware *fw, void *context) { struct dvb_frontend *fe = context; - struct xc2028_data *priv = fe->tuner_priv; + struct xc2028_data *priv; int rc; + if (!fe) { + pr_warn("xc2028: No frontend in %s\n", __func__); + return; + } + + priv = fe->tuner_priv; + tuner_dbg("request_firmware_nowait(): %s\n", fw ? "OK" : "error"); if (!fw) { tuner_err("Could not load firmware %s.\n", priv->fname); -- 2.25.1