Received: by 2002:a05:6a10:af89:0:0:0:0 with SMTP id iu9csp3625687pxb; Mon, 24 Jan 2022 13:49:30 -0800 (PST) X-Google-Smtp-Source: ABdhPJxqyUl7+LAgW775WO3ZPGkQ0dWQ3iAV0sI1DDtgwixHpA78WDgnqn2OA5lS8Yn/LU75YNSx X-Received: by 2002:a05:6a00:15d1:b0:4c1:e1d2:347c with SMTP id o17-20020a056a0015d100b004c1e1d2347cmr15706432pfu.29.1643060970120; Mon, 24 Jan 2022 13:49:30 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643060970; cv=none; d=google.com; s=arc-20160816; b=Jy2RUkd47FqIIfKcUgE3jsVb4AIzqcpwblXzlOjYf7UpPYNwdFZDUluW5wKTh32+Rt B6YbntHOjjuBbMNYQOT31wFhBzISpg3wJ1t/kszxDCEiTHkvsls8+qd28EQJhErLd3m4 mVi8T/D1o9+ZPCBMZZhF7mX/3NgUiMbvzgkPxXFHGF36g1vKJshTZFSa2MKOEpipQfac /HdDAjqxyIt0e102qHVoicRMJ59v7AogukoCX4KY1yrOiazQjW6z3HdkTRAnVO5/LsBO l98DrQCkk/wJ4aajj8PgK+EbMIHl3zHwsNDKVCW+CXo/NFZU7csbmIKAc5VRD75T4W0x 99Mw== 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=r4AW+uziYoQmevLrK8EHM5iS+BMm1g2l9tadh5+QWQecZi87KhYyY0XUuNoPpFTy7d zxKW5h0GokoZlVoe+4Z8rJBg6bO4bMbM7x01saE2Gm5ewhHuqObbDWf+vGoyrYc+M69f bTQs6QMr52Tin1uNMB7TevGcjSq02+Ug6aOx8FrklDlxXs9bkrhYM2aQLVRbDHI9p6Na MEywe49gWBcOFXh5Bs9r+DYM/PLCRRFxPocs3WqhQrF+sYiLae0q0tHp24gKLKuRlEod V2TBZfzzeVlZ9OPCXgczahlt2DuQwKoflFxu3y6aPUJsOvtUo/SYS1VKiKtVG43YPpFi 6xPQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=MqApB7r9; 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 b12si15003834pfv.230.2022.01.24.13.49.05; Mon, 24 Jan 2022 13:49:30 -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=MqApB7r9; 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 S1455945AbiAXVgw (ORCPT + 99 others); Mon, 24 Jan 2022 16:36:52 -0500 Received: from ams.source.kernel.org ([145.40.68.75]:55124 "EHLO ams.source.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1444379AbiAXVAs (ORCPT ); Mon, 24 Jan 2022 16:00:48 -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 925F7B80CCF; Mon, 24 Jan 2022 21:00:46 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id AE356C340E5; Mon, 24 Jan 2022 21:00:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1643058045; bh=E0GfmjPk2458BTHAGUlK7SfWSFjq1jqdNMCrK7Z2KfQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=MqApB7r96mJkfJI1B1F18fjlW3YgtE43lBtX9P3oiuRHuKvuG3U/CL+E7E2HvLRFi YZzW7IQv0qyHcz5VY2odUhuc6vcKw/+ywOKqnbAK9fVdfgzILB6DfVdWGvxStEzEf0 ycIHw7XKdy2AwrxysgVnZVgqr9Viz1/G45YeFpks= 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.16 0156/1039] media: dmxdev: fix UAF when dvb_register_device() fails Date: Mon, 24 Jan 2022 19:32:25 +0100 Message-Id: <20220124184130.414042699@linuxfoundation.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220124184125.121143506@linuxfoundation.org> References: <20220124184125.121143506@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