[PATCH V2] crypto: Mark intermediary memory as clean

Edward Adam Davis posted 1 patch 1 month, 2 weeks ago
There is a newer version of this series
crypto/jitterentropy-kcapi.c | 1 +
1 file changed, 1 insertion(+)
[PATCH V2] crypto: Mark intermediary memory as clean
Posted by Edward Adam Davis 1 month, 2 weeks ago
This is not a leak! The stack memroy is hashed and fed into the
entropy pool. We can't recover the original kernel memory from it.

Reported-by: syzbot+e8bcd7ee3db6cb5cb875@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=e8bcd7ee3db6cb5cb875
Signed-off-by: Edward Adam Davis <eadavis@qq.com>
---
V1 -> V2: mark it as unpoison

 crypto/jitterentropy-kcapi.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/crypto/jitterentropy-kcapi.c b/crypto/jitterentropy-kcapi.c
index 1266eb790708..4020a6e41b0e 100644
--- a/crypto/jitterentropy-kcapi.c
+++ b/crypto/jitterentropy-kcapi.c
@@ -117,6 +117,7 @@ int jent_hash_time(void *hash_state, __u64 time, u8 *addtl,
 		pr_warn_ratelimited("Unexpected digest size\n");
 		return -EINVAL;
 	}
+	kmsan_unpoison_memory(intermediary, SHA3_256_DIGEST_SIZE);
 
 	/*
 	 * This loop fills a buffer which is injected into the entropy pool.
-- 
2.43.0
Re: [PATCH V2] crypto: Mark intermediary memory as clean
Posted by Herbert Xu 1 month, 2 weeks ago
On Sun, Aug 17, 2025 at 06:59:15PM +0800, Edward Adam Davis wrote:
> This is not a leak! The stack memroy is hashed and fed into the
> entropy pool. We can't recover the original kernel memory from it.
> 
> Reported-by: syzbot+e8bcd7ee3db6cb5cb875@syzkaller.appspotmail.com
> Closes: https://syzkaller.appspot.com/bug?extid=e8bcd7ee3db6cb5cb875
> Signed-off-by: Edward Adam Davis <eadavis@qq.com>
> ---
> V1 -> V2: mark it as unpoison
> 
>  crypto/jitterentropy-kcapi.c | 1 +
>  1 file changed, 1 insertion(+)
> 
> diff --git a/crypto/jitterentropy-kcapi.c b/crypto/jitterentropy-kcapi.c
> index 1266eb790708..4020a6e41b0e 100644
> --- a/crypto/jitterentropy-kcapi.c
> +++ b/crypto/jitterentropy-kcapi.c
> @@ -117,6 +117,7 @@ int jent_hash_time(void *hash_state, __u64 time, u8 *addtl,
>  		pr_warn_ratelimited("Unexpected digest size\n");
>  		return -EINVAL;
>  	}
> +	kmsan_unpoison_memory(intermediary, SHA3_256_DIGEST_SIZE);

Please change SHA3_256_DIGEST_SIZE to sizeof(intermediary).

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
Re: [PATCH V2] crypto: Mark intermediary memory as clean
Posted by Edward Adam Davis 1 month, 2 weeks ago
On Sun, 17 Aug 2025 19:40:56 +0800, Herbert Xu wrote:
> > diff --git a/crypto/jitterentropy-kcapi.c b/crypto/jitterentropy-kcapi.c
> > index 1266eb790708..4020a6e41b0e 100644
> > --- a/crypto/jitterentropy-kcapi.c
> > +++ b/crypto/jitterentropy-kcapi.c
> > @@ -117,6 +117,7 @@ int jent_hash_time(void *hash_state, __u64 time, u8 *addtl,
> >  		pr_warn_ratelimited("Unexpected digest size\n");
> >  		return -EINVAL;
> >  	}
> > +	kmsan_unpoison_memory(intermediary, SHA3_256_DIGEST_SIZE);
> 
> Please change SHA3_256_DIGEST_SIZE to sizeof(intermediary).
Why?
Their values are equal, so why use sizeof to calculate?
Similarly, "if (sizeof(intermediary) != crypto_shash_digestsize(desc->tfm)) {",
why not just use SHA3_256_DIGEST_SIZE?

BR,
Edward
Re: [PATCH V2] crypto: Mark intermediary memory as clean
Posted by Herbert Xu 1 month, 2 weeks ago
On Mon, Aug 18, 2025 at 08:17:17PM +0800, Edward Adam Davis wrote:
>
> Their values are equal, so why use sizeof to calculate?
> Similarly, "if (sizeof(intermediary) != crypto_shash_digestsize(desc->tfm)) {",
> why not just use SHA3_256_DIGEST_SIZE?

Please be consistent with the existing code, every other place
in the function uses sizeof(intermediary) so your patch is the
odd one out.

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
Re: [PATCH V2] crypto: Mark intermediary memory as clean
Posted by Edward Adam Davis 1 month, 2 weeks ago
On Mon, 18 Aug 2025 20:30:29 +0800, Herbert Xu wrote:
> Their values are equal, so why use sizeof to calculate?
> Similarly, "if (sizeof(intermediary) != crypto_shash_digestsize(desc->tfm)) {",
> why not just use SHA3_256_DIGEST_SIZE?
Hi Stephan Mueller, can you explain it?

BR,
Edward
Re: [PATCH V2] crypto: Mark intermediary memory as clean
Posted by Stephan Mueller 1 month, 2 weeks ago
Am Montag, 18. August 2025, 14:43:36 Mitteleuropäische Sommerzeit schrieb 
Edward Adam Davis:

Hi Edward,

> On Mon, 18 Aug 2025 20:30:29 +0800, Herbert Xu wrote:
> > Their values are equal, so why use sizeof to calculate?
> > Similarly, "if (sizeof(intermediary) !=
> > crypto_shash_digestsize(desc->tfm)) {", why not just use
> > SHA3_256_DIGEST_SIZE?
> 
> Hi Stephan Mueller, can you explain it?

If the question is why using sizeof(intermediary) instead of 
SHA3_256_DIGEST_SIZE, then it is very trivial: I always want to avoid any kind 
of double work. If for any reason the buffer size of intermediary changes, the 
current code only requires *one* location to fix it.

When changing the branching condition to use SHA3_256_DIGEST_SIZE, we would 
have to change *two* locations which is more error-prone than to change one. 
This approach is my common coding style to try to minimize the possibilities 
where inconsistencies can occur.

Ciao
Stephan
[PATCH V3] crypto: Mark intermediary memory as clean
Posted by Edward Adam Davis 1 month, 2 weeks ago
This is not a leak! The stack memroy is hashed and fed into the
entropy pool. We can't recover the original kernel memory from it.

Reported-by: syzbot+e8bcd7ee3db6cb5cb875@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=e8bcd7ee3db6cb5cb875
Signed-off-by: Edward Adam Davis <eadavis@qq.com>
---
V1 -> V2: mark it as unpoison
V2 -> V3: replace to sizeof, minimize the possibilities where inconsistencies can occur

 crypto/jitterentropy-kcapi.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/crypto/jitterentropy-kcapi.c b/crypto/jitterentropy-kcapi.c
index 1266eb790708..4020a6e41b0e 100644
--- a/crypto/jitterentropy-kcapi.c
+++ b/crypto/jitterentropy-kcapi.c
@@ -117,6 +117,7 @@ int jent_hash_time(void *hash_state, __u64 time, u8 *addtl,
 		pr_warn_ratelimited("Unexpected digest size\n");
 		return -EINVAL;
 	}
+	kmsan_unpoison_memory(intermediary, sizeof(intermediary));
 
 	/*
 	 * This loop fills a buffer which is injected into the entropy pool.
-- 
2.43.0
Re: [PATCH V3] crypto: Mark intermediary memory as clean
Posted by Herbert Xu 1 month ago
On Mon, Aug 18, 2025 at 09:24:17PM +0800, Edward Adam Davis wrote:
> This is not a leak! The stack memroy is hashed and fed into the
> entropy pool. We can't recover the original kernel memory from it.
> 
> Reported-by: syzbot+e8bcd7ee3db6cb5cb875@syzkaller.appspotmail.com
> Closes: https://syzkaller.appspot.com/bug?extid=e8bcd7ee3db6cb5cb875
> Signed-off-by: Edward Adam Davis <eadavis@qq.com>
> ---
> V1 -> V2: mark it as unpoison
> V2 -> V3: replace to sizeof, minimize the possibilities where inconsistencies can occur
> 
>  crypto/jitterentropy-kcapi.c | 1 +
>  1 file changed, 1 insertion(+)

Patch applied.  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
Re: [PATCH V3] crypto: Mark intermediary memory as clean
Posted by Stephan Mueller 1 month, 2 weeks ago
Am Montag, 18. August 2025, 15:24:17 Mitteleuropäische Sommerzeit schrieb 
Edward Adam Davis:

Hi Edward,

> This is not a leak! The stack memroy is hashed and fed into the
> entropy pool. We can't recover the original kernel memory from it.
> 
> Reported-by: syzbot+e8bcd7ee3db6cb5cb875@syzkaller.appspotmail.com
> Closes: https://syzkaller.appspot.com/bug?extid=e8bcd7ee3db6cb5cb875
> Signed-off-by: Edward Adam Davis <eadavis@qq.com>

Thank you for the patch. Just for the records:

- the intermediary buffer could be initialized to 0 without any effect on the 
Jitter RNG, because all it wants is actually the execution of the Keccak 
operation as part of crypto_shhash_finup.

- the intermediary buffer is inserted into the Jitter RNG state to ensure that 
the compiler cannot optimize away the loop if the intermediary buffer would 
not be used at all

- the intermediary buffer is not credited with any entropy as we only want the 
Keccak operation

- by keeping the intermediary uninitialized, the Jitter RNG may get some 
variations from the uninitialized buffer so that its internal state may 
benefit from it

That said, I am fine with this current patch. But if there is still lingering 
concern, I am equally fine to have it initialized to zero.

Thanks a lot
Stephan

> ---
> V1 -> V2: mark it as unpoison
> V2 -> V3: replace to sizeof, minimize the possibilities where
> inconsistencies can occur
> 
>  crypto/jitterentropy-kcapi.c | 1 +
>  1 file changed, 1 insertion(+)
> 
> diff --git a/crypto/jitterentropy-kcapi.c b/crypto/jitterentropy-kcapi.c
> index 1266eb790708..4020a6e41b0e 100644
> --- a/crypto/jitterentropy-kcapi.c
> +++ b/crypto/jitterentropy-kcapi.c
> @@ -117,6 +117,7 @@ int jent_hash_time(void *hash_state, __u64 time, u8
> *addtl, pr_warn_ratelimited("Unexpected digest size\n");
>  		return -EINVAL;
>  	}
> +	kmsan_unpoison_memory(intermediary, sizeof(intermediary));
> 
>  	/*
>  	 * This loop fills a buffer which is injected into the entropy pool.


Ciao
Stephan