ACPI: processor: Fix a possible null-pointer dereference in acpi_processor_errata_piix4() when debug messages are enabled
PATCH v3 1/1 ACPI: processor: Fix a possible null-pointer dereference in acpi_processor_errata_piix4() when debug messages are enabled
In acpi_processor_errata_piix4(), the pointer dev is first assigned an IDE device and then reassigned an ISA device:
dev = pci_get_subsys(..., PCI_DEVICE_ID_INTEL_82371AB, ...); dev = pci_get_subsys(..., PCI_DEVICE_ID_INTEL_82371AB_0, ...);
If the first lookup succeeds but the second fails, dev becomes NULL. This leads to a potential null-pointer dereference when dev_dbg() is called:
if (errata.piix4.bmisx)
dev_dbg(&dev->dev, ...);To prevent this, use two temporary pointers and retrieve each device independently, avoiding overwriting dev with a possible NULL value.
---
v3:
* Initialize the new variables to NULL and drop redundant checks. Thanks Rafael J. Wysocki for helpful advice.
v2:
* Add checks for ide_dev and isa_dev before dev_dbg() Thanks Rafael J. Wysocki for helpful advice.
--- drivers/acpi/acpi_processor.c | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-)
drivers/acpi/acpi_processor.c +14 -13
@@ -50,6 +50,7 @@ static int acpi_processor_errata_piix4(struct pci_dev *dev) {u8 value1 =0 ;u8 value2 =0 ; +struct pci_dev *ide_dev =NULL , *isa_dev =NULL ;if (!dev)
@@ -107,12 +108,12 @@ static int acpi_processor_errata_piix4(struct pci_dev *dev)* each IDE controller's DMA status to make sure we catch all * DMA activity. */ - dev =pci_get_subsys (PCI_VENDOR_ID_INTEL , + ide_dev =pci_get_subsys (PCI_VENDOR_ID_INTEL ,PCI_DEVICE_ID_INTEL_82371AB ,PCI_ANY_ID ,PCI_ANY_ID ,NULL ); -if (dev) { - errata.piix4.bmisx =pci_resource_start (dev,4 ); -pci_dev_put (dev); +if (ide_dev) { + errata.piix4.bmisx =pci_resource_start (ide_dev,4 ); +pci_dev_put (ide_dev); }/*
@@ -124,24 +125,24 @@ static int acpi_processor_errata_piix4(struct pci_dev *dev)* disable C3 support if this is enabled, as some legacy * devices won't operate well if fast DMA is disabled. */ - dev =pci_get_subsys (PCI_VENDOR_ID_INTEL , + isa_dev =pci_get_subsys (PCI_VENDOR_ID_INTEL ,PCI_DEVICE_ID_INTEL_82371AB_0 ,PCI_ANY_ID ,PCI_ANY_ID ,NULL ); -if (dev) { -pci_read_config_byte (dev,0x76 , &value1); -pci_read_config_byte (dev,0x77 , &value2); +if (isa_dev) { +pci_read_config_byte (isa_dev,0x76 , &value1); +pci_read_config_byte (isa_dev,0x77 , &value2);if ((value1 &0x80 ) || (value2 &0x80 )) errata.piix4.fdma =1 ; -pci_dev_put (dev); +pci_dev_put (isa_dev); }break ; } -if (errata.piix4.bmisx) -dev_dbg (&dev->dev,"Bus master activity detection (BM-IDE) erratum enabled\n" ); -if (errata.piix4.fdma) -dev_dbg (&dev->dev,"Type-F DMA livelock erratum (C3 disabled)\n" ); +if (ide_dev) +dev_dbg (&ide_dev->dev,"Bus master activity detection (BM-IDE) erratum enabled\n" ); +if (isa_dev) +dev_dbg (&isa_dev->dev,"Type-F DMA livelock erratum (C3 disabled)\n" );return 0 ; }
Signature · 1 line
2.43.0
Quoted 83 lines · Tuo Li
In
acpi_processor_errata_piix4(), the pointer dev is first assigned an IDE device and then reassigned an ISA device:dev = pci_get_subsys(..., PCI_DEVICE_ID_INTEL_82371AB, ...); dev = pci_get_subsys(..., PCI_DEVICE_ID_INTEL_82371AB_0, ...);If the first lookup succeeds but the second fails, dev becomes NULL. This leads to a potential null-pointer dereference when
dev_dbg()is called:if (errata.piix4.bmisx) dev_dbg(&dev->dev, ...);To prevent this, use two temporary pointers and retrieve each device independently, avoiding overwriting dev with a possible NULL value.
Signed-off-by Tuo Li <[email protected]>---
v3:
* Initialize the new variables to NULL and drop redundant checks. Thanks Rafael J. Wysocki for helpful advice.
v2:
* Add checks foride_devandisa_devbeforedev_dbg()Thanks Rafael J. Wysocki for helpful advice.--- drivers/acpi/acpi_processor.c | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-)drivers/acpi/acpi_processor.c +10
-11@@ -50,6 +50,7 @@ static int acpi_processor_errata_piix4(struct pci_dev *dev) {u8 value1 =0 ;u8 value2 =0 ; +struct pci_dev *ide_dev =NULL , *isa_dev =NULL ;if (!dev)@@ -107,12 +108,12 @@ static int acpi_processor_errata_piix4(struct pci_dev *dev)* each IDE controller's DMA status to make sure we catch all * DMA activity. */ - dev =pci_get_subsys (PCI_VENDOR_ID_INTEL , + ide_dev =pci_get_subsys (PCI_VENDOR_ID_INTEL ,PCI_DEVICE_ID_INTEL_82371AB ,PCI_ANY_ID ,PCI_ANY_ID ,NULL ); -if (dev) { - errata.piix4.bmisx =pci_resource_start (dev,4 ); -pci_dev_put (dev); +if (ide_dev) { + errata.piix4.bmisx =pci_resource_start (ide_dev,4 ); +pci_dev_put (ide_dev); }/* @@ -124,24 +125,24 @@ static int acpi_processor_errata_piix4(struct pci_dev *dev)* disable C3 support if this is enabled, as some legacy * devices won't operate well if fast DMA is disabled. */ - dev =pci_get_subsys (PCI_VENDOR_ID_INTEL , + isa_dev =pci_get_subsys (PCI_VENDOR_ID_INTEL ,PCI_DEVICE_ID_INTEL_82371AB_0 ,PCI_ANY_ID ,PCI_ANY_ID ,NULL ); -if (dev) { -pci_read_config_byte (dev,0x76 , &value1); -pci_read_config_byte (dev,0x77 , &value2); +if (isa_dev) { +pci_read_config_byte (isa_dev,0x76 , &value1); +pci_read_config_byte (isa_dev,0x77 , &value2);if ((value1 &0x80 ) || (value2 &0x80 )) errata.piix4.fdma =1 ; -pci_dev_put (dev); +pci_dev_put (isa_dev); }break ; } -if (errata.piix4.bmisx) -dev_dbg (&dev->dev,"Bus master activity detection (BM-IDE) erratum enabled\n" );
drivers/acpi/acpi_processor.c static int acpi_processor_errata_piix4(struct pci_dev *dev)
Quoted 10 lines
drivers/acpi/acpi_processor.c +4
-3-if (errata.piix4.fdma) -dev_dbg (&dev->dev,"Type-F DMA livelock erratum (C3 disabled)\n" ); +if (ide_dev) +dev_dbg (&ide_dev->dev,"Bus master activity detection (BM-IDE) erratum enabled\n" ); +if (isa_dev) +dev_dbg (&isa_dev->dev,"Type-F DMA livelock erratum (C3 disabled)\n" );return 0 ; } --
Applied as 6.20 material, thanks!
Hi,
Quoted 9 lines · Tuo Li
In
acpi_processor_errata_piix4(), the pointer dev is first assigned an IDE device and then reassigned an ISA device:dev = pci_get_subsys(..., PCI_DEVICE_ID_INTEL_82371AB, ...); dev = pci_get_subsys(..., PCI_DEVICE_ID_INTEL_82371AB_0, ...);If the first lookup succeeds but the second fails, dev becomes NULL. This leads to a potential null-pointer dereference when
dev_dbg()is called:
Quoted 8 lines
if (errata.piix4.bmisx) dev_dbg(&dev->dev, ...);To prevent this, use two temporary pointers and retrieve each device independently, avoiding overwriting dev with a possible NULL value.
Signed-off-by Tuo Li <[email protected]>
AI review feedback inline below.
I don't know if the device lifetime issue is real, but the now unconditional debug message is definitely a functional change and potentially misleading.
Either case, why not just move the debug messages into the code setting errata.piix4.bmisx and errata.piix4.fdma ?
Thanks,
Guenter
Quoted 59 lines
---
v3:
* Initialize the new variables to NULL and drop redundant checks. Thanks Rafael J. Wysocki for helpful advice.
v2:
* Add checks foride_devandisa_devbeforedev_dbg()Thanks Rafael J. Wysocki for helpful advice.--- drivers/acpi/acpi_processor.c | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-)drivers/acpi/acpi_processor.c +10
-9@@ -50,6 +50,7 @@ static int acpi_processor_errata_piix4(struct pci_dev *dev) {u8 value1 =0 ;u8 value2 =0 ; +struct pci_dev *ide_dev =NULL , *isa_dev =NULL ;if (!dev)@@ -107,12 +108,12 @@ static int acpi_processor_errata_piix4(struct pci_dev *dev)* each IDE controller's DMA status to make sure we catch all * DMA activity. */ - dev =pci_get_subsys (PCI_VENDOR_ID_INTEL , + ide_dev =pci_get_subsys (PCI_VENDOR_ID_INTEL ,PCI_DEVICE_ID_INTEL_82371AB ,PCI_ANY_ID ,PCI_ANY_ID ,NULL ); -if (dev) { - errata.piix4.bmisx =pci_resource_start (dev,4 ); -pci_dev_put (dev); +if (ide_dev) { + errata.piix4.bmisx =pci_resource_start (ide_dev,4 ); +pci_dev_put (ide_dev); }/* @@ -124,24 +125,24 @@ static int acpi_processor_errata_piix4(struct pci_dev *dev)* disable C3 support if this is enabled, as some legacy * devices won't operate well if fast DMA is disabled. */ - dev =pci_get_subsys (PCI_VENDOR_ID_INTEL , + isa_dev =pci_get_subsys (PCI_VENDOR_ID_INTEL ,PCI_DEVICE_ID_INTEL_82371AB_0 ,PCI_ANY_ID ,PCI_ANY_ID ,NULL ); -if (dev) { -pci_read_config_byte (dev,0x76 , &value1); -pci_read_config_byte (dev,0x77 , &value2); +if (isa_dev) { +pci_read_config_byte (isa_dev,0x76 , &value1); +pci_read_config_byte (isa_dev,0x77 , &value2);if ((value1 &0x80 ) || (value2 &0x80 )) errata.piix4.fdma =1 ; -pci_dev_put (dev); +pci_dev_put (isa_dev); }
drivers/acpi/acpi_processor.c static int acpi_processor_errata_piix4(struct pci_dev *dev) · line 143
Quoted 10 lines
drivers/acpi/acpi_processor.c +2
-4break ; } -if (errata.piix4.bmisx) -dev_dbg (&dev->dev,"Bus master activity detection (BM-IDE) erratum enabled\n" ); -if (errata.piix4.fdma) -dev_dbg (&dev->dev,"Type-F DMA livelock erratum (C3 disabled)\n" ); +if (ide_dev) +dev_dbg (&ide_dev->dev,"Bus master activity detection (BM-IDE) erratum enabled\n" );
Is it safe to dereference ide_dev here? It looks like pci_dev_put(ide_dev) was already called above, which drops the reference and might lead to a use-after-free.
Also, does this change the logging behavior? The original code checked errata.piix4.bmisx before logging, but now it logs as long as ide_dev was found, even if the erratum is not actually enabled.
drivers/acpi/acpi_processor.c static int acpi_processor_errata_piix4(struct pci_dev *dev) · line 145
Quoted 2 lines
drivers/acpi/acpi_processor.c +2
-0+if (isa_dev) +dev_dbg (&isa_dev->dev,"Type-F DMA livelock erratum (C3 disabled)\n" );
Similarly, could this cause a use-after-free on isa_dev after pci_dev_put(isa_dev) was called?
Additionally, does this unconditionally print the "Type-F DMA livelock" message if the ISA controller is present, skipping the original check for errata.piix4.fdma?
Since the original dev parameter is no longer reassigned, would it be better to keep using &dev->dev and the original errata checks here to log against the original PIIX4 ACPI controller?
Quoted 6 lines
drivers/acpi/acpi_processor.c +0
-0return 0 ; }Signature · 1 line
2.43.0
Rafael J. Wysocki
maintainerPATCH 1/1 ACPI: processor: Fix previous acpi_processor_errata_piix4() fix
Quoted 106 lines · CET Guenter Roeck
Hi,
Quoted 9 lines · Tuo Li
In
acpi_processor_errata_piix4(), the pointer dev is first assigned an IDE device and then reassigned an ISA device:dev = pci_get_subsys(..., PCI_DEVICE_ID_INTEL_82371AB, ...); dev = pci_get_subsys(..., PCI_DEVICE_ID_INTEL_82371AB_0, ...);If the first lookup succeeds but the second fails, dev becomes NULL. This leads to a potential null-pointer dereference when
dev_dbg()is called:Quoted 8 lines
if (errata.piix4.bmisx) dev_dbg(&dev->dev, ...);To prevent this, use two temporary pointers and retrieve each device independently, avoiding overwriting dev with a possible NULL value.
Signed-off-by Tuo Li <[email protected]>AI review feedback inline below.
I don't know if the device lifetime issue is real, but the now unconditional debug message is definitely a functional change and potentially misleading.
Either case, why not just move the debug messages into the code setting errata.piix4.bmisx and errata.piix4.fdma ?
Thanks,
GuenterQuoted 59 lines
---
v3:
* Initialize the new variables to NULL and drop redundant checks. Thanks Rafael J. Wysocki for helpful advice.
v2:
* Add checks foride_devandisa_devbeforedev_dbg()Thanks Rafael J. Wysocki for helpful advice.--- drivers/acpi/acpi_processor.c | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-)drivers/acpi/acpi_processor.c +10
-9@@ -50,6 +50,7 @@ static int acpi_processor_errata_piix4(struct pci_dev *dev) {u8 value1 =0 ;u8 value2 =0 ; +struct pci_dev *ide_dev =NULL , *isa_dev =NULL ;if (!dev)@@ -107,12 +108,12 @@ static int acpi_processor_errata_piix4(struct pci_dev *dev)* each IDE controller's DMA status to make sure we catch all * DMA activity. */ - dev =pci_get_subsys (PCI_VENDOR_ID_INTEL , + ide_dev =pci_get_subsys (PCI_VENDOR_ID_INTEL ,PCI_DEVICE_ID_INTEL_82371AB ,PCI_ANY_ID ,PCI_ANY_ID ,NULL ); -if (dev) { - errata.piix4.bmisx =pci_resource_start (dev,4 ); -pci_dev_put (dev); +if (ide_dev) { + errata.piix4.bmisx =pci_resource_start (ide_dev,4 ); +pci_dev_put (ide_dev); }/* @@ -124,24 +125,24 @@ static int acpi_processor_errata_piix4(struct pci_dev *dev)* disable C3 support if this is enabled, as some legacy * devices won't operate well if fast DMA is disabled. */ - dev =pci_get_subsys (PCI_VENDOR_ID_INTEL , + isa_dev =pci_get_subsys (PCI_VENDOR_ID_INTEL ,PCI_DEVICE_ID_INTEL_82371AB_0 ,PCI_ANY_ID ,PCI_ANY_ID ,NULL ); -if (dev) { -pci_read_config_byte (dev,0x76 , &value1); -pci_read_config_byte (dev,0x77 , &value2); +if (isa_dev) { +pci_read_config_byte (isa_dev,0x76 , &value1); +pci_read_config_byte (isa_dev,0x77 , &value2);if ((value1 &0x80 ) || (value2 &0x80 )) errata.piix4.fdma =1 ; -pci_dev_put (dev); +pci_dev_put (isa_dev); }drivers/acpi/acpi_processor.c static int acpi_processor_errata_piix4(struct pci_dev *dev) · line 143
Quoted 10 lines
drivers/acpi/acpi_processor.c +2
-4break ; } -if (errata.piix4.bmisx) -dev_dbg (&dev->dev,"Bus master activity detection (BM-IDE) erratum enabled\n" ); -if (errata.piix4.fdma) -dev_dbg (&dev->dev,"Type-F DMA livelock erratum (C3 disabled)\n" ); +if (ide_dev) +dev_dbg (&ide_dev->dev,"Bus master activity detection (BM-IDE) erratum enabled\n" );Is it safe to dereference
ide_devhere? It looks likepci_dev_put(ide_dev)was already called above, which drops the reference and might lead to a use-after-free.
Quoted 3 lines
Also, does this change the logging behavior? The original code checked errata.piix4.bmisx before logging, but now it logs as long as
ide_devwas found, even if the erratum is not actually enabled.
Well, it has a point.
The patch below should fix this.
---
From: Rafael J. Wysocki <[email protected]>
Subject: [PATCH v1] ACPI: processor: Fix previous acpi_processor_errata_piix4() fix
After commi f132e089fe89 ("ACPI: processor: Fix NULL-pointer dereference in acpi_processor_errata_piix4()"), device pointers may be dereferenced after dropping references to the device objects pointed to by them, which may cause a use-after-free to occur.
Moreover, debug messages about enabling the errata may be printed if the errata flags corresponding to them are unset.
Address all of these issues by moving message printing to the points in the code where the errata flags are set.
--- drivers/acpi/acpi_processor.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-)
drivers/acpi/acpi_processor.c +8 -7
@@ -113,6 +113,10 @@ static int acpi_processor_errata_piix4(s PCI_ANY_ID, PCI_ANY_ID, NULL);if (ide_dev) { errata.piix4.bmisx =pci_resource_start (ide_dev,4 ); +if (errata.piix4.bmisx) +dev_dbg (&ide_dev->dev, +"Bus master activity detection (BM-IDE) erratum enabled\n" ); +pci_dev_put (ide_dev); }
@@ -131,20 +135,17 @@ static int acpi_processor_errata_piix4(sif (isa_dev) {pci_read_config_byte (isa_dev,0x76 , &value1);pci_read_config_byte (isa_dev,0x77 , &value2); -if ((value1 &0x80 ) || (value2 &0x80 )) +if ((value1 &0x80 ) || (value2 &0x80 )) { errata.piix4.fdma =1 ; +dev_dbg (&isa_dev->dev, +"Type-F DMA livelock erratum (C3 disabled)\n" ); + }pci_dev_put (isa_dev); }break ; } -if (ide_dev) -dev_dbg (&ide_dev->dev,"Bus master activity detection (BM-IDE) erratum enabled\n" ); - -if (isa_dev) -dev_dbg (&isa_dev->dev,"Type-F DMA livelock erratum (C3 disabled)\n" ); -return 0 ; }
Hi,
Quoted 117 lines · Guenter Roeck
Hi,
Quoted 9 lines · Tuo Li
In
acpi_processor_errata_piix4(), the pointer dev is first assigned an IDE device and then reassigned an ISA device:dev = pci_get_subsys(..., PCI_DEVICE_ID_INTEL_82371AB, ...); dev = pci_get_subsys(..., PCI_DEVICE_ID_INTEL_82371AB_0, ...);If the first lookup succeeds but the second fails, dev becomes NULL. This leads to a potential null-pointer dereference when
dev_dbg()is called:Quoted 8 lines
if (errata.piix4.bmisx) dev_dbg(&dev->dev, ...);To prevent this, use two temporary pointers and retrieve each device independently, avoiding overwriting dev with a possible NULL value.
Signed-off-by Tuo Li <[email protected]>AI review feedback inline below.
I don't know if the device lifetime issue is real, but the now unconditional debug message is definitely a functional change and potentially misleading.
Either case, why not just move the debug messages into the code setting errata.piix4.bmisx and errata.piix4.fdma ?
Thanks,
GuenterQuoted 59 lines
---
v3:
* Initialize the new variables to NULL and drop redundant checks. Thanks Rafael J. Wysocki for helpful advice.
v2:
* Add checks foride_devandisa_devbeforedev_dbg()Thanks Rafael J. Wysocki for helpful advice.--- drivers/acpi/acpi_processor.c | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-)drivers/acpi/acpi_processor.c +10
-9@@ -50,6 +50,7 @@ static int acpi_processor_errata_piix4(struct pci_dev *dev) {u8 value1 =0 ;u8 value2 =0 ; +struct pci_dev *ide_dev =NULL , *isa_dev =NULL ;if (!dev)@@ -107,12 +108,12 @@ static int acpi_processor_errata_piix4(struct pci_dev *dev)* each IDE controller's DMA status to make sure we catch all * DMA activity. */ - dev =pci_get_subsys (PCI_VENDOR_ID_INTEL , + ide_dev =pci_get_subsys (PCI_VENDOR_ID_INTEL ,PCI_DEVICE_ID_INTEL_82371AB ,PCI_ANY_ID ,PCI_ANY_ID ,NULL ); -if (dev) { - errata.piix4.bmisx =pci_resource_start (dev,4 ); -pci_dev_put (dev); +if (ide_dev) { + errata.piix4.bmisx =pci_resource_start (ide_dev,4 ); +pci_dev_put (ide_dev); }/* @@ -124,24 +125,24 @@ static int acpi_processor_errata_piix4(struct pci_dev *dev)* disable C3 support if this is enabled, as some legacy * devices won't operate well if fast DMA is disabled. */ - dev =pci_get_subsys (PCI_VENDOR_ID_INTEL , + isa_dev =pci_get_subsys (PCI_VENDOR_ID_INTEL ,PCI_DEVICE_ID_INTEL_82371AB_0 ,PCI_ANY_ID ,PCI_ANY_ID ,NULL ); -if (dev) { -pci_read_config_byte (dev,0x76 , &value1); -pci_read_config_byte (dev,0x77 , &value2); +if (isa_dev) { +pci_read_config_byte (isa_dev,0x76 , &value1); +pci_read_config_byte (isa_dev,0x77 , &value2);if ((value1 &0x80 ) || (value2 &0x80 )) errata.piix4.fdma =1 ; -pci_dev_put (dev); +pci_dev_put (isa_dev); }drivers/acpi/acpi_processor.c static int acpi_processor_errata_piix4(struct pci_dev *dev) · line 143
Quoted 10 lines
drivers/acpi/acpi_processor.c +2
-4break ; } -if (errata.piix4.bmisx) -dev_dbg (&dev->dev,"Bus master activity detection (BM-IDE) erratum enabled\n" ); -if (errata.piix4.fdma) -dev_dbg (&dev->dev,"Type-F DMA livelock erratum (C3 disabled)\n" ); +if (ide_dev) +dev_dbg (&ide_dev->dev,"Bus master activity detection (BM-IDE) erratum enabled\n" );Is it safe to dereference
ide_devhere? It looks likepci_dev_put(ide_dev)was already called above, which drops the reference and might lead to a use-after-free.Also, does this change the logging behavior? The original code checked errata.piix4.bmisx before logging, but now it logs as long as
ide_devwas found, even if the erratum is not actually enabled.drivers/acpi/acpi_processor.c static int acpi_processor_errata_piix4(struct pci_dev *dev) · line 145
Quoted 2 lines
drivers/acpi/acpi_processor.c +2
-0+if (isa_dev) +dev_dbg (&isa_dev->dev,"Type-F DMA livelock erratum (C3 disabled)\n" );Similarly, could this cause a use-after-free on
isa_devafterpci_dev_put(isa_dev)was called?
Quoted 14 lines
Additionally, does this unconditionally print the "Type-F DMA livelock" message if the ISA controller is present, skipping the original check for errata.piix4.fdma?
Since the original
devparameter is no longer reassigned, would it be better to keep using&dev->devand the original errata checks here to log against the original PIIX4 ACPI controller?Quoted 6 lines
drivers/acpi/acpi_processor.c +0
-1return 0 ; } --2.43.0
Thanks for pointing this out, and I am sorry for the oversight.
I think these issues are being fixed in a follow-up patch:
https://lore.kernel.org/[email protected]/
Thanks,
Tuo
Re: [PATCH v1] ACPI: processor: Fix previous acpi_processor_errata_piix4() fix
In reply to Rafael J. WysockiQuoted 127 lines · Rafael J. Wysocki
Quoted 106 lines · CET Guenter Roeck
Hi,
Quoted 9 lines · Tuo Li
In
acpi_processor_errata_piix4(), the pointer dev is first assigned an IDE device and then reassigned an ISA device:dev = pci_get_subsys(..., PCI_DEVICE_ID_INTEL_82371AB, ...); dev = pci_get_subsys(..., PCI_DEVICE_ID_INTEL_82371AB_0, ...);If the first lookup succeeds but the second fails, dev becomes NULL. This leads to a potential null-pointer dereference when
dev_dbg()is called:Quoted 8 lines
if (errata.piix4.bmisx) dev_dbg(&dev->dev, ...);To prevent this, use two temporary pointers and retrieve each device independently, avoiding overwriting dev with a possible NULL value.
Signed-off-by Tuo Li <[email protected]>AI review feedback inline below.
I don't know if the device lifetime issue is real, but the now unconditional debug message is definitely a functional change and potentially misleading.
Either case, why not just move the debug messages into the code setting errata.piix4.bmisx and errata.piix4.fdma ?
Thanks,
GuenterQuoted 59 lines
---
v3:
* Initialize the new variables to NULL and drop redundant checks. Thanks Rafael J. Wysocki for helpful advice.
v2:
* Add checks foride_devandisa_devbeforedev_dbg()Thanks Rafael J. Wysocki for helpful advice.--- drivers/acpi/acpi_processor.c | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-)drivers/acpi/acpi_processor.c +10
-9@@ -50,6 +50,7 @@ static int acpi_processor_errata_piix4(struct pci_dev *dev) {u8 value1 =0 ;u8 value2 =0 ; +struct pci_dev *ide_dev =NULL , *isa_dev =NULL ;if (!dev)@@ -107,12 +108,12 @@ static int acpi_processor_errata_piix4(struct pci_dev *dev)* each IDE controller's DMA status to make sure we catch all * DMA activity. */ - dev =pci_get_subsys (PCI_VENDOR_ID_INTEL , + ide_dev =pci_get_subsys (PCI_VENDOR_ID_INTEL ,PCI_DEVICE_ID_INTEL_82371AB ,PCI_ANY_ID ,PCI_ANY_ID ,NULL ); -if (dev) { - errata.piix4.bmisx =pci_resource_start (dev,4 ); -pci_dev_put (dev); +if (ide_dev) { + errata.piix4.bmisx =pci_resource_start (ide_dev,4 ); +pci_dev_put (ide_dev); }/* @@ -124,24 +125,24 @@ static int acpi_processor_errata_piix4(struct pci_dev *dev)* disable C3 support if this is enabled, as some legacy * devices won't operate well if fast DMA is disabled. */ - dev =pci_get_subsys (PCI_VENDOR_ID_INTEL , + isa_dev =pci_get_subsys (PCI_VENDOR_ID_INTEL ,PCI_DEVICE_ID_INTEL_82371AB_0 ,PCI_ANY_ID ,PCI_ANY_ID ,NULL ); -if (dev) { -pci_read_config_byte (dev,0x76 , &value1); -pci_read_config_byte (dev,0x77 , &value2); +if (isa_dev) { +pci_read_config_byte (isa_dev,0x76 , &value1); +pci_read_config_byte (isa_dev,0x77 , &value2);if ((value1 &0x80 ) || (value2 &0x80 )) errata.piix4.fdma =1 ; -pci_dev_put (dev); +pci_dev_put (isa_dev); }drivers/acpi/acpi_processor.c static int acpi_processor_errata_piix4(struct pci_dev *dev) · line 143
Quoted 10 lines
drivers/acpi/acpi_processor.c +2
-4break ; } -if (errata.piix4.bmisx) -dev_dbg (&dev->dev,"Bus master activity detection (BM-IDE) erratum enabled\n" ); -if (errata.piix4.fdma) -dev_dbg (&dev->dev,"Type-F DMA livelock erratum (C3 disabled)\n" ); +if (ide_dev) +dev_dbg (&ide_dev->dev,"Bus master activity detection (BM-IDE) erratum enabled\n" );Is it safe to dereference
ide_devhere? It looks likepci_dev_put(ide_dev)was already called above, which drops the reference and might lead to a use-after-free.Quoted 3 lines
Also, does this change the logging behavior? The original code checked errata.piix4.bmisx before logging, but now it logs as long as
ide_devwas found, even if the erratum is not actually enabled.Well, it has a point.
The patch below should fix this.
---
From: Rafael J. Wysocki <[email protected]>
Subject: [PATCH v1] ACPI: processor: Fix previousacpi_processor_errata_piix4()fixAfter commi
f132e089fe89("ACPI: processor: Fix NULL-pointer dereference inacpi_processor_errata_piix4()"), device pointers may be dereferenced after dropping references to the device objects pointed to by them, which may cause a use-after-free to occur.Moreover, debug messages about enabling the errata may be printed if the errata flags corresponding to them are unset.
Quoted 7 lines
Address all of these issues by moving message printing to the points in the code where the errata flags are set.
Fixes f132e089fe89 ("ACPI: processor: Fix NULL-pointer dereference in acpi_processor_errata_piix4()")Reported-by Guenter Roeck <[email protected]>Signed-off-by Rafael J. Wysocki <[email protected]>
Quoted 45 lines
--- drivers/acpi/acpi_processor.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-)drivers/acpi/acpi_processor.c +8
-7@@ -113,6 +113,10 @@ static int acpi_processor_errata_piix4(s PCI_ANY_ID, PCI_ANY_ID, NULL);if (ide_dev) { errata.piix4.bmisx =pci_resource_start (ide_dev,4 ); +if (errata.piix4.bmisx) +dev_dbg (&ide_dev->dev, +"Bus master activity detection (BM-IDE) erratum enabled\n" ); +pci_dev_put (ide_dev); }@@ -131,20 +135,17 @@ static int acpi_processor_errata_piix4(sif (isa_dev) {pci_read_config_byte (isa_dev,0x76 , &value1);pci_read_config_byte (isa_dev,0x77 , &value2); -if ((value1 &0x80 ) || (value2 &0x80 )) +if ((value1 &0x80 ) || (value2 &0x80 )) { errata.piix4.fdma =1 ; +dev_dbg (&isa_dev->dev, +"Type-F DMA livelock erratum (C3 disabled)\n" ); + }pci_dev_put (isa_dev); }break ; } -if (ide_dev) -dev_dbg (&ide_dev->dev,"Bus master activity detection (BM-IDE) erratum enabled\n" ); - -if (isa_dev) -dev_dbg (&isa_dev->dev,"Type-F DMA livelock erratum (C3 disabled)\n" ); -return 0 ; }