Skip to contentSkip to conversation

pci: endpoint: Fix double free in pci_epf_create()

PATCH Superseded Rolf Evers-Fischer 4 participants 12 messages Feb 21, 2018 – Feb 23, 2018

Subsystems (2)

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

cover pci: endpoint: Fix double free in pci_epf_create()

When I accidentally created a new endpoint device with an empty name, the kernel warned about "attempted to be registered with empty name!" and crashed afterwards.

It turned out that the crash was not caused by the 'device_add()' function itself, but by a double kfree of 'epf->name' and 'epf'.

The first patch just simplifies the code, while the second patch fixes the problem.

Rolf Evers-Fischer (2):
pci: endpoint: Free func_name after last usage
pci: endpoint: Fix kernel panic after put_device()

 drivers/pci/endpoint/pci-epf-core.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)
Signature · 1 line
2.16.2
#1 lore raw

PATCH 1/2 pci: endpoint: Free func_name after last usage

From: Rolf Evers-Fischer <[email protected]>

This commit decreases the number of jump labels and ensures that the next commit doesn't increase the number of occurrences of 'kfree(func_name)'.

Change-Id I0d1b6fd652395b85f82b11c43bf9b7db512854d1
---
 drivers/pci/endpoint/pci-epf-core.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
drivers/pci/endpoint/pci-epf-core.c +2 -5
index 766ce1dca2ec..23d0e128d1a5 100644
@@ -220,9 +220,10 @@ struct pci_epf *pci_epf_create(const char *name)
 	*buf = '\0';
 
 	epf->name = kstrdup(func_name, GFP_KERNEL);
+	kfree(func_name);
 	if (!epf->name) {
 		ret = -ENOMEM;
-		goto free_func_name;
+		goto free_epf;
 	}
 
 	dev = &epf->dev;
@@ -238,16 +239,12 @@ struct pci_epf *pci_epf_create(const char *name)
 	if (ret)
 		goto put_dev;
 
-	kfree(func_name);
 	return epf;
 
 put_dev:
 	put_device(dev);
 	kfree(epf->name);
 
-free_func_name:
-	kfree(func_name);
-
 free_epf:
 	kfree(epf);
 
Signature · 1 line
2.16.2
#2 lore raw

Re: [PATCH 1/2] pci: endpoint: Free func_name after last usage

Quoted 5 lines · Rolf Evers-Fischer

This commit decreases the number of jump labels and ensures that the next commit doesn't increase the number of occurrences of 'kfree(func_name)'.

Change-Id: I0d1b6fd652395b85f82b11c43bf9b7db512854d1

First of all, this shouldn't be here.

Quoted 2 lines

Looks like bipolar disorder?

To the topic.
This is a slow path and your change makes code slightly less readable b/c of patterns used in the kernel.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#3 lore raw

Re: [PATCH 1/2] pci: endpoint: Free func_name after last usage

Quoted 4 lines · Andy Shevchenko
Quoted 1 line

Change-Id: I0d1b6fd652395b85f82b11c43bf9b7db512854d1

First of all, this shouldn't be here.

Please excuse me. I know that I have to remove this, but unfortunately I forgot to do so (here and also in the other patch).

Quoted 5 lines
Quoted 2 lines

Looks like bipolar disorder?

You are right: One "Signed-off" should be enough.

Quoted 4 lines

To the topic.
This is a slow path and your change makes code slightly less readable b/c of patterns used in the kernel.

I see. It is probably not a good idea to free the memory in the middle of the function.
If you don't mind, I will remove this patch and change the other one accordingly.

With best regards,
Rolf Evers-Fischer

#4 lore raw

Re: [PATCH 1/2] pci: endpoint: Free func_name after last usage

Quoted 9 lines · Rolf Evers-Fischer
Quoted 3 lines · Andy Shevchenko

This is a slow path and your change makes code slightly less readable b/c of patterns used in the kernel.

I see. It is probably not a good idea to free the memory in the middle of the function.
If you don't mind, I will remove this patch and change the other one accordingly.

Sounds good to me!

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#5 lore raw

Re: [PATCH 1/2] pci: endpoint: Free func_name after last usage

In reply to Rolf Evers-Fischer
Quoted 18 lines · Rolf Evers-Fischer

From: Rolf Evers-Fischer <[email protected]>

This commit decreases the number of jump labels and ensures that the next commit doesn't increase the number of occurrences of 'kfree(func_name)'.

Change-Id I0d1b6fd652395b85f82b11c43bf9b7db512854d1
---
 drivers/pci/endpoint/pci-epf-core.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
drivers/pci/endpoint/pci-epf-core.c +0 -0
index 766ce1dca2ec..23d0e128d1a5 100644

drivers/pci/endpoint/pci-epf-core.c struct pci_epf *pci_epf_create(const char *name) · line 223

Quoted 4 lines
drivers/pci/endpoint/pci-epf-core.c +1 -0
@@ -220,9 +220,10 @@ struct pci_epf *pci_epf_create(const char *name)
 	*buf = '\0';
 
 	epf->name = kstrdup(func_name, GFP_KERNEL);
+	kfree(func_name);

I am certainly missing something but what if we reworked the code and just:

kstrdup(name, GFP_KERNEL);

once instead of allocating another local copy (that we then have to free) ?

Reworded: why

epf->name = func_name;

would not work ?

Thanks,
Lorenzo

Quoted 27 lines
drivers/pci/endpoint/pci-epf-core.c +1 -5
 	if (!epf->name) {
 		ret = -ENOMEM;
-		goto free_func_name;
+		goto free_epf;
 	}
 
 	dev = &epf->dev;
@@ -238,16 +239,12 @@ struct pci_epf *pci_epf_create(const char *name)
 	if (ret)
 		goto put_dev;
 
-	kfree(func_name);
 	return epf;
 
 put_dev:
 	put_device(dev);
 	kfree(epf->name);
 
-free_func_name:
-	kfree(func_name);
-
 free_epf:
 	kfree(epf);
 
Signature · 1 line
2.16.2
#6 lore raw

Re: [PATCH 1/2] pci: endpoint: Free func_name after last usage

Hi Lorenzo,

Quoted 27 lines · Lorenzo Pieralisi
Quoted 18 lines · Rolf Evers-Fischer

From: Rolf Evers-Fischer <[email protected]>

This commit decreases the number of jump labels and ensures that the next commit doesn't increase the number of occurrences of 'kfree(func_name)'.

Change-Id I0d1b6fd652395b85f82b11c43bf9b7db512854d1
---
 drivers/pci/endpoint/pci-epf-core.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
drivers/pci/endpoint/pci-epf-core.c +0 -0
index 766ce1dca2ec..23d0e128d1a5 100644

drivers/pci/endpoint/pci-epf-core.c struct pci_epf *pci_epf_create(const char *name) · line 223

Quoted 4 lines
drivers/pci/endpoint/pci-epf-core.c +1 -0
@@ -220,9 +220,10 @@ struct pci_epf *pci_epf_create(const char *name)
 	*buf = '\0';
 
 	epf->name = kstrdup(func_name, GFP_KERNEL);
+	kfree(func_name);

I am certainly missing something but what if we reworked the code and just:

Quoted 4 lines

kstrdup(name, GFP_KERNEL);

once instead of allocating another local copy (that we then have to free) ?

name will be something like pci_epf_test.0 and in epf->name we want to just have pci_epf_test.

Quoted 4 lines

Reworded: why

epf->name = func_name;

memory should be allocated for epf->name before it can be initialized. IMO without kstrdup, there will be a null pointer exception.

Thanks
Kishon

#7 lore raw

Re: [PATCH 1/2] pci: endpoint: Free func_name after last usage

Quoted 38 lines · Kishon Vijay Abraham I

Hi Lorenzo,

Quoted 27 lines · Lorenzo Pieralisi
Quoted 18 lines · Rolf Evers-Fischer

From: Rolf Evers-Fischer <[email protected]>

This commit decreases the number of jump labels and ensures that the next commit doesn't increase the number of occurrences of 'kfree(func_name)'.

Change-Id I0d1b6fd652395b85f82b11c43bf9b7db512854d1
---
 drivers/pci/endpoint/pci-epf-core.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
drivers/pci/endpoint/pci-epf-core.c +0 -0
index 766ce1dca2ec..23d0e128d1a5 100644

drivers/pci/endpoint/pci-epf-core.c struct pci_epf *pci_epf_create(const char *name) · line 223

Quoted 4 lines
drivers/pci/endpoint/pci-epf-core.c +1 -0
@@ -220,9 +220,10 @@ struct pci_epf *pci_epf_create(const char *name)
 	*buf = '\0';
 
 	epf->name = kstrdup(func_name, GFP_KERNEL);
+	kfree(func_name);

I am certainly missing something but what if we reworked the code and just:

Quoted 4 lines

kstrdup(name, GFP_KERNEL);

once instead of allocating another local copy (that we then have to free) ?

name will be something like pci_epf_test.0 and in epf->name we want to just have pci_epf_test.

Quoted 1 line
Quoted 6 lines
Quoted 3 lines

Reworded: why

epf->name = func_name;

memory should be allocated for epf->name before it can be initialized. IMO without kstrdup, there will be a null pointer exception.

I understand that but the point is that func_name *was* allocated with kstrdup() already I would like to understand why we need to do it twice (and kfree the first allocation).

Lorenzo

#8 lore raw

Re: [PATCH 1/2] pci: endpoint: Free func_name after last usage

Quoted 41 lines · Lorenzo Pieralisi
Quoted 40 lines · Kishon Vijay Abraham I

Hi Lorenzo,

Quoted 27 lines · Lorenzo Pieralisi
Quoted 18 lines · Rolf Evers-Fischer

From: Rolf Evers-Fischer <[email protected]>

This commit decreases the number of jump labels and ensures that the next commit doesn't increase the number of occurrences of 'kfree(func_name)'.

Change-Id I0d1b6fd652395b85f82b11c43bf9b7db512854d1
---
 drivers/pci/endpoint/pci-epf-core.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
drivers/pci/endpoint/pci-epf-core.c +0 -0
index 766ce1dca2ec..23d0e128d1a5 100644

drivers/pci/endpoint/pci-epf-core.c struct pci_epf *pci_epf_create(const char *name) · line 223

Quoted 4 lines
drivers/pci/endpoint/pci-epf-core.c +1 -0
@@ -220,9 +220,10 @@ struct pci_epf *pci_epf_create(const char *name)
 	*buf = '\0';
 
 	epf->name = kstrdup(func_name, GFP_KERNEL);
+	kfree(func_name);

I am certainly missing something but what if we reworked the code and just:

Quoted 4 lines

kstrdup(name, GFP_KERNEL);

once instead of allocating another local copy (that we then have to free) ?

name will be something like pci_epf_test.0 and in epf->name we want to just have pci_epf_test.

Quoted 3 lines

Reworded: why

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

epf->name = func_name;

memory should be allocated for epf->name before it can be initialized. IMO without kstrdup, there will be a null pointer exception.

I understand that but the point is that func_name *was* allocated with kstrdup() already I would like to understand why we need to do it twice (and kfree the first allocation).

func_name would be allocated for a size greater than what will be in epf->name. It won't be significant though.

Thanks
Kishon

#9 lore raw

Re: [PATCH 1/2] pci: endpoint: Free func_name after last usage

Hi Kishon,

Quoted 51 lines · Kishon Vijay Abraham I
Quoted 41 lines · Lorenzo Pieralisi
Quoted 40 lines · Kishon Vijay Abraham I

Hi Lorenzo,

Quoted 27 lines · Lorenzo Pieralisi
Quoted 18 lines · Rolf Evers-Fischer

From: Rolf Evers-Fischer <[email protected]>

This commit decreases the number of jump labels and ensures that the next commit doesn't increase the number of occurrences of 'kfree(func_name)'.

Change-Id I0d1b6fd652395b85f82b11c43bf9b7db512854d1
---
 drivers/pci/endpoint/pci-epf-core.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
drivers/pci/endpoint/pci-epf-core.c +0 -0
index 766ce1dca2ec..23d0e128d1a5 100644

drivers/pci/endpoint/pci-epf-core.c struct pci_epf *pci_epf_create(const char *name) · line 223

Quoted 4 lines
drivers/pci/endpoint/pci-epf-core.c +1 -0
@@ -220,9 +220,10 @@ struct pci_epf *pci_epf_create(const char *name)
 	*buf = '\0';
 
 	epf->name = kstrdup(func_name, GFP_KERNEL);
+	kfree(func_name);

I am certainly missing something but what if we reworked the code and just:

Quoted 4 lines

kstrdup(name, GFP_KERNEL);

once instead of allocating another local copy (that we then have to free) ?

name will be something like pci_epf_test.0 and in epf->name we want to just have pci_epf_test.

Quoted 3 lines

Reworded: why

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

epf->name = func_name;

memory should be allocated for epf->name before it can be initialized. IMO without kstrdup, there will be a null pointer exception.

I understand that but the point is that func_name *was* allocated with kstrdup() already I would like to understand why we need to do it twice (and kfree the first allocation).

Quoted 6 lines

func_name would be allocated for a size greater than what will be in epf->name. It won't be significant though.

Thanks
Kishon

I have just an idea, how to use only one copy, but with the shorter length. What about changing the code like this?
int len = strchrnul(name, '.') - name;
epf->name = kstrndup(name, len, GFP_KERNEL);

In this case we are only allocating the smaller amount of memory and we don't need the 'buf' and 'func_name' pointers anymore. They will be replaced with 'len' and some pointer arithmetic.

Kind regards,
Rolf

#10 lore raw

Re: [PATCH 1/2] pci: endpoint: Free func_name after last usage

Quoted 60 lines · Rolf Evers-Fischer

Hi Kishon,

Quoted 51 lines · Kishon Vijay Abraham I
Quoted 41 lines · Lorenzo Pieralisi
Quoted 40 lines · Kishon Vijay Abraham I

Hi Lorenzo,

Quoted 27 lines · Lorenzo Pieralisi
Quoted 18 lines · Rolf Evers-Fischer

From: Rolf Evers-Fischer <[email protected]>

This commit decreases the number of jump labels and ensures that the next commit doesn't increase the number of occurrences of 'kfree(func_name)'.

Change-Id I0d1b6fd652395b85f82b11c43bf9b7db512854d1
---
 drivers/pci/endpoint/pci-epf-core.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
drivers/pci/endpoint/pci-epf-core.c +0 -0
index 766ce1dca2ec..23d0e128d1a5 100644

drivers/pci/endpoint/pci-epf-core.c struct pci_epf *pci_epf_create(const char *name) · line 223

Quoted 4 lines
drivers/pci/endpoint/pci-epf-core.c +1 -0
@@ -220,9 +220,10 @@ struct pci_epf *pci_epf_create(const char *name)
 	*buf = '\0';
 
 	epf->name = kstrdup(func_name, GFP_KERNEL);
+	kfree(func_name);

I am certainly missing something but what if we reworked the code and just:

Quoted 4 lines

kstrdup(name, GFP_KERNEL);

once instead of allocating another local copy (that we then have to free) ?

name will be something like pci_epf_test.0 and in epf->name we want to just have pci_epf_test.

Quoted 3 lines

Reworded: why

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

epf->name = func_name;

memory should be allocated for epf->name before it can be initialized. IMO without kstrdup, there will be a null pointer exception.

I understand that but the point is that func_name *was* allocated with kstrdup() already I would like to understand why we need to do it twice (and kfree the first allocation).

Quoted 6 lines

func_name would be allocated for a size greater than what will be in epf->name. It won't be significant though.

Thanks
Kishon

Quoted 8 lines

I have just an idea, how to use only one copy, but with the shorter length. What about changing the code like this?
int len = strchrnul(name, '.') - name;
epf->name = kstrndup(name, len, GFP_KERNEL);

In this case we are only allocating the smaller amount of memory and we don't need the 'buf' and 'func_name' pointers anymore. They will be replaced with 'len' and some pointer arithmetic.

Yes, something along those lines, I do not see the point of having to allocate two buffers (and actually this simplifies the error path too).

Thanks,
Lorenzo

#11 lore raw

PATCH 2/2 pci: endpoint: Fix kernel panic after put_device()

In reply to Rolf Evers-Fischer

From: Rolf Evers-Fischer <[email protected]>

'put_device()' calls the relase function 'pci_epf_dev_release()', which already frees 'epf->name' and 'epf'.

Therefore we must not free them again after 'put_device()'.

Change-Id I14ca19f96abfbbb489dd1f4d489e085329d7112e
---
 drivers/pci/endpoint/pci-epf-core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/pci/endpoint/pci-epf-core.c +1 -1
index 23d0e128d1a5..3718b4c5814a 100644
@@ -243,7 +243,7 @@ struct pci_epf *pci_epf_create(const char *name)
 
 put_dev:
 	put_device(dev);
-	kfree(epf->name);
+	return ERR_PTR(ret);
 
 free_epf:
 	kfree(epf);
Signature · 1 line
2.16.2