public inbox for stable@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v3] zstd: fixed possible 'rtbTable' underflow in FSE_normalizeCount()
@ 2025-12-11 17:19 Ilya Krutskih
  2026-01-13 18:58 ` Felix Handte
  0 siblings, 1 reply; 2+ messages in thread
From: Ilya Krutskih @ 2025-12-11 17:19 UTC (permalink / raw)
  To: Nick Terrell
  Cc: Ilya Krutskih, David Sterba, linux-kernel, lvc-project, stable

'rtbTable' may be underflowed because 'proba' is used without
checking for a non-negative as index of rtbTable[].

Add check: proba >= 0

Cc: stable@vger.kernel.org # v5.10+
Fixes: e0c1b49f5b67 ("lib: zstd: Upgrade to latest upstream zstd version 1.4.10")
Signed-off-by: Ilya Krutskih <devsec@tpz.ru>
---
 lib/zstd/compress/fse_compress.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/lib/zstd/compress/fse_compress.c b/lib/zstd/compress/fse_compress.c
index 44a3c10becf2..6b83f8bc943a 100644
--- a/lib/zstd/compress/fse_compress.c
+++ b/lib/zstd/compress/fse_compress.c
@@ -492,9 +492,10 @@ size_t FSE_normalizeCount (short* normalizedCounter, unsigned tableLog,
                 stillToDistribute--;
             } else {
                 short proba = (short)((count[s]*step) >> scale);
-                if (proba<8) {
-                    U64 restToBeat = vStep * rtbTable[proba];
-                    proba += (count[s]*step) - ((U64)proba<<scale) > restToBeat;
+		if ((proba >= 0) && (proba < 8)) {
+			U64 restToBeat = vStep * rtbTable[proba];
+
+			proba += (count[s]*step) - ((U64)proba<<scale) > restToBeat;
                 }
                 if (proba > largestP) { largestP=proba; largest=s; }
                 normalizedCounter[s] = proba;
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH v3] zstd: fixed possible 'rtbTable' underflow in FSE_normalizeCount()
  2025-12-11 17:19 [PATCH v3] zstd: fixed possible 'rtbTable' underflow in FSE_normalizeCount() Ilya Krutskih
@ 2026-01-13 18:58 ` Felix Handte
  0 siblings, 0 replies; 2+ messages in thread
From: Felix Handte @ 2026-01-13 18:58 UTC (permalink / raw)
  To: Ilya Krutskih, Nick Terrell
  Cc: David Sterba, linux-kernel, lvc-project, stable

Ilya, can you share any context for this patch? Do you have any evidence 
that `proba` can be negative?

A discussion was just started about this patch on the zstd repo [0]. I'm 
happy to discuss this here or there, whichever is more convenient.

But to my first pass inspection, this seems to be protecting an 
impossible situation. (Separately: if it could happen, the correct 
behavior would to catch it and return an error, not just skip it like 
this patch proposes.)

Thanks,
Felix

[0] https://github.com/facebook/zstd/issues/4567

On 12/11/25 12:19 PM, Ilya Krutskih wrote:
> 'rtbTable' may be underflowed because 'proba' is used without
> checking for a non-negative as index of rtbTable[].
> 
> Add check: proba >= 0
> 
> Cc: stable@vger.kernel.org # v5.10+
> Fixes: e0c1b49f5b67 ("lib: zstd: Upgrade to latest upstream zstd version 1.4.10")
> Signed-off-by: Ilya Krutskih <devsec@tpz.ru>
> ---
>   lib/zstd/compress/fse_compress.c | 7 ++++---
>   1 file changed, 4 insertions(+), 3 deletions(-)
> 
> diff --git a/lib/zstd/compress/fse_compress.c b/lib/zstd/compress/fse_compress.c
> index 44a3c10becf2..6b83f8bc943a 100644
> --- a/lib/zstd/compress/fse_compress.c
> +++ b/lib/zstd/compress/fse_compress.c
> @@ -492,9 +492,10 @@ size_t FSE_normalizeCount (short* normalizedCounter, unsigned tableLog,
>                   stillToDistribute--;
>               } else {
>                   short proba = (short)((count[s]*step) >> scale);
> -                if (proba<8) {
> -                    U64 restToBeat = vStep * rtbTable[proba];
> -                    proba += (count[s]*step) - ((U64)proba<<scale) > restToBeat;
> +		if ((proba >= 0) && (proba < 8)) {
> +			U64 restToBeat = vStep * rtbTable[proba];
> +
> +			proba += (count[s]*step) - ((U64)proba<<scale) > restToBeat;
>                   }
>                   if (proba > largestP) { largestP=proba; largest=s; }
>                   normalizedCounter[s] = proba;


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-01-13 18:59 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-12-11 17:19 [PATCH v3] zstd: fixed possible 'rtbTable' underflow in FSE_normalizeCount() Ilya Krutskih
2026-01-13 18:58 ` Felix Handte

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox