* [PATCH 1/3] crypto: atmel-sha204a - fix memory leak at non-blocking RNG work_data
2026-03-23 21:27 [PATCH 0/3] crypto: atmel-sha204a - multiple RNG fixes Lothar Rubusch
@ 2026-03-23 21:27 ` Lothar Rubusch
2026-03-23 21:27 ` [PATCH 2/3] crypto: atmel-sha204a - fix truncated 32-byte blocking read Lothar Rubusch
2026-03-23 21:27 ` [PATCH 3/3] crypto: atmel-sha204a - fix non-blocking read logic Lothar Rubusch
2 siblings, 0 replies; 4+ messages in thread
From: Lothar Rubusch @ 2026-03-23 21:27 UTC (permalink / raw)
To: herbert, davem, nicolas.ferre, alexandre.belloni, claudiu.beznea,
ardb, linusw
Cc: linux-crypto, linux-arm-kernel, linux-kernel, l.rubusch
The driver allocated memory for work_data in the non-blocking read
path but never free'd it again. After first read-out the memory pointer
seemed to be recycled and never was allocated again, due to some errors
in the logic, so that the leak was not growing.
Add kfree(work_data) in the completion callback on error. then add
kfree(work_data) after the data is consumed in the subsequent read
call. Finally ensure atomic_dec() is called only after the data has
been consumed or an error occurred to prevent race conditions.
Fixes: da001fb651b0 ("crypto: atmel-i2c - add support for SHA204A random number generator")
Signed-off-by: Lothar Rubusch <l.rubusch@gmail.com>
---
drivers/crypto/atmel-sha204a.c | 44 +++++++++++++++++++++-------------
1 file changed, 27 insertions(+), 17 deletions(-)
diff --git a/drivers/crypto/atmel-sha204a.c b/drivers/crypto/atmel-sha204a.c
index 98d1023007e3..1baf4750d311 100644
--- a/drivers/crypto/atmel-sha204a.c
+++ b/drivers/crypto/atmel-sha204a.c
@@ -24,15 +24,20 @@ 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);
}
+
static int atmel_sha204a_rng_read_nonblocking(struct hwrng *rng, void *data,
size_t max)
{
@@ -41,31 +46,36 @@ static int atmel_sha204a_rng_read_nonblocking(struct hwrng *rng, void *data,
i2c_priv = container_of(rng, struct atmel_i2c_client_priv, hwrng);
- /* keep maximum 1 asynchronous read in flight at any time */
- if (!atomic_add_unless(&i2c_priv->tfm_count, 1, 1))
- return 0;
-
+ /* Verify if data available from last run */
if (rng->priv) {
work_data = (struct atmel_i2c_work_data *)rng->priv;
max = min(sizeof(work_data->cmd.data), max);
memcpy(data, &work_data->cmd.data, max);
- rng->priv = 0;
- } else {
- work_data = kmalloc_obj(*work_data, GFP_ATOMIC);
- if (!work_data) {
- atomic_dec(&i2c_priv->tfm_count);
- return -ENOMEM;
- }
- work_data->ctx = i2c_priv;
- work_data->client = i2c_priv->client;
- max = 0;
+ /* Now, free memory */
+ kfree(work_data);
+ rng->priv = 0;
+ atomic_dec(&i2c_priv->tfm_count);
+ return max;
}
+ /* When a request is still in-flight but not processed */
+ if (atomic_read(&i2c_priv->tfm_count) > 0)
+ return 0;
+
+ /* Start a new request */
+ work_data = kmalloc_obj(*work_data, GFP_ATOMIC);
+ if (!work_data)
+ return -ENOMEM;
+
+ atomic_inc(&i2c_priv->tfm_count);
+ work_data->ctx = i2c_priv;
+ work_data->client = i2c_priv->client;
+
atmel_i2c_init_random_cmd(&work_data->cmd);
atmel_i2c_enqueue(work_data, atmel_sha204a_rng_done, rng);
- return max;
+ return 0;
}
static int atmel_sha204a_rng_read(struct hwrng *rng, void *data, size_t max,
--
2.53.0
^ permalink raw reply related [flat|nested] 4+ messages in thread* [PATCH 2/3] crypto: atmel-sha204a - fix truncated 32-byte blocking read
2026-03-23 21:27 [PATCH 0/3] crypto: atmel-sha204a - multiple RNG fixes Lothar Rubusch
2026-03-23 21:27 ` [PATCH 1/3] crypto: atmel-sha204a - fix memory leak at non-blocking RNG work_data Lothar Rubusch
@ 2026-03-23 21:27 ` Lothar Rubusch
2026-03-23 21:27 ` [PATCH 3/3] crypto: atmel-sha204a - fix non-blocking read logic Lothar Rubusch
2 siblings, 0 replies; 4+ messages in thread
From: Lothar Rubusch @ 2026-03-23 21:27 UTC (permalink / raw)
To: herbert, davem, nicolas.ferre, alexandre.belloni, claudiu.beznea,
ardb, linusw
Cc: linux-crypto, linux-arm-kernel, linux-kernel, l.rubusch
The ATSHA204A returns a 35-byte packet consisting of a 1-byte count,
32 bytes of entropy, and a 2-byte CRC. The current blocking read
implementation was incorrectly copying data starting from the
count byte, leading to offset data and truncated entropy.
Additionally, the chip requires significant execution time to
generate random numbers, going by the datasheet. Reading the I2C bus
too early results in the chip NACK-ing or returning a partial buffer
followed by zeros.
Verification:
Tests before showed repeadetly reading only 8 bytes of entropy:
$ head -c 32 /dev/hwrng | hexdump -C
00000000 02 28 85 b3 47 40 f2 ee 00 00 00 00 00 00 00 00 |.(..G@..........|
00000010 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
00000020
After this patch applied, the result will be as follows:
$ head -c 32 /dev/hwrng | hexdump -C
00000000 5a fc 3f 13 14 68 fe 06 68 0a bd 04 83 6e 09 69 |Z.?..h..h....n.i|
00000010 75 ff cf 87 10 84 3b c9 c1 df ae eb 45 53 4c c3 |u.....;.....ESL.|
00000020
Fix these issues by:
Increase cmd.msecs to 30ms to provide sufficient execution time. Then
set cmd.rxsize to RANDOM_RSP_SIZE (35 bytes) to capture the entire
hardware response. Eventually, correct the memcpy() offset to index 1 of
the data buffer to skip the count byte and retrieve exactly 32 bytes of
entropy.
Fixes: da001fb651b0 ("crypto: atmel-i2c - add support for SHA204A random number generator")
Signed-off-by: Lothar Rubusch <l.rubusch@gmail.com>
---
drivers/crypto/atmel-sha204a.c | 9 +++++++--
1 file changed, 7 insertions(+), 2 deletions(-)
diff --git a/drivers/crypto/atmel-sha204a.c b/drivers/crypto/atmel-sha204a.c
index 1baf4750d311..350ba8618c69 100644
--- a/drivers/crypto/atmel-sha204a.c
+++ b/drivers/crypto/atmel-sha204a.c
@@ -18,6 +18,9 @@
#include <linux/workqueue.h>
#include "atmel-i2c.h"
+#define ATMEL_RNG_BLOCK_SIZE 32
+#define ATMEL_RNG_EXEC_TIME 30
+
static void atmel_sha204a_rng_done(struct atmel_i2c_work_data *work_data,
void *areq, int status)
{
@@ -91,13 +94,15 @@ static int atmel_sha204a_rng_read(struct hwrng *rng, void *data, size_t max,
i2c_priv = container_of(rng, struct atmel_i2c_client_priv, hwrng);
atmel_i2c_init_random_cmd(&cmd);
+ cmd.msecs = ATMEL_RNG_EXEC_TIME;
+ cmd.rxsize = RANDOM_RSP_SIZE;
ret = atmel_i2c_send_receive(i2c_priv->client, &cmd);
if (ret)
return ret;
- max = min(sizeof(cmd.data), max);
- memcpy(data, cmd.data, max);
+ max = min_t(size_t, ATMEL_RNG_BLOCK_SIZE, max);
+ memcpy(data, &cmd.data[1], max);
return max;
}
--
2.53.0
^ permalink raw reply related [flat|nested] 4+ messages in thread* [PATCH 3/3] crypto: atmel-sha204a - fix non-blocking read logic
2026-03-23 21:27 [PATCH 0/3] crypto: atmel-sha204a - multiple RNG fixes Lothar Rubusch
2026-03-23 21:27 ` [PATCH 1/3] crypto: atmel-sha204a - fix memory leak at non-blocking RNG work_data Lothar Rubusch
2026-03-23 21:27 ` [PATCH 2/3] crypto: atmel-sha204a - fix truncated 32-byte blocking read Lothar Rubusch
@ 2026-03-23 21:27 ` Lothar Rubusch
2 siblings, 0 replies; 4+ messages in thread
From: Lothar Rubusch @ 2026-03-23 21:27 UTC (permalink / raw)
To: herbert, davem, nicolas.ferre, alexandre.belloni, claudiu.beznea,
ardb, linusw
Cc: linux-crypto, linux-arm-kernel, linux-kernel, l.rubusch
The non-blocking path was (also) failing to provide valid entropy
due to improper buffer management and a lack of hardware execution
time.
Ensure cmd.msecs (30ms) and cmd.rxsize (35ms) are initialized before
enqueuing the background work. Fix the data offset to skip the
1-byte hardware count header when copying bits to the caller. Correctly
return 0 (busy) to the hwrng core while hardware execution is in
progress, preventing zero-filled buffers, which was the situation
before.
With this fix applied, tests will look similar to this:
$ socat -u OPEN:/dev/hwrng,nonblock - | head -c 32 | hexdump -C
00000000 23 cc 42 3c 90 b1 38 fc 54 37 35 4b 09 c5 e1 0d |#.B<..8.T75K....|
2026/03/23 14:30:18 socat[858] E read(5, 0x55be363000, 8192): Resource temporarily unavailable
00000010 73 3b af d9 02 70 76 bd 2d 59 4b 12 01 ac ae 2b |s;...pv.-YK....+|
00000020
Fixes: da001fb651b0 ("crypto: atmel-i2c - add support for SHA204A random number generator")
Signed-off-by: Lothar Rubusch <l.rubusch@gmail.com>
---
drivers/crypto/atmel-sha204a.c | 16 ++++++++++------
1 file changed, 10 insertions(+), 6 deletions(-)
diff --git a/drivers/crypto/atmel-sha204a.c b/drivers/crypto/atmel-sha204a.c
index 350ba8618c69..6700847c56a3 100644
--- a/drivers/crypto/atmel-sha204a.c
+++ b/drivers/crypto/atmel-sha204a.c
@@ -32,7 +32,6 @@ static void atmel_sha204a_rng_done(struct atmel_i2c_work_data *work_data,
"i2c transaction failed (%d)\n",
status);
kfree(work_data);
- rng->priv = 0;
atomic_dec(&i2c_priv->tfm_count);
return;
}
@@ -49,20 +48,19 @@ static int atmel_sha204a_rng_read_nonblocking(struct hwrng *rng, void *data,
i2c_priv = container_of(rng, struct atmel_i2c_client_priv, hwrng);
- /* Verify if data available from last run */
if (rng->priv) {
work_data = (struct atmel_i2c_work_data *)rng->priv;
- max = min(sizeof(work_data->cmd.data), max);
- memcpy(data, &work_data->cmd.data, max);
+ max = min_t(size_t, ATMEL_RNG_BLOCK_SIZE, max);
+ memcpy(data, &work_data->cmd.data[1], max); // Note the [1] index
- /* Now, free memory */
+ /* Free memory and clear the in-flight flag */
kfree(work_data);
rng->priv = 0;
atomic_dec(&i2c_priv->tfm_count);
return max;
}
- /* When a request is still in-flight but not processed */
+ /* If a request is still in-flight, return 0 (busy) */
if (atomic_read(&i2c_priv->tfm_count) > 0)
return 0;
@@ -76,8 +74,14 @@ static int atmel_sha204a_rng_read_nonblocking(struct hwrng *rng, void *data,
work_data->client = i2c_priv->client;
atmel_i2c_init_random_cmd(&work_data->cmd);
+
+ /* Set the execution time for the RNG command (from datasheet) */
+ work_data->cmd.msecs = ATMEL_RNG_EXEC_TIME;
+ work_data->cmd.rxsize = RANDOM_RSP_SIZE;
+
atmel_i2c_enqueue(work_data, atmel_sha204a_rng_done, rng);
+ /* Return 0 to indicate 'busy', data will be ready on next call */
return 0;
}
--
2.53.0
^ permalink raw reply related [flat|nested] 4+ messages in thread