unistd.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * Copyright (C) 2015 Dimitris Papastamos <sin@2f30.org>
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted.
  6. *
  7. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  10. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  12. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  13. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. */
  15. #ifndef _FORTIFY_UNISTD_H
  16. #define _FORTIFY_UNISTD_H
  17. __extension__
  18. #include_next <unistd.h>
  19. #if defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE > 0 && defined(__OPTIMIZE__) && __OPTIMIZE__ > 0
  20. #include "fortify-headers.h"
  21. #ifdef __cplusplus
  22. extern "C" {
  23. #endif
  24. #undef confstr
  25. #undef getcwd
  26. #undef getgroups
  27. #undef gethostname
  28. #undef getlogin_r
  29. #undef pread
  30. #undef read
  31. #undef readlink
  32. #undef readlinkat
  33. #undef ttyname_r
  34. #undef write
  35. _FORTIFY_FN(confstr) size_t confstr(int __n, char *__s, size_t __l)
  36. {
  37. size_t __b = __builtin_object_size(__s, 0);
  38. size_t __r = __orig_confstr(__n, __s, __b > __l ? __l : __b);
  39. if (__l > __b && __r > __b)
  40. __builtin_trap();
  41. return __r;
  42. }
  43. _FORTIFY_FN(getcwd) char *getcwd(char *__s, size_t __l)
  44. {
  45. size_t __b = __builtin_object_size(__s, 0);
  46. if (__l > __b)
  47. __builtin_trap();
  48. return __orig_getcwd(__s, __l);
  49. }
  50. #if defined(_GNU_SOURCE) || defined(_BSD_SOURCE)
  51. #undef getdomainname
  52. _FORTIFY_FN(getdomainname) int getdomainname(char *__s, size_t __l)
  53. {
  54. size_t __b = __builtin_object_size(__s, 0);
  55. if (__l > __b)
  56. __builtin_trap();
  57. return __orig_getdomainname(__s, __l);
  58. }
  59. #endif
  60. _FORTIFY_FN(getgroups) int getgroups(int __l, gid_t *__s)
  61. {
  62. size_t __b = __builtin_object_size(__s, 0);
  63. if (__l < 0 || (size_t)__l > __b / sizeof(gid_t))
  64. __builtin_trap();
  65. return __orig_getgroups(__l, __s);
  66. }
  67. _FORTIFY_FN(gethostname) int gethostname(char *__s, size_t __l)
  68. {
  69. size_t __b = __builtin_object_size(__s, 0);
  70. if (__l > __b)
  71. __builtin_trap();
  72. return __orig_gethostname(__s, __l);
  73. }
  74. _FORTIFY_FN(getlogin_r) int getlogin_r(char *__s, size_t __l)
  75. {
  76. size_t __b = __builtin_object_size(__s, 0);
  77. if (__l > __b)
  78. __builtin_trap();
  79. return __orig_getlogin_r(__s, __l);
  80. }
  81. _FORTIFY_FN(pread) ssize_t pread(int __f, void *__s, size_t __n, off_t __o)
  82. {
  83. size_t __b = __builtin_object_size(__s, 0);
  84. if (__n > __b)
  85. __builtin_trap();
  86. return __orig_pread(__f, __s, __n, __o);
  87. }
  88. _FORTIFY_FN(read) ssize_t read(int __f, void *__s, size_t __n)
  89. {
  90. size_t __b = __builtin_object_size(__s, 0);
  91. if (__n > __b)
  92. __builtin_trap();
  93. return __orig_read(__f, __s, __n);
  94. }
  95. _FORTIFY_FN(readlink) ssize_t readlink(const char *__p, char *__s, size_t __n)
  96. {
  97. size_t __b = __builtin_object_size(__s, 0);
  98. if (__n > __b)
  99. __builtin_trap();
  100. return __orig_readlink(__p, __s, __n);
  101. }
  102. _FORTIFY_FN(readlinkat) ssize_t readlinkat(int __f, const char *__p, char *__s, size_t __n)
  103. {
  104. size_t __b = __builtin_object_size(__s, 0);
  105. if (__n > __b)
  106. __builtin_trap();
  107. return __orig_readlinkat(__f, __p, __s, __n);
  108. }
  109. _FORTIFY_FN(ttyname_r) int ttyname_r(int __f, char *__s, size_t __n)
  110. {
  111. size_t __b = __builtin_object_size(__s, 0);
  112. if (__n > __b)
  113. __builtin_trap();
  114. return __orig_ttyname_r(__f, __s, __n);
  115. }
  116. _FORTIFY_FN(write) ssize_t write(int __f, const void *__s, size_t __n)
  117. {
  118. size_t __b = __builtin_object_size(__s, 0);
  119. if (__n > __b)
  120. __builtin_trap();
  121. return __orig_write(__f, __s, __n);
  122. }
  123. #ifdef __cplusplus
  124. }
  125. #endif
  126. #endif
  127. #endif