readonly-area.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /* Copyright (C) 2004-2019 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <http://www.gnu.org/licenses/>. */
  14. #include <errno.h>
  15. #include <stdint.h>
  16. #include <stdio.h>
  17. #include <stdio_ext.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include "libio/libioP.h"
  21. /* Return 1 if the whole area PTR .. PTR+SIZE is not writable.
  22. Return -1 if it is writable. */
  23. int
  24. __readonly_area (const char *ptr, size_t size)
  25. {
  26. const void *ptr_end = ptr + size;
  27. FILE *fp = fopen ("/proc/self/maps", "rce");
  28. if (fp == NULL)
  29. {
  30. /* It is the system administrator's choice to not have /proc
  31. available to this process (e.g., because it runs in a chroot
  32. environment. Don't fail in this case. */
  33. if (errno == ENOENT
  34. /* The kernel has a bug in that a process is denied access
  35. to the /proc filesystem if it is set[ug]id. There has
  36. been no willingness to change this in the kernel so
  37. far. */
  38. || errno == EACCES)
  39. return 1;
  40. return -1;
  41. }
  42. /* We need no locking. */
  43. __fsetlocking (fp, FSETLOCKING_BYCALLER);
  44. char *line = NULL;
  45. size_t linelen = 0;
  46. while (! __feof_unlocked (fp))
  47. {
  48. if (_IO_getdelim (&line, &linelen, '\n', fp) <= 0)
  49. break;
  50. char *p;
  51. uintptr_t from = strtoul (line, &p, 16);
  52. if (p == line || *p++ != '-')
  53. break;
  54. char *q;
  55. uintptr_t to = strtoul (p, &q, 16);
  56. if (q == p || *q++ != ' ')
  57. break;
  58. if (from < (uintptr_t) ptr_end && to > (uintptr_t) ptr)
  59. {
  60. /* Found an entry that at least partially covers the area. */
  61. if (*q++ != 'r' || *q++ != '-')
  62. break;
  63. if (from <= (uintptr_t) ptr && to >= (uintptr_t) ptr_end)
  64. {
  65. size = 0;
  66. break;
  67. }
  68. else if (from <= (uintptr_t) ptr)
  69. size -= to - (uintptr_t) ptr;
  70. else if (to >= (uintptr_t) ptr_end)
  71. size -= (uintptr_t) ptr_end - from;
  72. else
  73. size -= to - from;
  74. if (!size)
  75. break;
  76. }
  77. }
  78. fclose (fp);
  79. free (line);
  80. /* If the whole area between ptr and ptr_end is covered by read-only
  81. VMAs, return 1. Otherwise return -1. */
  82. return size == 0 ? 1 : -1;
  83. }