Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp440212iog; Mon, 13 Jun 2022 06:04:01 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxH/XR2e1yEVmFdzoNrs4RGQ1eQ4vIYIU4xR/nFT9o0YcYQiYy7F2GkV0i1Va3nyi5oHI7z X-Received: by 2002:a05:6402:84a:b0:423:fe99:8c53 with SMTP id b10-20020a056402084a00b00423fe998c53mr64213027edz.195.1655125441129; Mon, 13 Jun 2022 06:04:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655125441; cv=none; d=google.com; s=arc-20160816; b=hJ6I468yODPhV3NtHdsX4juRnFvgYZW3tn14M1MT9+CltWcvC18VhECCRECm1a//Pn Ba7BgHNtwsG4jEyaZBcCF8ryA3bGjrtEoRPX+Idxj5La8LEgOhLbkZV74tK6GeMc/xJg B/ymdp9kvrMkIlb2Dv3CNMr7ph9aMjaMqt9TAwBYar8AmpY3yoa7JZC2XNpUzvVRebgZ k9aKiHVyFBXnfLlNaDg0wfwAgktUBEZujgJbqZhny66wAJW+SKqbaD+p+paW3UEiJKtD GCWFRU9luPqCNaEyho587er9rcbk5kbWxS0fSLG04WMyS5OMtDWYKRexnvjg0oMWEEYk JkSA== 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=NXA3dFGARaQP6Th6RpTQgmZtT/cHf6O3Gdjbf4vKa28=; b=v0JMnPwGK563mBGH+zDPevpGFF5hcb0sIRtqsHBvCO/qgomilmdoiKWbLZx2DcP0T3 9q39dsmkyV/QmfBR3TT/HBBRT+HSMuvc4LVl8ULVCgfd7a6XU+Re8BXqZxCxxE6mh8// rOFIh1vjYpax5UI+THivoHkAPVbHmAXmARrtPFvxbp0TAGgNLI6FnE86XHI5E0WePQbB KNzdgWylIXDejhX5cS3u41ahZyUAF5c2ckBw8sj4li/I13Dh1Cq3ZagCxN31wt5zRjxn tEdhQ3eMOTX8ObJ8B0mPlveKPqmnn6YJPPT04HjfXL1vY19N0uVQJPOF7/+P0VrFBGDr vjyQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=JlrcHk1c; 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=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ho34-20020a1709070ea200b006fe8b1baec0si8276501ejc.709.2022.06.13.06.03.34; Mon, 13 Jun 2022 06:04:01 -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=@linuxfoundation.org header.s=korg header.b=JlrcHk1c; 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=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241453AbiFMKPU (ORCPT + 99 others); Mon, 13 Jun 2022 06:15:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57518 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241637AbiFMKOC (ORCPT ); Mon, 13 Jun 2022 06:14:02 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8BA70264; Mon, 13 Jun 2022 03:14:01 -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 ams.source.kernel.org (Postfix) with ESMTPS id 447DBB80E5E; Mon, 13 Jun 2022 10:14:00 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 90E54C34114; Mon, 13 Jun 2022 10:13:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1655115239; bh=XGv+7aoh72FCsShC+ms9MTYM67DoQgj7qCjNXrgPKvg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=JlrcHk1c3Nu4K7MVK0YZgXO7MI+2n8KC+/IQMSsuYwbsbVret4R4ks1QNHztw+ooq aDCxB8vcgUt2pDwXd0bQNepBCwnsLmomlYwcksn7tFfqd3NXfD5YBPJEaOiZog1tPW 2w4CaWspPIrM00a2D5tAwyoX/GjNSRN126V7TKOI= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Keita Suzuki , Alex Deucher , Sasha Levin Subject: [PATCH 4.9 012/167] drm/amd/pm: fix double free in si_parse_power_table() Date: Mon, 13 Jun 2022 12:08:06 +0200 Message-Id: <20220613094843.693662389@linuxfoundation.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220613094840.720778945@linuxfoundation.org> References: <20220613094840.720778945@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-8.3 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