* [PATCH] PCI: endpoint: Fix use after free in pci_epf_remove_cfs()
@ 2021-06-21 7:00 Shunsuke Mie
2021-06-21 8:39 ` Greg KH
` (2 more replies)
0 siblings, 3 replies; 7+ messages in thread
From: Shunsuke Mie @ 2021-06-21 7:00 UTC (permalink / raw)
To: kishon; +Cc: lorenzo.pieralisi, bhelgaas, linux-pci, linux-kernel, stable, mie
All of entries are freed in a loop, however, the freed entry is accessed
by list_del() after the loop.
When epf driver that includes pci-epf-test unload, the pci_epf_remove_cfs()
is called, and occurred the use after free. Therefore, kernel panics
randomly after or while the module unloading.
I tested this patch with r8a77951-Salvator-xs boards.
Fixes: ef1433f ("PCI: endpoint: Create configfs entry for each pci_epf_device_id table entry")
Signed-off-by: Shunsuke Mie <mie@igel.co.jp>
---
drivers/pci/endpoint/pci-epf-core.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/drivers/pci/endpoint/pci-epf-core.c b/drivers/pci/endpoint/pci-epf-core.c
index e9289d10f822..538e902b0ba6 100644
--- a/drivers/pci/endpoint/pci-epf-core.c
+++ b/drivers/pci/endpoint/pci-epf-core.c
@@ -202,8 +202,10 @@ static void pci_epf_remove_cfs(struct pci_epf_driver *driver)
return;
mutex_lock(&pci_epf_mutex);
- list_for_each_entry_safe(group, tmp, &driver->epf_group, group_entry)
+ list_for_each_entry_safe(group, tmp, &driver->epf_group, group_entry) {
+ list_del(&group->group_entry);
pci_ep_cfs_remove_epf_group(group);
+ }
list_del(&driver->epf_group);
mutex_unlock(&pci_epf_mutex);
}
--
2.17.1
^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [PATCH] PCI: endpoint: Fix use after free in pci_epf_remove_cfs()
2021-06-21 7:00 [PATCH] PCI: endpoint: Fix use after free in pci_epf_remove_cfs() Shunsuke Mie
@ 2021-06-21 8:39 ` Greg KH
2021-12-06 11:45 ` Lorenzo Pieralisi
2021-12-07 7:26 ` Kishon Vijay Abraham I
2 siblings, 0 replies; 7+ messages in thread
From: Greg KH @ 2021-06-21 8:39 UTC (permalink / raw)
To: Shunsuke Mie
Cc: kishon, lorenzo.pieralisi, bhelgaas, linux-pci, linux-kernel, stable
On Mon, Jun 21, 2021 at 04:00:58PM +0900, Shunsuke Mie wrote:
> All of entries are freed in a loop, however, the freed entry is accessed
> by list_del() after the loop.
>
> When epf driver that includes pci-epf-test unload, the pci_epf_remove_cfs()
> is called, and occurred the use after free. Therefore, kernel panics
> randomly after or while the module unloading.
>
> I tested this patch with r8a77951-Salvator-xs boards.
>
> Fixes: ef1433f ("PCI: endpoint: Create configfs entry for each pci_epf_device_id table entry")
> Signed-off-by: Shunsuke Mie <mie@igel.co.jp>
> ---
> drivers/pci/endpoint/pci-epf-core.c | 4 +++-
> 1 file changed, 3 insertions(+), 1 deletion(-)
<formletter>
This is not the correct way to submit patches for inclusion in the
stable kernel tree. Please read:
https://www.kernel.org/doc/html/latest/process/stable-kernel-rules.html
for how to do this properly.
</formletter>
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] PCI: endpoint: Fix use after free in pci_epf_remove_cfs()
2021-06-21 7:00 [PATCH] PCI: endpoint: Fix use after free in pci_epf_remove_cfs() Shunsuke Mie
2021-06-21 8:39 ` Greg KH
@ 2021-12-06 11:45 ` Lorenzo Pieralisi
2021-12-07 7:26 ` Kishon Vijay Abraham I
2 siblings, 0 replies; 7+ messages in thread
From: Lorenzo Pieralisi @ 2021-12-06 11:45 UTC (permalink / raw)
To: Shunsuke Mie, kishon; +Cc: bhelgaas, linux-pci, linux-kernel
[dropped stable, erroneously added to the CC list]
On Mon, Jun 21, 2021 at 04:00:58PM +0900, Shunsuke Mie wrote:
> All of entries are freed in a loop, however, the freed entry is accessed
> by list_del() after the loop.
>
> When epf driver that includes pci-epf-test unload, the pci_epf_remove_cfs()
> is called, and occurred the use after free. Therefore, kernel panics
> randomly after or while the module unloading.
>
> I tested this patch with r8a77951-Salvator-xs boards.
>
> Fixes: ef1433f ("PCI: endpoint: Create configfs entry for each pci_epf_device_id table entry")
> Signed-off-by: Shunsuke Mie <mie@igel.co.jp>
> ---
> drivers/pci/endpoint/pci-epf-core.c | 4 +++-
> 1 file changed, 3 insertions(+), 1 deletion(-)
Kishon, please review this patch, thanks.
Lorenzo
>
> diff --git a/drivers/pci/endpoint/pci-epf-core.c b/drivers/pci/endpoint/pci-epf-core.c
> index e9289d10f822..538e902b0ba6 100644
> --- a/drivers/pci/endpoint/pci-epf-core.c
> +++ b/drivers/pci/endpoint/pci-epf-core.c
> @@ -202,8 +202,10 @@ static void pci_epf_remove_cfs(struct pci_epf_driver *driver)
> return;
>
> mutex_lock(&pci_epf_mutex);
> - list_for_each_entry_safe(group, tmp, &driver->epf_group, group_entry)
> + list_for_each_entry_safe(group, tmp, &driver->epf_group, group_entry) {
> + list_del(&group->group_entry);
> pci_ep_cfs_remove_epf_group(group);
> + }
> list_del(&driver->epf_group);
> mutex_unlock(&pci_epf_mutex);
> }
> --
> 2.17.1
>
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] PCI: endpoint: Fix use after free in pci_epf_remove_cfs()
2021-06-21 7:00 [PATCH] PCI: endpoint: Fix use after free in pci_epf_remove_cfs() Shunsuke Mie
2021-06-21 8:39 ` Greg KH
2021-12-06 11:45 ` Lorenzo Pieralisi
@ 2021-12-07 7:26 ` Kishon Vijay Abraham I
2021-12-07 7:44 ` Shunsuke Mie
2 siblings, 1 reply; 7+ messages in thread
From: Kishon Vijay Abraham I @ 2021-12-07 7:26 UTC (permalink / raw)
To: Shunsuke Mie; +Cc: lorenzo.pieralisi, bhelgaas, linux-pci, linux-kernel, stable
Hi Shunsuke,
On 21/06/21 12:30 pm, Shunsuke Mie wrote:
> All of entries are freed in a loop, however, the freed entry is accessed
> by list_del() after the loop.
>
> When epf driver that includes pci-epf-test unload, the pci_epf_remove_cfs()
> is called, and occurred the use after free. Therefore, kernel panics
> randomly after or while the module unloading.
>
> I tested this patch with r8a77951-Salvator-xs boards.
Can you provide the crash dump?
>
> Fixes: ef1433f ("PCI: endpoint: Create configfs entry for each pci_epf_device_id table entry")
> Signed-off-by: Shunsuke Mie <mie@igel.co.jp>
> ---
> drivers/pci/endpoint/pci-epf-core.c | 4 +++-
> 1 file changed, 3 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/pci/endpoint/pci-epf-core.c b/drivers/pci/endpoint/pci-epf-core.c
> index e9289d10f822..538e902b0ba6 100644
> --- a/drivers/pci/endpoint/pci-epf-core.c
> +++ b/drivers/pci/endpoint/pci-epf-core.c
> @@ -202,8 +202,10 @@ static void pci_epf_remove_cfs(struct pci_epf_driver *driver)
> return;
>
> mutex_lock(&pci_epf_mutex);
> - list_for_each_entry_safe(group, tmp, &driver->epf_group, group_entry)
> + list_for_each_entry_safe(group, tmp, &driver->epf_group, group_entry) {
> + list_del(&group->group_entry);
Need full crash dump to see if this is really required or not. Ideally this
should be handled by configfs (or a configfs API could be used).
Thanks,
Kishon
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] PCI: endpoint: Fix use after free in pci_epf_remove_cfs()
2021-12-07 7:26 ` Kishon Vijay Abraham I
@ 2021-12-07 7:44 ` Shunsuke Mie
2021-12-10 11:09 ` Shunsuke Mie
0 siblings, 1 reply; 7+ messages in thread
From: Shunsuke Mie @ 2021-12-07 7:44 UTC (permalink / raw)
To: Kishon Vijay Abraham I
Cc: Lorenzo Pieralisi, Bjorn Helgaas, linux-pci,
Linux Kernel Mailing List, stable
Hi Kishon,
2021年12月7日(火) 16:26 Kishon Vijay Abraham I <kishon@ti.com>:
>
> Hi Shunsuke,
>
> On 21/06/21 12:30 pm, Shunsuke Mie wrote:
> > All of entries are freed in a loop, however, the freed entry is accessed
> > by list_del() after the loop.
> >
> > When epf driver that includes pci-epf-test unload, the pci_epf_remove_cfs()
> > is called, and occurred the use after free. Therefore, kernel panics
> > randomly after or while the module unloading.
> >
> > I tested this patch with r8a77951-Salvator-xs boards.
>
> Can you provide the crash dump?
Ok. However, that is use-after-free bug. so, the crash log is changed randomly.
I'll send some crash dumps.
> >
> > Fixes: ef1433f ("PCI: endpoint: Create configfs entry for each pci_epf_device_id table entry")
> > Signed-off-by: Shunsuke Mie <mie@igel.co.jp>
> > ---
> > drivers/pci/endpoint/pci-epf-core.c | 4 +++-
> > 1 file changed, 3 insertions(+), 1 deletion(-)
> >
> > diff --git a/drivers/pci/endpoint/pci-epf-core.c b/drivers/pci/endpoint/pci-epf-core.c
> > index e9289d10f822..538e902b0ba6 100644
> > --- a/drivers/pci/endpoint/pci-epf-core.c
> > +++ b/drivers/pci/endpoint/pci-epf-core.c
> > @@ -202,8 +202,10 @@ static void pci_epf_remove_cfs(struct pci_epf_driver *driver)
> > return;
> >
> > mutex_lock(&pci_epf_mutex);
> > - list_for_each_entry_safe(group, tmp, &driver->epf_group, group_entry)
> > + list_for_each_entry_safe(group, tmp, &driver->epf_group, group_entry) {
> > + list_del(&group->group_entry);
>
> Need full crash dump to see if this is really required or not. Ideally this
> should be handled by configfs (or a configfs API could be used).
>
> Thanks,
> Kishon
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] PCI: endpoint: Fix use after free in pci_epf_remove_cfs()
2021-12-07 7:44 ` Shunsuke Mie
@ 2021-12-10 11:09 ` Shunsuke Mie
0 siblings, 0 replies; 7+ messages in thread
From: Shunsuke Mie @ 2021-12-10 11:09 UTC (permalink / raw)
To: Kishon Vijay Abraham I
Cc: Lorenzo Pieralisi, Bjorn Helgaas, linux-pci,
Linux Kernel Mailing List, stable
[-- Attachment #1: Type: text/plain, Size: 2361 bytes --]
Hi Kishon,
This is the crash log. I think you can reproduce this problem by loading and
unloading the pci-epf-test modules some times, but I don't think you'll see
similar logs in your environment.
The pci_ep_cfs_remove_epf_group function calls kfree for the `group`, however
a member of group, `group_entry``, is accessed by a list_del() after the loop.
The patch changes to remove list entries to prevent the accessing after freed
object.
Thanks,
Shunsuke
2021年12月7日(火) 16:44 Shunsuke Mie <mie@igel.co.jp>:
>
> Hi Kishon,
>
> 2021年12月7日(火) 16:26 Kishon Vijay Abraham I <kishon@ti.com>:
> >
> > Hi Shunsuke,
> >
> > On 21/06/21 12:30 pm, Shunsuke Mie wrote:
> > > All of entries are freed in a loop, however, the freed entry is accessed
> > > by list_del() after the loop.
> > >
> > > When epf driver that includes pci-epf-test unload, the pci_epf_remove_cfs()
> > > is called, and occurred the use after free. Therefore, kernel panics
> > > randomly after or while the module unloading.
> > >
> > > I tested this patch with r8a77951-Salvator-xs boards.
> >
> > Can you provide the crash dump?
> Ok. However, that is use-after-free bug. so, the crash log is changed randomly.
> I'll send some crash dumps.
> > >
> > > Fixes: ef1433f ("PCI: endpoint: Create configfs entry for each pci_epf_device_id table entry")
> > > Signed-off-by: Shunsuke Mie <mie@igel.co.jp>
> > > ---
> > > drivers/pci/endpoint/pci-epf-core.c | 4 +++-
> > > 1 file changed, 3 insertions(+), 1 deletion(-)
> > >
> > > diff --git a/drivers/pci/endpoint/pci-epf-core.c b/drivers/pci/endpoint/pci-epf-core.c
> > > index e9289d10f822..538e902b0ba6 100644
> > > --- a/drivers/pci/endpoint/pci-epf-core.c
> > > +++ b/drivers/pci/endpoint/pci-epf-core.c
> > > @@ -202,8 +202,10 @@ static void pci_epf_remove_cfs(struct pci_epf_driver *driver)
> > > return;
> > >
> > > mutex_lock(&pci_epf_mutex);
> > > - list_for_each_entry_safe(group, tmp, &driver->epf_group, group_entry)
> > > + list_for_each_entry_safe(group, tmp, &driver->epf_group, group_entry) {
> > > + list_del(&group->group_entry);
> >
> > Need full crash dump to see if this is really required or not. Ideally this
> > should be handled by configfs (or a configfs API could be used).
> >
> > Thanks,
> > Kishon
[-- Attachment #2: dmesg_crash.log --]
[-- Type: application/octet-stream, Size: 35947 bytes --]
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH] PCI: endpoint: Fix use after free in pci_epf_remove_cfs()
@ 2021-06-11 3:50 Shunsuke Mie
0 siblings, 0 replies; 7+ messages in thread
From: Shunsuke Mie @ 2021-06-11 3:50 UTC (permalink / raw)
Cc: mie, Kishon Vijay Abraham I, Lorenzo Pieralisi, Bjorn Helgaas,
linux-pci, linux-kernel
All of entries are freed in a loop, however, the freed entry is accessed
by list_del() after the loop.
Fixes: ef1433f ("PCI: endpoint: Create configfs entry for each pci_epf_device_id table entry")
Signed-off-by: Shunsuke Mie <mie@igel.co.jp>
---
drivers/pci/endpoint/pci-epf-core.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/drivers/pci/endpoint/pci-epf-core.c b/drivers/pci/endpoint/pci-epf-core.c
index e9289d10f822..538e902b0ba6 100644
--- a/drivers/pci/endpoint/pci-epf-core.c
+++ b/drivers/pci/endpoint/pci-epf-core.c
@@ -202,8 +202,10 @@ static void pci_epf_remove_cfs(struct pci_epf_driver *driver)
return;
mutex_lock(&pci_epf_mutex);
- list_for_each_entry_safe(group, tmp, &driver->epf_group, group_entry)
+ list_for_each_entry_safe(group, tmp, &driver->epf_group, group_entry) {
+ list_del(&group->group_entry);
pci_ep_cfs_remove_epf_group(group);
+ }
list_del(&driver->epf_group);
mutex_unlock(&pci_epf_mutex);
}
--
2.17.1
^ permalink raw reply related [flat|nested] 7+ messages in thread
end of thread, other threads:[~2021-12-10 11:09 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-06-21 7:00 [PATCH] PCI: endpoint: Fix use after free in pci_epf_remove_cfs() Shunsuke Mie
2021-06-21 8:39 ` Greg KH
2021-12-06 11:45 ` Lorenzo Pieralisi
2021-12-07 7:26 ` Kishon Vijay Abraham I
2021-12-07 7:44 ` Shunsuke Mie
2021-12-10 11:09 ` Shunsuke Mie
-- strict thread matches above, loose matches on Subject: below --
2021-06-11 3:50 Shunsuke Mie
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.