Commit c39bd6b8 authored by Dongliang Mu's avatar Dongliang Mu Committed by jiayingbao
Browse files

platform/x86/intel: tpmi: Fix double free in tpmi_create_device()

mainline inclusion
from mainline-v6.3-rc5
commit 4d5a2a7d
category: feature
bugzilla: https://gitee.com/openeuler/intel-kernel/issues/I8WOEC

Reference: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=4d5a2a7d2c97dbd658533eea5f79dab1ad5dc0ee



-------------------------------------

The previous commit 6a192c0c ("platform/x86/intel/tpmi: Fix
double free reported by Smatch") incorrectly handle the deallocation of
res variable. As shown in the comment, intel_vsec_add_aux handles all
the deallocation of res and feature_vsec_dev. Therefore, kfree(res) can
still cause double free if intel_vsec_add_aux returns error.

Fix this by adjusting the error handling part in tpmi_create_device,
following the function intel_vsec_add_dev.

Intel-SIG: commit 4d5a2a7d platform/x86/intel: tpmi: Fix double free in tpmi_create_device().
Backport Intel_tpmi base driver.

Fixes: 6a192c0c ("platform/x86/intel/tpmi: Fix double free reported by Smatch")
Signed-off-by: default avatarDongliang Mu <dzm91@hust.edu.cn>
Link: https://lore.kernel.org/r/20230309040107.534716-2-dzm91@hust.edu.cn


Reviewed-by: default avatarHans de Goede <hdegoede@redhat.com>
Signed-off-by: default avatarHans de Goede <hdegoede@redhat.com>
[ Yingbao Jia: amend commit log ]
Signed-off-by: default avatarYingbao Jia <yingbao.jia@intel.com>
parent 5aba8945
Loading
Loading
Loading
Loading
+5 −14
Original line number Diff line number Diff line
@@ -203,7 +203,7 @@ static int tpmi_create_device(struct intel_tpmi_info *tpmi_info,
	struct intel_vsec_device *feature_vsec_dev;
	struct resource *res, *tmp;
	const char *name;
	int ret, i;
	int i;

	name = intel_tpmi_name(pfs->pfs_header.tpmi_id);
	if (!name)
@@ -215,8 +215,8 @@ static int tpmi_create_device(struct intel_tpmi_info *tpmi_info,

	feature_vsec_dev = kzalloc(sizeof(*feature_vsec_dev), GFP_KERNEL);
	if (!feature_vsec_dev) {
		ret = -ENOMEM;
		goto free_res;
		kfree(res);
		return -ENOMEM;
	}

	snprintf(feature_id_name, sizeof(feature_id_name), "tpmi-%s", name);
@@ -242,17 +242,8 @@ static int tpmi_create_device(struct intel_tpmi_info *tpmi_info,
	 * feature_vsec_dev memory is also freed as part of device
	 * delete.
	 */
	ret = intel_vsec_add_aux(vsec_dev->pcidev, &vsec_dev->auxdev.dev,
	return intel_vsec_add_aux(vsec_dev->pcidev, &vsec_dev->auxdev.dev,
				  feature_vsec_dev, feature_id_name);
	if (ret)
		goto free_res;

	return 0;

free_res:
	kfree(res);

	return ret;
}

static int tpmi_create_devices(struct intel_tpmi_info *tpmi_info)