Received: by 2002:a05:6520:4211:b029:f4:110d:56bc with SMTP id o17csp2105151lkv; Thu, 20 May 2021 03:31:48 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwYkvTmsecT3ZrirJjCXYBUzm9iQIyQE5G7aFWPQMWfIjxFcll0taISpD+2j2FanVm4H3Yq X-Received: by 2002:aa7:db90:: with SMTP id u16mr4203244edt.106.1621506708352; Thu, 20 May 2021 03:31:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1621506708; cv=none; d=google.com; s=arc-20160816; b=aTEnx3LVoG2MZisvH0BIhkakajJXd4Yq8uCQ+SS8ELPnej/N1466hmD52bo1Ep+vKe AQyvCSKckRm57IXDuGdMo013q+DRlp6RW6Hly3iSdpV337Gkvx7SZObGPvfF2uqBt0j2 1AkKN90prKzxk5hFRW6zN4NzZmhrRowXncqf7wTaRJyMTYpc3MMfmRXSkluE0U7JGFiO PPTWB/y2+rrlz2J1rzZfY/ykYf+tNY93q6qxu66X55AFitElAQnwCGNWWqKLKFCSqJ9I dCkotxamhYpgWE8BkWicdgYEDx9zQM3gOzbq70CTqcXanTNNVXcaPTgp01enGJkZumjk ZLMA== 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=Pxl0QvCQZuC7m7eYfHTsLo4wCcmgYmAEjzaR9FNBrnI=; b=YbuYmofc1nQWL5gPrmxFA2dTBc/tLpb6HNCAaMSzuKqY6mWbMy6TNBuvnefKa2HPNP PaOb1AcWcUK2CQ2t6E1eWRg94N82fkWAxrQwqePRqR4lUUwQ4aLPPI7gPUnZKItAs9MT kAjrdPTn6kPW0Y0i+Eo3ujmq2iWQ3TVy/kD7E5BGMhr4gZr1BtQb/LUHhzLaoUJtKpZc KM98IE1pzgq1Awp/0QyYSokxWMrm9F5Nu5ntjTsxnQ4SD7+JgvrCtmIY2eNsTtI1JsWM DFomt9Gm+CcV3pWS8sBOBFZcWPtxoslvSzXOpd9pvTU2xsYpsVaVCsEMvhK+BJ8IXaN0 0NdA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=vRCm5Bxl; 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 f25si2218961eja.351.2021.05.20.03.31.23; Thu, 20 May 2021 03:31:48 -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=vRCm5Bxl; 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 S236768AbhETKbJ (ORCPT + 99 others); Thu, 20 May 2021 06:31:09 -0400 Received: from mail.kernel.org ([198.145.29.99]:51822 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235218AbhETKTG (ORCPT ); Thu, 20 May 2021 06:19:06 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 23E9461456; Thu, 20 May 2021 09:46:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1621504007; bh=V8Rj1K/KDbBtx4ZE/Yd1Ptz4GMDXFa/meDs4lFk/l6o=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=vRCm5BxlFNKu/XfiLGcHnNZsk5Ev/ovjt2bykS4wJ7rmcWRQwV3m2Sgfl4vgs+nMu IXUmRJLDwL1l8cadxagKkZ4dO8AWgtttm25aNxRJt1eH0W/+CkHmoACODgpxoFC8kb 4TV4RiXIqG37GLM6ZFUGR9C3kD1lCwoP3G5SZvMQ= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Daniel Niv , Hans Verkuil , Mauro Carvalho Chehab , Sasha Levin Subject: [PATCH 4.14 051/323] media: media/saa7164: fix saa7164_encoder_register() memory leak bugs Date: Thu, 20 May 2021 11:19:03 +0200 Message-Id: <20210520092121.855404597@linuxfoundation.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210520092120.115153432@linuxfoundation.org> References: <20210520092120.115153432@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: Daniel Niv [ Upstream commit c759b2970c561e3b56aa030deb13db104262adfe ] Add a fix for the memory leak bugs that can occur when the saa7164_encoder_register() function fails. The function allocates memory without explicitly freeing it when errors occur. Add a better error handling that deallocate the unused buffers before the function exits during a fail. Signed-off-by: Daniel Niv Signed-off-by: Hans Verkuil Signed-off-by: Mauro Carvalho Chehab Signed-off-by: Sasha Levin --- drivers/media/pci/saa7164/saa7164-encoder.c | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/drivers/media/pci/saa7164/saa7164-encoder.c b/drivers/media/pci/saa7164/saa7164-encoder.c index f21c245a54f7..c6aeac4db17f 100644 --- a/drivers/media/pci/saa7164/saa7164-encoder.c +++ b/drivers/media/pci/saa7164/saa7164-encoder.c @@ -1024,7 +1024,7 @@ int saa7164_encoder_register(struct saa7164_port *port) printk(KERN_ERR "%s() failed (errno = %d), NO PCI configuration\n", __func__, result); result = -ENOMEM; - goto failed; + goto fail_pci; } /* Establish encoder defaults here */ @@ -1078,7 +1078,7 @@ int saa7164_encoder_register(struct saa7164_port *port) 100000, ENCODER_DEF_BITRATE); if (hdl->error) { result = hdl->error; - goto failed; + goto fail_hdl; } port->std = V4L2_STD_NTSC_M; @@ -1096,7 +1096,7 @@ int saa7164_encoder_register(struct saa7164_port *port) printk(KERN_INFO "%s: can't allocate mpeg device\n", dev->name); result = -ENOMEM; - goto failed; + goto fail_hdl; } port->v4l_device->ctrl_handler = hdl; @@ -1107,10 +1107,7 @@ int saa7164_encoder_register(struct saa7164_port *port) if (result < 0) { printk(KERN_INFO "%s: can't register mpeg device\n", dev->name); - /* TODO: We're going to leak here if we don't dealloc - The buffers above. The unreg function can't deal wit it. - */ - goto failed; + goto fail_reg; } printk(KERN_INFO "%s: registered device video%d [mpeg]\n", @@ -1132,9 +1129,14 @@ int saa7164_encoder_register(struct saa7164_port *port) saa7164_api_set_encoder(port); saa7164_api_get_encoder(port); + return 0; - result = 0; -failed: +fail_reg: + video_device_release(port->v4l_device); + port->v4l_device = NULL; +fail_hdl: + v4l2_ctrl_handler_free(hdl); +fail_pci: return result; } -- 2.30.2