Return-Path: Received: from mail-dm3nam03on0092.outbound.protection.outlook.com ([104.47.41.92]:19136 "EHLO NAM03-DM3-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S965847AbeCSQFv (ORCPT ); Mon, 19 Mar 2018 12:05:51 -0400 From: Sasha Levin To: "linux-kernel@vger.kernel.org" , "stable@vger.kernel.org" CC: Christophe JAILLET , Sasha Levin Subject: [PATCH AUTOSEL for 4.9 261/281] drm/vc4: Fix resource leak in 'vc4_get_hang_state_ioctl()' in error handling path Date: Mon, 19 Mar 2018 16:03:03 +0000 Message-ID: <20180319155742.13731-261-alexander.levin@microsoft.com> References: <20180319155742.13731-1-alexander.levin@microsoft.com> In-Reply-To: <20180319155742.13731-1-alexander.levin@microsoft.com> Content-Language: en-US Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Sender: stable-owner@vger.kernel.org List-ID: From: Christophe JAILLET [ Upstream commit d0b1d259a4b58b21a21ea82d7174bf7ea825e9cc ] If one 'drm_gem_handle_create()' fails, we leak somes handles and some memory. In order to fix it: - move the 'free(bo_state)' at the end of the function so that it is also called in the eror handling path. This has the side effect to also try to free it if the first 'kcalloc' fails. This is harmless. - add a new label, err_delete_handle, in order to delete already allocated handles in error handling path - remove the now useless 'err' label The way the code is now written will also delete the handles if the 'copy_to_user()' call fails. Signed-off-by: Christophe JAILLET Reviewed-by: Eric Anholt Link: http://patchwork.freedesktop.org/patch/msgid/20170512123803.1886-1-ch= ristophe.jaillet@wanadoo.fr Signed-off-by: Sasha Levin --- drivers/gpu/drm/vc4/vc4_gem.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/drivers/gpu/drm/vc4/vc4_gem.c b/drivers/gpu/drm/vc4/vc4_gem.c index ab3016982466..b608cd463d4e 100644 --- a/drivers/gpu/drm/vc4/vc4_gem.c +++ b/drivers/gpu/drm/vc4/vc4_gem.c @@ -110,8 +110,8 @@ vc4_get_hang_state_ioctl(struct drm_device *dev, void *= data, &handle); =20 if (ret) { - state->bo_count =3D i - 1; - goto err; + state->bo_count =3D i; + goto err_delete_handle; } bo_state[i].handle =3D handle; bo_state[i].paddr =3D vc4_bo->base.paddr; @@ -123,13 +123,16 @@ vc4_get_hang_state_ioctl(struct drm_device *dev, void= *data, state->bo_count * sizeof(*bo_state))) ret =3D -EFAULT; =20 - kfree(bo_state); +err_delete_handle: + if (ret) { + for (i =3D 0; i < state->bo_count; i++) + drm_gem_handle_delete(file_priv, bo_state[i].handle); + } =20 err_free: - vc4_free_hang_state(dev, kernel_state); + kfree(bo_state); =20 -err: return ret; } =20 --=20 2.14.1