Skip to contentSkip to conversation

crypto/ccp: Properly NULL variables on sev exit

PATCH In review John Allen 3 participants 6 messages Mar 3, 2020 – Mar 12, 2020

Subsystems (3)
lore
Share & tools
lore

John Allen

author
#0 lore raw

cover crypto/ccp: Properly NULL variables on sev exit

Stale pointers left in static variables misc_dev and sp_dev_master will trigger use-after-free error when DEBUG_TEST_DRIVER_REMOVE is configured.

John Allen (2):
crypto/ccp: Cleanup misc_dev on sev exit crypto/ccp: Cleanup sp_dev_master on psp device destroy

 drivers/crypto/ccp/psp-dev.c | 3 +++
 drivers/crypto/ccp/sev-dev.c | 6 +++---
 drivers/crypto/ccp/sp-dev.h  | 1 +
 drivers/crypto/ccp/sp-pci.c  | 9 +++++++++
 4 files changed, 16 insertions(+), 3 deletions(-)
Signature · 1 line
2.18.2
author
#1 lore raw

PATCH 1/2 crypto/ccp: Cleanup misc_dev on sev_exit()

Explicitly free and clear misc_dev in sev_exit(). Since devm_kzalloc() associates misc_dev with the first device that gets probed, change from devm_kzalloc() to kzalloc() and explicitly free memory in sev_exit() as the first device probed is not guaranteed to be the last device released. To ensure that the variable gets properly set to NULL, remove the local definition of misc_dev.

Fixes 200664d5237f ("crypto: ccp: Add Secure Encrypted Virtualization (SEV) command support")
---
 drivers/crypto/ccp/sev-dev.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/crypto/ccp/sev-dev.c +3 -3
index e467860f797d..aa591dae067c 100644
@@ -896,9 +896,9 @@ EXPORT_SYMBOL_GPL(sev_guest_df_flush);
 
 static void sev_exit(struct kref *ref)
 {
-	struct sev_misc_dev *misc_dev = container_of(ref, struct sev_misc_dev, refcount);
-
 	misc_deregister(&misc_dev->misc);
+	kfree(misc_dev);
+	misc_dev = NULL;
 }
 
 static int sev_misc_init(struct sev_device *sev)
@@ -916,7 +916,7 @@ static int sev_misc_init(struct sev_device *sev)
 	if (!misc_dev) {
 		struct miscdevice *misc;
 
-		misc_dev = devm_kzalloc(dev, sizeof(*misc_dev), GFP_KERNEL);
+		misc_dev = kzalloc(sizeof(*misc_dev), GFP_KERNEL);
 		if (!misc_dev)
 			return -ENOMEM;
 
Signature · 1 line
2.18.2
maintainer Acked-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] crypto/ccp: Cleanup misc_dev on sev_exit()

Quoted 9 lines · John Allen

Explicitly free and clear misc_dev in sev_exit(). Since devm_kzalloc() associates misc_dev with the first device that gets probed, change from devm_kzalloc() to kzalloc() and explicitly free memory in sev_exit() as the first device probed is not guaranteed to be the last device released. To ensure that the variable gets properly set to NULL, remove the local definition of misc_dev.

Fixes 200664d5237f ("crypto: ccp: Add Secure Encrypted Virtualization (SEV) command support")
Quoted 30 lines
---
 drivers/crypto/ccp/sev-dev.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/crypto/ccp/sev-dev.c +3 -3
index e467860f797d..aa591dae067c 100644
@@ -896,9 +896,9 @@ EXPORT_SYMBOL_GPL(sev_guest_df_flush);
 
 static void sev_exit(struct kref *ref)
 {
-	struct sev_misc_dev *misc_dev = container_of(ref, struct sev_misc_dev, refcount);
-
 	misc_deregister(&misc_dev->misc);
+	kfree(misc_dev);
+	misc_dev = NULL;
 }
 
 static int sev_misc_init(struct sev_device *sev)
@@ -916,7 +916,7 @@ static int sev_misc_init(struct sev_device *sev)
 	if (!misc_dev) {
 		struct miscdevice *misc;
 
-		misc_dev = devm_kzalloc(dev, sizeof(*misc_dev), GFP_KERNEL);
+		misc_dev = kzalloc(sizeof(*misc_dev), GFP_KERNEL);
 		if (!misc_dev)
 			return -ENOMEM;
 
author
#3 lore raw

PATCH 2/2 crypto/ccp: Cleanup sp_dev_master in psp_dev_destroy()

In reply to John Allen

Introduce clear_psp_master_device() to ensure that sp_dev_master gets properly cleared on the release of a psp device.

Fixes 2a6170dfe755 ("crypto: ccp: Add Platform Security Processor (PSP) device support")
---
 drivers/crypto/ccp/psp-dev.c | 3 +++
 drivers/crypto/ccp/sp-dev.h  | 1 +
 drivers/crypto/ccp/sp-pci.c  | 9 +++++++++
 3 files changed, 13 insertions(+)
drivers/crypto/ccp/psp-dev.c +3 -0
index e95e7aa5dbf1..ae7b44599914 100644
@@ -215,6 +215,9 @@ void psp_dev_destroy(struct sp_device *sp)
 	tee_dev_destroy(psp);
 
 	sp_free_psp_irq(sp, psp);
+
+	if (sp->clear_psp_master_device)
+		sp->clear_psp_master_device(sp);
 }
 
 void psp_set_sev_irq_handler(struct psp_device *psp, psp_irq_handler_t handler,
drivers/crypto/ccp/sp-dev.h +1 -0
index 423594608ad1..f913f1494af9 100644
@@ -90,6 +90,7 @@ struct sp_device {
 	/* get and set master device */
 	struct sp_device*(*get_psp_master_device)(void);
 	void (*set_psp_master_device)(struct sp_device *);
+	void (*clear_psp_master_device)(struct sp_device *);
 
 	bool irq_registered;
 	bool use_tasklet;
drivers/crypto/ccp/sp-pci.c +9 -0
index 56c1f61c0f84..cb6cb47053f4 100644
@@ -146,6 +146,14 @@ static struct sp_device *psp_get_master(void)
 	return sp_dev_master;
 }
 
+static void psp_clear_master(struct sp_device *sp)
+{
+	if (sp == sp_dev_master) {
+		sp_dev_master = NULL;
+		dev_dbg(sp->dev, "Cleared sp_dev_master\n");
+	}
+}
+
 static int sp_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
 {
 	struct sp_device *sp;
@@ -206,6 +214,7 @@ static int sp_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
 	pci_set_master(pdev);
 	sp->set_psp_master_device = psp_set_master;
 	sp->get_psp_master_device = psp_get_master;
+	sp->clear_psp_master_device = psp_clear_master;
 
 	ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(48));
 	if (ret) {
Signature · 1 line
2.18.2
maintainer Acked-by on v1 2/2
#4 lore raw

Re: [PATCH 2/2] crypto/ccp: Cleanup sp_dev_master in psp_dev_destroy()

Quoted 5 lines · John Allen

Introduce clear_psp_master_device() to ensure that sp_dev_master gets properly cleared on the release of a psp device.

Fixes 2a6170dfe755 ("crypto: ccp: Add Platform Security Processor (PSP) device support")
Quoted 60 lines
---
 drivers/crypto/ccp/psp-dev.c | 3 +++
 drivers/crypto/ccp/sp-dev.h  | 1 +
 drivers/crypto/ccp/sp-pci.c  | 9 +++++++++
 3 files changed, 13 insertions(+)
drivers/crypto/ccp/psp-dev.c +3 -0
index e95e7aa5dbf1..ae7b44599914 100644
@@ -215,6 +215,9 @@ void psp_dev_destroy(struct sp_device *sp)
 	tee_dev_destroy(psp);
 
 	sp_free_psp_irq(sp, psp);
+
+	if (sp->clear_psp_master_device)
+		sp->clear_psp_master_device(sp);
 }
 
 void psp_set_sev_irq_handler(struct psp_device *psp, psp_irq_handler_t handler,
drivers/crypto/ccp/sp-dev.h +1 -0
index 423594608ad1..f913f1494af9 100644
@@ -90,6 +90,7 @@ struct sp_device {
 	/* get and set master device */
 	struct sp_device*(*get_psp_master_device)(void);
 	void (*set_psp_master_device)(struct sp_device *);
+	void (*clear_psp_master_device)(struct sp_device *);
 
 	bool irq_registered;
 	bool use_tasklet;
drivers/crypto/ccp/sp-pci.c +9 -0
index 56c1f61c0f84..cb6cb47053f4 100644
@@ -146,6 +146,14 @@ static struct sp_device *psp_get_master(void)
 	return sp_dev_master;
 }
 
+static void psp_clear_master(struct sp_device *sp)
+{
+	if (sp == sp_dev_master) {
+		sp_dev_master = NULL;
+		dev_dbg(sp->dev, "Cleared sp_dev_master\n");
+	}
+}
+
 static int sp_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
 {
 	struct sp_device *sp;
@@ -206,6 +214,7 @@ static int sp_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
 	pci_set_master(pdev);
 	sp->set_psp_master_device = psp_set_master;
 	sp->get_psp_master_device = psp_get_master;
+	sp->clear_psp_master_device = psp_clear_master;
 
 	ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(48));
 	if (ret) {
maintainer
#5 lore raw
In reply to John Allen
Quoted 7 lines · John Allen

Stale pointers left in static variables misc_dev and sp_dev_master will trigger use-after-free error when DEBUG_TEST_DRIVER_REMOVE is configured.

John Allen (2):
crypto/ccp: Cleanup misc_dev on sev exit crypto/ccp: Cleanup sp_dev_master on psp device destroy

Quoted 5 lines
 drivers/crypto/ccp/psp-dev.c | 3 +++
 drivers/crypto/ccp/sev-dev.c | 6 +++---
 drivers/crypto/ccp/sp-dev.h  | 1 +
 drivers/crypto/ccp/sp-pci.c  | 9 +++++++++
 4 files changed, 16 insertions(+), 3 deletions(-)

All applied. Thanks.

Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt