Hi all,

I figured out the cause behind #1428; there's (sometimes) a mismatch
between the size of the temporary stack and the number of arguments that
we tell C_reclaim to restore from the temporary stack (which gets stored
in C_restart_c and read by the trampoline).

The fun bit is that CHICKEN_run would ignore C_restart_c and directly
calculate the effective temporary stack size, which means we'd usually
ignore this problem (but we'd call C_restart_trampoline with C_restart_c
instead of argcount, which _should_ cause issues too...).  However,
C_callback would do it as designed, by looking at C_restart_c.

This means that in most cases we wouldn't even notice when C_restart_c
was larger than the temporary stack, only in C_callback could this fail.

When is C_restart_c too large?  Well, just check the patch; there are
a few places in the code where we call C_save() a number of times but
then pass "c" (the size of the argvector for the called function) to
C_reclaim(), but we would save less values than the called function
accepted.

All of this means we'd read bytes beyond the bottom of the temporary
stack, which is memory that *might* be unallocated (but will typically
just be the mutation stack, unless either of them was resized).

I think this cannot be exploited except perhaps if you're extremely
[un]lucky as a denial of service via segfault (but would love a second
opinion on this).

Attached are patches for master and chicken-5.

An alternative would be to always copy everything from the temporary
stack (like we do with "argcount" in CHICKEN_run before the patch),
which would be somewhat simpler but maybe also more susceptible to
subtle bugs, so I prefer (for now at least) the current patch.

Cheers,
Peter
From 1faf857230dd1e494aaeab08a6b6cf099920f1ef Mon Sep 17 00:00:00 2001
From: Peter Bex <[email protected]>
Date: Fri, 1 Dec 2017 21:29:17 +0100
Subject: [PATCH] Fix calls to C_reclaim to use the number of saved objects
 (#1428)

When the argvector change was introduced, several C_reclaim calls were
incorrectly changed to use the size of the argvector to the C function
that would invoke C_reclaim.  In many cases we don't save all the
arguments, which means "c" would be larger than the size of the
temporary stack.

The value of "c" gets set to C_restart_c, which will be used in the
setjmp trampoline as the size of the buffer to set aside for the
re-invoked closure's argvector.  Then, we read C_restart_c words from
the stack top, which might overshoot the temporary stack bottom, which
means we're potentially reading beyond malloced memory.

Finally, CHICKEN_initialize would save 2 objects on the temp stack but
neglected to set C_restart_c to the correct value.  This appeared to
work only because CHICKEN_run happened to look at the effective stack
size rather than the restart count.  Now, we use the restart count and
also assert that it's always identical to the stack size.  Any
mismatch must be due to a bug.

This is not an exploitable bug because the copied bytes are
inaccessible: they're lying just beyond the highest argument in the
argvector, which is inaccessible from Scheme.  The worst that can
happen is that we try to read from a page that doesn't belong to us
and we'll segfault.
---
 NEWS      |  2 ++
 runtime.c | 19 ++++++++++---------
 2 files changed, 12 insertions(+), 9 deletions(-)

diff --git a/NEWS b/NEWS
index 10d677b9..f3b00b40 100644
--- a/NEWS
+++ b/NEWS
@@ -167,6 +167,8 @@
   - Correctly calculate memory requirements of Scheme objects produced
     from foreign types with "const" qualifiers, avoiding memory
     corruption (#1424, thanks to Vasilij Schneidermann and Lemonboy)
+  - Do not read beyond temporary stack buffer, which could lead to
+    a crash when returning from a foreign callback (#1428).
 
 4.12.0
 
diff --git a/runtime.c b/runtime.c
index 499fc922..ba79a917 100644
--- a/runtime.c
+++ b/runtime.c
@@ -845,6 +845,7 @@ int CHICKEN_initialize(int heap, int stack, int symbols, void *toplevel)
   C_set_block_item(k0, 0, (C_word)termination_continuation);
   C_save(k0);
   C_save(C_SCHEME_UNDEFINED);
+  C_restart_c = 2;
   return 1;
 }
 
@@ -1536,10 +1537,9 @@ C_word CHICKEN_run(void *toplevel)
     /* We must copy the argvector onto the stack, because
      * any subsequent save() will otherwise clobber it.
      */
-    int argcount = C_temporary_stack_bottom - C_temporary_stack;
-    C_word *p = C_alloc(argcount);
-
-    C_memcpy(p, C_temporary_stack, argcount * sizeof(C_word));
+    C_word *p = C_alloc(C_restart_c);
+    assert(C_restart_c == (C_temporary_stack_bottom - C_temporary_stack));
+    C_memcpy(p, C_temporary_stack, C_restart_c * sizeof(C_word));
     C_temporary_stack = C_temporary_stack_bottom;
     ((C_proc)C_restart_trampoline)(C_restart_c, p);
   }
@@ -2115,6 +2115,7 @@ C_word C_fcall C_callback(C_word closure, int argc)
      * any subsequent save() will otherwise clobber it.
      */
     C_word *p = C_alloc(C_restart_c);
+    assert(C_restart_c == (C_temporary_stack_bottom - C_temporary_stack));
     C_memcpy(p, C_temporary_stack, C_restart_c * sizeof(C_word));
     C_temporary_stack = C_temporary_stack_bottom;
     ((C_proc)C_restart_trampoline)(C_restart_c, p);
@@ -9421,7 +9422,7 @@ void C_ccall C_gc(C_word c, C_word *av)
   }
   else if(f) C_fromspace_top = C_fromspace_limit;
 
-  C_reclaim((void *)gc_2, c);
+  C_reclaim((void *)gc_2, 1);
 }
 
 
@@ -10740,7 +10741,7 @@ void C_ccall C_ensure_heap_reserve(C_word c, C_word *av)
   C_save(k);
 
   if(!C_demand(C_bytestowords(C_unfix(n))))
-    C_reclaim((void *)generic_trampoline, c);
+    C_reclaim((void *)generic_trampoline, 1);
 
   p = C_temporary_stack;
   C_temporary_stack = C_temporary_stack_bottom;
@@ -10764,7 +10765,7 @@ void C_ccall C_return_to_host(C_word c, C_word *av)
 
   return_to_host = 1;
   C_save(k);
-  C_reclaim((void *)generic_trampoline, c);
+  C_reclaim((void *)generic_trampoline, 1);
 }
 
 
@@ -12245,7 +12246,7 @@ void C_ccall C_dump_heap_state(C_word c, C_word *av)
   /* make sure heap is compacted */
   C_save(k);
   C_fromspace_top = C_fromspace_limit; /* force major GC */
-  C_reclaim((void *)dump_heap_state_2, c);
+  C_reclaim((void *)dump_heap_state_2, 1);
 }
 
 
@@ -12473,7 +12474,7 @@ void C_ccall C_filter_heap_objects(C_word c, C_word *av)
   C_save(userarg);
   C_save(func);
   C_fromspace_top = C_fromspace_limit; /* force major GC */
-  C_reclaim((void *)filter_heap_objects_2, c);
+  C_reclaim((void *)filter_heap_objects_2, 4);
 }
 
 C_regparm C_word C_fcall C_i_process_sleep(C_word n)
-- 
2.11.0

From bb3e26de2df1f3d00520de60ea0e1b972cf99863 Mon Sep 17 00:00:00 2001
From: Peter Bex <[email protected]>
Date: Fri, 1 Dec 2017 21:29:17 +0100
Subject: [PATCH] Fix calls to C_reclaim to use the number of saved objects
 (#1428)

When the argvector change was introduced, several C_reclaim calls were
incorrectly changed to use the size of the argvector to the C function
that would invoke C_reclaim.  In many cases we don't save all the
arguments, which means "c" would be larger than the size of the
temporary stack.

The value of "c" gets set to C_restart_c, which will be used in the
setjmp trampoline as the size of the buffer to set aside for the
re-invoked closure's argvector.  Then, we read C_restart_c words from
the stack top, which might overshoot the temporary stack bottom, which
means we're potentially reading beyond malloced memory.

Finally, CHICKEN_initialize would save 2 objects on the temp stack but
neglected to set C_restart_c to the correct value.  This appeared to
work only because CHICKEN_run happened to look at the effective stack
size rather than the restart count.  Now, we use the restart count and
also assert that it's always identical to the stack size.  Any
mismatch must be due to a bug.

This is not an exploitable bug because the copied bytes are
inaccessible: they're lying just beyond the highest argument in the
argvector, which is inaccessible from Scheme.  The worst that can
happen is that we try to read from a page that doesn't belong to us
and we'll segfault.
---
 NEWS      |  2 ++
 runtime.c | 18 ++++++++++--------
 2 files changed, 12 insertions(+), 8 deletions(-)

diff --git a/NEWS b/NEWS
index 3b36ebde..aea045f8 100644
--- a/NEWS
+++ b/NEWS
@@ -48,6 +48,8 @@
   - Correctly calculate memory requirements of Scheme objects produced
     from foreign types with "const" qualifiers, avoiding memory
     corruption (#1424, thanks to Vasilij Schneidermann and Lemonboy)
+  - Do not read beyond temporary stack buffer, which could lead to
+    a crash when returning from a foreign callback (#1428).
 
 4.12.0
 
diff --git a/runtime.c b/runtime.c
index 1113defc..eaed0a88 100644
--- a/runtime.c
+++ b/runtime.c
@@ -822,6 +822,7 @@ int CHICKEN_initialize(int heap, int stack, int symbols, void *toplevel)
   C_set_block_item(k0, 0, (C_word)termination_continuation);
   C_save(k0);
   C_save(C_SCHEME_UNDEFINED);
+  C_restart_c = 2;
   return 1;
 }
 
@@ -1510,9 +1511,9 @@ C_word CHICKEN_run(void *toplevel)
     /* We must copy the argvector onto the stack, because
      * any subsequent save() will otherwise clobber it.
      */
-    int argcount = C_temporary_stack_bottom - C_temporary_stack;
-    C_word *p = C_alloc(argcount);
-    C_memcpy(p, C_temporary_stack, argcount * sizeof(C_word));
+    C_word *p = C_alloc(C_restart_c);
+    assert(C_restart_c == (C_temporary_stack_bottom - C_temporary_stack));
+    C_memcpy(p, C_temporary_stack, C_restart_c * sizeof(C_word));
     C_temporary_stack = C_temporary_stack_bottom;
     ((C_proc)C_restart_trampoline)(C_restart_c, p);
   }
@@ -2017,6 +2018,7 @@ C_word C_fcall C_callback(C_word closure, int argc)
      * any subsequent save() will otherwise clobber it.
      */
     C_word *p = C_alloc(C_restart_c);
+    assert(C_restart_c == (C_temporary_stack_bottom - C_temporary_stack));
     C_memcpy(p, C_temporary_stack, C_restart_c * sizeof(C_word));
     C_temporary_stack = C_temporary_stack_bottom;
     ((C_proc)C_restart_trampoline)(C_restart_c, p);
@@ -7447,7 +7449,7 @@ void C_ccall C_gc(C_word c, C_word *av)
   }
   else if(f) C_fromspace_top = C_fromspace_limit;
 
-  C_reclaim((void *)gc_2, c);
+  C_reclaim((void *)gc_2, 1);
 }
 
 
@@ -8251,7 +8253,7 @@ void C_ccall C_ensure_heap_reserve(C_word c, C_word *av)
   C_save(k);
 
   if(!C_demand(C_bytestowords(C_unfix(n))))
-    C_reclaim((void *)generic_trampoline, c);
+    C_reclaim((void *)generic_trampoline, 1);
 
   p = C_temporary_stack;
   C_temporary_stack = C_temporary_stack_bottom;
@@ -8275,7 +8277,7 @@ void C_ccall C_return_to_host(C_word c, C_word *av)
 
   return_to_host = 1;
   C_save(k);
-  C_reclaim((void *)generic_trampoline, c);
+  C_reclaim((void *)generic_trampoline, 1);
 }
 
 
@@ -9606,7 +9608,7 @@ void C_ccall C_dump_heap_state(C_word c, C_word *av)
   /* make sure heap is compacted */
   C_save(k);
   C_fromspace_top = C_fromspace_limit; /* force major GC */
-  C_reclaim((void *)dump_heap_state_2, c);
+  C_reclaim((void *)dump_heap_state_2, 1);
 }
 
 
@@ -9830,7 +9832,7 @@ void C_ccall C_filter_heap_objects(C_word c, C_word *av)
   C_save(userarg);
   C_save(func);
   C_fromspace_top = C_fromspace_limit; /* force major GC */
-  C_reclaim((void *)filter_heap_objects_2, c);
+  C_reclaim((void *)filter_heap_objects_2, 4);
 }
 
 
-- 
2.11.0

Attachment: signature.asc
Description: PGP signature

_______________________________________________
Chicken-hackers mailing list
[email protected]
https://lists.nongnu.org/mailman/listinfo/chicken-hackers

Reply via email to