[PATCH v6 1/5] support: Add support_stack_alloc

Adhemerval Zanella adhemerval.zanella@linaro.org
Thu Jun 24 11:33:35 GMT 2021



On 24/06/2021 06:15, Florian Weimer wrote:
> * Adhemerval Zanella via Libc-alpha:
> 
>> diff --git a/support/support_stack_alloc.c b/support/support_stack_alloc.c
>> new file mode 100644
>> index 0000000000..08323f43d5
>> --- /dev/null
>> +++ b/support/support_stack_alloc.c
>> @@ -0,0 +1,76 @@
> 
>> +  /* The guard bands need to be large enough to intercept offset
>> +     accesses from a stack address that might otherwise hit another
>> +     mapping.  Make them at least twice as big as the stack itself, to
>> +     defend against an offset by the entire size of a large
>> +     stack-allocated array.  The minimum is 1MiB, which is arbitrarily
>> +     chosen to be larger than any "typical" wild pointer offset.
>> +     Again, no matter what the number is, round it up to a whole
>> +     number of pages.  */
>> +  size_t guardsize = roundup (MAX (2 * stacksize, 1024 * 1024), pagesize);
>> +  size_t alloc_size = guardsize + stacksize + guardsize;
>> +  /* Use MAP_NORESERVE so that RAM will not be wasted on the guard
>> +     bands; touch all the pages of the actual stack before returning,
>> +     so we know they are allocated.  */
>> +  void *alloc_base = xmmap (0,
>> +                            alloc_size,
>> +                            PROT_NONE,
>> +                            MAP_PRIVATE|MAP_ANONYMOUS|MAP_NORESERVE|MAP_STACK,
>> +                            -1);
>> +  xmprotect (alloc_base + guardsize, stacksize, PROT_READ | PROT_WRITE);
>> +  memset (alloc_base + guardsize, 0xA5, stacksize);
>> +  return (struct support_stack) { alloc_base + guardsize,
>> +                                  stacksize, guardsize };
>> +}
> 
> Missing _STACK_GROWS_DOWN/_STACK_GROWS_UP support for guard location
> handling, and missing executable stack handling (in case it's needed on
> Hurd for trampolines; I'm not sure what the current state there is).
> 
> But I see it was already missing, so maybe that's not a big deal.

It seems a worth addition, I will update the patch.


More information about the Libc-alpha mailing list