Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp59401pxj; Thu, 20 May 2021 04:33:20 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwIdDqy8qaNCdNx4oYPMYyRFkdcVbk9iM7JQBRlXzfvVelJNL/9xJ1cwjTx6FtVdI1FwrZ2 X-Received: by 2002:a05:6402:1256:: with SMTP id l22mr4509267edw.207.1621510399779; Thu, 20 May 2021 04:33:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1621510399; cv=none; d=google.com; s=arc-20160816; b=yDOs8rlvFtoXltJxwZ/+7TVM7BD2YWqhbNSzr/5Pjne6rAkNyyxbszmk7eU3CvUnFe /ANepVi3Zj+P8tUfOyMBqFxlMzqS0DyATcUsCtLLafZnpT3x5a1n2YnAJy7wA9O+dBlt QDjT9VxUK89Tk4j35+t6dkWtErkgMcqHnGiILMYvzZjAXT++ULUUBQId2ih1xCIDyjms dDSLMsjfw7UV5OKMm3p5z8w1etpXscu+MYpBMUWt3ERs2QWfx8hHlwa4rqoTjUTX5XuT pA6D8a2ROPuhrvvR0ZlxAzGCNwGSdkVHdPwddlEVnb1yhB5Y0PUQ9gQVdouvqa5yuy0V OtJw== 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=k5yv9N/xsmgaAVSkZAhZcyhNr+U4nCcUdZl+8rNcg2Y=; b=SC7mrxoOvnsVCp+3yiGPqKJjvdlTAm/xOUBVZaQOa5ve2hczCjCsZL2HlglEXkgWo5 nOdkpPp4+XMsdvDPt1JQzzxk+9RC7pXyeb6ZYQgQVAHkFYefTrNCRfoBlKeYU9lbD7an AFwThelFayqnDb5bYlcSFN7VItNmjZO8Z04hJeY+yW1gAyUfk56s+OrjAJO6PanQWfr9 UvByQmVKnyJbf+BMoYHIUnTTysyHbmfj/ZSdjIV6ld9ijjSJ5CJoLsXk4lQAgJbgRB1C pdmu7vHstE/YSp1Glt6ne3NaBpa/UmFwsAWhE8Q8YSiFOV3k6BJxrcpLbUtRSo4p5Ab8 /OTA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="V/A39bR6"; 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 d15si2156331edn.606.2021.05.20.04.32.48; Thu, 20 May 2021 04:33:19 -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="V/A39bR6"; 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 S241518AbhETL3Q (ORCPT + 99 others); Thu, 20 May 2021 07:29:16 -0400 Received: from mail.kernel.org ([198.145.29.99]:55128 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240238AbhETLJc (ORCPT ); Thu, 20 May 2021 07:09:32 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 8C7E061935; Thu, 20 May 2021 10:06:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1621505199; bh=tdsfINl8WT06Bq7ZxsVz/BBSClxn1h0PBiNLgj8e6j8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=V/A39bR6krBartEkzdOXSI7gyfCr9QXkpG4niPBtjBEJyXzXmzH/uG+v7UKT33lea wGopJKTgQ4b93yAvk9vcR1ZKLBN4J+15hG5BrOb5JKyFvTtKmacu8N6ymUiyYrHuZt 574YLH3j2hzXKIPCkJuxyiSxLypNd/vNziCXo8N8= 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.4 026/190] media: media/saa7164: fix saa7164_encoder_register() memory leak bugs Date: Thu, 20 May 2021 11:21:30 +0200 Message-Id: <20210520092103.039746970@linuxfoundation.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210520092102.149300807@linuxfoundation.org> References: <20210520092102.149300807@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 1b184c39ba97..966de363c575 100644 --- a/drivers/media/pci/saa7164/saa7164-encoder.c +++ b/drivers/media/pci/saa7164/saa7164-encoder.c @@ -1031,7 +1031,7 @@ int saa7164_encoder_register(struct saa7164_port *port) "(errno = %d), NO PCI configuration\n", __func__, result); result = -ENOMEM; - goto failed; + goto fail_pci; } /* Establish encoder defaults here */ @@ -1085,7 +1085,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; @@ -1103,7 +1103,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; @@ -1114,10 +1114,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", @@ -1139,9 +1136,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