Debian.pm 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. # Copyright © 2009-2011 Raphaël Hertzog <hertzog@debian.org>
  2. #
  3. # Hardening build flags handling derived from work of:
  4. # Copyright © 2009-2011 Kees Cook <kees@debian.org>
  5. # Copyright © 2007-2008 Canonical, Ltd.
  6. #
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 2 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. package Dpkg::Vendor::Debian;
  20. use strict;
  21. use warnings;
  22. our $VERSION = '0.01';
  23. use parent qw(Dpkg::Vendor::Default);
  24. use Dpkg::Gettext;
  25. use Dpkg::ErrorHandling;
  26. use Dpkg::Control::Types;
  27. use Dpkg::BuildOptions;
  28. use Dpkg::Arch qw(get_host_arch debarch_to_debtriplet);
  29. =encoding utf8
  30. =head1 NAME
  31. Dpkg::Vendor::Debian - Debian vendor object
  32. =head1 DESCRIPTION
  33. This vendor object customize the behaviour of dpkg scripts
  34. for Debian specific actions.
  35. =cut
  36. sub run_hook {
  37. my ($self, $hook, @params) = @_;
  38. if ($hook eq 'keyrings') {
  39. return ('/usr/share/keyrings/debian-keyring.gpg',
  40. '/usr/share/keyrings/debian-maintainers.gpg');
  41. } elsif ($hook eq 'register-custom-fields') {
  42. } elsif ($hook eq 'extend-patch-header') {
  43. my ($textref, $ch_info) = @params;
  44. if ($ch_info->{'Closes'}) {
  45. foreach my $bug (split(/\s+/, $ch_info->{'Closes'})) {
  46. $$textref .= "Bug-Debian: https://bugs.debian.org/$bug\n";
  47. }
  48. }
  49. # XXX: Layer violation...
  50. require Dpkg::Vendor::Ubuntu;
  51. my $b = Dpkg::Vendor::Ubuntu::find_launchpad_closes($ch_info->{'Changes'});
  52. foreach my $bug (@$b) {
  53. $$textref .= "Bug-Ubuntu: https://bugs.launchpad.net/bugs/$bug\n";
  54. }
  55. } elsif ($hook eq 'update-buildflags') {
  56. $self->_add_qa_flags(@params);
  57. $self->_add_reproducible_flags(@params);
  58. $self->_add_hardening_flags(@params);
  59. } else {
  60. return $self->SUPER::run_hook($hook, @params);
  61. }
  62. }
  63. sub _parse_build_options {
  64. my ($self, $variable, $area, $use_feature) = @_;
  65. # Adjust features based on user or maintainer's desires.
  66. my $opts = Dpkg::BuildOptions->new(envvar => $variable);
  67. foreach my $feature (split(/,/, $opts->get($area) // '')) {
  68. $feature = lc($feature);
  69. if ($feature =~ s/^([+-])//) {
  70. my $value = ($1 eq '+') ? 1 : 0;
  71. if ($feature eq 'all') {
  72. $use_feature->{$_} = $value foreach keys %{$use_feature};
  73. } else {
  74. if (exists $use_feature->{$feature}) {
  75. $use_feature->{$feature} = $value;
  76. } else {
  77. warning(g_('unknown %s feature in %s variable: %s'),
  78. $area, $variable, $feature);
  79. }
  80. }
  81. } else {
  82. warning(g_('incorrect value in %s option of %s variable: %s'),
  83. $area, $variable, $feature);
  84. }
  85. }
  86. }
  87. sub _parse_feature_area {
  88. my ($self, $area, $use_feature) = @_;
  89. $self->_parse_build_options('DEB_BUILD_OPTIONS', $area, $use_feature);
  90. $self->_parse_build_options('DEB_BUILD_MAINT_OPTIONS', $area, $use_feature);
  91. }
  92. sub _add_qa_flags {
  93. my ($self, $flags) = @_;
  94. # Default feature states.
  95. my %use_feature = (
  96. bug => 0,
  97. canary => 0,
  98. );
  99. # Adjust features based on user or maintainer's desires.
  100. $self->_parse_feature_area('qa', \%use_feature);
  101. # Warnings that detect actual bugs.
  102. if ($use_feature{bug}) {
  103. foreach my $warnflag (qw(array-bounds clobbered volatile-register-var
  104. implicit-function-declaration)) {
  105. $flags->append('CFLAGS', "-Werror=$warnflag");
  106. $flags->append('CXXFLAGS', "-Werror=$warnflag");
  107. }
  108. }
  109. # Inject dummy canary options to detect issues with build flag propagation.
  110. if ($use_feature{canary}) {
  111. require Digest::MD5;
  112. my $id = Digest::MD5::md5_hex(int rand 4096);
  113. foreach my $flag (qw(CPPFLAGS CFLAGS OBJCFLAGS CXXFLAGS OBJCXXFLAGS)) {
  114. $flags->append($flag, "-D__DEB_CANARY_${flag}_${id}__");
  115. }
  116. $flags->append('LDFLAGS', "-Wl,-z,deb-canary-${id}");
  117. }
  118. # Store the feature usage.
  119. while (my ($feature, $enabled) = each %use_feature) {
  120. $flags->set_feature('qa', $feature, $enabled);
  121. }
  122. }
  123. sub _add_reproducible_flags {
  124. my ($self, $flags) = @_;
  125. # Default feature states.
  126. my %use_feature = (
  127. timeless => 0,
  128. );
  129. # Adjust features based on user or maintainer's desires.
  130. $self->_parse_feature_area('reproducible', \%use_feature);
  131. # Warn when the __TIME__, __DATE__ and __TIMESTAMP__ macros are used.
  132. if ($use_feature{timeless}) {
  133. $flags->append('CPPFLAGS', '-Wdate-time');
  134. }
  135. # Store the feature usage.
  136. while (my ($feature, $enabled) = each %use_feature) {
  137. $flags->set_feature('reproducible', $feature, $enabled);
  138. }
  139. }
  140. sub _add_hardening_flags {
  141. my ($self, $flags) = @_;
  142. my $arch = get_host_arch();
  143. my ($abi, $os, $cpu) = debarch_to_debtriplet($arch);
  144. unless (defined $abi and defined $os and defined $cpu) {
  145. warning(g_("unknown host architecture '%s'"), $arch);
  146. ($abi, $os, $cpu) = ('', '', '');
  147. }
  148. # Default feature states.
  149. my %use_feature = (
  150. pie => 0,
  151. stackprotector => 1,
  152. stackprotectorstrong => 1,
  153. fortify => 1,
  154. format => 1,
  155. relro => 1,
  156. bindnow => 0,
  157. );
  158. # Adjust features based on user or maintainer's desires.
  159. $self->_parse_feature_area('hardening', \%use_feature);
  160. # Mask features that are not available on certain architectures.
  161. if ($os !~ /^(?:linux|knetbsd|hurd)$/ or
  162. $cpu =~ /^(?:hppa|avr32)$/) {
  163. # Disabled on non-linux/knetbsd/hurd (see #430455 and #586215).
  164. # Disabled on hppa, avr32
  165. # (#574716).
  166. $use_feature{pie} = 0;
  167. }
  168. if ($cpu =~ /^(?:ia64|alpha|hppa)$/ or $arch eq 'arm') {
  169. # Stack protector disabled on ia64, alpha, hppa.
  170. # "warning: -fstack-protector not supported for this target"
  171. # Stack protector disabled on arm (ok on armel).
  172. # compiler supports it incorrectly (leads to SEGV)
  173. $use_feature{stackprotector} = 0;
  174. }
  175. if ($cpu =~ /^(?:ia64|hppa|avr32)$/) {
  176. # relro not implemented on ia64, hppa, avr32.
  177. $use_feature{relro} = 0;
  178. }
  179. # Mask features that might be influenced by other flags.
  180. if ($flags->{build_options}->has('noopt')) {
  181. # glibc 2.16 and later warn when using -O0 and _FORTIFY_SOURCE.
  182. $use_feature{fortify} = 0;
  183. }
  184. # Handle logical feature interactions.
  185. if ($use_feature{relro} == 0) {
  186. # Disable bindnow if relro is not enabled, since it has no
  187. # hardening ability without relro and may incur load penalties.
  188. $use_feature{bindnow} = 0;
  189. }
  190. if ($use_feature{stackprotector} == 0) {
  191. # Disable stackprotectorstrong if stackprotector is disabled.
  192. $use_feature{stackprotectorstrong} = 0;
  193. }
  194. # PIE
  195. if ($use_feature{pie}) {
  196. my $flag = '-fPIE';
  197. $flags->append('CFLAGS', $flag);
  198. $flags->append('OBJCFLAGS', $flag);
  199. $flags->append('OBJCXXFLAGS', $flag);
  200. $flags->append('FFLAGS', $flag);
  201. $flags->append('FCFLAGS', $flag);
  202. $flags->append('CXXFLAGS', $flag);
  203. $flags->append('GCJFLAGS', $flag);
  204. $flags->append('LDFLAGS', '-fPIE -pie');
  205. }
  206. # Stack protector
  207. if ($use_feature{stackprotectorstrong}) {
  208. my $flag = '-fstack-protector-strong';
  209. $flags->append('CFLAGS', $flag);
  210. $flags->append('OBJCFLAGS', $flag);
  211. $flags->append('OBJCXXFLAGS', $flag);
  212. $flags->append('FFLAGS', $flag);
  213. $flags->append('FCFLAGS', $flag);
  214. $flags->append('CXXFLAGS', $flag);
  215. $flags->append('GCJFLAGS', $flag);
  216. } elsif ($use_feature{stackprotector}) {
  217. my $flag = '-fstack-protector --param=ssp-buffer-size=4';
  218. $flags->append('CFLAGS', $flag);
  219. $flags->append('OBJCFLAGS', $flag);
  220. $flags->append('OBJCXXFLAGS', $flag);
  221. $flags->append('FFLAGS', $flag);
  222. $flags->append('FCFLAGS', $flag);
  223. $flags->append('CXXFLAGS', $flag);
  224. $flags->append('GCJFLAGS', $flag);
  225. }
  226. # Fortify Source
  227. if ($use_feature{fortify}) {
  228. $flags->append('CPPFLAGS', '-D_FORTIFY_SOURCE=2');
  229. }
  230. # Format Security
  231. if ($use_feature{format}) {
  232. my $flag = '-Wformat -Werror=format-security';
  233. $flags->append('CFLAGS', $flag);
  234. $flags->append('CXXFLAGS', $flag);
  235. $flags->append('OBJCFLAGS', $flag);
  236. $flags->append('OBJCXXFLAGS', $flag);
  237. }
  238. # Read-only Relocations
  239. if ($use_feature{relro}) {
  240. $flags->append('LDFLAGS', '-Wl,-z,relro');
  241. }
  242. # Bindnow
  243. if ($use_feature{bindnow}) {
  244. $flags->append('LDFLAGS', '-Wl,-z,now');
  245. }
  246. # Store the feature usage.
  247. while (my ($feature, $enabled) = each %use_feature) {
  248. $flags->set_feature('hardening', $feature, $enabled);
  249. }
  250. }
  251. =head1 CHANGES
  252. =head2 Version 0.xx
  253. This is a private module.
  254. =cut
  255. 1;