Received: by 2002:a05:6a10:af89:0:0:0:0 with SMTP id iu9csp3600593pxb; Mon, 24 Jan 2022 13:13:56 -0800 (PST) X-Google-Smtp-Source: ABdhPJxK3YN8juqP7ZoPz7iP2z9Ph73xeGnlAUR6wioYsjDzlw7mg83O8yGD8IvrCZv9uo6kVd2d X-Received: by 2002:a05:6a00:be5:b0:4c7:f615:41de with SMTP id x37-20020a056a000be500b004c7f61541demr10095931pfu.34.1643058836628; Mon, 24 Jan 2022 13:13:56 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643058836; cv=none; d=google.com; s=arc-20160816; b=H+jOFPT2C6iBhFc7lTbi5JP/TBYhQDd5DqzFN/8C/K92IEtaUHhb+GYVuWaeOzTdf7 jOApygSsiG3hCotxo/fIii3xOuweEoEFaoPEmGa1Dw14FPsBaL/sIjD1ucRbcbn/muT6 xxQ206pbleWbQznkpUa8s3CocDRf8D2kzJNR7/jSCHDxT5Bsvs2c9ezmlt4mHVIwTawN 4D/vmdPC6W06EozJtf6slDfXWqk5kHykIsYyBcFiz0MKrSgsAxvviy9i3F1PHX+PJaOM qfFj69OZ2A17IyE+DqjYVZVfImYXFNBS3/Zzydb44eMhc92aPsLi5H36zfYKrkWfUJ2G RrLA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=QMvu+pXSSzR5VDaBYRxCex3nuibPGGhOMCQt0PavJTc=; b=cU/AraycqTD7xi03PdpWl93TXyNu7SQWiUCAVful4VKyV6NoZInIzLrLikoOKzP93z pWYjVfaWmyMDRglYgpIAg3so/bDPWKikkJt+yQUrbSCz8cth5rPImfuJ4XnHhKqxYoJF OH6fdF7KGSi/QJ7J4DLw/xUXPAH4ejs7VYUQ0tR2CYC26+7g35H1aFVW7k7ja+OqpHlf tZYDPpUTB3m8gXeFrZtzqIaUuXhb2/3080YMJfj5W2k4gffJqa3AyhhbHzMqoHFVw+Z7 pcv7tyRJV9+2+psjDrbTiBJJWieoNDlOvPGPqR/cjC/VvJ9jgIy55QWPofkY+xDshIZQ eo1w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=HAdPD3E9; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id e3si14381211pgk.443.2022.01.24.13.13.43; Mon, 24 Jan 2022 13:13:56 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=HAdPD3E9; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1387992AbiAXUhm (ORCPT + 99 others); Mon, 24 Jan 2022 15:37:42 -0500 Received: from ams.source.kernel.org ([145.40.68.75]:55276 "EHLO ams.source.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1380611AbiAXUQe (ORCPT ); Mon, 24 Jan 2022 15:16:34 -0500 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id CCBB7B8121A; Mon, 24 Jan 2022 20:16:33 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 19AD7C340E5; Mon, 24 Jan 2022 20:16:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1643055392; bh=E0GfmjPk2458BTHAGUlK7SfWSFjq1jqdNMCrK7Z2KfQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=HAdPD3E9tWVCLV7y0pTfc6idNmqJDqf6LxHiEMyVUE1Yof78JQEuZNu4Xa2luCl8i q9ljwkxOoYYmZk+LrsQbsnlfq2hPW+50yQzU4h7nnyxuIEHr9dxnakeytPHyaVdWVL iktGpEIKZoit15nUJf+hEZq/BDljKqYphU2mSVtY= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Hulk Robot , Wang Hai , Mauro Carvalho Chehab , Sasha Levin Subject: [PATCH 5.15 136/846] media: dmxdev: fix UAF when dvb_register_device() fails Date: Mon, 24 Jan 2022 19:34:13 +0100 Message-Id: <20220124184105.686782805@linuxfoundation.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220124184100.867127425@linuxfoundation.org> References: <20220124184100.867127425@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Wang Hai [ Upstream commit ab599eb11882f834951c436cc080c3455ba32b9b ] I got a use-after-free report: dvbdev: dvb_register_device: failed to create device dvb1.dvr0 (-12) ... ================================================================== BUG: KASAN: use-after-free in dvb_dmxdev_release+0xce/0x2f0 ... Call Trace: dump_stack_lvl+0x6c/0x8b print_address_description.constprop.0+0x48/0x70 kasan_report.cold+0x82/0xdb __asan_load4+0x6b/0x90 dvb_dmxdev_release+0xce/0x2f0 ... Allocated by task 7666: kasan_save_stack+0x23/0x50 __kasan_kmalloc+0x83/0xa0 kmem_cache_alloc_trace+0x22e/0x470 dvb_register_device+0x12f/0x980 dvb_dmxdev_init+0x1f3/0x230 ... Freed by task 7666: kasan_save_stack+0x23/0x50 kasan_set_track+0x20/0x30 kasan_set_free_info+0x24/0x40 __kasan_slab_free+0xf2/0x130 kfree+0xd1/0x5c0 dvb_register_device.cold+0x1ac/0x1fa dvb_dmxdev_init+0x1f3/0x230 ... When dvb_register_device() in dvb_dmxdev_init() fails, dvb_dmxdev_init() does not return a failure, and the memory pointed to by dvbdev or dvr_dvbdev is invalid at this point. If they are used subsequently, it will result in UFA or null-ptr-deref. If dvb_register_device() in dvb_dmxdev_init() fails, fix the bug by making dvb_dmxdev_init() return an error as well. Link: https://lore.kernel.org/linux-media/20211015085741.1203283-1-wanghai38@huawei.com Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2") Reported-by: Hulk Robot Signed-off-by: Wang Hai Signed-off-by: Mauro Carvalho Chehab Signed-off-by: Sasha Levin --- drivers/media/dvb-core/dmxdev.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/drivers/media/dvb-core/dmxdev.c b/drivers/media/dvb-core/dmxdev.c index 5d5a48475a54f..01f288fa37e0e 100644 --- a/drivers/media/dvb-core/dmxdev.c +++ b/drivers/media/dvb-core/dmxdev.c @@ -1413,7 +1413,7 @@ static const struct dvb_device dvbdev_dvr = { }; int dvb_dmxdev_init(struct dmxdev *dmxdev, struct dvb_adapter *dvb_adapter) { - int i; + int i, ret; if (dmxdev->demux->open(dmxdev->demux) < 0) return -EUSERS; @@ -1432,14 +1432,26 @@ int dvb_dmxdev_init(struct dmxdev *dmxdev, struct dvb_adapter *dvb_adapter) DMXDEV_STATE_FREE); } - dvb_register_device(dvb_adapter, &dmxdev->dvbdev, &dvbdev_demux, dmxdev, + ret = dvb_register_device(dvb_adapter, &dmxdev->dvbdev, &dvbdev_demux, dmxdev, DVB_DEVICE_DEMUX, dmxdev->filternum); - dvb_register_device(dvb_adapter, &dmxdev->dvr_dvbdev, &dvbdev_dvr, + if (ret < 0) + goto err_register_dvbdev; + + ret = dvb_register_device(dvb_adapter, &dmxdev->dvr_dvbdev, &dvbdev_dvr, dmxdev, DVB_DEVICE_DVR, dmxdev->filternum); + if (ret < 0) + goto err_register_dvr_dvbdev; dvb_ringbuffer_init(&dmxdev->dvr_buffer, NULL, 8192); return 0; + +err_register_dvr_dvbdev: + dvb_unregister_device(dmxdev->dvbdev); +err_register_dvbdev: + vfree(dmxdev->filter); + dmxdev->filter = NULL; + return ret; } EXPORT_SYMBOL(dvb_dmxdev_init); -- 2.34.1