diff mbox series

PCI/sysfs: Fix double free in error path

Message ID 20221007065618.2169880-1-s.hauer@pengutronix.de (mailing list archive)
State Accepted
Delegated to: Bjorn Helgaas
Headers show
Series PCI/sysfs: Fix double free in error path | expand

Commit Message

Sascha Hauer Oct. 7, 2022, 6:56 a.m. UTC
When pci_create_attr() fails then pci_remove_resource_files() is called
which will iterate over the res_attr[_wc] arrays and frees every non
NULL entry. To avoid a double free here we have to set the failed entry
to NULL in pci_create_attr() when freeing it.

Fixes: b562ec8f74e4 ("PCI: Don't leak memory if sysfs_create_bin_file() fails")
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Cc: <stable@vger.kernel.org>
---
 drivers/pci/pci-sysfs.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

Comments

Sascha Hauer Oct. 7, 2022, 7:07 a.m. UTC | #1
On Fri, Oct 07, 2022 at 08:56:18AM +0200, Sascha Hauer wrote:
> When pci_create_attr() fails then pci_remove_resource_files() is called
> which will iterate over the res_attr[_wc] arrays and frees every non
> NULL entry. To avoid a double free here we have to set the failed entry
> to NULL in pci_create_attr() when freeing it.
> 

You might consider applying this alternative version instead which IMO
looks a bit better.

Sascha

-------------------------------8<-----------------------------

From fe8e0e6f914c14395c751b7dc165967b12427995 Mon Sep 17 00:00:00 2001
From: Sascha Hauer <s.hauer@pengutronix.de>
Date: Fri, 7 Oct 2022 07:35:35 +0200
Subject: [PATCH] PCI/sysfs: Fix double free in error path

When pci_create_attr() fails then pci_remove_resource_files() is called
which will iterate over the res_attr[_wc] arrays and frees every non
NULL entry. To avoid a double free here set the array entry only after
it's clear we successfully initialized it.

Fixes: b562ec8f74e4 ("PCI: Don't leak memory if sysfs_create_bin_file() fails")
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Cc: <stable@vger.kernel.org>
---
 drivers/pci/pci-sysfs.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)

diff --git a/drivers/pci/pci-sysfs.c b/drivers/pci/pci-sysfs.c
index fc804e08e3cb5..6dd4050c9f2ed 100644
--- a/drivers/pci/pci-sysfs.c
+++ b/drivers/pci/pci-sysfs.c
@@ -1174,11 +1174,9 @@ static int pci_create_attr(struct pci_dev *pdev, int num, int write_combine)
 
 	sysfs_bin_attr_init(res_attr);
 	if (write_combine) {
-		pdev->res_attr_wc[num] = res_attr;
 		sprintf(res_attr_name, "resource%d_wc", num);
 		res_attr->mmap = pci_mmap_resource_wc;
 	} else {
-		pdev->res_attr[num] = res_attr;
 		sprintf(res_attr_name, "resource%d", num);
 		if (pci_resource_flags(pdev, num) & IORESOURCE_IO) {
 			res_attr->read = pci_read_resource_io;
@@ -1196,10 +1194,17 @@ static int pci_create_attr(struct pci_dev *pdev, int num, int write_combine)
 	res_attr->size = pci_resource_len(pdev, num);
 	res_attr->private = (void *)(unsigned long)num;
 	retval = sysfs_create_bin_file(&pdev->dev.kobj, res_attr);
-	if (retval)
+	if (retval) {
 		kfree(res_attr);
+		return retval;
+	}
+
+	if (write_combine)
+		pdev->res_attr_wc[num] = res_attr;
+	else
+		pdev->res_attr[num] = res_attr;
 
-	return retval;
+	return 0;
 }
 
 /**
Sascha Hauer Nov. 1, 2022, 9:40 a.m. UTC | #2
Hi Bjorn,

On Fri, Oct 07, 2022 at 08:56:18AM +0200, Sascha Hauer wrote:
> When pci_create_attr() fails then pci_remove_resource_files() is called
> which will iterate over the res_attr[_wc] arrays and frees every non
> NULL entry. To avoid a double free here we have to set the failed entry
> to NULL in pci_create_attr() when freeing it.
> 
> Fixes: b562ec8f74e4 ("PCI: Don't leak memory if sysfs_create_bin_file() fails")
> Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
> Cc: <stable@vger.kernel.org>
> ---
>  drivers/pci/pci-sysfs.c | 7 ++++++-
>  1 file changed, 6 insertions(+), 1 deletion(-)

Any input to this one? There's this long unfixed race condition
described here:

https://patchwork.kernel.org/project/linux-pci/patch/20200716110423.xtfyb3n6tn5ixedh@pali/#23547255

And this patch at least prevents my system from crashing when this race
condition occurs.

Sascha

> 
> diff --git a/drivers/pci/pci-sysfs.c b/drivers/pci/pci-sysfs.c
> index fc804e08e3cb5..a07381d46ddae 100644
> --- a/drivers/pci/pci-sysfs.c
> +++ b/drivers/pci/pci-sysfs.c
> @@ -1196,8 +1196,13 @@ static int pci_create_attr(struct pci_dev *pdev, int num, int write_combine)
>  	res_attr->size = pci_resource_len(pdev, num);
>  	res_attr->private = (void *)(unsigned long)num;
>  	retval = sysfs_create_bin_file(&pdev->dev.kobj, res_attr);
> -	if (retval)
> +	if (retval) {
> +		if (write_combine)
> +			pdev->res_attr_wc[num] = NULL;
> +		else
> +			pdev->res_attr[num] = NULL;
>  		kfree(res_attr);
> +	}
>  
>  	return retval;
>  }
> -- 
> 2.30.2
> 
>
Bjorn Helgaas Nov. 9, 2022, 11:07 p.m. UTC | #3
[+cc Sergey, Alex, Krzysztof since you all posted similar patches in
the past]

On Fri, Oct 07, 2022 at 09:07:35AM +0200, Sascha Hauer wrote:
> On Fri, Oct 07, 2022 at 08:56:18AM +0200, Sascha Hauer wrote:
> > When pci_create_attr() fails then pci_remove_resource_files() is called
> > which will iterate over the res_attr[_wc] arrays and frees every non
> > NULL entry. To avoid a double free here we have to set the failed entry
> > to NULL in pci_create_attr() when freeing it.
> > 
> 
> You might consider applying this alternative version instead which IMO
> looks a bit better.

Thanks, I agree, I like how this one doesn't set res_attr[] until we
know we're going to return success.

Applied to pci/sysfs for v6.2, thanks!

> -------------------------------8<-----------------------------
> 
> From fe8e0e6f914c14395c751b7dc165967b12427995 Mon Sep 17 00:00:00 2001
> From: Sascha Hauer <s.hauer@pengutronix.de>
> Date: Fri, 7 Oct 2022 07:35:35 +0200
> Subject: [PATCH] PCI/sysfs: Fix double free in error path
> 
> When pci_create_attr() fails then pci_remove_resource_files() is called
> which will iterate over the res_attr[_wc] arrays and frees every non
> NULL entry. To avoid a double free here set the array entry only after
> it's clear we successfully initialized it.
> 
> Fixes: b562ec8f74e4 ("PCI: Don't leak memory if sysfs_create_bin_file() fails")
> Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
> Cc: <stable@vger.kernel.org>
> ---
>  drivers/pci/pci-sysfs.c | 13 +++++++++----
>  1 file changed, 9 insertions(+), 4 deletions(-)
> 
> diff --git a/drivers/pci/pci-sysfs.c b/drivers/pci/pci-sysfs.c
> index fc804e08e3cb5..6dd4050c9f2ed 100644
> --- a/drivers/pci/pci-sysfs.c
> +++ b/drivers/pci/pci-sysfs.c
> @@ -1174,11 +1174,9 @@ static int pci_create_attr(struct pci_dev *pdev, int num, int write_combine)
>  
>  	sysfs_bin_attr_init(res_attr);
>  	if (write_combine) {
> -		pdev->res_attr_wc[num] = res_attr;
>  		sprintf(res_attr_name, "resource%d_wc", num);
>  		res_attr->mmap = pci_mmap_resource_wc;
>  	} else {
> -		pdev->res_attr[num] = res_attr;
>  		sprintf(res_attr_name, "resource%d", num);
>  		if (pci_resource_flags(pdev, num) & IORESOURCE_IO) {
>  			res_attr->read = pci_read_resource_io;
> @@ -1196,10 +1194,17 @@ static int pci_create_attr(struct pci_dev *pdev, int num, int write_combine)
>  	res_attr->size = pci_resource_len(pdev, num);
>  	res_attr->private = (void *)(unsigned long)num;
>  	retval = sysfs_create_bin_file(&pdev->dev.kobj, res_attr);
> -	if (retval)
> +	if (retval) {
>  		kfree(res_attr);
> +		return retval;
> +	}
> +
> +	if (write_combine)
> +		pdev->res_attr_wc[num] = res_attr;
> +	else
> +		pdev->res_attr[num] = res_attr;
>  
> -	return retval;
> +	return 0;
>  }
>  
>  /**
> -- 
> 2.30.2
> 
> -- 
> Pengutronix e.K.                           |                             |
> Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
> 31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
> Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |
diff mbox series

Patch

diff --git a/drivers/pci/pci-sysfs.c b/drivers/pci/pci-sysfs.c
index fc804e08e3cb5..a07381d46ddae 100644
--- a/drivers/pci/pci-sysfs.c
+++ b/drivers/pci/pci-sysfs.c
@@ -1196,8 +1196,13 @@  static int pci_create_attr(struct pci_dev *pdev, int num, int write_combine)
 	res_attr->size = pci_resource_len(pdev, num);
 	res_attr->private = (void *)(unsigned long)num;
 	retval = sysfs_create_bin_file(&pdev->dev.kobj, res_attr);
-	if (retval)
+	if (retval) {
+		if (write_combine)
+			pdev->res_attr_wc[num] = NULL;
+		else
+			pdev->res_attr[num] = NULL;
 		kfree(res_attr);
+	}
 
 	return retval;
 }