From 24fa3261869514f678c9cf9b5fe030546c2373d5 Mon Sep 17 00:00:00 2001 From: Davide Faconti Date: Wed, 11 Nov 2026 09:46:41 +0100 Subject: [PATCH] kiss_fft: validate sizes in the alloc functions and next_fast_size - kiss_fft_alloc(0) passed the overflow check and computed sizeof(kiss_fft_cpx)*(nfft-1) with nfft-1 == -1, returning a config. Reject nfft <= 0 (negative sizes were only rejected by accident of the signed/unsigned comparison, which is now an explicit cast). - kiss_fftr_alloc(0) halved 0 and built a config around a NULL sub-state. Reject nfft <= 0 along with odd sizes, and compute nfft*3/2 in size_t. - kiss_fftnd_alloc multiplied the dimensions in an int, so {65536,65536} wrapped to 0 and {46342,46342} to a negative value: the size query reported a bogus size and a far too small buffer was accepted. Reject dims <= 0 and products that do not fit in an int (st->dimprod) or whose buffer size overflows size_t, reporting *lenmem = (size_t)-1 as kiss_fftndr_alloc does. - kiss_fft_next_fast_size incremented n past INT_MAX for n > 2125764000 (the largest 2,3,5-smooth int) and returned a negative value. It now returns n itself there (documented in kiss_fft.h): no 2,3,5-smooth int >= n exists, and n is still a valid, if slower, size for callers that pass the result straight to an alloc. - kfc.c read an uninitialized length when the size query rejected nfft; it now returns NULL for an invalid size. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01ME1hnNaoD3ZcMj9H8TSYkZ --- kfc.c | 4 +- kiss_fft.c | 6 +- kiss_fft.h | 3 +- kiss_fftnd.c | 7 ++ kiss_fftr.c | 6 +- test/CMakeLists.txt | 1 + test/Makefile | 11 ++- test/test_size_validation.c | 162 ++++++++++++++++++++++++++++++++++++ 8 files changed, 191 insertions(+), 9 deletions(-) create mode 100644 test/test_size_validation.c diff --git a/kfc.c b/kfc.c index a405d9b..270944a 100644 --- a/kfc.c +++ b/kfc.c @@ -23,7 +23,7 @@ static int ncached=0; static kiss_fft_cfg find_cached_fft(int nfft,int inverse) { - size_t len; + size_t len = 0; kfc_cfg cur=cache_root; kfc_cfg prev=NULL; while ( cur ) { @@ -35,6 +35,8 @@ static kiss_fft_cfg find_cached_fft(int nfft,int inverse) if (cur== NULL) { /* no cached node found, need to create a new one*/ kiss_fft_alloc(nfft,inverse,0,&len); + if (len == 0) + return NULL; /* invalid nfft */ #ifdef USE_SIMD int padding = (16-sizeof(struct cached_fft)) & 15; // make sure the cfg aligns on a 16 byte boundary diff --git a/kiss_fft.c b/kiss_fft.c index c97515f..18c196a 100644 --- a/kiss_fft.c +++ b/kiss_fft.c @@ -339,8 +339,8 @@ kiss_fft_cfg kiss_fft_alloc(int nfft,int inverse_fft,void * mem,size_t * lenmem KISS_FFT_ALIGN_CHECK(mem) kiss_fft_cfg st=NULL; - // check for overflow condition {memneeded > SIZE_MAX}. - if (nfft >= (SIZE_MAX - 2*sizeof(struct kiss_fft_state))/sizeof(kiss_fft_cpx)) + // reject empty/negative sizes and the overflow condition {memneeded > SIZE_MAX}. + if (nfft <= 0 || (size_t)nfft >= (SIZE_MAX - 2*sizeof(struct kiss_fft_state))/sizeof(kiss_fft_cpx)) return NULL; size_t memneeded = KISS_FFT_ALIGN_SIZE_UP(sizeof(struct kiss_fft_state) @@ -413,6 +413,8 @@ int kiss_fft_next_fast_size(int n) { if (n <= 0) /* nothing to factor; avoid infinite loop for n<=0 */ return 1; + if (n > 2125764000) /* 2^5 * 3^12 * 5^3 is the largest 2,3,5-smooth int; n++ would overflow */ + return n; while(1) { int m=n; while ( (m%2) == 0 ) m/=2; diff --git a/kiss_fft.h b/kiss_fft.h index e6906d5..a568076 100644 --- a/kiss_fft.h +++ b/kiss_fft.h @@ -173,7 +173,8 @@ void KISS_FFT_API kiss_fft_cleanup(void); /* - * Returns the smallest integer k, such that k>=n and k has only "fast" factors (2,3,5) + * Returns the smallest integer k, such that k>=n and k has only "fast" factors (2,3,5). + * Returns 1 for n<=0, and n itself if no such k fits in an int (n > 2125764000). */ int KISS_FFT_API kiss_fft_next_fast_size(int n); diff --git a/kiss_fftnd.c b/kiss_fftnd.c index 5d5b089..550d231 100644 --- a/kiss_fftnd.c +++ b/kiss_fftnd.c @@ -29,6 +29,13 @@ kiss_fftnd_cfg kiss_fftnd_alloc(const int *dims,int ndims,int inverse_fft,void*m for (i=0;idimprod) or the tmpbuf size */ + if (dims[i] <= 0 || dimprod > INT_MAX / dims[i] + || (size_t)(dimprod * dims[i]) > (size_t)(-1) / 2 / sizeof(kiss_fft_cpx)) { + if (lenmem != NULL) + *lenmem = (size_t)(-1); + return NULL; + } kiss_fft_alloc (dims[i], inverse_fft, NULL, &sublen); memneeded += sublen; /* st->states[i] */ dimprod *= dims[i]; diff --git a/kiss_fftr.c b/kiss_fftr.c index 08b6d7b..d1d2c04 100644 --- a/kiss_fftr.c +++ b/kiss_fftr.c @@ -26,14 +26,14 @@ kiss_fftr_cfg kiss_fftr_alloc(int nfft,int inverse_fft,void * mem,size_t * lenme kiss_fftr_cfg st = NULL; size_t subsize = 0, memneeded; - if (nfft & 1) { - KISS_FFT_ERROR("Real FFT optimization must be even."); + if (nfft <= 0 || (nfft & 1)) { + KISS_FFT_ERROR("Real FFT optimization must be even and positive."); return NULL; } nfft >>= 1; kiss_fft_alloc (nfft, inverse_fft, NULL, &subsize); - memneeded = sizeof(struct kiss_fftr_state) + subsize + sizeof(kiss_fft_cpx) * ( nfft * 3 / 2); + memneeded = sizeof(struct kiss_fftr_state) + subsize + sizeof(kiss_fft_cpx) * ( (size_t)nfft * 3 / 2); if (lenmem == NULL) { st = (kiss_fftr_cfg) KISS_FFT_MALLOC (memneeded); diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 505a748..ab16cf4 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -48,6 +48,7 @@ target_compile_definitions(tkfc PRIVATE KFC_TEST) add_kissfft_test_executable(ffr twotonetest.c) add_kissfft_test_executable(tr test_real.c) +add_kissfft_test_executable(tsizeval test_size_validation.c) add_kissfft_test_executable(next_fast_size next_fast_size.c) diff --git a/test/Makefile b/test/Makefile index e53704a..17dcf92 100644 --- a/test/Makefile +++ b/test/Makefile @@ -43,6 +43,7 @@ ifneq ($(KISSFFT_OPENMP),1) BENCHFFTW = bm-fftw-$(KISSFFT_DATATYPE) SELFTEST = st-$(KISSFFT_DATATYPE) TESTREAL = tr-$(KISSFFT_DATATYPE) + TESTSIZEVAL = tsizeval-$(KISSFFT_DATATYPE) TESTKFC = tkfc-$(KISSFFT_DATATYPE) TESTFASTFILT = fastfilt-$(KISSFFT_DATATYPE) TESTCPP = testcpp-$(KISSFFT_DATATYPE) @@ -52,6 +53,7 @@ else BENCHFFTW = bm-fftw-$(KISSFFT_DATATYPE)-openmp SELFTEST = st-$(KISSFFT_DATATYPE)-openmp TESTREAL = tr-$(KISSFFT_DATATYPE)-openmp + TESTSIZEVAL = tsizeval-$(KISSFFT_DATATYPE)-openmp TESTKFC = tkfc-$(KISSFFT_DATATYPE)-openmp TESTFASTFILT = fastfilt-$(KISSFFT_DATATYPE)-openmp TESTCPP = testcpp-$(KISSFFT_DATATYPE)-openmp @@ -103,7 +105,7 @@ endif # Target: "make all" # -all: $(BENCHKISS) $(SELFTEST) $(BENCHFFTW) $(TESTREAL) $(TESTKFC) $(TESTFASTFILT) +all: $(BENCHKISS) $(SELFTEST) $(BENCHFFTW) $(TESTREAL) $(TESTSIZEVAL) $(TESTKFC) $(TESTFASTFILT) # # Individual test make rules @@ -118,6 +120,9 @@ $(TESTKFC): ../kfc.c $(TESTREAL): test_real.c $(CC) -o $@ $(CFLAGS) $(TYPEFLAGS) $< -L.. -l$(KISSFFTLIB_SHORTNAME) -lm +$(TESTSIZEVAL): test_size_validation.c + $(CC) -o $@ $(CFLAGS) $(TYPEFLAGS) $< -L.. -l$(KISSFFTLIB_SHORTNAME) -lm + $(BENCHKISS): benchkiss.c pstats.c $(CC) -o $@ $(CFLAGS) $(TYPEFLAGS) $^ -L.. -l$(KISSFFTLIB_SHORTNAME) -lm @@ -175,6 +180,8 @@ endif @LD_LIBRARY_PATH="$(LD_LIBRARY_PATH):.." ./$(SELFTEST) $(warning ======real FFT (type= $(KISSFFT_DATATYPE) )) @LD_LIBRARY_PATH="$(LD_LIBRARY_PATH):.." ./$(TESTREAL) + $(warning ======allocation size validation regression test (type= $(KISSFFT_DATATYPE) )) + @LD_LIBRARY_PATH="$(LD_LIBRARY_PATH):.." ./$(TESTSIZEVAL) $(warning ======timing test (type=$(KISSFFT_DATATYPE))) @LD_LIBRARY_PATH="$(LD_LIBRARY_PATH):.." ./$(BENCHKISS) -x $(NUMFFTS) -n $(NFFT) @[ -x ./$(BENCHFFTW) ] && LD_LIBRARY_PATH="$(LD_LIBRARY_PATH):.." ./$(BENCHFFTW) -x $(NUMFFTS) -n $(NFFT) || true @@ -186,4 +193,4 @@ endif # clean: - rm -f *~ bm-* st-* tr-* kf-* tkfc-* ff-* fastfilt-* *.pyc *.pyo *.dat testcpp-* testsimd testsimd-* _test_library_dummy _test_library_dummy.c + rm -f *~ bm-* st-* tr-* tsizeval-* kf-* tkfc-* ff-* fastfilt-* *.pyc *.pyo *.dat testcpp-* testsimd testsimd-* _test_library_dummy _test_library_dummy.c diff --git a/test/test_size_validation.c b/test/test_size_validation.c new file mode 100644 index 0000000..f44d1f4 --- /dev/null +++ b/test/test_size_validation.c @@ -0,0 +1,162 @@ +/* + * Copyright (c) 2003-2010, Mark Borgerding. All rights reserved. + * This file is part of KISS FFT - https://github.com/mborgerding/kissfft + * + * SPDX-License-Identifier: BSD-3-Clause + * See COPYING file for more information. + */ + +/* Regression tests for size validation in the allocation functions: + * 1. kiss_fftnd_alloc must not overflow the product of the dimensions (an int): + * {65536,65536} wraps to 0 and {46342,46342} to a negative number, so + * the library reports a bogus size and accepts a buffer that is far too small + * 2. kiss_fft_alloc(0) and kiss_fftr_alloc(0) must fail + * 3. kiss_fft_next_fast_size must not overflow int for n above the largest + * representable 2,3,5-smooth number (2125764000) + * + * Huge allocations are never attempted: the nd tests only use the size query + * and a small caller-supplied buffer, and the nfft == 0 objects are never + * transformed. + */ +#include +#include +#include +#include +#include +#include "kiss_fft.h" +#include "kiss_fftr.h" +#include "kiss_fftnd.h" + +static int failures = 0; + +static void fail(const char *msg, long a, long b) +{ + printf("FAIL: %s (%ld,%ld)\n", msg, a, b); + ++failures; +} + +/* ---- 1. kiss_fftnd_alloc: dimension product overflow ---------------------- */ + +#define SMALL_BUF_SIZE (16u * 1024 * 1024) /* above the wrapped size, far below the real one */ +#define BUF_ALIGN 64 + +static void test_nd_overflow(int d0, int d1) +{ + int dims[2]; + size_t len = 0, buflen; + double need; /* the tmpbuf alone needs this many bytes */ + void *raw, *buf; + kiss_fftnd_cfg st; + + dims[0] = d0; + dims[1] = d1; + need = (double)d0 * (double)d1 * (double)sizeof(kiss_fft_cpx); + + /* size query: must report "too big" ((size_t)-1) or a size that really fits the data, + * not a wrapped-around number */ + (void)kiss_fftnd_alloc(dims, 2, 0, NULL, &len); + if (len != (size_t)-1 && !((double)len >= need && (double)len <= 2 * need)) + fail("kiss_fftnd_alloc size query reports a bogus size, dims", d0, d1); + + /* a 16MB buffer is far too small for the transform and must be refused */ + raw = malloc(SMALL_BUF_SIZE + BUF_ALIGN); + if (raw == NULL) { + fail("out of memory", d0, d1); + return; + } + buf = (void *)(((uintptr_t)raw + BUF_ALIGN - 1) & ~(uintptr_t)(BUF_ALIGN - 1)); + buflen = SMALL_BUF_SIZE; + st = kiss_fftnd_alloc(dims, 2, 0, buf, &buflen); + if (st != NULL) + fail("kiss_fftnd_alloc accepted a 16MB buffer for dims", d0, d1); + free(raw); +} + +/* ---- 2. nfft == 0 --------------------------------------------------------- */ + +static void test_zero_and_negative_size(void) +{ + kiss_fft_cfg c; + kiss_fftr_cfg r; + + c = kiss_fft_alloc(0, 0, NULL, NULL); + if (c != NULL) + fail("kiss_fft_alloc(0) did not return NULL", 0, 0); + kiss_fft_free(c); + + r = kiss_fftr_alloc(0, 0, NULL, NULL); + if (r != NULL) + fail("kiss_fftr_alloc(0) did not return NULL", 0, 0); + kiss_fftr_free(r); + + c = kiss_fft_alloc(-1, 0, NULL, NULL); + if (c != NULL) + fail("kiss_fft_alloc(-1) did not return NULL", -1, 0); + kiss_fft_free(c); +} + +/* ---- 3. kiss_fft_next_fast_size ------------------------------------------ */ + +/* n has no prime factors other than 2, 3 and 5 */ +static int is_smooth(long long n) +{ + if (n < 1) + return 0; + while (n % 2 == 0) + n /= 2; + while (n % 3 == 0) + n /= 3; + while (n % 5 == 0) + n /= 5; + return n == 1; +} + +/* smallest smooth number >= n, computed in 64 bits */ +static long long ref_next_fast_size(long long n) +{ + while (!is_smooth(n)) + ++n; + return n; +} + +static void test_next_fast_size(void) +{ + static const int spot[] = {1001, 1000001, 2000000000, 2100000001, 2125763999, 2125764000}; + static const int huge[] = {2125764001, 2147483000, INT_MAX}; + int n, i, bad = 0, firstn = 0; + + for (n = 1; n <= 2000; ++n) { + if (kiss_fft_next_fast_size(n) != ref_next_fast_size(n)) { + if (!bad) + firstn = n; + ++bad; + } + } + if (bad) { + printf("(%d of 2000 sizes in 1..2000 wrong)\n", bad); + fail("kiss_fft_next_fast_size differs from reference, first n", firstn, 0); + } + + for (i = 0; i < (int)(sizeof(spot) / sizeof(spot[0])); ++i) { + int got = kiss_fft_next_fast_size(spot[i]); + if (got != ref_next_fast_size(spot[i])) + fail("kiss_fft_next_fast_size wrong, n", spot[i], got); + } + + /* the next smooth number does not fit in an int: n itself (still a valid size) is returned */ + for (i = 0; i < (int)(sizeof(huge) / sizeof(huge[0])); ++i) { + int got = kiss_fft_next_fast_size(huge[i]); + if (got != huge[i]) + fail("kiss_fft_next_fast_size should return n when no larger fast size fits, n", huge[i], got); + } +} + +int main(void) +{ + test_nd_overflow(65536, 65536); + test_nd_overflow(46342, 46342); + test_zero_and_negative_size(); + test_next_fast_size(); + printf("%s\n", failures ? "FAIL" : "PASS"); + return failures ? 1 : 0; +}