Skip to contentSkip to conversation

crypto/ccp: Fix use-after-free on error path

PATCH In review Alper Ak 3 participants 3 messages Feb 9 – Feb 28

Subsystems (3)
lore
Share & tools
lore

Alper Ak

author
#0 lore raw

PATCH 1/1 crypto/ccp: Fix use-after-free on error path

In the error path of sev_tsm_init_locked(), the code dereferences 't' after it has been freed with kfree(). The pr_err() statement attempts to access t->tio_en and t->tio_init_done after the memory has been released.

Move the pr_err() call before kfree(t) to access the fields while the memory is still valid.

This issue reported by Smatch static analyser

Fixes 4be423572da1 ("crypto/ccp: Implement SEV-TIO PCIe IDE (phase1)")
---
 drivers/crypto/ccp/sev-dev-tsm.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/crypto/ccp/sev-dev-tsm.c +1 -1
index 3cdc38e84500..e0d2e3dd063d 100644
@@ -378,9 +378,9 @@ void sev_tsm_init_locked(struct sev_device *sev, void *tio_status_page)
 	return;
 
 error_exit:
-	kfree(t);
 	pr_err("Failed to enable SEV-TIO: ret=%d en=%d initdone=%d SEV=%d\n",
 	       ret, t->tio_en, t->tio_init_done, boot_cpu_has(X86_FEATURE_SEV));
+	kfree(t);
 }
 
 void sev_tsm_uninit(struct sev_device *sev)
Signature · 1 line
2.43.0
maintainer Acked-by on v1 1/1
#1 lore raw
Quoted 5 lines · Alper Ak

In the error path of sev_tsm_init_locked(), the code dereferences 't' after it has been freed with kfree(). The pr_err() statement attempts to access t->tio_en and t->tio_init_done after the memory has been released.

Quoted 7 lines

Move the pr_err() call before kfree(t) to access the fields while the memory is still valid.

This issue reported by Smatch static analyser

Fixes 4be423572da1 ("crypto/ccp: Implement SEV-TIO PCIe IDE (phase1)")
Quoted 19 lines
---
 drivers/crypto/ccp/sev-dev-tsm.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/crypto/ccp/sev-dev-tsm.c +1 -1
index 3cdc38e84500..e0d2e3dd063d 100644
@@ -378,9 +378,9 @@ void sev_tsm_init_locked(struct sev_device *sev, void *tio_status_page)
 	return;
 
 error_exit:
-	kfree(t);
 	pr_err("Failed to enable SEV-TIO: ret=%d en=%d initdone=%d SEV=%d\n",
 	       ret, t->tio_en, t->tio_init_done, boot_cpu_has(X86_FEATURE_SEV));
+	kfree(t);
 }
 
 void sev_tsm_uninit(struct sev_device *sev)
maintainer
#2 lore raw
In reply to Alper Ak
Quoted 8 lines · Alper Ak

In the error path of sev_tsm_init_locked(), the code dereferences 't' after it has been freed with kfree(). The pr_err() statement attempts to access t->tio_en and t->tio_init_done after the memory has been released.

Move the pr_err() call before kfree(t) to access the fields while the memory is still valid.

Quoted 7 lines

This issue reported by Smatch static analyser

Fixes 4be423572da1 ("crypto/ccp: Implement SEV-TIO PCIe IDE (phase1)")
---
 drivers/crypto/ccp/sev-dev-tsm.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Patch 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