dpkg-buildflags.1 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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 "2012-04-03" "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 \-\-status
  76. Display any information that can be useful to explain the behaviour of
  77. \fBdpkg\-buildflags\fP: relevant environment variables, current vendor,
  78. state of all feature flags. Also print the resulting compiler flags with
  79. their origin.
  80. This is intended to be run from \fBdebian/rules\fP, so that the build log
  81. keeps a clear trace of the build flags used. This can be useful to diagnose
  82. problems related to them.
  83. .TP
  84. .BI \-\-export= format
  85. Print to standard output shell (if \fIformat\fP is \fBsh\fP) or make
  86. (if \fIformat\fP is \fBmake\fP) commands that can be used to export
  87. all the compilation flags in the environment. If \fIformat\fP is
  88. \fBconfigure\fP then the output can be used on a \fB./configure\fP
  89. command-line. If the \fIformat\fP value is not
  90. given, \fBsh\fP is assumed. Only compilation flags starting with an
  91. upper case character are included, others are assumed to not be suitable
  92. for the environment.
  93. .TP
  94. .BI \-\-get " flag"
  95. Print the value of the flag on standard output. Exits with 0
  96. if the flag is known otherwise exits with 1.
  97. .TP
  98. .BI \-\-origin " flag"
  99. Print the origin of the value that is returned by \fB\-\-get\fP. Exits
  100. with 0 if the flag is known otherwise exits with 1. The origin can be one
  101. of the following values:
  102. .RS
  103. .TP
  104. .B vendor
  105. the original flag set by the vendor is returned;
  106. .TP
  107. .B system
  108. the flag is set/modified by a system-wide configuration;
  109. .TP
  110. .B user
  111. the flag is set/modified by a user-specific configuration;
  112. .TP
  113. .B env
  114. the flag is set/modified by an environment-specific configuration.
  115. .RE
  116. .TP
  117. .BI \-\-query\-features " area"
  118. Print the features enabled for a given area. The only currently recognized
  119. area is \fBhardening\fP. Exits with 0 if the area is known otherwise exits
  120. with 1.
  121. .IP
  122. The output format is RFC822 header-style, with one section per feature.
  123. For example:
  124. .IP
  125. .nf
  126. Feature: pie
  127. Enabled: no
  128. Feature: stackprotector
  129. Enabled: yes
  130. .fi
  131. .TP
  132. .B \-\-help
  133. Show the usage message and exit.
  134. .TP
  135. .B \-\-version
  136. Show the version and exit.
  137. .
  138. .SH SUPPORTED FLAGS
  139. .TP
  140. .B CFLAGS
  141. Options for the C compiler. The default value set by the vendor
  142. includes \fI\-g\fP and the default optimization level (\fI\-O2\fP usually,
  143. or \fI\-O0\fP if the \fBDEB_BUILD_OPTIONS\fP environment variable defines
  144. \fInoopt\fP).
  145. .TP
  146. .B CPPFLAGS
  147. Options for the C preprocessor. Default value: empty.
  148. .TP
  149. .B CXXFLAGS
  150. Options for the C++ compiler. Same as \fBCFLAGS\fP.
  151. .TP
  152. .B FFLAGS
  153. Options for the Fortran compiler. Same as \fBCFLAGS\fP.
  154. .TP
  155. .B LDFLAGS
  156. Options passed to the compiler when linking executables or shared
  157. objects (if the linker is called directly, then
  158. .B \-Wl
  159. and
  160. .B ,
  161. have to be stripped from these options). Default value: empty.
  162. .
  163. .SH FILES
  164. .TP
  165. .B /etc/dpkg/buildflags.conf
  166. System wide configuration file.
  167. .TP
  168. .BR $XDG_CONFIG_HOME/dpkg/buildflags.conf " or " $HOME/.config/dpkg/buildflags.conf
  169. User configuration file.
  170. .SH ENVIRONMENT
  171. There are 2 sets of environment variables doing the same operations, the
  172. first one (DEB_\fIflag\fP_\fIop\fP) should never be used within
  173. \fBdebian/rules\fP. It's meant for any user that wants to rebuild the
  174. source package with different build flags. The second set
  175. (DEB_\fIflag\fP_MAINT_\fIop\fP) should only be used in \fBdebian/rules\fP
  176. by package maintainers to change the resulting build flags.
  177. .TP
  178. .BI DEB_ flag _SET
  179. .TQ
  180. .BI DEB_ flag _MAINT_SET
  181. This variable can be used to force the value returned for the given
  182. \fIflag\fP.
  183. .TP
  184. .BI DEB_ flag _STRIP
  185. .TQ
  186. .BI DEB_ flag _MAINT_STRIP
  187. This variable can be used to provide a space separated list of options
  188. that will be stripped from the set of flags returned for the given
  189. \fIflag\fP.
  190. .TP
  191. .BI DEB_ flag _APPEND
  192. .TQ
  193. .BI DEB_ flag _MAINT_APPEND
  194. This variable can be used to append supplementary options to the value
  195. returned for the given \fIflag\fP.
  196. .TP
  197. .BI DEB_ flag _PREPEND
  198. .TQ
  199. .BI DEB_ flag _MAINT_PREPEND
  200. This variable can be used to prepend supplementary options to the value
  201. returned for the given \fIflag\fP.
  202. .TP
  203. .B DEB_BUILD_MAINT_OPTIONS
  204. This variable can be used to disable/enable various hardening build
  205. flags through the \fBhardening\fP option. See the \fBHARDENING\fP section
  206. for details.
  207. .
  208. .SH HARDENING
  209. Several compile-time options (detailed below) can be used to help harden
  210. a resulting binary against memory corruption attacks, or provide
  211. additional warning messages during compilation. Except as noted below,
  212. these are enabled by default for architectures that support them.
  213. .P
  214. Each hardening feature can be enabled and disabled in the
  215. \fBDEB_BUILD_MAINT_OPTIONS\fP environment variable's \fBhardening\fP
  216. value with the "+" and "\-" modifier. For example, to enable the
  217. "pie" feature and disable the "fortify" feature you can do this
  218. in \fBdebian/rules\fP:
  219. .P
  220. export DEB_BUILD_MAINT_OPTIONS=hardening=+pie,\-fortify
  221. .P
  222. The special feature \fBall\fP can be used to enable or disable all
  223. hardening features at the same time. Thus disabling everything and
  224. enabling only "format" and "fortify" can be achieved with:
  225. .P
  226. export DEB_BUILD_MAINT_OPTIONS=hardening=\-all,+format,+fortify
  227. .
  228. .TP
  229. .B format
  230. This setting (enabled by default) adds
  231. .B \-Wformat \-Werror=format\-security
  232. to \fBCFLAGS\fP and \fBCXXFLAGS\fP. This will warn about improper format
  233. string uses, and will fail when format functions are used in a way
  234. that represent possible security problems. At present, this warns about
  235. calls to \fBprintf\fP and \fBscanf\fP functions where the format string is
  236. not a string literal and there are no format arguments, as in
  237. \fBprintf(foo);\fP instead of \fPprintf("%s", foo);\fP
  238. This may be a security hole if the format string came from untrusted
  239. input and contains "%n".
  240. .
  241. .TP
  242. .B fortify
  243. This setting (enabled by default) adds
  244. .B \-D_FORTIFY_SOURCE=2
  245. to \fBCPPFLAGS\fP. During code generation the compiler
  246. knows a great deal of information about buffer sizes (where possible), and
  247. attempts to replace insecure unlimited length buffer function calls with
  248. length-limited ones. This is especially useful for old, crufty code.
  249. Additionally, format strings in writable memory that contain '%n' are
  250. blocked. If an application depends on such a format string, it will need
  251. to be worked around.
  252. Note that for this option to have any effect, the source must also
  253. be compiled with \fB\-O1\fP or higher.
  254. .TP
  255. .B stackprotector
  256. This setting (enabled by default) adds
  257. .B \-fstack-protector \-\-param=ssp\-buffer\-size=4
  258. to \fBCFLAGS\fP and \fBCXXFLAGS\fP. This adds safety checks against stack
  259. overwrites. This renders many potential code injection attacks into
  260. aborting situations. In the best case this turns code injection
  261. vulnerabilities into denial of service or into non-issues (depending on
  262. the application).
  263. This feature requires linking against glibc (or another provider of
  264. \fB__stack_chk_fail\fP), so needs to be disabled when building with
  265. \fB\-nostdlib\fP or \fB\-ffreestanding\fP or similar.
  266. .
  267. .TP
  268. .B relro
  269. This setting (enabled by default) adds
  270. .B \-Wl,\-z,relro
  271. to \fBLDFLAGS\fP. During program load, several ELF memory sections need
  272. to be written to by the linker. This flags the loader to turn these
  273. sections read-only before turning over control to the program. Most
  274. notably this prevents GOT overwrite attacks. If this option is disabled,
  275. \fBbindnow\fP will become disabled as well.
  276. .
  277. .TP
  278. .B bindnow
  279. This setting (disabled by default) adds
  280. .B \-Wl,\-z,now
  281. to \fBLDFLAGS\fP. During program load, all dynamic symbols are resolved,
  282. allowing for the entire PLT to be marked read-only (due to \fBrelro\fP
  283. above). The option cannot become enabled if \fBrelro\fP is not enabled.
  284. .
  285. .TP
  286. .B pie
  287. This setting (disabled by default) adds \fB\-fPIE\fP to \fBCFLAGS\fP and
  288. \fBCXXFLAGS\fP, and \fB\-fPIE \-pie\fP to \fBLDFLAGS\fP. Position Independent
  289. Executable are needed to take advantage of Address Space Layout
  290. Randomization, supported by some kernel versions. While ASLR can already
  291. be enforced for data areas in the stack and heap (brk and mmap), the code
  292. areas must be compiled as position-independent. Shared libraries already
  293. do this (\-fPIC), so they gain ASLR automatically, but binary .text
  294. regions need to be build PIE to gain ASLR. When this happens, ROP (Return
  295. Oriented Programming) attacks are much harder since there are no static
  296. locations to bounce off of during a memory corruption attack.
  297. This is not compatible with \fB\-fPIC\fP so care must be taken when
  298. building shared objects.
  299. Additionally, since PIE is implemented via a general register, some
  300. architectures (most notably i386) can see performance losses of up to
  301. 15% in very text-segment-heavy application workloads; most workloads
  302. see less than 1%. Architectures with more general registers (e.g. amd64)
  303. do not see as high a worst-case penalty.