dpkg-buildflags.1 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. .\" dpkg manual page - dpkg-buildflags(1)
  2. .\"
  3. .\" Copyright © 2010-2011 Raphaël Hertzog <hertzog@debian.org>
  4. .\" Copyright © 2011 Kees Cook <kees@debian.org>
  5. .\"
  6. .\" This is free software; you can redistribute it and/or modify
  7. .\" it under the terms of the GNU General Public License as published by
  8. .\" the Free Software Foundation; either version 2 of the License, or
  9. .\" (at your option) any later version.
  10. .\"
  11. .\" This is distributed in the hope that it will be useful,
  12. .\" but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. .\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. .\" GNU General Public License for more details.
  15. .\"
  16. .\" You should have received a copy of the GNU General Public License
  17. .\" along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. .
  19. .TH dpkg\-buildflags 1 "2011-09-13" "Debian Project" "dpkg suite"
  20. .SH NAME
  21. dpkg\-buildflags \- returns build flags to use during package build
  22. .
  23. .SH SYNOPSIS
  24. .B dpkg\-buildflags
  25. .RI [ option "...] [" command ]
  26. .
  27. .SH DESCRIPTION
  28. \fBdpkg\-buildflags\fP is a tool to retrieve compilation flags to use during
  29. build of Debian packages.
  30. .
  31. The default flags are defined by the vendor but they can be
  32. extended/overriden in several ways:
  33. .IP 1.
  34. system-wide with \fB/etc/dpkg/buildflags.conf\fP;
  35. .IP 2.
  36. for the current user with \fB$XDG_CONFIG_HOME/dpkg/buildflags.conf\fP
  37. where \fB$XDG_CONFIG_HOME\fP defaults to \fB$HOME/.config\fP;
  38. .IP 3.
  39. temporarily by the user with environment variables (see section
  40. \fBENVIRONMENT\fP);
  41. .IP 4.
  42. dynamically by the package maintainer with environment variables set via
  43. \fBdebian/rules\fP (see section \fBENVIRONMENT\fP).
  44. .P
  45. The configuration files can contain two types of directives:
  46. .TP
  47. .BI SET " flag value"
  48. Override the flag named \fIflag\fP to have the value \fIvalue\fP.
  49. .TP
  50. .BI STRIP " flag value"
  51. Strip from the flag named \fIflag\fP all the build flags listed in \fIvalue\fP.
  52. .TP
  53. .BI APPEND " flag value"
  54. Extend the flag named \fIflag\fP by appending the options given in \fIvalue\fP.
  55. A space is prepended to the appended value if the flag's current value is non-empty.
  56. .TP
  57. .BI PREPEND " flag value"
  58. Extend the flag named \fIflag\fP by prepending the options given in \fIvalue\fP.
  59. A space is appended to the prepended value if the flag's current value is non-empty.
  60. .P
  61. The configuration files can contain comments on lines starting with a hash
  62. (#). Empty lines are also ignored.
  63. .SH COMMANDS
  64. .TP
  65. .BI \-\-dump
  66. Print to standard output all compilation flags and their values. It prints
  67. one flag per line separated from its value by an equal sign
  68. ("\fIflag\fP=\fIvalue\fP"). This is the default action.
  69. .TP
  70. .BI \-\-list
  71. Print the list of flags supported by the current vendor
  72. (one per line). See the \fBSUPPORTED FLAGS\fP section for more
  73. information about them.
  74. .TP
  75. .BI \-\-export= format
  76. Print to standard output shell (if \fIformat\fP is \fBsh\fP) or make
  77. (if \fIformat\fP is \fBmake\fP) commands that can be used to export
  78. all the compilation flags in the environment. If \fIformat\fP is
  79. \fBconfigure\fP then the output can be used on a \fB./configure\fP
  80. command-line. If the \fIformat\fP value is not
  81. given, \fBsh\fP is assumed. Only compilation flags starting with an
  82. upper case character are included, others are assumed to not be suitable
  83. for the environment.
  84. .TP
  85. .BI \-\-get " flag"
  86. Print the value of the flag on standard output. Exits with 0
  87. if the flag is known otherwise exits with 1.
  88. .TP
  89. .BI \-\-origin " flag"
  90. Print the origin of the value that is returned by \fB\-\-get\fP. Exits
  91. with 0 if the flag is known otherwise exits with 1. The origin can be one
  92. of the following values:
  93. .RS
  94. .TP
  95. .B vendor
  96. the original flag set by the vendor is returned;
  97. .TP
  98. .B system
  99. the flag is set/modified by a system-wide configuration;
  100. .TP
  101. .B user
  102. the flag is set/modified by a user-specific configuration;
  103. .TP
  104. .B env
  105. the flag is set/modified by an environment-specific configuration.
  106. .RE
  107. .TP
  108. .BI \-\-query\-features " area"
  109. Print the features enabled for a given area. The only currently recognized
  110. area is \fBhardening\fP. Exits with 0 if the area is known otherwise exits
  111. with 1.
  112. .IP
  113. The output format is RFC822 header-style, with one section per feature.
  114. For example:
  115. .IP
  116. .nf
  117. Feature: pie
  118. Enabled: no
  119. Feature: stackprotector
  120. Enabled: yes
  121. .fi
  122. .TP
  123. .B \-\-help
  124. Show the usage message and exit.
  125. .TP
  126. .B \-\-version
  127. Show the version and exit.
  128. .
  129. .SH SUPPORTED FLAGS
  130. .TP
  131. .B CFLAGS
  132. Options for the C compiler. The default value set by the vendor
  133. includes \fI\-g\fP and the default optimization level (\fI\-O2\fP usually,
  134. or \fI\-O0\fP if the \fBDEB_BUILD_OPTIONS\fP environment variable defines
  135. \fInoopt\fP).
  136. .TP
  137. .B CPPFLAGS
  138. Options for the C preprocessor. Default value: empty.
  139. .TP
  140. .B CXXFLAGS
  141. Options for the C++ compiler. Same as \fBCFLAGS\fP.
  142. .TP
  143. .B FFLAGS
  144. Options for the Fortran compiler. Same as \fBCFLAGS\fP.
  145. .TP
  146. .B LDFLAGS
  147. Options passed to the compiler when linking executables or shared
  148. objects (if the linker is called directly, then
  149. .B \-Wl
  150. and
  151. .B ,
  152. have to be stripped from these options). Default value: empty.
  153. .
  154. .SH FILES
  155. .TP
  156. .B /etc/dpkg/buildflags.conf
  157. System wide configuration file.
  158. .TP
  159. .BR $XDG_CONFIG_HOME/dpkg/buildflags.conf " or " $HOME/.config/dpkg/buildflags.conf
  160. User configuration file.
  161. .SH ENVIRONMENT
  162. There are 2 sets of environment variables doing the same operations, the
  163. first one (DEB_\fIflag\fP_\fIop\fP) should never be used within
  164. \fBdebian/rules\fP. It's meant for any user that wants to rebuild the
  165. source package with different build flags. The second set
  166. (DEB_\fIflag\fP_MAINT_\fIop\fP) should only be used in \fBdebian/rules\fP
  167. by package maintainers to change the resulting build flags.
  168. .TP
  169. .BI DEB_ flag _SET
  170. .TQ
  171. .BI DEB_ flag _MAINT_SET
  172. This variable can be used to force the value returned for the given
  173. \fIflag\fP.
  174. .TP
  175. .BI DEB_ flag _STRIP
  176. .TQ
  177. .BI DEB_ flag _MAINT_STRIP
  178. This variable can be used to provide a space separated list of options
  179. that will be stripped from the set of flags returned for the given
  180. \fIflag\fP.
  181. .TP
  182. .BI DEB_ flag _APPEND
  183. .TQ
  184. .BI DEB_ flag _MAINT_APPEND
  185. This variable can be used to append supplementary options to the value
  186. returned for the given \fIflag\fP.
  187. .TP
  188. .BI DEB_ flag _PREPEND
  189. .TQ
  190. .BI DEB_ flag _MAINT_PREPEND
  191. This variable can be used to prepend supplementary options to the value
  192. returned for the given \fIflag\fP.
  193. .TP
  194. .B DEB_BUILD_MAINT_OPTIONS
  195. This variable can be used to disable/enable various hardening build
  196. flags through the \fBhardening\fP option. See the \fBHARDENING\fP section
  197. for details.
  198. .
  199. .SH HARDENING
  200. Several compile-time options (detailed below) can be used to help harden
  201. a resulting binary against memory corruption attacks, or provide
  202. additional warning messages during compilation. Except as noted below,
  203. these are enabled by default for architectures that support them.
  204. .P
  205. Each hardening feature can be enabled and disabled in the
  206. \fBDEB_BUILD_MAINT_OPTIONS\fP environment variable's \fBhardening\fP
  207. value with the "+" and "\-" modifier. For example, to enable the
  208. "pie" feature and disable the "fortify" feature you can do this
  209. in \fBdebian/rules\fP:
  210. .P
  211. export DEB_BUILD_MAINT_OPTIONS=hardening=+pie,\-fortify
  212. .P
  213. The special feature \fBall\fP can be used to enable or disable all
  214. hardening features at the same time. Thus disabling everything and
  215. enabling only "format" and "fortify" can be achieved with:
  216. .P
  217. export DEB_BUILD_MAINT_OPTIONS=hardening=\-all,+format,+fortify
  218. .
  219. .TP
  220. .B format
  221. This setting (enabled by default) adds
  222. .B \-Wformat \-Wformat\-security \-Werror=format\-security
  223. to \fBCFLAGS\fP and \fBCXXFLAGS\fP. This will warn about improper format
  224. string uses, and will fail when format functions are used in a way that
  225. that represent possible security problems. At present, this warns about
  226. calls to \fBprintf\fP and \fBscanf\fP functions where the format string is
  227. not a string literal and there are no format arguments, as in
  228. \fBprintf(foo);\fP instead of \fPprintf("%s", foo);\fP
  229. This may be a security hole if the format string came from untrusted
  230. input and contains "%n".
  231. .
  232. .TP
  233. .B fortify
  234. This setting (enabled by default) adds
  235. .B \-D_FORTIFY_SOURCE=2
  236. to \fBCPPFLAGS\fP. During code generation the compiler
  237. knows a great deal of information about buffer sizes (where possible), and
  238. attempts to replace insecure unlimited length buffer function calls with
  239. length-limited ones. This is especially useful for old, crufty code.
  240. Additionally, format strings in writable memory that contain '%n' are
  241. blocked. If an application depends on such a format string, it will need
  242. to be worked around.
  243. Note that for this option to have any effect, the source must also
  244. be compiled with \fB\-O1\fP or higher.
  245. .TP
  246. .B stackprotector
  247. This setting (enabled by default) adds
  248. .B \-fstack-protector \-\-param=ssp\-buffer\-size=4
  249. to \fBCFLAGS\fP and \fBCXXFLAGS\fP. This adds safety checks against stack
  250. overwrites. This renders many potential code injection attacks into
  251. aborting situations. In the best case this turns code injection
  252. vulnerabilities into denial of service or into non-issues (depending on
  253. the application).
  254. This feature requires linking against glibc (or another provider of
  255. \fB__stack_chk_fail\fP), so needs to be disabled when building with
  256. \fB\-nostdlib\fP or \fB\-ffreestanding\fP or similar.
  257. .
  258. .TP
  259. .B relro
  260. This setting (enabled by default) adds
  261. .B \-Wl,\-z,relro
  262. to \fBLDFLAGS\fP. During program load, several ELF memory sections need
  263. to be written to by the linker. This flags the loader to turn these
  264. sections read-only before turning over control to the program. Most
  265. notably this prevents GOT overwrite attacks. If this option is disabled,
  266. \fBbindnow\fP will become disabled as well.
  267. .
  268. .TP
  269. .B bindnow
  270. This setting (disabled by default) adds
  271. .B \-Wl,\-z,now
  272. to \fBLDFLAGS\fP. During program load, all dynamic symbols are resolved,
  273. allowing for the entire PLT to be marked read-only (due to \fBrelro\fP
  274. above). The option cannot become enabled if \fBrelro\fP is not enabled.
  275. .
  276. .TP
  277. .B pie
  278. This setting (disabled by default) adds \fB\-fPIE\fP to \fBCFLAGS\fP and
  279. \fBCXXFLAGS\fP, and \fB\-fPIE \-pie\fP to \fBLDFLAGS\fP. Position Independent
  280. Executable are needed to take advantage of Address Space Layout
  281. Randomization, supported by some kernel versions. While ASLR can already
  282. be enforced for data areas in the stack and heap (brk and mmap), the code
  283. areas must be compiled as position-independent. Shared libraries already
  284. do this (\-fPIC), so they gain ASLR automatically, but binary .text
  285. regions need to be build PIE to gain ASLR. When this happens, ROP (Return
  286. Oriented Programming) attacks are much harder since there are no static
  287. locations to bounce off of during a memory corruption attack.
  288. This is not compatible with \fB\-fPIC\fP so care must be taken when
  289. building shared objects.
  290. Additionally, since PIE is implemented via a general register, some
  291. architectures (most notably i386) can see performance losses of up to
  292. 15% in very text-segment-heavy application workloads; most workloads
  293. see less than 1%. Architectures with more general registers (e.g. amd64)
  294. do not see as high a worst-case penalty.