Skip to contentSkip to conversation

platform/x86/intel/tpmi: Fix memory leak in mem_write() error path

PATCH In review ZhaoJinming 3 participants 14 messages May 19 – May 21

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 platform/x86/intel/tpmi: Fix memory leak in mem_write() error path

In mem_write(), when the IS_ALIGNED() check fails, the function returns -EINVAL directly without freeing the 'array' allocated by parse_int_array_user(). This causes a memory leak.

Other error paths in the same function correctly use 'goto exit_write' to free the array before returning. Fix this inconsistency by using the same pattern for the alignment check.

Fixes 8e0a2fc68ec3 ("platform/x86/intel/tpmi: Use 32 bit aligned address for debugfs mem write")
---
 drivers/platform/x86/intel/vsec_tpmi.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/platform/x86/intel/vsec_tpmi.c +4 -2
index 16fd7aa41f20..2a428bfcb209 100644
@@ -495,8 +495,10 @@ static ssize_t mem_write(struct file *file, const char __user *userbuf, size_t l
 	addr = array[2];
 	value = array[3];
 
-	if (!IS_ALIGNED(addr, sizeof(u32)))
-		return -EINVAL;
+	if (!IS_ALIGNED(addr, sizeof(u32))) {
+		ret = -EINVAL;
+		goto exit_write;
+	}
 
 	if (punit >= pfs->pfs_header.num_entries) {
 		ret = -EINVAL;
Signature · 1 line
2.20.1
maintainer
#1 lore raw
Quoted 22 lines · ZhaoJinming

In mem_write(), when the IS_ALIGNED() check fails, the function returns -EINVAL directly without freeing the 'array' allocated by parse_int_array_user(). This causes a memory leak.

Other error paths in the same function correctly use 'goto exit_write' to free the array before returning. Fix this inconsistency by using the same pattern for the alignment check.

Fixes 8e0a2fc68ec3 ("platform/x86/intel/tpmi: Use 32 bit aligned address for debugfs mem write")
---
 drivers/platform/x86/intel/vsec_tpmi.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/platform/x86/intel/vsec_tpmi.c +0 -0
index 16fd7aa41f20..2a428bfcb209 100644
@@ -495,8 +495,10 @@ static ssize_t mem_write(struct file *file, const char __user *userbuf, size_t l
 	addr = array[2];
 	value = array[3];

drivers/platform/x86/intel/vsec_tpmi.c static ssize_t mem_write(struct file *file, const char __user *userbuf, size_t l · line 504

Quoted 10 lines
drivers/platform/x86/intel/vsec_tpmi.c +4 -2
 
-	if (!IS_ALIGNED(addr, sizeof(u32)))
-		return -EINVAL;
+	if (!IS_ALIGNED(addr, sizeof(u32))) {
+		ret = -EINVAL;
+		goto exit_write;
+	}
 
 	if (punit >= pfs->pfs_header.num_entries) {
 		ret = -EINVAL;

Hi,

Thanks for finding this problem. This function looks a prime candidate for cleanup.h conversion.

Please do this with a 2 patch series. The first patch converts kfree() to __free() and moves array declaration next to parse_int_array_user() (as instructed by the long comment in cleanup.h) and has fixes tag. The second patch should converyt the mutex lock/unlock to guard and doesn't need fixes tag. This way, we get rid of all gotos here.

Signature · 1 line
 i.
#2 lore raw

Hi,

Thank you for your kindness. I hope I got your point.

#3 lore raw

PATCH 1/2 platform/x86/intel/tpmi: use cleanup helpers in mem_write()

In mem_write(), the temporary array returned by parse_int_array_user() must be released on all error paths. Convert the array variable to use cleanup.h scope-based cleanup so it is freed automatically on return.

This also moves the array declaration next to parse_int_array_user() as required by cleanup.h usage guidelines.

Fixes 8e0a2fc68ec3 ("platform/x86/intel/tpmi: Use 32 bit aligned address for debugfs mem write")
---
 drivers/platform/x86/intel/vsec_tpmi.c | 26 +++++++++-----------------
 1 file changed, 9 insertions(+), 17 deletions(-)
drivers/platform/x86/intel/vsec_tpmi.c +9 -17
index 16fd7aa41f20..e7bc3474c7aa 100644
@@ -51,6 +51,7 @@
 #include <linux/bitfield.h>
 #include <linux/debugfs.h>
 #include <linux/delay.h>
+#include <linux/cleanup.h>
 #include <linux/intel_tpmi.h>
 #include <linux/intel_vsec.h>
 #include <linux/io.h>
@@ -473,7 +474,7 @@ static ssize_t mem_write(struct file *file, const char __user *userbuf, size_t l
 	struct seq_file *m = file->private_data;
 	struct intel_tpmi_pm_feature *pfs = m->private;
 	u32 addr, value, punit, size;
-	u32 num_elems, *array;
+	u32 num_elems;
 	void __iomem *mem;
 	int ret;
 
@@ -481,15 +482,14 @@ static ssize_t mem_write(struct file *file, const char __user *userbuf, size_t l
 	if (!size)
 		return -EIO;
 
+	u32 *array __free(kfree) = NULL;
 	ret = parse_int_array_user(userbuf, len, (int **)&array);
 	if (ret < 0)
 		return ret;
 
 	num_elems = *array;
-	if (num_elems != 3) {
-		ret = -EINVAL;
-		goto exit_write;
-	}
+	if (num_elems != 3)
+		return -EINVAL;
 
 	punit = array[1];
 	addr = array[2];
@@ -498,15 +498,11 @@ static ssize_t mem_write(struct file *file, const char __user *userbuf, size_t l
 	if (!IS_ALIGNED(addr, sizeof(u32)))
 		return -EINVAL;
 
-	if (punit >= pfs->pfs_header.num_entries) {
-		ret = -EINVAL;
-		goto exit_write;
-	}
+	if (punit >= pfs->pfs_header.num_entries)
+		return -EINVAL;
 
-	if (addr >= size) {
-		ret = -EINVAL;
-		goto exit_write;
-	}
+	if (addr >= size)
+		return -EINVAL;
 
 	mutex_lock(&tpmi_dev_lock);
 
@@ -522,12 +518,8 @@ static ssize_t mem_write(struct file *file, const char __user *userbuf, size_t l
 
 	ret = len;
 
-unlock_mem_write:
 	mutex_unlock(&tpmi_dev_lock);
 
-exit_write:
-	kfree(array);
-
 	return ret;
 }
 
Signature · 1 line
2.20.1
maintainer
#4 lore raw

Re: [PATCH 1/2] platform/x86/intel/tpmi: use cleanup helpers in mem_write()

Quoted 1 line · ZhaoJinming

In mem_write(), the temporary array returned by parse_int_array_user() must be released on all error paths. Convert the array variable to use cleanup.h scope-based cleanup so it is freed automatically on return.

Not only on "error paths" but also when no error occurs.

Quoted 1 line

This also moves the array declaration next to parse_int_array_user() as required by cleanup.h usage guidelines.

Please fold any text paragraphs so they don't exceed 72 characters per row.

Quoted 68 lines
Fixes 8e0a2fc68ec3 ("platform/x86/intel/tpmi: Use 32 bit aligned address for debugfs mem write")
---
 drivers/platform/x86/intel/vsec_tpmi.c | 26 +++++++++-----------------
 1 file changed, 9 insertions(+), 17 deletions(-)
drivers/platform/x86/intel/vsec_tpmi.c +9 -13
index 16fd7aa41f20..e7bc3474c7aa 100644
@@ -51,6 +51,7 @@
 #include <linux/bitfield.h>
 #include <linux/debugfs.h>
 #include <linux/delay.h>
+#include <linux/cleanup.h>
 #include <linux/intel_tpmi.h>
 #include <linux/intel_vsec.h>
 #include <linux/io.h>
@@ -473,7 +474,7 @@ static ssize_t mem_write(struct file *file, const char __user *userbuf, size_t l
 	struct seq_file *m = file->private_data;
 	struct intel_tpmi_pm_feature *pfs = m->private;
 	u32 addr, value, punit, size;
-	u32 num_elems, *array;
+	u32 num_elems;
 	void __iomem *mem;
 	int ret;
 
@@ -481,15 +482,14 @@ static ssize_t mem_write(struct file *file, const char __user *userbuf, size_t l
 	if (!size)
 		return -EIO;
 
+	u32 *array __free(kfree) = NULL;
 	ret = parse_int_array_user(userbuf, len, (int **)&array);
 	if (ret < 0)
 		return ret;
 
 	num_elems = *array;
-	if (num_elems != 3) {
-		ret = -EINVAL;
-		goto exit_write;
-	}
+	if (num_elems != 3)
+		return -EINVAL;
 
 	punit = array[1];
 	addr = array[2];
@@ -498,15 +498,11 @@ static ssize_t mem_write(struct file *file, const char __user *userbuf, size_t l
 	if (!IS_ALIGNED(addr, sizeof(u32)))
 		return -EINVAL;
 
-	if (punit >= pfs->pfs_header.num_entries) {
-		ret = -EINVAL;
-		goto exit_write;
-	}
+	if (punit >= pfs->pfs_header.num_entries)
+		return -EINVAL;
 
-	if (addr >= size) {
-		ret = -EINVAL;
-		goto exit_write;
-	}
+	if (addr >= size)
+		return -EINVAL;
 
 	mutex_lock(&tpmi_dev_lock);
 

drivers/platform/x86/intel/vsec_tpmi.c static ssize_t mem_write(struct file *file, const char __user *userbuf, size_t l · line 525

Quoted 4 lines
drivers/platform/x86/intel/vsec_tpmi.c +0 -1
@@ -522,12 +518,8 @@ static ssize_t mem_write(struct file *file, const char __user *userbuf, size_t l
 
 	ret = len;
 
-unlock_mem_write:

The last goto is only removed in the second patch so this will cause a build failure mid-series which would be a problem when using git bisect.

Other than that, this change looked okay.

Quoted 9 lines
drivers/platform/x86/intel/vsec_tpmi.c +0 -3
 	mutex_unlock(&tpmi_dev_lock);
 
-exit_write:
-	kfree(array);
-
 	return ret;
 }
 
Signature · 1 line
 i.
#5 lore raw

Updated two patches, moved remove-goto-tag to the second patch. Enabled CONFIG_INTEL_TPMI, build -> patch 001 -> build -> patch 002 -> build. Build kernel successfully.

#6 lore raw

PATCH 1/2 platform/x86/intel/tpmi: use cleanup helpers in mem_write()

In mem_write(), the temporary array returned by parse_int_array_user() must be released on all exit paths. Convert the array variable to use cleanup.h scope-based cleanup so it is freed automatically on return.

This also moves the array declaration next to parse_int_array_user() as required by cleanup.h usage guidelines.

Fixes 8e0a2fc68ec3 ("platform/x86/intel/tpmi: Use 32 bit aligned address for debugfs mem write")
---
 drivers/platform/x86/intel/vsec_tpmi.c | 25 +++++++++----------------
 1 file changed, 9 insertions(+), 16 deletions(-)
drivers/platform/x86/intel/vsec_tpmi.c +9 -16
index 16fd7aa41f20..88f14d0ad410 100644
@@ -50,6 +50,7 @@
 #include <linux/auxiliary_bus.h>
 #include <linux/bitfield.h>
 #include <linux/debugfs.h>
+#include <linux/cleanup.h>
 #include <linux/delay.h>
 #include <linux/intel_tpmi.h>
 #include <linux/intel_vsec.h>
@@ -473,7 +474,7 @@ static ssize_t mem_write(struct file *file, const char __user *userbuf, size_t l
 	struct seq_file *m = file->private_data;
 	struct intel_tpmi_pm_feature *pfs = m->private;
 	u32 addr, value, punit, size;
-	u32 num_elems, *array;
+	u32 num_elems;
 	void __iomem *mem;
 	int ret;
 
@@ -481,15 +482,14 @@ static ssize_t mem_write(struct file *file, const char __user *userbuf, size_t l
 	if (!size)
 		return -EIO;
 
+	u32 *array __free(kfree) = NULL;
 	ret = parse_int_array_user(userbuf, len, (int **)&array);
 	if (ret < 0)
 		return ret;
 
 	num_elems = *array;
-	if (num_elems != 3) {
-		ret = -EINVAL;
-		goto exit_write;
-	}
+	if (num_elems != 3)
+		return -EINVAL;
 
 	punit = array[1];
 	addr = array[2];
@@ -498,15 +498,11 @@ static ssize_t mem_write(struct file *file, const char __user *userbuf, size_t l
 	if (!IS_ALIGNED(addr, sizeof(u32)))
 		return -EINVAL;
 
-	if (punit >= pfs->pfs_header.num_entries) {
-		ret = -EINVAL;
-		goto exit_write;
-	}
+	if (punit >= pfs->pfs_header.num_entries)
+		return -EINVAL;
 
-	if (addr >= size) {
-		ret = -EINVAL;
-		goto exit_write;
-	}
+	if (addr >= size)
+		return -EINVAL;
 
 	mutex_lock(&tpmi_dev_lock);
 
@@ -525,9 +521,6 @@ static ssize_t mem_write(struct file *file, const char __user *userbuf, size_t l
 unlock_mem_write:
 	mutex_unlock(&tpmi_dev_lock);
 
-exit_write:
-	kfree(array);
-
 	return ret;
 }
 
Signature · 1 line
2.20.1
maintainer
#7 lore raw

Re: [PATCH 1/2] platform/x86/intel/tpmi: use cleanup helpers in mem_write()

Quoted 8 lines · ZhaoJinming

In mem_write(), the temporary array returned by parse_int_array_user() must be released on all exit paths. Convert the array variable to use cleanup.h scope-based cleanup so it is freed automatically on return.

This also moves the array declaration next to parse_int_array_user() as required by cleanup.h usage guidelines.

Now you made these much shorter than 72 chars. :-(

Quoted 69 lines
Fixes 8e0a2fc68ec3 ("platform/x86/intel/tpmi: Use 32 bit aligned address for debugfs mem write")
---
 drivers/platform/x86/intel/vsec_tpmi.c | 25 +++++++++----------------
 1 file changed, 9 insertions(+), 16 deletions(-)
drivers/platform/x86/intel/vsec_tpmi.c +9 -13
index 16fd7aa41f20..88f14d0ad410 100644
@@ -50,6 +50,7 @@
 #include <linux/auxiliary_bus.h>
 #include <linux/bitfield.h>
 #include <linux/debugfs.h>
+#include <linux/cleanup.h>
 #include <linux/delay.h>
 #include <linux/intel_tpmi.h>
 #include <linux/intel_vsec.h>
@@ -473,7 +474,7 @@ static ssize_t mem_write(struct file *file, const char __user *userbuf, size_t l
 	struct seq_file *m = file->private_data;
 	struct intel_tpmi_pm_feature *pfs = m->private;
 	u32 addr, value, punit, size;
-	u32 num_elems, *array;
+	u32 num_elems;
 	void __iomem *mem;
 	int ret;
 
@@ -481,15 +482,14 @@ static ssize_t mem_write(struct file *file, const char __user *userbuf, size_t l
 	if (!size)
 		return -EIO;
 
+	u32 *array __free(kfree) = NULL;
 	ret = parse_int_array_user(userbuf, len, (int **)&array);
 	if (ret < 0)
 		return ret;
 
 	num_elems = *array;
-	if (num_elems != 3) {
-		ret = -EINVAL;
-		goto exit_write;
-	}
+	if (num_elems != 3)
+		return -EINVAL;
 
 	punit = array[1];
 	addr = array[2];
@@ -498,15 +498,11 @@ static ssize_t mem_write(struct file *file, const char __user *userbuf, size_t l
 	if (!IS_ALIGNED(addr, sizeof(u32)))
 		return -EINVAL;
 
-	if (punit >= pfs->pfs_header.num_entries) {
-		ret = -EINVAL;
-		goto exit_write;
-	}
+	if (punit >= pfs->pfs_header.num_entries)
+		return -EINVAL;
 
-	if (addr >= size) {
-		ret = -EINVAL;
-		goto exit_write;
-	}
+	if (addr >= size)
+		return -EINVAL;
 
 	mutex_lock(&tpmi_dev_lock);
 

drivers/platform/x86/intel/vsec_tpmi.c static ssize_t mem_write(struct file *file, const char __user *userbuf, size_t l · line 526

Quoted 9 lines
drivers/platform/x86/intel/vsec_tpmi.c +0 -3
@@ -525,9 +521,6 @@ static ssize_t mem_write(struct file *file, const char __user *userbuf, size_t l
 unlock_mem_write:
 	mutex_unlock(&tpmi_dev_lock);
 
-exit_write:
-	kfree(array);
-
 	return ret;
 }
 

The code change looks okay now.

BUT, please send the next version properly versioned (it "v4" or so in it's subject, I think) and in a fresh thread. As is, b4 gets confused which patches are the latest version so I cannot apply these with my maintainer tools.

Signature · 1 line
 i.
author
#8 lore raw

Re: Re: [PATCH 1/2] platform/x86/intel/tpmi: use cleanup helpers in mem_write()

Quoted 388 lines · ZhaoJinming
Quoted 31 lines

In mem_write(), the temporary array returned by

parse_int_array_user() must be released on all exit paths.

Convert the array variable to use cleanup.h scope-based

cleanup so it is freed automatically on return.

This also moves the array declaration next to

Quoted 5 lines

parse_int_array_user() as required by cleanup.h usage

guidelines.

Now you made these much shorter than 72 chars. :-(

Quoted 300 lines
Fixes 8e0a2fc68ec3 ("platform/x86/intel/tpmi: Use 32 bit aligned address for debugfs mem write")

---

? drivers/platform/x86/intel/vsec_tpmi.c | 25 +++++++++----------------

? 1 file changed, 9 insertions(+), 16 deletions(-)

drivers/platform/x86/intel/vsec_tpmi.c +0 -0

index 16fd7aa41f20..88f14d0ad410 100644

--- a/drivers/platform/x86/intel/vsec_tpmi.c

+++ b/drivers/platform/x86/intel/vsec_tpmi.c

drivers/platform/x86/intel/vsec_tpmi.c +0 -0
@@ -50,6 +50,7 @@
 
 
 

? #include <linux/auxiliary_bus.h>

? #include <linux/bitfield.h>

? #include <linux/debugfs.h>

+#include <linux/cleanup.h>

? #include <linux/delay.h>

? #include <linux/intel_tpmi.h>

? #include <linux/intel_vsec.h>

drivers/platform/x86/intel/vsec_tpmi.c +0 -0
@@ -473,7 +474,7 @@ static ssize_t mem_write(struct file *file, const char __user *userbuf, size_t l
 
 
 
 ? 	struct seq_file *m = file->private_data;
 
 
 

? struct intel_tpmi_pm_feature *pfs = m->private;

? u32 addr, value, punit, size;

drivers/platform/x86/intel/vsec_tpmi.c +1 -1
-	u32 num_elems, *array;
 
 
 
+	u32 num_elems;

? void __iomem *mem;

? int ret;

?

drivers/platform/x86/intel/vsec_tpmi.c +0 -0
@@ -481,15 +482,14 @@ static ssize_t mem_write(struct file *file, const char __user *userbuf, size_t l
 
 
 
 ? 	if (!size)
 
 
 
 ? 		return -EIO;
 
 
 

?

drivers/platform/x86/intel/vsec_tpmi.c +1 -0
+	u32 *array __free(kfree) = NULL;

? ret = parse_int_array_user(userbuf, len, (int **)&array);

? if (ret < 0)

? return ret;

?

? num_elems = *array;

drivers/platform/x86/intel/vsec_tpmi.c +2 -4
-	if (num_elems != 3) {
 
 
 
-		ret = -EINVAL;
 
 
 
-		goto exit_write;
 
 
 
-	}
 
 
 
+	if (num_elems != 3)
 
 
 
+		return -EINVAL;

?

? punit = array[1];

? addr = array[2];

drivers/platform/x86/intel/vsec_tpmi.c +0 -0
@@ -498,15 +498,11 @@ static ssize_t mem_write(struct file *file, const char __user *userbuf, size_t l
 
 
 
 ? 	if (!IS_ALIGNED(addr, sizeof(u32)))
 
 
 
 ? 		return -EINVAL;
 
 
 

?

drivers/platform/x86/intel/vsec_tpmi.c +2 -4
-	if (punit >= pfs->pfs_header.num_entries) {
 
 
 
-		ret = -EINVAL;
 
 
 
-		goto exit_write;
 
 
 
-	}
 
 
 
+	if (punit >= pfs->pfs_header.num_entries)
 
 
 
+		return -EINVAL;

?

drivers/platform/x86/intel/vsec_tpmi.c +2 -4
-	if (addr >= size) {
 
 
 
-		ret = -EINVAL;
 
 
 
-		goto exit_write;
 
 
 
-	}
 
 
 
+	if (addr >= size)
 
 
 
+		return -EINVAL;

?

? mutex_lock(&tpmi_dev_lock);

?

drivers/platform/x86/intel/vsec_tpmi.c +0 -0
@@ -525,9 +521,6 @@ static ssize_t mem_write(struct file *file, const char __user *userbuf, size_t l
 
 
 

? unlock_mem_write:

? mutex_unlock(&tpmi_dev_lock);

?

drivers/platform/x86/intel/vsec_tpmi.c +0 -3
-exit_write:
 
 
 
-	kfree(array);
 
 
 
-

? return ret;

Quoted 9 lines

? }

?

The code change looks okay now.

BUT, please send the next version properly versioned (it "v4" or so in

it's subject, I think) and in a fresh thread. As is, b4 gets confused

Quoted 9 lines

which patches are the latest version so I cannot apply these with my

maintainer tools.

Thanks for your reply.
I've sent the v4 patches in a new thread.

Quoted 13 lines
Signature · 1 line
i.
#9 lore raw

PATCH 2/2 platform/x86/intel/tpmi: convert mutex in mem_write() to guard

In reply to ZhaoJinming

Convert the explicit mutex_lock/mutex_unlock pair in mem_write() into a cleanup.h guard(mutex)() scope-based lock acquisition. This removes the remaining goto-based cleanup path and keeps the lock held until the end of the mem_write() scope.

---
 drivers/platform/x86/intel/vsec_tpmi.c | 15 ++++-----------
 1 file changed, 4 insertions(+), 11 deletions(-)
drivers/platform/x86/intel/vsec_tpmi.c +4 -11
index 88f14d0ad410..9bfd05eaa002 100644
@@ -504,24 +504,17 @@ static ssize_t mem_write(struct file *file, const char __user *userbuf, size_t l
 	if (addr >= size)
 		return -EINVAL;
 
-	mutex_lock(&tpmi_dev_lock);
+	guard(mutex)(&tpmi_dev_lock);
 
 	mem = ioremap(pfs->vsec_offset + punit * size, size);
-	if (!mem) {
-		ret = -ENOMEM;
-		goto unlock_mem_write;
-	}
+	if (!mem)
+		return -ENOMEM;
 
 	writel(value, mem + addr);
 
 	iounmap(mem);
 
-	ret = len;
-
-unlock_mem_write:
-	mutex_unlock(&tpmi_dev_lock);
-
-	return ret;
+	return len;
 }
 
 static int mem_write_show(struct seq_file *s, void *unused)
Signature · 1 line
2.20.1
#10 lore raw

PATCH 2/2 platform/x86/intel/tpmi: convert mutex lock/unlock in mem_write() to guard

In reply to ZhaoJinming

Convert the explicit mutex_lock/mutex_unlock pair in mem_write() into a cleanup.h guard(mutex)() scope-based lock acquisition. This removes the remaining goto-based cleanup path and keeps the lock held until the end of the mem_write() scope.

---
 drivers/platform/x86/intel/vsec_tpmi.c | 14 ++++----------
 1 file changed, 4 insertions(+), 10 deletions(-)
drivers/platform/x86/intel/vsec_tpmi.c +4 -10
index e7bc3474c7aa..72b78b505e03 100644
@@ -504,23 +504,17 @@ static ssize_t mem_write(struct file *file, const char __user *userbuf, size_t l
 	if (addr >= size)
 		return -EINVAL;
 
-	mutex_lock(&tpmi_dev_lock);
+	guard(mutex)(&tpmi_dev_lock);
 
 	mem = ioremap(pfs->vsec_offset + punit * size, size);
-	if (!mem) {
-		ret = -ENOMEM;
-		goto unlock_mem_write;
-	}
+	if (!mem)
+		return -ENOMEM;
 
 	writel(value, mem + addr);
 
 	iounmap(mem);
 
-	ret = len;
-
-	mutex_unlock(&tpmi_dev_lock);
-
-	return ret;
+	return len;
 }
 
 static int mem_write_show(struct seq_file *s, void *unused)
Signature · 1 line
2.20.1
#11 lore raw

PATCH 1/2 platform/x86/intel/tpmi: use cleanup helpers in mem_write()

In reply to Ilpo Järvinen

In mem_write(), the temporary array returned by parse_int_array_user() must be released on all error paths. Convert the array variable to use cleanup.h scope-based cleanup so it is freed automatically on return.

This also moves the array declaration next to parse_int_array_user() as required by cleanup.h usage guidelines.

Fixes 8e0a2fc68ec3 ("platform/x86/intel/tpmi: Use 32 bit aligned address for debugfs mem write")
---
 drivers/platform/x86/intel/vsec_tpmi.c | 26 +++++++++-----------------
 1 file changed, 9 insertions(+), 17 deletions(-)
drivers/platform/x86/intel/vsec_tpmi.c +9 -17
index 16fd7aa41f20..e7bc3474c7aa 100644
@@ -51,6 +51,7 @@
 #include <linux/bitfield.h>
 #include <linux/debugfs.h>
 #include <linux/delay.h>
+#include <linux/cleanup.h>
 #include <linux/intel_tpmi.h>
 #include <linux/intel_vsec.h>
 #include <linux/io.h>
@@ -473,7 +474,7 @@ static ssize_t mem_write(struct file *file, const char __user *userbuf, size_t l
 	struct seq_file *m = file->private_data;
 	struct intel_tpmi_pm_feature *pfs = m->private;
 	u32 addr, value, punit, size;
-	u32 num_elems, *array;
+	u32 num_elems;
 	void __iomem *mem;
 	int ret;
 
@@ -481,15 +482,14 @@ static ssize_t mem_write(struct file *file, const char __user *userbuf, size_t l
 	if (!size)
 		return -EIO;
 
+	u32 *array __free(kfree) = NULL;
 	ret = parse_int_array_user(userbuf, len, (int **)&array);
 	if (ret < 0)
 		return ret;
 
 	num_elems = *array;
-	if (num_elems != 3) {
-		ret = -EINVAL;
-		goto exit_write;
-	}
+	if (num_elems != 3)
+		return -EINVAL;
 
 	punit = array[1];
 	addr = array[2];
@@ -498,15 +498,11 @@ static ssize_t mem_write(struct file *file, const char __user *userbuf, size_t l
 	if (!IS_ALIGNED(addr, sizeof(u32)))
 		return -EINVAL;
 
-	if (punit >= pfs->pfs_header.num_entries) {
-		ret = -EINVAL;
-		goto exit_write;
-	}
+	if (punit >= pfs->pfs_header.num_entries)
+		return -EINVAL;
 
-	if (addr >= size) {
-		ret = -EINVAL;
-		goto exit_write;
-	}
+	if (addr >= size)
+		return -EINVAL;
 
 	mutex_lock(&tpmi_dev_lock);
 
@@ -522,12 +518,8 @@ static ssize_t mem_write(struct file *file, const char __user *userbuf, size_t l
 
 	ret = len;
 
-unlock_mem_write:
 	mutex_unlock(&tpmi_dev_lock);
 
-exit_write:
-	kfree(array);
-
 	return ret;
 }
 
Signature · 1 line
2.20.1
#12 lore raw

PATCH 2/2 platform/x86/intel/tpmi: convert mutex lock/unlock in mem_write() to guard

Convert the explicit mutex_lock/mutex_unlock pair in mem_write() into a cleanup.h guard(mutex)() scope-based lock acquisition. This removes the remaining goto-based cleanup path and keeps the lock held until the end of the mem_write() scope.

---
 drivers/platform/x86/intel/vsec_tpmi.c | 14 ++++----------
 1 file changed, 4 insertions(+), 10 deletions(-)
drivers/platform/x86/intel/vsec_tpmi.c +4 -10
index e7bc3474c7aa..72b78b505e03 100644
@@ -504,23 +504,17 @@ static ssize_t mem_write(struct file *file, const char __user *userbuf, size_t l
 	if (addr >= size)
 		return -EINVAL;
 
-	mutex_lock(&tpmi_dev_lock);
+	guard(mutex)(&tpmi_dev_lock);
 
 	mem = ioremap(pfs->vsec_offset + punit * size, size);
-	if (!mem) {
-		ret = -ENOMEM;
-		goto unlock_mem_write;
-	}
+	if (!mem)
+		return -ENOMEM;
 
 	writel(value, mem + addr);
 
 	iounmap(mem);
 
-	ret = len;
-
-	mutex_unlock(&tpmi_dev_lock);
-
-	return ret;
+	return len;
 }
 
 static int mem_write_show(struct seq_file *s, void *unused)
Signature · 1 line
2.20.1
#13 lore raw
In reply to ZhaoJinming
Quoted 10 lines · ZhaoJinming

In mem_write(), when the IS_ALIGNED() check fails, the function returns
-EINVAL directly without freeing the 'array' allocated by parse_int_array_user(). This causes a memory leak.

Other error paths in the same function correctly use 'goto exit_write'
to free the array before returning. Fix this inconsistency by using the same pattern for the alignment check.

Thanks. I see Ilpo suggested using cleanup.h. Let me know if you have issue in doing that.

-Srinivas

Quoted 27 lines
Fixes 8e0a2fc68ec3 ("platform/x86/intel/tpmi: Use 32 bit aligned

address for debugfs mem write")

---
 drivers/platform/x86/intel/vsec_tpmi.c | 6 ++++--

 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/platform/x86/intel/vsec_tpmi.c +0 -0
index 16fd7aa41f20..2a428bfcb209 100644
@@ -495,8 +495,10 @@ static ssize_t mem_write(struct file *file,

const char __user *userbuf, size_t l
  addr = array[2];
  value = array[3];

drivers/platform/x86/intel/vsec_tpmi.c +4 -2
-	if (!IS_ALIGNED(addr, sizeof(u32)))
-		return -EINVAL;
+	if (!IS_ALIGNED(addr, sizeof(u32))) {
+		ret = -EINVAL;
+		goto exit_write;
+	}


  if (punit >= pfs->pfs_header.num_entries) { ret = -EINVAL;