Skip to contentSkip to conversation

crypto: atmel-sha204a - fix heap info leak on I2C transfer failure

PATCH RESEND Superseded Lothar Rubusch 2 participants 4 messages Jun 9 – Jun 13

Subsystems (3)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH RESEND v2 1/1 crypto: atmel-sha204a - fix heap info leak on I2C transfer failure

The nonblocking RNG path allocates a work_data structure to track the state of an in-flight asynchronous I2C request. This pointer is stored in rng->priv and later consumed by the read path once the transaction completes.

If the underlying I2C transfer fails, the completion callback is invoked with a non-zero status. In this case, the allocated work_data is not usable for producing RNG output and must not remain associated with the hwrng state.

Previously, the failure path only logged a warning but left the pointer state uncleared, which can result in subsequent read attempts observing stale state and interpreting it as valid completion data.

Fix this by freeing the pending work_data and clearing rng->priv when the I2C transaction reports an error. This ensures that failed requests do not leave residual state behind that could be interpreted as valid RNG data on later reads.

The explicit clearing of rng->priv in the error path is retained as a defensive measure. While it may overlap with existing state handling in the read path, the ownership and lifecycle across asynchronous completion, read, and teardown paths is not fully localised. Clearing the pointer ensures no stale state remains after a failed transaction.

Fixes da001fb651b0 ("crypto: atmel-i2c - add support for SHA204A random number generator")
Assisted-by Gemini:1.5 Pro [google]
---
 drivers/crypto/atmel-sha204a.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/crypto/atmel-sha204a.c +6 -1
index 4c9af737b33a..20cd915ea8a3 100644
@@ -31,10 +31,15 @@ static void atmel_sha204a_rng_done(struct atmel_i2c_work_data *work_data,
 	struct atmel_i2c_client_priv *i2c_priv = work_data->ctx;
 	struct hwrng *rng = areq;
 
-	if (status)
+	if (status) {
 		dev_warn_ratelimited(&i2c_priv->client->dev,
 				     "i2c transaction failed (%d)\n",
 				     status);
+		kfree(work_data);
+		rng->priv = 0;
+		atomic_dec(&i2c_priv->tfm_count);
+		return;
+	}
 
 	rng->priv = (unsigned long)work_data;
 	atomic_dec(&i2c_priv->tfm_count);

base-commit: 79bbe453e5bfa6e1c6aa2e8329bfc8f152b81c9b

Signature · 1 line
2.53.0
maintainer
#1 lore raw
Quoted 6 lines · Lothar Rubusch
drivers/crypto/atmel-sha204a.c +0 -0
index 4c9af737b33a..20cd915ea8a3 100644

drivers/crypto/atmel-sha204a.c static void atmel_sha204a_rng_done(struct atmel_i2c_work_data *work_data, · line 39

Quoted 10 lines
drivers/crypto/atmel-sha204a.c +3 -1
@@ -31,10 +31,15 @@ static void atmel_sha204a_rng_done(struct atmel_i2c_work_data *work_data,
 	struct atmel_i2c_client_priv *i2c_priv = work_data->ctx;
 	struct hwrng *rng = areq;
 
-	if (status)
+	if (status) {
 		dev_warn_ratelimited(&i2c_priv->client->dev,
 				     "i2c transaction failed (%d)\n",
 				     status);
+		kfree(work_data);
+		rng->priv = 0;

Why is this necessary? It appears that rng_read_nonblocking already zeroes rng->priv.

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
#2 lore raw
Quoted 11 lines · Herbert Xu
Quoted 9 lines · Lothar Rubusch
drivers/crypto/atmel-sha204a.c +0 -0
index 4c9af737b33a..20cd915ea8a3 100644
@@ -31,10 +31,15 @@ static void atmel_sha204a_rng_done(struct atmel_i2c_work_data *work_data,
      struct atmel_i2c_client_priv *i2c_priv = work_data->ctx;
      struct hwrng *rng = areq;
 
Quoted 11 lines

drivers/crypto/atmel-sha204a.c static void atmel_sha204a_rng_done(struct atmel_i2c_work_data *work_data, · line 39

Quoted 7 lines
drivers/crypto/atmel-sha204a.c +3 -1
-     if (status)
+     if (status) {
              dev_warn_ratelimited(&i2c_priv->client->dev,
                                   "i2c transaction failed (%d)\n",
                                   status);
+             kfree(work_data);
+             rng->priv = 0;

Why is this necessary? It appears that rng_read_nonblocking already zeroes rng->priv.

IMHO this is not the same. The patch targets the error path. If the status in atmel_sha204a_rng_done() is failed, then failed work_data is still assigned and rng->priv is not zeroed at the moment. Only a subsequent call to rng_read_nonblocking() will set rng->priv = 0;

The call order is something like this:
1. atmel_sha204a_init // module setup
2. atmel_sha204a_rng_read_nonblocking // call 1
3. atmel_sha204a_rng_done // if fail, still copies
work_data <-- patch clears here
...
4. atmel_sha204a_rng_read_nonblocking // call 2, clears rng->priv = 0

Originally this was a sashiko finding, when I move the RNG part into the common driver. Reason: Actually all Atmel ECC and Atmel SHA204a devices support the same RNG mech. Thus part of my refactoring is moving it to the common core driver atmel_i2c. I was advised by the maintainer to use also sashiko's feedback. So, I went on identifying sashiko issues and have a look into it, if I can provide a fix for it. This is one of them.

Sashiko asked:
"If the I2C transaction fails here, we still assign the work_data to rng->priv. Since kmalloc_obj() uses GFP_ATOMIC and does not zero memory, does this risk leaking uninitialized slab memory or stale data from previous reads when the next non-blocking read copies from work_data->cmd.data?"

ref: https://sashiko.dev/#/patchset/20260512224349.64621-1-l.rubusch%40gmail.com
[search for atmel_i2c_rng_done on that link]

I'm not sure about the risk or the (real) severity sashiko mentiones here. But it seems to
be correct, when atmel_sha204a_rng_done() fails in the status, it continues assigning the
failed result in the work_data:

    static void atmel_sha204a_rng_done(struct atmel_i2c_work_data *work_data,
                       void *areq, int status)
    {
        struct atmel_i2c_client_priv *i2c_priv = work_data->ctx;
        struct hwrng *rng = areq;

        if (status)
            dev_warn_ratelimited(&i2c_priv->client->dev,
                         "i2c transaction failed (%d)\n",
                         status);

        rng->priv = (unsigned long)work_data;
        atomic_dec(&i2c_priv->tfm_count);
    }

Hence, my proposed patch will stop it passing work_data, if status is failed. It will not
assign rng->priv anymore then containing old data, but clear it. It will free the work_data
to provoke a new allocation happening in atmel_sha204a_rng_read_nonblocking().

The patch is sashiko and maintainer reviewed and solves sashikos complaints.
ref: https://sashiko.dev/#/patchset/20260609094723.47237-1-l.rubusch%40gmail.com
Setting rng->priv = 0; is rather safety here.

Thank you for asking. Accept, drop or modification needed - please, leave me a note,
I'd highly appreciate.

Best,
L

Quoted 5 lines

Thanks,
--
Email: Herbert Xu <[email protected]> Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt

maintainer
#3 lore raw
Quoted 24 lines · Lothar Rubusch
Quoted 11 lines · Herbert Xu
Quoted 9 lines · Lothar Rubusch
drivers/crypto/atmel-sha204a.c +0 -0
index 4c9af737b33a..20cd915ea8a3 100644
@@ -31,10 +31,15 @@ static void atmel_sha204a_rng_done(struct atmel_i2c_work_data *work_data,
      struct atmel_i2c_client_priv *i2c_priv = work_data->ctx;
      struct hwrng *rng = areq;
 
Quoted 11 lines

drivers/crypto/atmel-sha204a.c static void atmel_sha204a_rng_done(struct atmel_i2c_work_data *work_data, · line 39

Quoted 7 lines
drivers/crypto/atmel-sha204a.c +3 -1
-     if (status)
+     if (status) {
              dev_warn_ratelimited(&i2c_priv->client->dev,
                                   "i2c transaction failed (%d)\n",
                                   status);
+             kfree(work_data);
+             rng->priv = 0;

Why is this necessary? It appears that rng_read_nonblocking already zeroes rng->priv.

Quoted 4 lines

IMHO this is not the same. The patch targets the error path. If the status in atmel_sha204a_rng_done() is failed, then failed work_data is still assigned and rng->priv is not zeroed at the moment. Only a subsequent call to rng_read_nonblocking() will set rng->priv = 0;

Right, the rng->priv gets set on the error path prior to your patch. But with your patch, there is no need to clear rng->priv because it never gets set on the error path.

All I'm asking for is to remove the rng->priv = 0 because it only causes confusion.

Cheers,

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