xsigstack.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /* sigaltstack wrappers.
  2. Copyright (C) 2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <support/xsignal.h>
  16. #include <support/support.h>
  17. #include <support/xunistd.h>
  18. #include <support/check.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <sys/mman.h>
  22. #include <sys/param.h> /* roundup, MAX */
  23. #ifndef MAP_NORESERVE
  24. # define MAP_NORESERVE 0
  25. #endif
  26. #ifndef MAP_STACK
  27. # define MAP_STACK 0
  28. #endif
  29. /* The "cookie" returned by xalloc_sigstack points to one of these
  30. structures. */
  31. struct sigstack_desc
  32. {
  33. void *alloc_base; /* Base address of the complete allocation. */
  34. size_t alloc_size; /* Size of the complete allocation. */
  35. stack_t alt_stack; /* The address and size of the stack itself. */
  36. stack_t old_stack; /* The previous signal stack. */
  37. };
  38. void *
  39. xalloc_sigstack (size_t size)
  40. {
  41. size_t pagesize = sysconf (_SC_PAGESIZE);
  42. if (pagesize == -1)
  43. FAIL_EXIT1 ("sysconf (_SC_PAGESIZE): %m\n");
  44. /* Always supply at least MINSIGSTKSZ space; passing 0 as size means
  45. only that much space. No matter what the number is, round it up
  46. to a whole number of pages. */
  47. size_t stacksize = roundup (size + MINSIGSTKSZ, pagesize);
  48. /* The guard bands need to be large enough to intercept offset
  49. accesses from a stack address that might otherwise hit another
  50. mapping. Make them at least twice as big as the stack itself, to
  51. defend against an offset by the entire size of a large
  52. stack-allocated array. The minimum is 1MiB, which is arbitrarily
  53. chosen to be larger than any "typical" wild pointer offset.
  54. Again, no matter what the number is, round it up to a whole
  55. number of pages. */
  56. size_t guardsize = roundup (MAX (2 * stacksize, 1024 * 1024), pagesize);
  57. struct sigstack_desc *desc = xmalloc (sizeof (struct sigstack_desc));
  58. desc->alloc_size = guardsize + stacksize + guardsize;
  59. /* Use MAP_NORESERVE so that RAM will not be wasted on the guard
  60. bands; touch all the pages of the actual stack before returning,
  61. so we know they are allocated. */
  62. desc->alloc_base = xmmap (0,
  63. desc->alloc_size,
  64. PROT_READ|PROT_WRITE,
  65. MAP_PRIVATE|MAP_ANONYMOUS|MAP_NORESERVE|MAP_STACK,
  66. -1);
  67. xmprotect (desc->alloc_base, guardsize, PROT_NONE);
  68. xmprotect (desc->alloc_base + guardsize + stacksize, guardsize, PROT_NONE);
  69. memset (desc->alloc_base + guardsize, 0xA5, stacksize);
  70. desc->alt_stack.ss_sp = desc->alloc_base + guardsize;
  71. desc->alt_stack.ss_flags = 0;
  72. desc->alt_stack.ss_size = stacksize;
  73. if (sigaltstack (&desc->alt_stack, &desc->old_stack))
  74. FAIL_EXIT1 ("sigaltstack (new stack: sp=%p, size=%zu, flags=%u): %m\n",
  75. desc->alt_stack.ss_sp, desc->alt_stack.ss_size,
  76. desc->alt_stack.ss_flags);
  77. return desc;
  78. }
  79. void
  80. xfree_sigstack (void *stack)
  81. {
  82. struct sigstack_desc *desc = stack;
  83. if (sigaltstack (&desc->old_stack, 0))
  84. FAIL_EXIT1 ("sigaltstack (restore old stack: sp=%p, size=%zu, flags=%u): "
  85. "%m\n", desc->old_stack.ss_sp, desc->old_stack.ss_size,
  86. desc->old_stack.ss_flags);
  87. xmunmap (desc->alloc_base, desc->alloc_size);
  88. free (desc);
  89. }
  90. void
  91. xget_sigstack_location (const void *stack, unsigned char **addrp, size_t *sizep)
  92. {
  93. const struct sigstack_desc *desc = stack;
  94. *addrp = desc->alt_stack.ss_sp;
  95. *sizep = desc->alt_stack.ss_size;
  96. }