Object-Size-Checking.html 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <!-- Copyright (C) 1988-2017 Free Software Foundation, Inc.
  4. Permission is granted to copy, distribute and/or modify this document
  5. under the terms of the GNU Free Documentation License, Version 1.3 or
  6. any later version published by the Free Software Foundation; with the
  7. Invariant Sections being "Funding Free Software", the Front-Cover
  8. Texts being (a) (see below), and with the Back-Cover Texts being (b)
  9. (see below). A copy of the license is included in the section entitled
  10. "GNU Free Documentation License".
  11. (a) The FSF's Front-Cover Text is:
  12. A GNU Manual
  13. (b) The FSF's Back-Cover Text is:
  14. You have freedom to copy and modify this GNU Manual, like GNU
  15. software. Copies published by the Free Software Foundation raise
  16. funds for GNU development. -->
  17. <!-- Created by GNU Texinfo 5.2, http://www.gnu.org/software/texinfo/ -->
  18. <head>
  19. <title>Using the GNU Compiler Collection (GCC): Object Size Checking</title>
  20. <meta name="description" content="Using the GNU Compiler Collection (GCC): Object Size Checking">
  21. <meta name="keywords" content="Using the GNU Compiler Collection (GCC): Object Size Checking">
  22. <meta name="resource-type" content="document">
  23. <meta name="distribution" content="global">
  24. <meta name="Generator" content="makeinfo">
  25. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  26. <link href="index.html#Top" rel="start" title="Top">
  27. <link href="Option-Index.html#Option-Index" rel="index" title="Option Index">
  28. <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
  29. <link href="C-Extensions.html#C-Extensions" rel="up" title="C Extensions">
  30. <link href="Pointer-Bounds-Checker-builtins.html#Pointer-Bounds-Checker-builtins" rel="next" title="Pointer Bounds Checker builtins">
  31. <link href="x86-specific-memory-model-extensions-for-transactional-memory.html#x86-specific-memory-model-extensions-for-transactional-memory" rel="prev" title="x86 specific memory model extensions for transactional memory">
  32. <style type="text/css">
  33. <!--
  34. a.summary-letter {text-decoration: none}
  35. blockquote.smallquotation {font-size: smaller}
  36. div.display {margin-left: 3.2em}
  37. div.example {margin-left: 3.2em}
  38. div.indentedblock {margin-left: 3.2em}
  39. div.lisp {margin-left: 3.2em}
  40. div.smalldisplay {margin-left: 3.2em}
  41. div.smallexample {margin-left: 3.2em}
  42. div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
  43. div.smalllisp {margin-left: 3.2em}
  44. kbd {font-style:oblique}
  45. pre.display {font-family: inherit}
  46. pre.format {font-family: inherit}
  47. pre.menu-comment {font-family: serif}
  48. pre.menu-preformatted {font-family: serif}
  49. pre.smalldisplay {font-family: inherit; font-size: smaller}
  50. pre.smallexample {font-size: smaller}
  51. pre.smallformat {font-family: inherit; font-size: smaller}
  52. pre.smalllisp {font-size: smaller}
  53. span.nocodebreak {white-space:nowrap}
  54. span.nolinebreak {white-space:nowrap}
  55. span.roman {font-family:serif; font-weight:normal}
  56. span.sansserif {font-family:sans-serif; font-weight:normal}
  57. ul.no-bullet {list-style: none}
  58. -->
  59. </style>
  60. </head>
  61. <body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
  62. <a name="Object-Size-Checking"></a>
  63. <div class="header">
  64. <p>
  65. Next: <a href="Pointer-Bounds-Checker-builtins.html#Pointer-Bounds-Checker-builtins" accesskey="n" rel="next">Pointer Bounds Checker builtins</a>, Previous: <a href="x86-specific-memory-model-extensions-for-transactional-memory.html#x86-specific-memory-model-extensions-for-transactional-memory" accesskey="p" rel="prev">x86 specific memory model extensions for transactional memory</a>, Up: <a href="C-Extensions.html#C-Extensions" accesskey="u" rel="up">C Extensions</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Option-Index.html#Option-Index" title="Index" rel="index">Index</a>]</p>
  66. </div>
  67. <hr>
  68. <a name="Object-Size-Checking-Built_002din-Functions"></a>
  69. <h3 class="section">6.56 Object Size Checking Built-in Functions</h3>
  70. <a name="index-_005f_005fbuiltin_005fobject_005fsize"></a>
  71. <a name="index-_005f_005fbuiltin_005f_005f_005fmemcpy_005fchk"></a>
  72. <a name="index-_005f_005fbuiltin_005f_005f_005fmempcpy_005fchk"></a>
  73. <a name="index-_005f_005fbuiltin_005f_005f_005fmemmove_005fchk"></a>
  74. <a name="index-_005f_005fbuiltin_005f_005f_005fmemset_005fchk"></a>
  75. <a name="index-_005f_005fbuiltin_005f_005f_005fstrcpy_005fchk"></a>
  76. <a name="index-_005f_005fbuiltin_005f_005f_005fstpcpy_005fchk"></a>
  77. <a name="index-_005f_005fbuiltin_005f_005f_005fstrncpy_005fchk"></a>
  78. <a name="index-_005f_005fbuiltin_005f_005f_005fstrcat_005fchk"></a>
  79. <a name="index-_005f_005fbuiltin_005f_005f_005fstrncat_005fchk"></a>
  80. <a name="index-_005f_005fbuiltin_005f_005f_005fsprintf_005fchk"></a>
  81. <a name="index-_005f_005fbuiltin_005f_005f_005fsnprintf_005fchk"></a>
  82. <a name="index-_005f_005fbuiltin_005f_005f_005fvsprintf_005fchk"></a>
  83. <a name="index-_005f_005fbuiltin_005f_005f_005fvsnprintf_005fchk"></a>
  84. <a name="index-_005f_005fbuiltin_005f_005f_005fprintf_005fchk"></a>
  85. <a name="index-_005f_005fbuiltin_005f_005f_005fvprintf_005fchk"></a>
  86. <a name="index-_005f_005fbuiltin_005f_005f_005ffprintf_005fchk"></a>
  87. <a name="index-_005f_005fbuiltin_005f_005f_005fvfprintf_005fchk"></a>
  88. <p>GCC implements a limited buffer overflow protection mechanism that can
  89. prevent some buffer overflow attacks by determining the sizes of objects
  90. into which data is about to be written and preventing the writes when
  91. the size isn&rsquo;t sufficient. The built-in functions described below yield
  92. the best results when used together and when optimization is enabled.
  93. For example, to detect object sizes across function boundaries or to
  94. follow pointer assignments through non-trivial control flow they rely
  95. on various optimization passes enabled with <samp>-O2</samp>. However, to
  96. a limited extent, they can be used without optimization as well.
  97. </p>
  98. <dl>
  99. <dt><a name="index-_005f_005fbuiltin_005fobject_005fsize-1"></a>Built-in Function: <em>size_t</em> <strong>__builtin_object_size</strong> <em>(const void * <var>ptr</var>, int <var>type</var>)</em></dt>
  100. <dd><p>is a built-in construct that returns a constant number of bytes from
  101. <var>ptr</var> to the end of the object <var>ptr</var> pointer points to
  102. (if known at compile time). <code>__builtin_object_size</code> never evaluates
  103. its arguments for side-effects. If there are any side-effects in them, it
  104. returns <code>(size_t) -1</code> for <var>type</var> 0 or 1 and <code>(size_t) 0</code>
  105. for <var>type</var> 2 or 3. If there are multiple objects <var>ptr</var> can
  106. point to and all of them are known at compile time, the returned number
  107. is the maximum of remaining byte counts in those objects if <var>type</var> &amp; 2 is
  108. 0 and minimum if nonzero. If it is not possible to determine which objects
  109. <var>ptr</var> points to at compile time, <code>__builtin_object_size</code> should
  110. return <code>(size_t) -1</code> for <var>type</var> 0 or 1 and <code>(size_t) 0</code>
  111. for <var>type</var> 2 or 3.
  112. </p>
  113. <p><var>type</var> is an integer constant from 0 to 3. If the least significant
  114. bit is clear, objects are whole variables, if it is set, a closest
  115. surrounding subobject is considered the object a pointer points to.
  116. The second bit determines if maximum or minimum of remaining bytes
  117. is computed.
  118. </p>
  119. <div class="smallexample">
  120. <pre class="smallexample">struct V { char buf1[10]; int b; char buf2[10]; } var;
  121. char *p = &amp;var.buf1[1], *q = &amp;var.b;
  122. /* Here the object p points to is var. */
  123. assert (__builtin_object_size (p, 0) == sizeof (var) - 1);
  124. /* The subobject p points to is var.buf1. */
  125. assert (__builtin_object_size (p, 1) == sizeof (var.buf1) - 1);
  126. /* The object q points to is var. */
  127. assert (__builtin_object_size (q, 0)
  128. == (char *) (&amp;var + 1) - (char *) &amp;var.b);
  129. /* The subobject q points to is var.b. */
  130. assert (__builtin_object_size (q, 1) == sizeof (var.b));
  131. </pre></div>
  132. </dd></dl>
  133. <p>There are built-in functions added for many common string operation
  134. functions, e.g., for <code>memcpy</code> <code>__builtin___memcpy_chk</code>
  135. built-in is provided. This built-in has an additional last argument,
  136. which is the number of bytes remaining in object the <var>dest</var>
  137. argument points to or <code>(size_t) -1</code> if the size is not known.
  138. </p>
  139. <p>The built-in functions are optimized into the normal string functions
  140. like <code>memcpy</code> if the last argument is <code>(size_t) -1</code> or if
  141. it is known at compile time that the destination object will not
  142. be overflown. If the compiler can determine at compile time the
  143. object will be always overflown, it issues a warning.
  144. </p>
  145. <p>The intended use can be e.g.
  146. </p>
  147. <div class="smallexample">
  148. <pre class="smallexample">#undef memcpy
  149. #define bos0(dest) __builtin_object_size (dest, 0)
  150. #define memcpy(dest, src, n) \
  151. __builtin___memcpy_chk (dest, src, n, bos0 (dest))
  152. char *volatile p;
  153. char buf[10];
  154. /* It is unknown what object p points to, so this is optimized
  155. into plain memcpy - no checking is possible. */
  156. memcpy (p, &quot;abcde&quot;, n);
  157. /* Destination is known and length too. It is known at compile
  158. time there will be no overflow. */
  159. memcpy (&amp;buf[5], &quot;abcde&quot;, 5);
  160. /* Destination is known, but the length is not known at compile time.
  161. This will result in __memcpy_chk call that can check for overflow
  162. at run time. */
  163. memcpy (&amp;buf[5], &quot;abcde&quot;, n);
  164. /* Destination is known and it is known at compile time there will
  165. be overflow. There will be a warning and __memcpy_chk call that
  166. will abort the program at run time. */
  167. memcpy (&amp;buf[6], &quot;abcde&quot;, 5);
  168. </pre></div>
  169. <p>Such built-in functions are provided for <code>memcpy</code>, <code>mempcpy</code>,
  170. <code>memmove</code>, <code>memset</code>, <code>strcpy</code>, <code>stpcpy</code>, <code>strncpy</code>,
  171. <code>strcat</code> and <code>strncat</code>.
  172. </p>
  173. <p>There are also checking built-in functions for formatted output functions.
  174. </p><div class="smallexample">
  175. <pre class="smallexample">int __builtin___sprintf_chk (char *s, int flag, size_t os, const char *fmt, ...);
  176. int __builtin___snprintf_chk (char *s, size_t maxlen, int flag, size_t os,
  177. const char *fmt, ...);
  178. int __builtin___vsprintf_chk (char *s, int flag, size_t os, const char *fmt,
  179. va_list ap);
  180. int __builtin___vsnprintf_chk (char *s, size_t maxlen, int flag, size_t os,
  181. const char *fmt, va_list ap);
  182. </pre></div>
  183. <p>The added <var>flag</var> argument is passed unchanged to <code>__sprintf_chk</code>
  184. etc. functions and can contain implementation specific flags on what
  185. additional security measures the checking function might take, such as
  186. handling <code>%n</code> differently.
  187. </p>
  188. <p>The <var>os</var> argument is the object size <var>s</var> points to, like in the
  189. other built-in functions. There is a small difference in the behavior
  190. though, if <var>os</var> is <code>(size_t) -1</code>, the built-in functions are
  191. optimized into the non-checking functions only if <var>flag</var> is 0, otherwise
  192. the checking function is called with <var>os</var> argument set to
  193. <code>(size_t) -1</code>.
  194. </p>
  195. <p>In addition to this, there are checking built-in functions
  196. <code>__builtin___printf_chk</code>, <code>__builtin___vprintf_chk</code>,
  197. <code>__builtin___fprintf_chk</code> and <code>__builtin___vfprintf_chk</code>.
  198. These have just one additional argument, <var>flag</var>, right before
  199. format string <var>fmt</var>. If the compiler is able to optimize them to
  200. <code>fputc</code> etc. functions, it does, otherwise the checking function
  201. is called and the <var>flag</var> argument passed to it.
  202. </p>
  203. <hr>
  204. <div class="header">
  205. <p>
  206. Next: <a href="Pointer-Bounds-Checker-builtins.html#Pointer-Bounds-Checker-builtins" accesskey="n" rel="next">Pointer Bounds Checker builtins</a>, Previous: <a href="x86-specific-memory-model-extensions-for-transactional-memory.html#x86-specific-memory-model-extensions-for-transactional-memory" accesskey="p" rel="prev">x86 specific memory model extensions for transactional memory</a>, Up: <a href="C-Extensions.html#C-Extensions" accesskey="u" rel="up">C Extensions</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Option-Index.html#Option-Index" title="Index" rel="index">Index</a>]</p>
  207. </div>
  208. </body>
  209. </html>