platform/x86/intel/tpmi: Fix memory leak in mem_write() error path
Subsystems (3)
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.
--- drivers/platform/x86/intel/vsec_tpmi.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/platform/x86/intel/vsec_tpmi.c +4 -2
@@ -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
Ilpo Järvinen
maintainerQuoted 22 lines · ZhaoJinming
In
mem_write(), when theIS_ALIGNED()check fails, the function returns-EINVALdirectly without freeing the 'array' allocated byparse_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")Signed-off-by ZhaoJinming <[email protected]>--- drivers/platform/x86/intel/vsec_tpmi.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)drivers/platform/x86/intel/vsec_tpmi.c +0
-0@@ -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.
Hi,
Thank you for your kindness. I hope I got your point.
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.
--- drivers/platform/x86/intel/vsec_tpmi.c | 26 +++++++++----------------- 1 file changed, 9 insertions(+), 17 deletions(-)
drivers/platform/x86/intel/vsec_tpmi.c +9 -17
@@ -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 lstruct 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 lif (!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 lif (!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
Ilpo Järvinen
maintainerRe: [PATCH 1/2] platform/x86/intel/tpmi: use cleanup helpers in mem_write()
Quoted 1 line · ZhaoJinming
In
mem_write(), the temporary array returned byparse_int_array_user()must be released on all error paths. Convert the array variable to usecleanup.hscope-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 bycleanup.husage 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")Signed-off-by ZhaoJinming <[email protected]>--- drivers/platform/x86/intel/vsec_tpmi.c | 26 +++++++++----------------- 1 file changed, 9 insertions(+), 17 deletions(-)drivers/platform/x86/intel/vsec_tpmi.c +9
-13@@ -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 lstruct 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 lif (!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 lif (!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
-3mutex_unlock (&tpmi_dev_lock); -exit_write: -kfree (array); -return ret; }
Signature · 1 line
i.
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.
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.
--- drivers/platform/x86/intel/vsec_tpmi.c | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-)
drivers/platform/x86/intel/vsec_tpmi.c +9 -16
@@ -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 lstruct 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 lif (!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 lif (!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
Ilpo Järvinen
maintainerRe: [PATCH 1/2] platform/x86/intel/tpmi: use cleanup helpers in mem_write()
Quoted 8 lines · ZhaoJinming
In
mem_write(), the temporary array returned byparse_int_array_user()must be released on all exit paths. Convert the array variable to usecleanup.hscope-based cleanup so it is freed automatically on return.This also moves the array declaration next to
parse_int_array_user()as required bycleanup.husage 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")Signed-off-by ZhaoJinming <[email protected]>--- drivers/platform/x86/intel/vsec_tpmi.c | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-)drivers/platform/x86/intel/vsec_tpmi.c +9
-13@@ -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 lstruct 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 lif (!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 lif (!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.
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.hscope-basedcleanup so it is freed automatically on return.
This also moves the array declaration next to
Quoted 5 lines
parse_int_array_user()as required bycleanup.husageguidelines.
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")Signed-off-by ZhaoJinming <[email protected]>---
?
drivers/platform/x86/intel/vsec_tpmi.c| 25 +++++++++----------------? 1 file changed, 9 insertions(+), 16 deletions(-)
drivers/platform/x86/intel/vsec_tpmi.c +0
-0index
16fd7aa41f20..88f14d0ad410100644---
a/drivers/platform/x86/intel/vsec_tpmi.c+++
b/drivers/platform/x86/intel/vsec_tpmi.cdrivers/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.
PATCH 2/2 platform/x86/intel/tpmi: convert mutex in mem_write() to guard
In reply to ZhaoJinmingConvert 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
@@ -504,24 +504,17 @@ static ssize_t mem_write(struct file *file, const char __user *userbuf, size_t lif (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
PATCH 2/2 platform/x86/intel/tpmi: convert mutex lock/unlock in mem_write() to guard
In reply to ZhaoJinmingConvert 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
@@ -504,23 +504,17 @@ static ssize_t mem_write(struct file *file, const char __user *userbuf, size_t lif (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
PATCH 1/2 platform/x86/intel/tpmi: use cleanup helpers in mem_write()
In reply to Ilpo JärvinenIn 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.
--- drivers/platform/x86/intel/vsec_tpmi.c | 26 +++++++++----------------- 1 file changed, 9 insertions(+), 17 deletions(-)
drivers/platform/x86/intel/vsec_tpmi.c +9 -17
@@ -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 lstruct 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 lif (!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 lif (!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
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
@@ -504,23 +504,17 @@ static ssize_t mem_write(struct file *file, const char __user *userbuf, size_t lif (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
srinivas pandruvada
maintainerQuoted 10 lines · ZhaoJinming
In
mem_write(), when theIS_ALIGNED()check fails, the function returns-EINVALdirectly without freeing the 'array' allocated byparse_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 alignedaddress for debugfs mem write")
Signed-off-by ZhaoJinming <[email protected]>---
drivers/platform/x86/intel/vsec_tpmi.c | 6 ++++--1 file changed, 4 insertions(+), 2 deletions(-)drivers/platform/x86/intel/vsec_tpmi.c +0
-0@@ -495,8 +495,10 @@ static ssize_t mem_write(struct file *file,const char
__user*userbuf,size_tl
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;