* [PATCH RESEND v2 1/1] crypto: atmel-sha204a - fix heap info leak on I2C transfer failure
@ 2026-06-09 9:47 Lothar Rubusch
2026-06-11 4:58 ` Herbert Xu
0 siblings, 1 reply; 4+ messages in thread
From: Lothar Rubusch @ 2026-06-09 9:47 UTC (permalink / raw)
To: thorsten.blum, herbert, davem, nicolas.ferre, alexandre.belloni,
claudiu.beznea, ardb, krzk+dt
Cc: linux-crypto, linux-arm-kernel, linux-kernel, l.rubusch
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")
Signed-off-by: Lothar Rubusch <l.rubusch@gmail.com>
Assisted-by: Gemini:1.5 Pro [google]
Reviewed-by: Thorsten Blum <thorsten.blum@linux.dev>
---
drivers/crypto/atmel-sha204a.c | 7 ++++++-
1 file changed, 6 insertions(+), 1 deletion(-)
diff --git a/drivers/crypto/atmel-sha204a.c b/drivers/crypto/atmel-sha204a.c
index 4c9af737b33a..20cd915ea8a3 100644
--- a/drivers/crypto/atmel-sha204a.c
+++ b/drivers/crypto/atmel-sha204a.c
@@ -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
--
2.53.0
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: [PATCH RESEND v2 1/1] crypto: atmel-sha204a - fix heap info leak on I2C transfer failure 2026-06-09 9:47 [PATCH RESEND v2 1/1] crypto: atmel-sha204a - fix heap info leak on I2C transfer failure Lothar Rubusch @ 2026-06-11 4:58 ` Herbert Xu 2026-06-13 8:52 ` Lothar Rubusch 0 siblings, 1 reply; 4+ messages in thread From: Herbert Xu @ 2026-06-11 4:58 UTC (permalink / raw) To: Lothar Rubusch Cc: thorsten.blum, davem, nicolas.ferre, alexandre.belloni, claudiu.beznea, ardb, krzk+dt, linux-crypto, linux-arm-kernel, linux-kernel On Tue, Jun 09, 2026 at 09:47:23AM +0000, Lothar Rubusch wrote: > > diff --git a/drivers/crypto/atmel-sha204a.c b/drivers/crypto/atmel-sha204a.c > index 4c9af737b33a..20cd915ea8a3 100644 > --- a/drivers/crypto/atmel-sha204a.c > +++ b/drivers/crypto/atmel-sha204a.c > @@ -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, -- Email: Herbert Xu <herbert@gondor.apana.org.au> Home Page: http://gondor.apana.org.au/~herbert/ PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt ^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH RESEND v2 1/1] crypto: atmel-sha204a - fix heap info leak on I2C transfer failure 2026-06-11 4:58 ` Herbert Xu @ 2026-06-13 8:52 ` Lothar Rubusch 2026-06-13 12:28 ` Herbert Xu 0 siblings, 1 reply; 4+ messages in thread From: Lothar Rubusch @ 2026-06-13 8:52 UTC (permalink / raw) To: Herbert Xu Cc: thorsten.blum, davem, nicolas.ferre, alexandre.belloni, claudiu.beznea, ardb, krzk+dt, linux-crypto, linux-arm-kernel, linux-kernel On Thu, Jun 11, 2026 at 6:59 AM Herbert Xu <herbert@gondor.apana.org.au> wrote: > > On Tue, Jun 09, 2026 at 09:47:23AM +0000, Lothar Rubusch wrote: > > > > diff --git a/drivers/crypto/atmel-sha204a.c b/drivers/crypto/atmel-sha204a.c > > index 4c9af737b33a..20cd915ea8a3 100644 > > --- a/drivers/crypto/atmel-sha204a.c > > +++ b/drivers/crypto/atmel-sha204a.c > > @@ -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. > 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 > Thanks, > -- > Email: Herbert Xu <herbert@gondor.apana.org.au> > Home Page: http://gondor.apana.org.au/~herbert/ > PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt ^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH RESEND v2 1/1] crypto: atmel-sha204a - fix heap info leak on I2C transfer failure 2026-06-13 8:52 ` Lothar Rubusch @ 2026-06-13 12:28 ` Herbert Xu 0 siblings, 0 replies; 4+ messages in thread From: Herbert Xu @ 2026-06-13 12:28 UTC (permalink / raw) To: Lothar Rubusch Cc: thorsten.blum, davem, nicolas.ferre, alexandre.belloni, claudiu.beznea, ardb, krzk+dt, linux-crypto, linux-arm-kernel, linux-kernel On Sat, Jun 13, 2026 at 10:52:25AM +0200, Lothar Rubusch wrote: > On Thu, Jun 11, 2026 at 6:59 AM Herbert Xu <herbert@gondor.apana.org.au> wrote: > > > > On Tue, Jun 09, 2026 at 09:47:23AM +0000, Lothar Rubusch wrote: > > > > > > diff --git a/drivers/crypto/atmel-sha204a.c b/drivers/crypto/atmel-sha204a.c > > > index 4c9af737b33a..20cd915ea8a3 100644 > > > --- a/drivers/crypto/atmel-sha204a.c > > > +++ b/drivers/crypto/atmel-sha204a.c > > > @@ -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. > > > > 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, -- Email: Herbert Xu <herbert@gondor.apana.org.au> Home Page: http://gondor.apana.org.au/~herbert/ PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt ^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-06-13 12:29 UTC | newest] Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2026-06-09 9:47 [PATCH RESEND v2 1/1] crypto: atmel-sha204a - fix heap info leak on I2C transfer failure Lothar Rubusch 2026-06-11 4:58 ` Herbert Xu 2026-06-13 8:52 ` Lothar Rubusch 2026-06-13 12:28 ` Herbert Xu
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®