Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp2483102pxj; Mon, 10 May 2021 04:11:02 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwTHApzCEkhTUaBdowgpoiCUaw8UbXPJ5B32ghxIihm++Cau74uROPEQKAUTjlPiHXy0mUI X-Received: by 2002:a05:6e02:1110:: with SMTP id u16mr2817477ilk.64.1620645061846; Mon, 10 May 2021 04:11:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1620645061; cv=none; d=google.com; s=arc-20160816; b=IjAvwKUxW6rcUp8fnIHJiR01F1fNfdxX3zi3JhRwmJwq9+iwdUHdA8n+Y12URYHh1J 93Arj1CSwV3M4Vc/EEje37hUf45OD0SPeb9o++DScyyHNZvC2/O0pT0KdHlmtyTzKydV wJNlfDg2FMKYQiEGLcfrjVFvWyp/wxSKVdWkWr4GxTq4sTHXXW54HBBW2NyVJM7JX15q qMawNdGCTIEBmoQ4Cud+h4+nJI/8flmM7DcbR74L9ttRkkaH3KIAEjXcuz9rQxDhZqGa Ez/Nay0LXJiD4EUCrYINi/cwxewI7HcagiPES4Om+5ZKWnDGTPaWIbdfUjzzBUHw9jbV UAgQ== 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=G+XJdFUCaKSLKo57uzgvJhwkloUnZ3KhXDLJWZlMZsE=; b=i2hbaJIExnLtwONuwk+5Bu61fHc1N0vcmt5g2eoLVwn9A1NC4FAVlcJ44g0M57nj8z SNvgsI1Jt6iszRXBvVjrfweagoD9Hw66HSu2CzGQs4yEE5oRX5+LeXEEIk9q1DkQyRRo Va7JUWNR+gI3F/7zP/HoVAL7EiqIlJKsRQaVPnSancAD2bW5fKVGvweqVvhqAE7sKCw/ 51Cs3dkZUzT6wQF0lk/STdzHfpAiiRfj52bPN2lcXCgLivLBXXMufBJB2GqOG6I0/Uwm mMKbDTkZvFukQhl/VAxp4KpwHcjEfzdywUue0lSh/5IolLs5LDaoV19i531ilVQxwpIS LINg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=ECtyDvNo; 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 r2si14701897ioh.103.2021.05.10.04.10.49; Mon, 10 May 2021 04:11:01 -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=ECtyDvNo; 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 S234923AbhEJLFa (ORCPT + 99 others); Mon, 10 May 2021 07:05:30 -0400 Received: from mail.kernel.org ([198.145.29.99]:59360 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232039AbhEJKpw (ORCPT ); Mon, 10 May 2021 06:45:52 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 72BF0613C5; Mon, 10 May 2021 10:36:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1620642995; bh=Wh17wluSKUTBOWNzYuYLyW+TL60RDkDmKbWLKi2/MZc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ECtyDvNoGfwmD9uG0W5oiN9KP7SQg6EvEgoPLcKd0E2oTtIyLUVQ7KPov5C4oFUZs zrGbsf2MDCxWyn1UqtPSBOV82EGXI+zSOQ71duZG+zOLOR+h7AE+gSBFGHFEX/GE0u OV9Pfc6L6bNBz72mtUiNTUQIDBUzbQdH8374ufzk= 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 5.10 136/299] media: media/saa7164: fix saa7164_encoder_register() memory leak bugs Date: Mon, 10 May 2021 12:18:53 +0200 Message-Id: <20210510102009.465477769@linuxfoundation.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210510102004.821838356@linuxfoundation.org> References: <20210510102004.821838356@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 11e1eb6a6809..1d1d32e043f1 100644 --- a/drivers/media/pci/saa7164/saa7164-encoder.c +++ b/drivers/media/pci/saa7164/saa7164-encoder.c @@ -1008,7 +1008,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 */ @@ -1062,7 +1062,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; @@ -1080,7 +1080,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; @@ -1091,10 +1091,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", @@ -1116,9 +1113,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