Received: by 2002:a05:6602:18e:0:0:0:0 with SMTP id m14csp5317397ioo; Wed, 1 Jun 2022 03:02:40 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwKW/jxEEH9anxd7yRG1NXTvZPUtcewZtzmodfTWKlf6fQF+Zp7cFVOs2lHzqw8ukcHjmLx X-Received: by 2002:a05:6402:354c:b0:42b:4e22:203b with SMTP id f12-20020a056402354c00b0042b4e22203bmr54533452edd.308.1654077759948; Wed, 01 Jun 2022 03:02:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1654077759; cv=none; d=google.com; s=arc-20160816; b=YCTZa79ANZoNwbkym1v5lUuUi7deNq79mI1O3zxgmVRRjDwqyZfyd/YlqbeJoZ42Fo 8jMcBXFIPi9gp5+VTNccEr5pvWwN2dwmWZVknFWBdUm78gmtPg6s5KYFwAeuE1Cs6khG DBqzM/LFBPzBN2RZpRBMLiXEmVTPAlN9/iYX5BV0cuaVHmxYn78pp7omh0FqnT3HxSiJ weTCa9GucC60+bbUsi7JkaGTNy9077KaRyaCdnCFQeTRyfq1LyAW0/89h1sHVdx1mlHm RmKXG9MC4VhLCU+6L1nAy/+IsApQ8PAp/9o1aHu5sXATMSfv4jHslwbjpY9+87o6+78f 3fQQ== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=NXA3dFGARaQP6Th6RpTQgmZtT/cHf6O3Gdjbf4vKa28=; b=sHdM1r/4gU3yIrFydvR9lNvdWmmKcjc70BfsjXzK5cShM+QRKTJOlg0btpTSM0bUjn lvkC9GchSV/KKOztVvhrweQbWcuZx24YyrbzWo9hl/nyg1ltr7qnJNEY4EXZIoiTKdqe FozKbam+18Zo0kdEOi6f6yq3jxC4gbnXeaTi8zIqSSm0qkHGBUI8C56HhJeuP4v33BpD uBjhi/E+ejOhi+RKKZ4jUahkXC0fnH7gcn5KWwyNwJBTLz7GYVENkdRJQYn+mnJAiFZN ROZClPKA+d7+LC+F5b8ING/iDGoQwKEdysMSzZJbAeCT1ue/dBsRV8obOXv+JojH59LP vCPA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=dxOuXeJA; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id k26-20020a170906681a00b007065d999015si1029502ejr.232.2022.06.01.03.02.13; Wed, 01 Jun 2022 03:02:39 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=dxOuXeJA; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241926AbiE3Ou4 (ORCPT + 99 others); Mon, 30 May 2022 10:50:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44712 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242417AbiE3ObE (ORCPT ); Mon, 30 May 2022 10:31:04 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A8B15BC6CA; Mon, 30 May 2022 06:52:57 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 58B8E60F74; Mon, 30 May 2022 13:52:34 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 99612C3411C; Mon, 30 May 2022 13:52:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1653918753; bh=XGv+7aoh72FCsShC+ms9MTYM67DoQgj7qCjNXrgPKvg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=dxOuXeJA/m/fCHVhBJBukmaokoWtBGO6jnk4dV0LruS2Qumau7Q50m8FJACLWGilk H44BsLi0qZlEqsYFsrCSI7Y0Vb0Lwy00hdXlQ090JWEIhOJmL5SkR8WODZjkAptG6e qIaM08ICImTXg1uvdrQD5GZU+1lrVPbVvgTyzSzv7sU0RnE8kmXDGP+2FqBMpIv7dZ uf2OY1Ry/XssA3JMRxx0Nyky/fxsPP0ntKwZmYrnzdWccXm5iTTorIqRIF8ZGKhSjc mFosVZwGUkMw7zU7yyWMKL5dOnwbnWf23X5wwulJahc5iG5JB51CIO2yfIoQlOdduT KAVorWtLiKyFQ== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Keita Suzuki , Alex Deucher , Sasha Levin , christian.koenig@amd.com, Xinhui.Pan@amd.com, airlied@linux.ie, daniel@ffwll.ch, amd-gfx@lists.freedesktop.org, dri-devel@lists.freedesktop.org Subject: [PATCH AUTOSEL 4.9 08/24] drm/amd/pm: fix double free in si_parse_power_table() Date: Mon, 30 May 2022 09:51:55 -0400 Message-Id: <20220530135211.1937674-8-sashal@kernel.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220530135211.1937674-1-sashal@kernel.org> References: <20220530135211.1937674-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.7 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Keita Suzuki [ Upstream commit f3fa2becf2fc25b6ac7cf8d8b1a2e4a86b3b72bd ] In function si_parse_power_table(), array adev->pm.dpm.ps and its member is allocated. If the allocation of each member fails, the array itself is freed and returned with an error code. However, the array is later freed again in si_dpm_fini() function which is called when the function returns an error. This leads to potential double free of the array adev->pm.dpm.ps, as well as leak of its array members, since the members are not freed in the allocation function and the array is not nulled when freed. In addition adev->pm.dpm.num_ps, which keeps track of the allocated array member, is not updated until the member allocation is successfully finished, this could also lead to either use after free, or uninitialized variable access in si_dpm_fini(). Fix this by postponing the free of the array until si_dpm_fini() and increment adev->pm.dpm.num_ps everytime the array member is allocated. Signed-off-by: Keita Suzuki Signed-off-by: Alex Deucher Signed-off-by: Sasha Levin --- drivers/gpu/drm/amd/amdgpu/si_dpm.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/drivers/gpu/drm/amd/amdgpu/si_dpm.c b/drivers/gpu/drm/amd/amdgpu/si_dpm.c index 4826befc1bc3..fe6fda42fde8 100644 --- a/drivers/gpu/drm/amd/amdgpu/si_dpm.c +++ b/drivers/gpu/drm/amd/amdgpu/si_dpm.c @@ -7313,17 +7313,15 @@ static int si_parse_power_table(struct amdgpu_device *adev) if (!adev->pm.dpm.ps) return -ENOMEM; power_state_offset = (u8 *)state_array->states; - for (i = 0; i < state_array->ucNumEntries; i++) { + for (adev->pm.dpm.num_ps = 0, i = 0; i < state_array->ucNumEntries; i++) { u8 *idx; power_state = (union pplib_power_state *)power_state_offset; non_clock_array_index = power_state->v2.nonClockInfoIndex; non_clock_info = (struct _ATOM_PPLIB_NONCLOCK_INFO *) &non_clock_info_array->nonClockInfo[non_clock_array_index]; ps = kzalloc(sizeof(struct si_ps), GFP_KERNEL); - if (ps == NULL) { - kfree(adev->pm.dpm.ps); + if (ps == NULL) return -ENOMEM; - } adev->pm.dpm.ps[i].ps_priv = ps; si_parse_pplib_non_clock_info(adev, &adev->pm.dpm.ps[i], non_clock_info, @@ -7345,8 +7343,8 @@ static int si_parse_power_table(struct amdgpu_device *adev) k++; } power_state_offset += 2 + power_state->v2.ucNumDPMLevels; + adev->pm.dpm.num_ps++; } - adev->pm.dpm.num_ps = state_array->ucNumEntries; /* fill in the vce power states */ for (i = 0; i < AMDGPU_MAX_VCE_LEVELS; i++) { -- 2.35.1