musllvm

musl and LLVM cross-toolchain build system
git clone https://git.daat.foo/musllvm.git
Log | Files | Refs | README | LICENSE

007-b3291b9a9f77f1f993d2b4f8c68a26cf09221ae7.patch (2935B)


      1 From b3291b9a9f77f1f993d2b4f8c68a26cf09221ae7 Mon Sep 17 00:00:00 2001
      2 From: Rich Felker <dalias@aerifal.cx>
      3 Date: Thu, 9 Apr 2026 23:40:53 -0400
      4 Subject: qsort: hard-preclude oob array writes independent of any invariants
      5 
      6 while the root cause of CVE-2026-40200 was a faulty ctz primitive, the
      7 fallout of the bug would have been limited to erroneous sorting or
      8 infinite loop if not for the stores to a stack-based array that
      9 depended on trusting invariants in order not to go out of bounds.
     10 
     11 increase the size of the array to a power of two so that we can mask
     12 indices into it to force them into range. in the absence of any
     13 further bug, the masking is a no-op, but it does not have any
     14 measurable performance cost, and it makes spatial memory safety
     15 trivial to prove (and for readers not familiar with the algorithms to
     16 trust).
     17 ---
     18  src/stdlib/qsort.c | 20 +++++++++++++-------
     19  1 file changed, 13 insertions(+), 7 deletions(-)
     20 
     21 diff --git a/src/stdlib/qsort.c b/src/stdlib/qsort.c
     22 index 13219ab3..e4bce9f7 100644
     23 --- a/src/stdlib/qsort.c
     24 +++ b/src/stdlib/qsort.c
     25 @@ -89,10 +89,16 @@ static inline void shr(size_t p[2], int n)
     26  	p[1] >>= n;
     27  }
     28  
     29 +/* power-of-two length for working array so that we can mask indices and
     30 + * not depend on any invariant of the algorithm for spatial memory safety.
     31 + * the original size was just 14*sizeof(size_t)+1 */
     32 +#define AR_LEN  (16 * sizeof(size_t))
     33 +#define AR_MASK (AR_LEN - 1)
     34 +
     35  static void sift(unsigned char *head, size_t width, cmpfun cmp, void *arg, int pshift, size_t lp[])
     36  {
     37  	unsigned char *rt, *lf;
     38 -	unsigned char *ar[14 * sizeof(size_t) + 1];
     39 +	unsigned char *ar[AR_LEN];
     40  	int i = 1;
     41  
     42  	ar[0] = head;
     43 @@ -104,16 +110,16 @@ static void sift(unsigned char *head, size_t width, cmpfun cmp, void *arg, int p
     44  			break;
     45  		}
     46  		if(cmp(lf, rt, arg) >= 0) {
     47 -			ar[i++] = lf;
     48 +			ar[i++ & AR_MASK] = lf;
     49  			head = lf;
     50  			pshift -= 1;
     51  		} else {
     52 -			ar[i++] = rt;
     53 +			ar[i++ & AR_MASK] = rt;
     54  			head = rt;
     55  			pshift -= 2;
     56  		}
     57  	}
     58 -	cycle(width, ar, i);
     59 +	cycle(width, ar, i & AR_MASK);
     60  }
     61  
     62  static void trinkle(unsigned char *head, size_t width, cmpfun cmp, void *arg, size_t pp[2], int pshift, int trusty, size_t lp[])
     63 @@ -121,7 +127,7 @@ static void trinkle(unsigned char *head, size_t width, cmpfun cmp, void *arg, si
     64  	unsigned char *stepson,
     65  	              *rt, *lf;
     66  	size_t p[2];
     67 -	unsigned char *ar[14 * sizeof(size_t) + 1];
     68 +	unsigned char *ar[AR_LEN];
     69  	int i = 1;
     70  	int trail;
     71  
     72 @@ -142,7 +148,7 @@ static void trinkle(unsigned char *head, size_t width, cmpfun cmp, void *arg, si
     73  			}
     74  		}
     75  
     76 -		ar[i++] = stepson;
     77 +		ar[i++ & AR_MASK] = stepson;
     78  		head = stepson;
     79  		trail = pntz(p);
     80  		shr(p, trail);
     81 @@ -150,7 +156,7 @@ static void trinkle(unsigned char *head, size_t width, cmpfun cmp, void *arg, si
     82  		trusty = 0;
     83  	}
     84  	if(!trusty) {
     85 -		cycle(width, ar, i);
     86 +		cycle(width, ar, i & AR_MASK);
     87  		sift(head, width, cmp, arg, pshift, lp);
     88  	}
     89  }
     90 -- 
     91 cgit v1.2.1
     92