Received: by 2002:a5d:9c59:0:0:0:0:0 with SMTP id 25csp2256131iof; Wed, 8 Jun 2022 00:37:02 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw/p9HpTro8q/oqyXwuj6Ql3+xIMfK5B16ArEigmMFdqVcAJ/menoaKRDNOAeGVBjD3PK4z X-Received: by 2002:a65:6943:0:b0:3fd:8a02:59f2 with SMTP id w3-20020a656943000000b003fd8a0259f2mr15528719pgq.375.1654673822818; Wed, 08 Jun 2022 00:37:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1654673822; cv=none; d=google.com; s=arc-20160816; b=hSkAM+yYIBkdK6kcCD+B5RczetmiJ5U4pLTcVNqwas9Jylm4bwgKAf6dj8ohQBCm/f 7OUZwQSTu1H4I5rIk+cmemQslS03dRHOXElmXEH965DWpwT/7j/GT6BxzBN0CEyFfFsb MA6jJEdElrbGAxXC2HsTYAUQikcVVhsmyZyexylEDHCBOTqpHorNkyj1RcDRPgfeyAFA 09NtBVDHLetEweCVA/k+uuS5pKYa88cJivEPfJ5HYyRxefJeVHNonLOom9qjGX6vXtnS ddU7/RE7+eM548tkeEb+mZvizBBYT4x6VI0E9rEw3SMNE8j86MEj6dNM8XQectuGHSUh MZSQ== 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=aOHP8SyJlz5dmIHBo1965Xal6/rz66yooTC5mRwC8xo=; b=u25IASbqcwIBWellKeAN2tIX2Ojc02DlC6YOTLcU7uJZQ/uyhC1mH84GRmno+vu3se yW4C6oS4n2ySDopCGF38HjJnaHzhOq+q4tPd+kaxtJQooyKcY9ag7a6ufbYm69BnGDXZ QJbSTgIJGrNzX3Tw6p9x9/QGiBJWdF66o0gBFiOJi9hja3D5uxq09U519MBRaLL0oQ9O 5JtBVWXaNf/yqf05a9kKLCPYC/dBpsoIdzHJEcvUNvzH0Ulp0Dy/EFOAIk6CWoioPXYH yTy69muZzC6KtwrTTR1hH32PMxP/EDfrsd1FuEJ3Jl3Eu+uI4x9nCfgXiB1fDvVg5lMa ud3w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=OYMn5ibT; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1: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 lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id l8-20020a170903244800b0016774f4875fsi14275854pls.526.2022.06.08.00.37.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Jun 2022 00:37:02 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=OYMn5ibT; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1: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: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 067FE24E01E; Wed, 8 Jun 2022 00:08:05 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1352999AbiFGWJB (ORCPT + 99 others); Tue, 7 Jun 2022 18:09:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33462 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1379703AbiFGVGP (ORCPT ); Tue, 7 Jun 2022 17:06:15 -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 CD3F52109D6; Tue, 7 Jun 2022 11:50:09 -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 ED681B81FE1; Tue, 7 Jun 2022 18:49:57 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 610DEC385A2; Tue, 7 Jun 2022 18:49:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1654627796; bh=Sn2E5T7+eZANOZsUBkk4ShXP8E0TUT3ruFCM8ktCFR0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=OYMn5ibTP6E5H8TS4asnICu2Yj7fngka75HK44yy2mj4sJHVQGayy+uD/cjS9ZZbv GjmUPzzA63XgRnrVkGmZapuNJtaatKkRwEKrh7uhWah1wHxFq/j5VP292D+5r1vLOm KvhEdfPOiUpObbgw3svBL0y/bXSK7I7KKk7xEhY4= 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 5.18 095/879] drm/amd/pm: fix double free in si_parse_power_table() Date: Tue, 7 Jun 2022 18:53:33 +0200 Message-Id: <20220607165005.450407171@linuxfoundation.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220607165002.659942637@linuxfoundation.org> References: <20220607165002.659942637@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=-3.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=unavailable 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/pm/legacy-dpm/si_dpm.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/drivers/gpu/drm/amd/pm/legacy-dpm/si_dpm.c b/drivers/gpu/drm/amd/pm/legacy-dpm/si_dpm.c index 633dab14f51c..49c398ec0aaf 100644 --- a/drivers/gpu/drm/amd/pm/legacy-dpm/si_dpm.c +++ b/drivers/gpu/drm/amd/pm/legacy-dpm/si_dpm.c @@ -7297,17 +7297,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, @@ -7329,8 +7327,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 < adev->pm.dpm.num_of_vce_states; i++) { -- 2.35.1