Skip to contentSkip to conversation

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

PATCH In review Dongliang Mu 4 participants 18 messages Mar 9, 2023 – Mar 20, 2023 1 message not in the archive

Subsystems (2)
lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw

PATCH v2 1/3 platform/x86/intel/tpmi: Fix double free in tpmi_create_device()

The previous commit 6a192c0cbf38 ("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.

Fixes 6a192c0cbf38 ("platform/x86/intel/tpmi: Fix double free reported by Smatch")
---
 drivers/platform/x86/intel/tpmi.c | 17 ++++-------------
 1 file changed, 4 insertions(+), 13 deletions(-)
drivers/platform/x86/intel/tpmi.c +4 -13
index c999732b0f1e..882fe5e4763f 100644
@@ -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,
-				 feature_vsec_dev, feature_id_name);
-	if (ret)
-		goto free_res;
-
-	return 0;
-
-free_res:
-	kfree(res);
-
-	return ret;
+	return intel_vsec_add_aux(vsec_dev->pcidev, &vsec_dev->auxdev.dev,
+				  feature_vsec_dev, feature_id_name);
 }
 
 static int tpmi_create_devices(struct intel_tpmi_info *tpmi_info)
Signature · 1 line
2.39.2
Reviewed-by on v2 1/3
#2 lore raw
Quoted 6 lines · Dongliang Mu

The previous commit 6a192c0cbf38 ("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.

Quoted 6 lines

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

Fixes 6a192c0cbf38 ("platform/x86/intel/tpmi: Fix double free reported by Smatch")

---

Yeah. These patches are right. The earlier fix still has a double free. Devres stuff is confusing...

regards,
dan carpenter

#3 lore raw
In reply to Dongliang Mu

Hi,

Quoted 11 lines · Dongliang Mu

The previous commit 6a192c0cbf38 ("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.

Fixes 6a192c0cbf38 ("platform/x86/intel/tpmi: Fix double free reported by Smatch")

IIRC then after this v2 was posted I still saw some comments on the original v1 which was not posted on the list. Without the v1 comments being on the list and this archived, I have lost track of what the status of these patches is.

Srinivas, can you let me know if I should merge these, or if more changes are necessary ?

From the off-list discussion of v1 I got the impression more changes are necessary, but I'm not sure.

Regards,

Hans

Quoted 39 lines
---
 drivers/platform/x86/intel/tpmi.c | 17 ++++-------------
 1 file changed, 4 insertions(+), 13 deletions(-)
drivers/platform/x86/intel/tpmi.c +4 -13
index c999732b0f1e..882fe5e4763f 100644
@@ -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,
-				 feature_vsec_dev, feature_id_name);
-	if (ret)
-		goto free_res;
-
-	return 0;
-
-free_res:
-	kfree(res);
-
-	return ret;
+	return intel_vsec_add_aux(vsec_dev->pcidev, &vsec_dev->auxdev.dev,
+				  feature_vsec_dev, feature_id_name);
 }
 
 static int tpmi_create_devices(struct intel_tpmi_info *tpmi_info)
#4 lore raw

Hi Hans,

Quoted 19 lines · Hans de Goede

Hi,

Quoted 16 lines · Dongliang Mu

The previous commit 6a192c0cbf38 ("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.

Fixes 6a192c0cbf38 ("platform/x86/intel/tpmi: Fix double free

reported by Smatch")

Quoted 11 lines

IIRC then after this v2 was posted I still saw some comments on the original v1 which was not posted on the list. Without the v1 comments being on the list and this archived, I have lost track of what the status of these patches is.

Srinivas, can you let me know if I should merge these, or if more changes are necessary ?

From the off-list discussion of v1 I got the impression more changes are necessary, but I'm not sure.

I was looking for changes submitted by the following patch
"
[PATCH linux-next v2 3/3] drivers/platform/x86/intel: fix a memory leak in intel_vsec_add_aux
"

Since I was not copied on this, I was unaware. So I was requesting this change.

Thanks,
Srinivas

Quoted 57 lines

Regards,

Hans

Quoted 48 lines

---
 drivers/platform/x86/intel/tpmi.c | 17 ++++-------------

 1 file changed, 4 insertions(+), 13 deletions(-)
drivers/platform/x86/intel/tpmi.c +0 -0
index c999732b0f1e..882fe5e4763f 100644
@@ -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);

drivers/platform/x86/intel/tpmi.c +1 -11
@@ -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,
-                                feature_vsec_dev,
 feature_id_name);
-       if (ret)
-               goto free_res;
-
-       return 0;
-
-free_res:
-       kfree(res);
-
-       return ret;
+       return intel_vsec_add_aux(vsec_dev->pcidev, &vsec_dev-
Quoted 1 line

auxdev.dev,

+                                 feature_vsec_dev, feature_id_name);
 }

 static int tpmi_create_devices(struct intel_tpmi_info *tpmi_info)

#5 lore raw
Quoted 40 lines · srinivas pandruvada

Hi Hans,

Quoted 19 lines · Hans de Goede

Hi,

Quoted 16 lines · Dongliang Mu

The previous commit 6a192c0cbf38 ("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.

Fixes 6a192c0cbf38 ("platform/x86/intel/tpmi: Fix double free

reported by Smatch")

Quoted 10 lines

IIRC then after this v2 was posted I still saw some comments on the original v1 which was not posted on the list. Without the v1 comments being on the list and this archived, I have lost track of what the status of these patches is.

Srinivas, can you let me know if I should merge these, or if more changes are necessary ?

From the off-list discussion of v1 I got the impression more changes
are necessary, but I'm not sure.

I was looking for changes submitted by the following patch
"
[PATCH linux-next v2 3/3] drivers/platform/x86/intel: fix a memory leak in intel_vsec_add_aux
"

Quoted 5 lines

Since I was not copied on this, I was unaware. So I was requesting this change.

Thanks,
Srinivas

Hi Srinivas and Hans,

How about folding these three patches into one patch and resend a v3 patch?

This will get all people together and avoid the previous embarrassing sitation.

Dongliang Mu

Quoted 56 lines
Quoted 56 lines

Regards,

Hans

Quoted 48 lines
---
  drivers/platform/x86/intel/tpmi.c | 17 ++++-------------
  1 file changed, 4 insertions(+), 13 deletions(-)
drivers/platform/x86/intel/tpmi.c +2 -2
index c999732b0f1e..882fe5e4763f 100644
@@ -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);

drivers/platform/x86/intel/tpmi.c +1 -11
@@ -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,
-                                feature_vsec_dev,
 feature_id_name);
-       if (ret)
-               goto free_res;
-
-       return 0;
-
-free_res:
-       kfree(res);
-
-       return ret;
+       return intel_vsec_add_aux(vsec_dev->pcidev, &vsec_dev-
Quoted 1 line

auxdev.dev,

+                                 feature_vsec_dev, feature_id_name);
 }

 static int tpmi_create_devices(struct intel_tpmi_info *tpmi_info)

#6 lore raw

Hi,

Quoted 45 lines · Dongliang Mu
Quoted 43 lines · srinivas pandruvada

Hi Hans,

Quoted 19 lines · Hans de Goede

Hi,

Quoted 16 lines · Dongliang Mu

The previous commit 6a192c0cbf38 ("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.

Fixes 6a192c0cbf38 ("platform/x86/intel/tpmi: Fix double free

reported by Smatch")

Quoted 10 lines

IIRC then after this v2 was posted I still saw some comments on the original v1 which was not posted on the list. Without the v1 comments being on the list and this archived, I have lost track of what the status of these patches is.

Srinivas, can you let me know if I should merge these, or if more changes are necessary ?

 From the off-list discussion of v1 I got the impression more changes are necessary, but I'm not sure.

I was looking for changes submitted by the following patch
"
[PATCH linux-next v2 3/3] drivers/platform/x86/intel: fix a memory leak in intel_vsec_add_aux
"

Since I was not copied on this, I was unaware. So I was requesting this change.

Quoted 8 lines
Quoted 2 lines

Thanks,
Srinivas

Hi Srinivas and Hans,

How about folding these three patches into one patch and resend a v3 patch?

This will get all people together and avoid the previous embarrassing sitation.

If I understand things correctly then patch 1/3 needs 3/3 to function correctly, right ?

I would not fold them together, smaller patches are easier to review / understand, but maybe change the order and put patch 3/3 first? (so make it 1/3) ?

I can even do that when applying if you agree that is the better order.

Regards,

Hans

Quoted 45 lines
Quoted 45 lines
Quoted 45 lines
Quoted 45 lines

---
  drivers/platform/x86/intel/tpmi.c | 17 ++++-------------

  1 file changed, 4 insertions(+), 13 deletions(-)
drivers/platform/x86/intel/tpmi.c +2 -2
index c999732b0f1e..882fe5e4763f 100644
@@ -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);

drivers/platform/x86/intel/tpmi.c +1 -11
@@ -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,
-                                feature_vsec_dev,
 feature_id_name);
-       if (ret)
-               goto free_res;
-
-       return 0;
-
-free_res:
-       kfree(res);
-
-       return ret;
+       return intel_vsec_add_aux(vsec_dev->pcidev, &vsec_dev-
Quoted 1 line

auxdev.dev,

+                                 feature_vsec_dev, feature_id_name);
  }
    static int tpmi_create_devices(struct intel_tpmi_info *tpmi_info)

#7 lore raw

Hi,

...
...

Quoted 10 lines
Quoted 9 lines

Hi Srinivas and Hans,

How about folding these three patches into one patch and resend a v3 patch?

This will get all people together and avoid the previous embarrassing sitation.

Quoted 6 lines

If I understand things correctly then patch 1/3 needs 3/3 to function correctly, right ?

I would not fold them together, smaller patches are easier to review / understand, but maybe change the order and put patch 3/3 first? (so make it 1/3) ?

That should be correct order. The patch 3/3 should be the first.

Quoted 3 lines

I can even do that when applying if you agree that is the better order.

Agree.

Thanks,
Srinivas

Quoted 58 lines

Regards,

Hans

Quoted 50 lines
Quoted 50 lines
Quoted 50 lines
Quoted 50 lines

---
  drivers/platform/x86/intel/tpmi.c | 17 ++++-------------

  1 file changed, 4 insertions(+), 13 deletions(-)
drivers/platform/x86/intel/tpmi.c +0 -0
index c999732b0f1e..882fe5e4763f 100644
@@ -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);

drivers/platform/x86/intel/tpmi.c +0 -0
@@ -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-

Quoted 1 line

auxdev.dev,

-                                feature_vsec_dev, feature_id_name);
-       if (ret)
-               goto free_res;

drivers/platform/x86/intel/tpmi.c +1 -7
-
-       return 0;
-
-free_res:
-       kfree(res);
-
-       return ret;
+       return intel_vsec_add_aux(vsec_dev->pcidev,

&vsec_dev-

Quoted 1 line

auxdev.dev,

+                                 feature_vsec_dev, feature_id_name);
  }
    static int tpmi_create_devices(struct intel_tpmi_info *tpmi_info)

#8 lore raw
Quoted 12 lines · srinivas pandruvada

Hi,

...
...

Quoted 7 lines
Quoted 7 lines

Hi Srinivas and Hans,

How about folding these three patches into one patch and resend a v3 patch?

This will get all people together and avoid the previous embarrassing sitation.

Quoted 7 lines
Quoted 6 lines

If I understand things correctly then patch 1/3 needs 3/3 to function correctly, right ?

I would not fold them together, smaller patches are easier to review / understand, but maybe change the order and put patch 3/3 first? (so make it 1/3) ?

That should be correct order. The patch 3/3 should be the first.

Oh, yeah. The memory leak fix 3/3 should be the first. This is more reasonable.

BTW, I separated this memory leak fix due to that the kernel mainline is still vulnerable to this memory leak problem.

Quoted 64 lines
Quoted 3 lines

I can even do that when applying if you agree that is the better order.

Agree.

Thanks,
Srinivas

Quoted 56 lines

Regards,

Hans

Quoted 50 lines
Quoted 50 lines
Quoted 50 lines
Quoted 50 lines
---
   drivers/platform/x86/intel/tpmi.c | 17 ++++-------------
   1 file changed, 4 insertions(+), 13 deletions(-)
drivers/platform/x86/intel/tpmi.c +2 -2
index c999732b0f1e..882fe5e4763f 100644
@@ -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);

drivers/platform/x86/intel/tpmi.c +0 -0
@@ -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-

Quoted 1 line

auxdev.dev,

-                                feature_vsec_dev, feature_id_name);
-       if (ret)
-               goto free_res;

drivers/platform/x86/intel/tpmi.c +1 -7
-
-       return 0;
-
-free_res:
-       kfree(res);
-
-       return ret;
+       return intel_vsec_add_aux(vsec_dev->pcidev,

&vsec_dev-

Quoted 1 line

auxdev.dev,

+                                 feature_vsec_dev, feature_id_name);
  }
    static int tpmi_create_devices(struct intel_tpmi_info
*tpmi_info)

#9 lore raw
In reply to Dongliang Mu

Hi Dongliang,

Quoted 1 line

...
...

Quoted 7 lines

Hi Srinivas and Hans,

How about folding these three patches into one patch and resend a v3 patch?

This will get all people together and avoid the previous embarrassing sitation.

This is NOT an embarrassing situation.
Thanks for finding and fixing the issue.

Thanks,
Srinivas

Quoted 62 lines

Dongliang Mu

Quoted 59 lines
Quoted 59 lines

Regards,

Hans

Quoted 51 lines

---
  drivers/platform/x86/intel/tpmi.c | 17 ++++-------------

  1 file changed, 4 insertions(+), 13 deletions(-)
drivers/platform/x86/intel/tpmi.c +0 -0
index c999732b0f1e..882fe5e4763f 100644
@@ -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);

drivers/platform/x86/intel/tpmi.c +0 -0
@@ -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-

Quoted 1 line

auxdev.dev,

-                                feature_vsec_dev, feature_id_name);
-       if (ret)
-               goto free_res;

drivers/platform/x86/intel/tpmi.c +1 -7
-
-       return 0;
-
-free_res:
-       kfree(res);
-
-       return ret;
+       return intel_vsec_add_aux(vsec_dev->pcidev, &vsec_dev-
Quoted 1 line

auxdev.dev,

+                                 feature_vsec_dev, feature_id_name);
  }

  static int tpmi_create_devices(struct intel_tpmi_info *tpmi_info)

#10 lore raw
Quoted 11 lines · srinivas pandruvada

Hi Dongliang,

...
...

Quoted 5 lines

Hi Srinivas and Hans,

How about folding these three patches into one patch and resend a v3 patch?

Quoted 8 lines
Quoted 2 lines

This will get all people together and avoid the previous embarrassing sitation.

This is NOT an embarrassing situation.
Thanks for finding and fixing the issue.

Thanks,
Srinivas

Hi Srinivas,

Any conclusion about this patch set?

Quoted 60 lines
Quoted 60 lines

Dongliang Mu

Quoted 58 lines
Quoted 58 lines

Regards,

Hans

Quoted 51 lines
---
   drivers/platform/x86/intel/tpmi.c | 17 ++++-------------
   1 file changed, 4 insertions(+), 13 deletions(-)
drivers/platform/x86/intel/tpmi.c +2 -2
index c999732b0f1e..882fe5e4763f 100644
@@ -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);

drivers/platform/x86/intel/tpmi.c +0 -0
@@ -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-

Quoted 1 line

auxdev.dev,

-                                feature_vsec_dev, feature_id_name);
-       if (ret)
-               goto free_res;

drivers/platform/x86/intel/tpmi.c +1 -7
-
-       return 0;
-
-free_res:
-       kfree(res);
-
-       return ret;
+       return intel_vsec_add_aux(vsec_dev->pcidev, &vsec_dev-
Quoted 1 line

auxdev.dev,

+                                 feature_vsec_dev, feature_id_name);
  }

  static int tpmi_create_devices(struct intel_tpmi_info
*tpmi_info)

#11 lore raw
Quoted 20 lines · Dongliang Mu
Quoted 18 lines · srinivas pandruvada

Hi Dongliang,

...
...

Quoted 9 lines

Hi Srinivas and Hans,

How about folding these three patches into one patch and resend a v3
patch?

This will get all people together and avoid the previous embarrassing
sitation.

This is NOT an embarrassing situation.
Thanks for finding and fixing the issue.

Quoted 6 lines
Quoted 3 lines

Thanks,
Srinivas

Hi Srinivas,

Any conclusion about this patch set?

Hans can reorder patches as he suggested and apply.

Thanks,
Srinivas

Quoted 66 lines
Quoted 66 lines
Quoted 66 lines

Dongliang Mu

Quoted 63 lines
Quoted 63 lines

Regards,

Hans

Quoted 56 lines

---
   drivers/platform/x86/intel/tpmi.c | 17 ++++-------------

   1 file changed, 4 insertions(+), 13 deletions(-)
drivers/platform/x86/intel/tpmi.c +0 -0
index c999732b0f1e..882fe5e4763f 100644
@@ -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);

drivers/platform/x86/intel/tpmi.c +0 -0
@@ -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-

Quoted 1 line

auxdev.dev,

-                                feature_vsec_dev, feature_id_name);
-       if (ret)
-               goto free_res;

drivers/platform/x86/intel/tpmi.c +1 -7
-
-       return 0;
-
-free_res:
-       kfree(res);
-
-       return ret;
+       return intel_vsec_add_aux(vsec_dev->pcidev,

&vsec_dev-

Quoted 1 line

auxdev.dev,

+                                 feature_vsec_dev, feature_id_name);
   }

   static int tpmi_create_devices(struct intel_tpmi_info *tpmi_info)

#12 lore raw
In reply to Dongliang Mu

Hi,

Quoted 11 lines · Dongliang Mu

The previous commit 6a192c0cbf38 ("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.

Fixes 6a192c0cbf38 ("platform/x86/intel/tpmi: Fix double free reported by Smatch")

Note this patch causes the following compiler warnings:

CC [M] drivers/platform/x86/intel/tpmi.o
drivers/platform/x86/intel/tpmi.c: In function ‘tpmi_create_device’:
drivers/platform/x86/intel/tpmi.c:206:13: warning: unused variable ‘ret’ [-Wunused-variable]
206 | int ret, i;
| ^~~

I have fixed this and squashed the fix into the patch while applying it, so there is no need to send a new version:

Thank you for your patch, I've applied this patch to my review-hans branch:
https://git.kernel.org/pub/scm/linux/kernel/git/pdx86/platform-drivers-x86.git/log/?h=review-hans

Note it will show up in my review-hans branch once I've pushed my local branch there, which might take a while.

Once I've run some tests on this branch the patches there will be added to the platform-drivers-x86/for-next branch and eventually will be included in the pdx86 pull-request to Linus for the next merge-window.

Regards,

Hans

Quoted 39 lines
---
 drivers/platform/x86/intel/tpmi.c | 17 ++++-------------
 1 file changed, 4 insertions(+), 13 deletions(-)
drivers/platform/x86/intel/tpmi.c +4 -13
index c999732b0f1e..882fe5e4763f 100644
@@ -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,
-				 feature_vsec_dev, feature_id_name);
-	if (ret)
-		goto free_res;
-
-	return 0;
-
-free_res:
-	kfree(res);
-
-	return ret;
+	return intel_vsec_add_aux(vsec_dev->pcidev, &vsec_dev->auxdev.dev,
+				  feature_vsec_dev, feature_id_name);
 }
 
 static int tpmi_create_devices(struct intel_tpmi_info *tpmi_info)
#13 lore raw

PATCH v2 2/3 drivers/platform/x86/intel/tpmi: revise the comment of intel_vsec_add_aux

In reply to a message not in the archive

intel_vsec_add_aux() is resource managed including res and feature_vsec_dev memory.

Fix this by revising the comment of intel_vsec_add_aux since res variable will also be freed in the intel_vsec_add_aux.

---
 drivers/platform/x86/intel/tpmi.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/platform/x86/intel/tpmi.c +2 -2
index 882fe5e4763f..036d0e0dba19 100644
@@ -239,8 +239,8 @@ static int tpmi_create_device(struct intel_tpmi_info *tpmi_info,
 	/*
 	 * intel_vsec_add_aux() is resource managed, no explicit
 	 * delete is required on error or on module unload.
-	 * feature_vsec_dev memory is also freed as part of device
-	 * delete.
+	 * feature_vsec_dev and res memory are also freed as part of
+	 * device deletion.
 	 */
 	return intel_vsec_add_aux(vsec_dev->pcidev, &vsec_dev->auxdev.dev,
 				  feature_vsec_dev, feature_id_name);
Signature · 1 line
2.39.2
#14 lore raw

Re: [PATCH linux-next v2 2/3] drivers/platform/x86/intel/tpmi: revise the comment of intel_vsec_add_aux

Hi,

Quoted 7 lines · Dongliang Mu

intel_vsec_add_aux() is resource managed including res and feature_vsec_dev memory.

Fix this by revising the comment of intel_vsec_add_aux since res variable will also be freed in the intel_vsec_add_aux.

Thank you for your patch, I've applied this patch to my review-hans branch:
https://git.kernel.org/pub/scm/linux/kernel/git/pdx86/platform-drivers-x86.git/log/?h=review-hans

Note it will show up in my review-hans branch once I've pushed my local branch there, which might take a while.

Once I've run some tests on this branch the patches there will be added to the platform-drivers-x86/for-next branch and eventually will be included in the pdx86 pull-request to Linus for the next merge-window.

Regards,

Hans

Quoted 19 lines
---
 drivers/platform/x86/intel/tpmi.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/platform/x86/intel/tpmi.c +2 -2
index 882fe5e4763f..036d0e0dba19 100644
@@ -239,8 +239,8 @@ static int tpmi_create_device(struct intel_tpmi_info *tpmi_info,
 	/*
 	 * intel_vsec_add_aux() is resource managed, no explicit
 	 * delete is required on error or on module unload.
-	 * feature_vsec_dev memory is also freed as part of device
-	 * delete.
+	 * feature_vsec_dev and res memory are also freed as part of
+	 * device deletion.
 	 */
 	return intel_vsec_add_aux(vsec_dev->pcidev, &vsec_dev->auxdev.dev,
 				  feature_vsec_dev, feature_id_name);
#15 lore raw

Re: [PATCH linux-next v2 2/3] drivers/platform/x86/intel/tpmi: revise the comment of intel_vsec_add_aux

Quoted 17 lines · Hans de Goede

Hi,

Quoted 7 lines · Dongliang Mu

intel_vsec_add_aux() is resource managed including res and feature_vsec_dev memory.

Fix this by revising the comment of intel_vsec_add_aux since res variable will also be freed in the intel_vsec_add_aux.

Thank you for your patch, I've applied this patch to my review-hans branch:
https://git.kernel.org/pub/scm/linux/kernel/git/pdx86/platform-drivers-x86.git/log/?h=review-hans

Note it will show up in my review-hans branch once I've pushed my local branch there, which might take a while.

Quoted 8 lines

Once I've run some tests on this branch the patches there will be added to the platform-drivers-x86/for-next branch and eventually will be included in the pdx86 pull-request to Linus for the next merge-window.

Regards,

Hans

Thanks, Hans.

Shall I send the patch related to memory leak to the mainline? The mainline is also prone to this issue?

Quoted 22 lines
Quoted 22 lines


---
  drivers/platform/x86/intel/tpmi.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
drivers/platform/x86/intel/tpmi.c +2 -2
index 882fe5e4763f..036d0e0dba19 100644
@@ -239,8 +239,8 @@ static int tpmi_create_device(struct intel_tpmi_info *tpmi_info,
  	/*
  	 * intel_vsec_add_aux() is resource managed, no explicit
  	 * delete is required on error or on module unload.
-	 * feature_vsec_dev memory is also freed as part of device
-	 * delete.
+	 * feature_vsec_dev and res memory are also freed as part of
+	 * device deletion.
  	 */
  	return intel_vsec_add_aux(vsec_dev->pcidev, &vsec_dev->auxdev.dev,
  				  feature_vsec_dev, feature_id_name);
#16 lore raw

Re: [PATCH linux-next v2 2/3] drivers/platform/x86/intel/tpmi: revise the comment of intel_vsec_add_aux

Hi,

Quoted 24 lines · Dongliang Mu
Quoted 22 lines · Hans de Goede

Hi,

Quoted 7 lines · Dongliang Mu

intel_vsec_add_aux() is resource managed including res and feature_vsec_dev memory.

Fix this by revising the comment of intel_vsec_add_aux since res variable will also be freed in the intel_vsec_add_aux.

Thank you for your patch, I've applied this patch to my review-hans branch:
https://git.kernel.org/pub/scm/linux/kernel/git/pdx86/platform-drivers-x86.git/log/?h=review-hans

Note it will show up in my review-hans branch once I've pushed my local branch there, which might take a while.

Once I've run some tests on this branch the patches there will be added to the platform-drivers-x86/for-next branch and eventually will be included in the pdx86 pull-request to Linus for the next merge-window.

Quoted 7 lines
Quoted 3 lines

Regards,

Hans

Thanks, Hans.

Shall I send the patch related to memory leak to the mainline? The mainline is also prone to this issue?

There is no need for this, I have just moved these from my review-hans branch to the fixes
branch: https://git.kernel.org/pub/scm/linux/kernel/git/pdx86/platform-drivers-x86.git/log/?h=fixes

So these will be included in my next fixes pull-req for mainline.

Regards,

Hans

Quoted 20 lines
Quoted 19 lines
Quoted 19 lines

---
  drivers/platform/x86/intel/tpmi.c | 4 ++--

  1 file changed, 2 insertions(+), 2 deletions(-)
drivers/platform/x86/intel/tpmi.c +2 -2
index 882fe5e4763f..036d0e0dba19 100644
@@ -239,8 +239,8 @@ static int tpmi_create_device(struct intel_tpmi_info *tpmi_info,
       /*
        * intel_vsec_add_aux() is resource managed, no explicit
        * delete is required on error or on module unload.
-     * feature_vsec_dev memory is also freed as part of device
-     * delete.
+     * feature_vsec_dev and res memory are also freed as part of
+     * device deletion.
        */
       return intel_vsec_add_aux(vsec_dev->pcidev, &vsec_dev->auxdev.dev,
                     feature_vsec_dev, feature_id_name);
#17 lore raw

PATCH v2 3/3 drivers/platform/x86/intel: fix a memory leak in intel_vsec_add_aux

In reply to a message not in the archive

The first error handling code in intel_vsec_add_aux misses the deallocation of intel_vsec_dev->resource.

Fix this by adding kfree(intel_vsec_dev->resource) in the error handling code.

---
 drivers/platform/x86/intel/vsec.c | 1 +
 1 file changed, 1 insertion(+)
drivers/platform/x86/intel/vsec.c +1 -0
index 13decf36c6de..2311c16cb975 100644
@@ -154,6 +154,7 @@ int intel_vsec_add_aux(struct pci_dev *pdev, struct device *parent,
 	ret = ida_alloc(intel_vsec_dev->ida, GFP_KERNEL);
 	mutex_unlock(&vsec_ida_lock);
 	if (ret < 0) {
+		kfree(intel_vsec_dev->resource);
 		kfree(intel_vsec_dev);
 		return ret;
 	}
Signature · 1 line
2.39.2
#18 lore raw

Re: [PATCH linux-next v2 3/3] drivers/platform/x86/intel: fix a memory leak in intel_vsec_add_aux

Hi,

Quoted 8 lines · Dongliang Mu

The first error handling code in intel_vsec_add_aux misses the deallocation of intel_vsec_dev->resource.

Fix this by adding kfree(intel_vsec_dev->resource) in the error handling code.

Thank you for your patch, I've applied this patch (as first patch in the series) to my review-hans branch:
https://git.kernel.org/pub/scm/linux/kernel/git/pdx86/platform-drivers-x86.git/log/?h=review-hans

Note it will show up in my review-hans branch once I've pushed my local branch there, which might take a while.

Once I've run some tests on this branch the patches there will be added to the platform-drivers-x86/for-next branch and eventually will be included in the pdx86 pull-request to Linus for the next merge-window.

Regards,

Hans

Quoted 16 lines
---
 drivers/platform/x86/intel/vsec.c | 1 +
 1 file changed, 1 insertion(+)
drivers/platform/x86/intel/vsec.c +1 -0
index 13decf36c6de..2311c16cb975 100644
@@ -154,6 +154,7 @@ int intel_vsec_add_aux(struct pci_dev *pdev, struct device *parent,
 	ret = ida_alloc(intel_vsec_dev->ida, GFP_KERNEL);
 	mutex_unlock(&vsec_ida_lock);
 	if (ret < 0) {
+		kfree(intel_vsec_dev->resource);
 		kfree(intel_vsec_dev);
 		return ret;
 	}