Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp2469689pxj; Mon, 10 May 2021 03:52:56 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxgPMJeONhbOQ13me3By03RDBiX3mBw9WQEQl+PfmZtiYh1qa7h79kvzUHDtf5fsovBM/f9 X-Received: by 2002:a17:906:685:: with SMTP id u5mr25209587ejb.449.1620643975794; Mon, 10 May 2021 03:52:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1620643975; cv=none; d=google.com; s=arc-20160816; b=PbTM8iQhp3ov5zw2Cr0sninTROPxpnCyxAEXkdwoCHea3pKcp0Qpv1d9wy1aQ56ESs wt9RkGixeKNF9nkqlAtmJiIo51xa220jgf+ni0VPQ9nN/ghin4hf1voF8qNuLBkE0ob6 tiPZaCseJxIgAiqaZuLRvnyY/0sHn0K9Lgn28NVdJ00NSxmheoz2F5RcdGQwoWi03WyZ nmXuV9AgL/Fgzsf96kdHYcDYJgBTVyCUjQTM6eAtx5F3Cfk8KjMFFVxqX0c1z9C6R6jR LUT0676JUKpHIkcXWOqEsmM2ZY71S99aQ4X4AUV3lMgdH+swgVE2ccca6EpVQdM3B1bC JNkg== 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=1rtG/qFJ+vkJKMsIkGPb7PtXlro5RkxVD/HCnGjVHYU=; b=TE1JcO+N9hz4CQR9X+/+UnPiTDQ8hZeNQdP0xqzT2L3LkIy22BqArfn+auhsApQYfF Occ0VHJMyF8zcPWONJyQkAay9Kob1LLFAmAqkmKwYz7EY544pTjZcNvLoVAmy9/SYfOl ujbAdCetNpZxh6GXZQN79zk9EYt5+21+qi0PuCCXuV7PgdVK2MnE60ZESfvfr8BROBCw LZm4TkVGj9eDJMtogECHWIdMFRrIsZukhOTaYlyhSvE4qnZVGkV5srgIG5ZD2pPQPCjs 6AN12HmMNQ9vceJRaAtaz8nPKCefpe9r1BFeiBkmuDrlNTO7HcmxqdxWyD/deObdMIeP PYRw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=B78Z6NKe; 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 u17si12946077ejf.662.2021.05.10.03.52.32; Mon, 10 May 2021 03:52:55 -0700 (PDT) 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=B78Z6NKe; 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 S233484AbhEJKuH (ORCPT + 99 others); Mon, 10 May 2021 06:50:07 -0400 Received: from mail.kernel.org ([198.145.29.99]:48222 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232898AbhEJKks (ORCPT ); Mon, 10 May 2021 06:40:48 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 0C42B61960; Mon, 10 May 2021 10:31:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1620642677; bh=Ef1iNY3LV1+geSqjKTWCHHmqZlGPYGe+YirmUhwHamY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=B78Z6NKexnyZaPngklI0CIzJxmpkddh/EmrOumADjLQboSv73qyBr/7WmgAahXTgF ctqEwJVn3O2ZF0aHSpgN4+J8LAYWBfncfjLU4QKcxrXOOse2uVqGvScPL2H55kAtXr xkoYlxQerZzTxMHtnVMdwa06aHSj8LTM5R+7yhEs= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Takashi Iwai , Sean Young , Mauro Carvalho Chehab Subject: [PATCH 5.4 168/184] media: dvb-usb: Fix memory leak at error in dvb_usb_device_init() Date: Mon, 10 May 2021 12:21:02 +0200 Message-Id: <20210510101955.626664130@linuxfoundation.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210510101950.200777181@linuxfoundation.org> References: <20210510101950.200777181@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: Takashi Iwai commit 13a79f14ab285120bc4977e00a7c731e8143f548 upstream. dvb_usb_device_init() allocates a dvb_usb_device object, but it doesn't release the object by itself even at errors. The object is released in the callee side (dvb_usb_init()) in some error cases via dvb_usb_exit() call, but it also missed the object free in other error paths. And, the caller (it's only dvb_usb_device_init()) doesn't seem caring the resource management as well, hence those memories are leaked. This patch assures releasing the memory at the error path in dvb_usb_device_init(). Now dvb_usb_init() frees the resources it allocated but leaves the passed dvb_usb_device object intact. In turn, the dvb_usb_device object is released in dvb_usb_device_init() instead. We could use dvb_usb_exit() function for releasing everything in the callee (as it was used for some error cases in the original code), but releasing the passed object in the callee is non-intuitive and error-prone. So I took this approach (which is more standard in Linus kernel code) although it ended with a bit more open codes. Along with the change, the patch makes sure that USB intfdata is reset and don't return the bogus pointer to the caller of dvb_usb_device_init() at the error path, too. Cc: Signed-off-by: Takashi Iwai Signed-off-by: Sean Young Signed-off-by: Mauro Carvalho Chehab Signed-off-by: Greg Kroah-Hartman --- drivers/media/usb/dvb-usb/dvb-usb-init.c | 47 ++++++++++++++++++++----------- 1 file changed, 31 insertions(+), 16 deletions(-) --- a/drivers/media/usb/dvb-usb/dvb-usb-init.c +++ b/drivers/media/usb/dvb-usb/dvb-usb-init.c @@ -170,22 +170,20 @@ static int dvb_usb_init(struct dvb_usb_d if (d->props.priv_init != NULL) { ret = d->props.priv_init(d); - if (ret != 0) { - kfree(d->priv); - d->priv = NULL; - return ret; - } + if (ret != 0) + goto err_priv_init; } } /* check the capabilities and set appropriate variables */ dvb_usb_device_power_ctrl(d, 1); - if ((ret = dvb_usb_i2c_init(d)) || - (ret = dvb_usb_adapter_init(d, adapter_nums))) { - dvb_usb_exit(d); - return ret; - } + ret = dvb_usb_i2c_init(d); + if (ret) + goto err_i2c_init; + ret = dvb_usb_adapter_init(d, adapter_nums); + if (ret) + goto err_adapter_init; if ((ret = dvb_usb_remote_init(d))) err("could not initialize remote control."); @@ -193,6 +191,17 @@ static int dvb_usb_init(struct dvb_usb_d dvb_usb_device_power_ctrl(d, 0); return 0; + +err_adapter_init: + dvb_usb_adapter_exit(d); +err_i2c_init: + dvb_usb_i2c_exit(d); + if (d->priv && d->props.priv_destroy) + d->props.priv_destroy(d); +err_priv_init: + kfree(d->priv); + d->priv = NULL; + return ret; } /* determine the name and the state of the just found USB device */ @@ -296,15 +305,21 @@ int dvb_usb_device_init(struct usb_inter usb_set_intfdata(intf, d); - if (du != NULL) + ret = dvb_usb_init(d, adapter_nums); + if (ret) { + info("%s error while loading driver (%d)", desc->name, ret); + goto error; + } + + if (du) *du = d; - ret = dvb_usb_init(d, adapter_nums); + info("%s successfully initialized and connected.", desc->name); + return 0; - if (ret == 0) - info("%s successfully initialized and connected.", desc->name); - else - info("%s error while loading driver (%d)", desc->name, ret); + error: + usb_set_intfdata(intf, NULL); + kfree(d); return ret; } EXPORT_SYMBOL(dvb_usb_device_init);