On Fri, Mar 10, 2017 at 04:36:25PM +0100, lemonboy wrote: > Hello hackers, > during one hell of a trip trough the srfi-4 code I noticed that a > check was missing when we try to allocate a srfi-4 vector on the heap. > You can see by yourself that `(make-u8vector -7 #f #t)' (on a 64-bit > platform) ends up malloc-ing a 1-byte buffer and then calls > C_block_header_init on it, writing over a chunk of memory that's over > the buffer end. Attached is a patch against the chicken-5 branch, I > think it should apply cleanly to the 4.x branch too.
Thanks for the patch. Attached is a different patch that fixes this issue, because I think the error message I got after applying your patch is confusing/misleading: #;2> (make-u8vector -1 1 #t) Error: (make-u8vector) not enough memory - cannot allocate external number vector: -1 The attached patch simplifies the type and range checking by moving it to the "alloc" procedure, to reduce code duplication. The error message is taken from make-vector, and it simply says "size is negative". I decided to keep the header size mask check in C, with the out of memory error message, which is slightly wrong but less invasive than properly fixing it. I think we need to take a good look at how barf() works, and see if we can get rid of it or simplify it, and make it externally callable. But this is something for another patch :) Because this should go to master as well, and we've only started "officially" requiring C99 for CHICKEN 5 I've moved the declaration of the buffer up to the start of the function in that branch. This is probably overly conservative, but better safe than sorry. I also took the opportunity to get rid of some square brackets in this code :) Considering this is a security fix I also added an entry to NEWS. Cheers, Peter
From 0e7fa976b37c4a0f015401f729bef4ed1b50eaac Mon Sep 17 00:00:00 2001 From: LemonBoy <[email protected]> Date: Fri, 10 Mar 2017 16:29:47 +0100 Subject: [PATCH] Add bound checking to all srfi-4 vector allocations. Do what C_allocate_vector already does and prevent the creation of a vector that's too big or too small. We should be very careful to avoid the latter case because the allocation size is directly fed into `malloc' as 'x + sizeof(C_header)' thus making possible to successfully allocate a vector smaller than the C_header structure and get C_block_header_init to write over uninitialized memory. To reduce code duplication, type checking is moved from each of the make-*vector procedures to the common "alloc" helper procedure. Signed-off-by: Peter Bex <[email protected]> --- NEWS | 5 +++++ srfi-4.scm | 33 +++++++++++++-------------------- 2 files changed, 18 insertions(+), 20 deletions(-) diff --git a/NEWS b/NEWS index 46636a9..537745d 100644 --- a/NEWS +++ b/NEWS @@ -61,6 +61,11 @@ 4.12.1 +- Security fixes + - Remove unchecked malloc() call in SRFI-4 constructors when + allocating in non-GC memory, resulting in potential 1-word + buffer overrun and/or segfault (thanks to Lemonboy). + - Core Libraries - Unit "posix": If file-lock, file-lock/blocking or file-unlock are interrupted by a signal, we now retry (thanks to Joerg Wittenberger). diff --git a/srfi-4.scm b/srfi-4.scm index d135815..1e3dc24 100644 --- a/srfi-4.scm +++ b/srfi-4.scm @@ -355,24 +355,27 @@ EOF (define make-u64vector) (define release-number-vector) -(let* ([ext-alloc - (foreign-lambda* scheme-object ([int bytes]) +(let* ((ext-alloc + (foreign-lambda* scheme-object ((int bytes)) + "if (bytes > C_HEADER_SIZE_MASK) C_return(C_SCHEME_FALSE);" "C_word *buf = (C_word *)C_malloc(bytes + sizeof(C_header));" "if(buf == NULL) C_return(C_SCHEME_FALSE);" "C_block_header_init(buf, C_make_header(C_BYTEVECTOR_TYPE, bytes));" - "C_return(buf);") ] - [ext-free - (foreign-lambda* void ([scheme-object bv]) - "C_free((void *)C_block_item(bv, 1));") ] - [alloc + "C_return(buf);") ) + (ext-free + (foreign-lambda* void ((scheme-object bv)) + "C_free((void *)C_block_item(bv, 1));") ) + (alloc (lambda (loc len ext?) + (##sys#check-fixnum len loc) + (when (fx< len 0) (##sys#error loc "size is negative" len)) (if ext? - (let ([bv (ext-alloc len)]) + (let ((bv (ext-alloc len))) (or bv (##sys#error loc "not enough memory - cannot allocate external number vector" len)) ) - (let ([bv (##sys#allocate-vector len #t #f #t)]) ; this could be made better... + (let ((bv (##sys#allocate-vector len #t #f #t))) ; this could be made better... (##core#inline "C_string_to_bytevector" bv) - bv) ) ) ] ) + bv) ) ) ) ) (set! release-number-vector (lambda (v) @@ -382,7 +385,6 @@ EOF (set! make-u8vector (lambda (len #!optional (init #f) (ext? #f) (fin? #t)) - (##sys#check-fixnum len 'make-u8vector) (let ((v (##sys#make-structure 'u8vector (alloc 'make-u8vector len ext?)))) (when (and ext? fin?) (set-finalizer! v ext-free)) (if (not init) @@ -395,7 +397,6 @@ EOF (set! make-s8vector (lambda (len #!optional (init #f) (ext? #f) (fin? #t)) - (##sys#check-fixnum len 'make-s8vector) (let ((v (##sys#make-structure 's8vector (alloc 'make-s8vector len ext?)))) (when (and ext? fin?) (set-finalizer! v ext-free)) (if (not init) @@ -408,7 +409,6 @@ EOF (set! make-u16vector (lambda (len #!optional (init #f) (ext? #f) (fin? #t)) - (##sys#check-fixnum len 'make-u16vector) (let ((v (##sys#make-structure 'u16vector (alloc 'make-u16vector (##core#inline "C_fixnum_shift_left" len 1) ext?)))) (when (and ext? fin?) (set-finalizer! v ext-free)) (if (not init) @@ -421,7 +421,6 @@ EOF (set! make-s16vector (lambda (len #!optional (init #f) (ext? #f) (fin? #t)) - (##sys#check-fixnum len 'make-s16vector) (let ((v (##sys#make-structure 's16vector (alloc 'make-s16vector (##core#inline "C_fixnum_shift_left" len 1) ext?)))) (when (and ext? fin?) (set-finalizer! v ext-free)) (if (not init) @@ -434,7 +433,6 @@ EOF (set! make-u32vector (lambda (len #!optional (init #f) (ext? #f) (fin? #t)) - (##sys#check-fixnum len 'make-u32vector) (let ((v (##sys#make-structure 'u32vector (alloc 'make-u32vector (##core#inline "C_fixnum_shift_left" len 2) ext?)))) (when (and ext? fin?) (set-finalizer! v ext-free)) (if (not init) @@ -447,7 +445,6 @@ EOF (set! make-u64vector (lambda (len #!optional (init #f) (ext? #f) (fin? #t)) - (##sys#check-fixnum len 'make-u64vector) (let ((v (##sys#make-structure 'u64vector (alloc 'make-u64vector (##core#inline "C_fixnum_shift_left" len 3) ext?)))) (when (and ext? fin?) (set-finalizer! v ext-free)) (if (not init) @@ -460,7 +457,6 @@ EOF (set! make-s32vector (lambda (len #!optional (init #f) (ext? #f) (fin? #t)) - (##sys#check-fixnum len 'make-s32vector) (let ((v (##sys#make-structure 's32vector (alloc 'make-s32vector (##core#inline "C_fixnum_shift_left" len 2) ext?)))) (when (and ext? fin?) (set-finalizer! v ext-free)) (if (not init) @@ -473,7 +469,6 @@ EOF (set! make-s64vector (lambda (len #!optional (init #f) (ext? #f) (fin? #t)) - (##sys#check-fixnum len 'make-s64vector) (let ((v (##sys#make-structure 's64vector (alloc 'make-s64vector (##core#inline "C_fixnum_shift_left" len 3) ext?)))) (when (and ext? fin?) (set-finalizer! v ext-free)) (if (not init) @@ -486,7 +481,6 @@ EOF (set! make-f32vector (lambda (len #!optional (init #f) (ext? #f) (fin? #t)) - (##sys#check-fixnum len 'make-f32vector) (let ((v (##sys#make-structure 'f32vector (alloc 'make-f32vector (##core#inline "C_fixnum_shift_left" len 2) ext?)))) (when (and ext? fin?) (set-finalizer! v ext-free)) (if (not init) @@ -501,7 +495,6 @@ EOF (set! make-f64vector (lambda (len #!optional (init #f) (ext? #f) (fin? #t)) - (##sys#check-fixnum len 'make-f64vector) (let ((v (##sys#make-structure 'f64vector (alloc 'make-f64vector (##core#inline "C_fixnum_shift_left" len 3) ext?)))) -- 2.1.4
From df82379509a8b8b835a10c45264d7fec75c3ce24 Mon Sep 17 00:00:00 2001 From: LemonBoy <[email protected]> Date: Fri, 10 Mar 2017 16:29:47 +0100 Subject: [PATCH] Add bound checking to all srfi-4 vector allocations. Do what C_allocate_vector already does and prevent the creation of a vector that's too big or too small. We should be very careful to avoid the latter case because the allocation size is directly fed into `malloc' as 'x + sizeof(C_header)' thus making possible to successfully allocate a vector smaller than the C_header structure and get C_block_header_init to write over uninitialized memory. To reduce code duplication, type checking is moved from each of the make-*vector procedures to the common "alloc" helper procedure. Signed-off-by: Peter Bex <[email protected]> --- NEWS | 5 +++++ srfi-4.scm | 32 ++++++++++++++------------------ 2 files changed, 19 insertions(+), 18 deletions(-) diff --git a/NEWS b/NEWS index 32f5186..b134e0e 100644 --- a/NEWS +++ b/NEWS @@ -1,5 +1,10 @@ 4.12.1 +- Security fixes + - Remove unchecked malloc() call in SRFI-4 constructors when + allocating in non-GC memory, resulting in potential 1-word + buffer overrun and/or segfault (thanks to Lemonboy). + - Core Libraries - Unit "posix": If file-lock, file-lock/blocking or file-unlock are interrupted by a signal, we now retry (thanks to Joerg Wittenberger). diff --git a/srfi-4.scm b/srfi-4.scm index 7f5412b..8db486f 100644 --- a/srfi-4.scm +++ b/srfi-4.scm @@ -255,24 +255,28 @@ EOF ;;; Basic constructors: -(let* ([ext-alloc +(let* ((ext-alloc (foreign-lambda* scheme-object ([int bytes]) - "C_word *buf = (C_word *)C_malloc(bytes + sizeof(C_header));" + "C_word *buf;" + "if (bytes > C_HEADER_SIZE_MASK) C_return(C_SCHEME_FALSE);" + "buf = (C_word *)C_malloc(bytes + sizeof(C_header));" "if(buf == NULL) C_return(C_SCHEME_FALSE);" "C_block_header_init(buf, C_make_header(C_BYTEVECTOR_TYPE, bytes));" - "C_return(buf);") ] - [ext-free - (foreign-lambda* void ([scheme-object bv]) - "C_free((void *)C_block_item(bv, 1));") ] - [alloc + "C_return(buf);") ) + (ext-free + (foreign-lambda* void ((scheme-object bv)) + "C_free((void *)C_block_item(bv, 1));") ) + (alloc (lambda (loc len ext?) + (##sys#check-exact len loc) + (when (fx< len 0) (##sys#error loc "size is negative" len)) (if ext? - (let ([bv (ext-alloc len)]) + (let ((bv (ext-alloc len))) (or bv (##sys#error loc "not enough memory - cannot allocate external number vector" len)) ) - (let ([bv (##sys#allocate-vector len #t #f #t)]) ; this could be made better... + (let ((bv (##sys#allocate-vector len #t #f #t))) ; this could be made better... (##core#inline "C_string_to_bytevector" bv) - bv) ) ) ] ) + bv) ) ) ) ) (set! release-number-vector (lambda (v) @@ -282,7 +286,6 @@ EOF (set! make-u8vector (lambda (len #!optional (init #f) (ext? #f) (fin? #t)) - (##sys#check-exact len 'make-u8vector) (let ((v (##sys#make-structure 'u8vector (alloc 'make-u8vector len ext?)))) (when (and ext? fin?) (set-finalizer! v ext-free)) (if (not init) @@ -295,7 +298,6 @@ EOF (set! make-s8vector (lambda (len #!optional (init #f) (ext? #f) (fin? #t)) - (##sys#check-exact len 'make-s8vector) (let ((v (##sys#make-structure 's8vector (alloc 'make-s8vector len ext?)))) (when (and ext? fin?) (set-finalizer! v ext-free)) (if (not init) @@ -308,7 +310,6 @@ EOF (set! make-u16vector (lambda (len #!optional (init #f) (ext? #f) (fin? #t)) - (##sys#check-exact len 'make-u16vector) (let ((v (##sys#make-structure 'u16vector (alloc 'make-u16vector (##core#inline "C_fixnum_shift_left" len 1) ext?)))) (when (and ext? fin?) (set-finalizer! v ext-free)) (if (not init) @@ -321,7 +322,6 @@ EOF (set! make-s16vector (lambda (len #!optional (init #f) (ext? #f) (fin? #t)) - (##sys#check-exact len 'make-s16vector) (let ((v (##sys#make-structure 's16vector (alloc 'make-s16vector (##core#inline "C_fixnum_shift_left" len 1) ext?)))) (when (and ext? fin?) (set-finalizer! v ext-free)) (if (not init) @@ -334,7 +334,6 @@ EOF (set! make-u32vector (lambda (len #!optional (init #f) (ext? #f) (fin? #t)) - (##sys#check-exact len 'make-u32vector) (let ((v (##sys#make-structure 'u32vector (alloc 'make-u32vector (##core#inline "C_fixnum_shift_left" len 2) ext?)))) (when (and ext? fin?) (set-finalizer! v ext-free)) (if (not init) @@ -347,7 +346,6 @@ EOF (set! make-s32vector (lambda (len #!optional (init #f) (ext? #f) (fin? #t)) - (##sys#check-exact len 'make-s32vector) (let ((v (##sys#make-structure 's32vector (alloc 'make-s32vector (##core#inline "C_fixnum_shift_left" len 2) ext?)))) (when (and ext? fin?) (set-finalizer! v ext-free)) (if (not init) @@ -360,7 +358,6 @@ EOF (set! make-f32vector (lambda (len #!optional (init #f) (ext? #f) (fin? #t)) - (##sys#check-exact len 'make-f32vector) (let ((v (##sys#make-structure 'f32vector (alloc 'make-f32vector (##core#inline "C_fixnum_shift_left" len 2) ext?)))) (when (and ext? fin?) (set-finalizer! v ext-free)) (if (not init) @@ -375,7 +372,6 @@ EOF (set! make-f64vector (lambda (len #!optional (init #f) (ext? #f) (fin? #t)) - (##sys#check-exact len 'make-f64vector) (let ((v (##sys#make-structure 'f64vector (alloc 'make-f64vector (##core#inline "C_fixnum_shift_left" len 3) ext?)))) -- 2.1.4
signature.asc
Description: Digital signature
_______________________________________________ Chicken-hackers mailing list [email protected] https://lists.nongnu.org/mailman/listinfo/chicken-hackers
