crypto: atmel-sha204a - Fix potential UAF and memory leak in remove path
PATCH v2 1/1 crypto: atmel-sha204a - Fix potential UAF and memory leak in remove path
Unregister the hwrng to prevent new ->read() calls and flush the Atmel I2C workqueue before teardown to prevent a potential UAF if a queued callback runs while the device is being removed.
Drop the early return to ensure sysfs entries are removed and ->hwrng.priv is freed, preventing a memory leak.
---
Changes in v2:
- Unregister hwrng to avoid new ->read() calls and then flush the queue
- Drop the ->tfm_count check and error logging after flushing (Herbert)
- Link to v1: https://lore.kernel.org/lkml/[email protected]/
--- drivers/crypto/atmel-sha204a.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)
drivers/crypto/atmel-sha204a.c +2 -4
@@ -191,10 +191,8 @@ static void atmel_sha204a_remove(struct i2c_client *client) {struct atmel_i2c_client_priv *i2c_priv =i2c_get_clientdata (client); -if (atomic_read (&i2c_priv->tfm_count)) { -dev_emerg (&client->dev,"Device is busy, will remove it anyhow\n" ); -return ; - } +devm_hwrng_unregister (&client->dev, &i2c_priv->hwrng); +atmel_i2c_flush_queue ();sysfs_remove_group (&client->dev.kobj, &atmel_sha204a_groups);
Herbert Xu
maintainerQuoted 6 lines · Thorsten Blum
drivers/crypto/atmel-sha204a.c +0
-0
drivers/crypto/atmel-sha204a.c static void atmel_sha204a_remove(struct i2c_client *client) · line 194
Quoted 8 lines
drivers/crypto/atmel-sha204a.c +1
-4@@ -191,10 +191,8 @@ static void atmel_sha204a_remove(struct i2c_client *client) {struct atmel_i2c_client_priv *i2c_priv =i2c_get_clientdata (client); -if (atomic_read (&i2c_priv->tfm_count)) { -dev_emerg (&client->dev,"Device is busy, will remove it anyhow\n" ); -return ; - } +devm_hwrng_unregister (&client->dev, &i2c_priv->hwrng);
Is it OK to explicitly call devm_hwrng_unregister?
Perhaps it's best to remove the devm management?
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
Quoted 18 lines · Herbert Xu
Quoted 6 lines · Thorsten Blum
drivers/crypto/atmel-sha204a.c +0
-0drivers/crypto/atmel-sha204a.c static void atmel_sha204a_remove(struct i2c_client *client) · line 194
Quoted 8 lines
drivers/crypto/atmel-sha204a.c +1
-4@@ -191,10 +191,8 @@ static void atmel_sha204a_remove(struct i2c_client *client) {struct atmel_i2c_client_priv *i2c_priv =i2c_get_clientdata (client); -if (atomic_read (&i2c_priv->tfm_count)) { -dev_emerg (&client->dev,"Device is busy, will remove it anyhow\n" ); -return ; - } +devm_hwrng_unregister (&client->dev, &i2c_priv->hwrng);Is it OK to explicitly call
devm_hwrng_unregister?
Quoted 1 line
Perhaps it's best to remove the devm management?
Yes, it should be safe since explicitly unregistering the hwrng removes the devres entry, and the automatic devm cleanup later essentially becomes a no-op.
Switching to hwrng_{register,unregister} is more explicit, but it would require a bool hwrng_registered field in atmel_i2c_client_priv to keep track of RNG registration success/failure.
Which approach do you prefer? Perhaps the smaller devm change for backporting, and then switching to hwrng_{register,unregister} in a separate cleanup patch?
Herbert Xu
maintainerQuoted 4 lines · Thorsten Blum
Yes, it should be safe since explicitly unregistering the hwrng removes the devres entry, and the automatic devm cleanup later essentially becomes a no-op.
If it's safe then let's just keep it this way.
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
Herbert Xu
maintainerQuoted 10 lines · Thorsten Blum
Unregister the hwrng to prevent new ->read() calls and flush the Atmel I2C workqueue before teardown to prevent a potential UAF if a queued callback runs while the device is being removed.
Drop the early return to ensure sysfs entries are removed and ->hwrng.priv is freed, preventing a memory leak.
Fixes da001fb651b0 ("crypto: atmel-i2c - add support for SHA204A random number generator")Signed-off-by Thorsten Blum <[email protected]>
Quoted 8 lines
---
Changes in v2:
- Unregister hwrng to avoid new ->read() calls and then flush the queue
- Drop the ->tfm_count check and error logging after flushing (Herbert)
- Link to v1: https://lore.kernel.org/lkml/[email protected]/--- drivers/crypto/atmel-sha204a.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)
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