Received: by 2002:a05:6358:f14:b0:e5:3b68:ec04 with SMTP id b20csp1474636rwj; Sun, 18 Dec 2022 08:54:46 -0800 (PST) X-Google-Smtp-Source: AMrXdXvpEW8pF9BdkoFNmUBcrLszqT9+8LqCWw3h91K3jIViRWAUJn8syaS4zEqt/9wmqQmOg1rT X-Received: by 2002:a17:907:9d19:b0:81b:f931:cb08 with SMTP id kt25-20020a1709079d1900b0081bf931cb08mr1546201ejc.47.1671382486676; Sun, 18 Dec 2022 08:54:46 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1671382486; cv=none; d=google.com; s=arc-20160816; b=l5+82tX7TDvtToZAhyspWEg5NOE8ejVFRhqtNmIVTZrNHLsfsqRy7TV5NVkGyPv2xb eC3IwKKbW5lh8NyWiDI5ih1BOMoEPeMJk6GIPLfFLHIF52VGFcUqPzqMJEQkzqqOwV2e PP9MDUDuGrdH4NfM8zLa7pJLL+Tzvz6fgNbRBf0IyaplbH5oyXTPA//pJPQgWMAArSOb fwNjJM6bJwxUsCU2koPapMrob4Iw+BKC0DuNyExlEx0XZrkD+YVec752cxPsp30zR5uD u4YFh9LA9CYYMYrIeI2sg2fSdpF1d7pA6/oeyZBVJr4Im7XmuaCYZqIdbghT+oqJljDR QzNQ== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=4jMDm4IQ9TRQjV7GvNphh52e8GoaHmCH4iAsc6qJuoE=; b=pOR5ECB9qdY3pogs9c9NQ8SXVgbMTFrrzrHOwQ9oqYFSpp733rsSOxytnUS7xBRvob CSd+GF3GyRXIdxRa0jYQ/0eH0yMIwBqdo7tmCVqQdcidgxjXbh30QI7NOtFBuLQmQrdZ ZNerH/y9Cjv2xoQGw8dW3EreP59qQns3vv1OwZqHTUyQZd4rutbEl3cTGKPlRFbaOO0A 27uvvpD0ywddl59CFMEzI7x3sxJSHCw2Nw5cmFQ9kbXyTXJWHqoBQZkneg1nPwtqpUQY COuXoAC/d6mTxqJ1KfMiLc8K9RItVKSAa+cIkSeA3rmgC3RchqonIEId7kHITD0gGK4M ZqQg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=RLOIPUGL; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id qa16-20020a170907869000b007c500a2f33asi8012758ejc.208.2022.12.18.08.54.30; Sun, 18 Dec 2022 08:54:46 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=RLOIPUGL; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232619AbiLRQoC (ORCPT + 70 others); Sun, 18 Dec 2022 11:44:02 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43954 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232767AbiLRQmZ (ORCPT ); Sun, 18 Dec 2022 11:42:25 -0500 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DB45F19027; Sun, 18 Dec 2022 08:15:28 -0800 (PST) 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 787C6B80B45; Sun, 18 Dec 2022 16:15:27 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 64DD2C433EF; Sun, 18 Dec 2022 16:15:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1671380126; bh=xEMiQK885xmLg3qtc8QmYHFfWoEH/3qKOlQ4YuaktGU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=RLOIPUGLtQq4LfEOVK01ZvTt0C60qT4qkE42hVSgYz88eFRaVGItdDvhupLvfM6wO z6s46xuHDmZmW8i+eTdqFtVpvLsPGzJuL9UsKqtVeaS6UYUCCNSutlMZlqu9LCO5VM 2gDQ7yhRnqcP+E1Pey63sEYdFubM6wsfPo0LuR5herUShBKCZ5xRbrLTTOwFmRnO7L HwIc5eGCzU9FaWEMbXa7JX0NnqRST/JK4xMtUyb85ASVZKVtVSYpjbHY/caQ+E6hcw 1VJn+7uZMC5LMNIFCIDF5ONHOcy1/FZF1yAhwMbA/x7YqUcJq0/mimMN1X+6Py80lA x8WZooLH/BDdw== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Mazin Al Haddad , syzbot+f66dd31987e6740657be@syzkaller.appspotmail.com, Mauro Carvalho Chehab , Sasha Levin , linux-media@vger.kernel.org Subject: [PATCH AUTOSEL 5.15 36/46] media: dvb-usb: fix memory leak in dvb_usb_adapter_init() Date: Sun, 18 Dec 2022 11:12:34 -0500 Message-Id: <20221218161244.930785-36-sashal@kernel.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20221218161244.930785-1-sashal@kernel.org> References: <20221218161244.930785-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Mazin Al Haddad [ Upstream commit 94d90fb06b94a90c176270d38861bcba34ce377d ] Syzbot reports a memory leak in "dvb_usb_adapter_init()". The leak is due to not accounting for and freeing current iteration's adapter->priv in case of an error. Currently if an error occurs, it will exit before incrementing "num_adapters_initalized", which is used as a reference counter to free all adap->priv in "dvb_usb_adapter_exit()". There are multiple error paths that can exit from before incrementing the counter. Including the error handling paths for "dvb_usb_adapter_stream_init()", "dvb_usb_adapter_dvb_init()" and "dvb_usb_adapter_frontend_init()" within "dvb_usb_adapter_init()". This means that in case of an error in any of these functions the current iteration is not accounted for and the current iteration's adap->priv is not freed. Fix this by freeing the current iteration's adap->priv in the "stream_init_err:" label in the error path. The rest of the (accounted for) adap->priv objects are freed in dvb_usb_adapter_exit() as expected using the num_adapters_initalized variable. Syzbot report: BUG: memory leak unreferenced object 0xffff8881172f1a00 (size 512): comm "kworker/0:2", pid 139, jiffies 4294994873 (age 10.960s) hex dump (first 32 bytes): 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ backtrace: [] dvb_usb_adapter_init drivers/media/usb/dvb-usb/dvb-usb-init.c:75 [inline] [] dvb_usb_init drivers/media/usb/dvb-usb/dvb-usb-init.c:184 [inline] [] dvb_usb_device_init.cold+0x4e5/0x79e drivers/media/usb/dvb-usb/dvb-usb-init.c:308 [] dib0700_probe+0x8d/0x1b0 drivers/media/usb/dvb-usb/dib0700_core.c:883 [] usb_probe_interface+0x177/0x370 drivers/usb/core/driver.c:396 [] call_driver_probe drivers/base/dd.c:542 [inline] [] really_probe.part.0+0xe7/0x310 drivers/base/dd.c:621 [] really_probe drivers/base/dd.c:583 [inline] [] __driver_probe_device+0x10c/0x1e0 drivers/base/dd.c:752 [] driver_probe_device+0x2a/0x120 drivers/base/dd.c:782 [] __device_attach_driver+0xf6/0x140 drivers/base/dd.c:899 [] bus_for_each_drv+0xb7/0x100 drivers/base/bus.c:427 [] __device_attach+0x122/0x260 drivers/base/dd.c:970 [] bus_probe_device+0xc6/0xe0 drivers/base/bus.c:487 [] device_add+0x5fb/0xdf0 drivers/base/core.c:3405 [] usb_set_configuration+0x8f2/0xb80 drivers/usb/core/message.c:2170 [] usb_generic_driver_probe+0x8c/0xc0 drivers/usb/core/generic.c:238 [] usb_probe_device+0x5c/0x140 drivers/usb/core/driver.c:293 [] call_driver_probe drivers/base/dd.c:542 [inline] [] really_probe.part.0+0xe7/0x310 drivers/base/dd.c:621 [] really_probe drivers/base/dd.c:583 [inline] [] __driver_probe_device+0x10c/0x1e0 drivers/base/dd.c:752 Link: https://syzkaller.appspot.com/bug?extid=f66dd31987e6740657be Reported-and-tested-by: syzbot+f66dd31987e6740657be@syzkaller.appspotmail.com Link: https://lore.kernel.org/linux-media/20220824012152.539788-1-mazinalhaddad05@gmail.com Signed-off-by: Mazin Al Haddad Signed-off-by: Mauro Carvalho Chehab Signed-off-by: Sasha Levin --- drivers/media/usb/dvb-usb/dvb-usb-init.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/drivers/media/usb/dvb-usb/dvb-usb-init.c b/drivers/media/usb/dvb-usb/dvb-usb-init.c index 61439c8f33ca..58eea8ab5477 100644 --- a/drivers/media/usb/dvb-usb/dvb-usb-init.c +++ b/drivers/media/usb/dvb-usb/dvb-usb-init.c @@ -81,7 +81,7 @@ static int dvb_usb_adapter_init(struct dvb_usb_device *d, short *adapter_nrs) ret = dvb_usb_adapter_stream_init(adap); if (ret) - return ret; + goto stream_init_err; ret = dvb_usb_adapter_dvb_init(adap, adapter_nrs); if (ret) @@ -114,6 +114,8 @@ static int dvb_usb_adapter_init(struct dvb_usb_device *d, short *adapter_nrs) dvb_usb_adapter_dvb_exit(adap); dvb_init_err: dvb_usb_adapter_stream_exit(adap); +stream_init_err: + kfree(adap->priv); return ret; } -- 2.35.1