Control: tags -1 patch

Upstream patch cherry-picked.

--
tobi

On Wed, 14 Jan 2015 15:13:04 +0100 Moritz Muehlenhoff <j...@inutil.org>
wrote:
> Package: chicken
> Severity: important
> Tags: security
> 
> Hi,
> please see http://www.openwall.com/lists/oss-security/2015/01/12/3
> for details.
> 
> Cheers,
>         Moritz
> 
> 
From 230eed2745ea2b57de3c9073e8596892b1da2d8c Mon Sep 17 00:00:00 2001
From: Moritz Heidkamp <address@hidden>
Date: Sun, 14 Dec 2014 23:33:52 +0100
Subject: [PATCH] Fix buffer overrun in substring-index[-ci]

When passing a start index greater than 0, substring-index[-ci] would
scan past the end of the subject string, leading to bogus results in
case the substring is accidentally run into beyond the end of the
subject. This patch fixes the issue and also adds a range check for the
start index.
---
 data-structures.scm             | 22 ++++++++++++++--------
 tests/data-structures-tests.scm | 11 ++++++++++-
 2 files changed, 24 insertions(+), 9 deletions(-)

--- a/data-structures.scm
+++ b/data-structures.scm
@@ -303,15 +303,21 @@
   (define (traverse which where start test loc)
     (##sys#check-string which loc)
     (##sys#check-string where loc)
-    (let ([wherelen (##sys#size where)]
-	  [whichlen (##sys#size which)] )
+    (let* ((wherelen (##sys#size where))
+	   (whichlen (##sys#size which))
+	   (end (fx- wherelen whichlen)))
       (##sys#check-exact start loc)
-      (let loop ([istart start] [iend whichlen])
-	(cond [(fx> iend wherelen) #f]
-	      [(test istart whichlen) istart]
-	      [else 
-	       (loop (fx+ istart 1)
-		     (fx+ iend 1) ) ] ) ) ) )
+      (if (and (fx>= start 0)
+	       (fx> wherelen start))
+	  (let loop ((istart start))
+	    (cond ((fx> istart end) #f)
+		  ((test istart whichlen) istart)
+		  (else (loop (fx+ istart 1)))))
+	  (##sys#error-hook (foreign-value "C_OUT_OF_RANGE_ERROR" int)
+			    loc
+			    start
+			    wherelen))))
+
   (set! ##sys#substring-index 
     (lambda (which where start)
       (traverse 
--- a/tests/data-structures-tests.scm
+++ b/tests/data-structures-tests.scm
@@ -1,6 +1,6 @@
 ;;;; data-structures-tests.scm
 
-(use data-structures)
+(use data-structures lolevel)
 
 (define-syntax assert-error
   (syntax-rules ()
@@ -54,6 +54,15 @@
 (assert (string=? "x" (string-translate* "ab" '(("ab" . "x")))))
 (assert (string=? "xy" (string-translate* "xyz" '(("z" . "")))))
 
+
+;; This used to fail because substring-index and co. used to search
+;; beyond the end of the subject string when a start index > 0 was
+;; provided. We use object-evict to ensure that the strings are placed
+;; in adjacent memory ranges so we can detect this error.
+(let* ((foo (object-evict (make-string 32 #\x)))
+       (bar (object-evict "y")))
+  (assert (not (substring-index "y" foo 30))))
+
 ;; topological-sort
 
 (assert (equal? '() (topological-sort '() eq?)))

Reply via email to