Debian.pm 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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_hardening_flags(@params);
  57. } else {
  58. return $self->SUPER::run_hook($hook, @params);
  59. }
  60. }
  61. sub _parse_feature_area {
  62. my ($self, $area, $use_feature) = @_;
  63. # Adjust features based on Maintainer's desires.
  64. my $opts = Dpkg::BuildOptions->new(envvar => 'DEB_BUILD_MAINT_OPTIONS');
  65. foreach my $feature (split(/,/, $opts->get($area) // '')) {
  66. $feature = lc($feature);
  67. if ($feature =~ s/^([+-])//) {
  68. my $value = ($1 eq '+') ? 1 : 0;
  69. if ($feature eq 'all') {
  70. $use_feature->{$_} = $value foreach keys %{$use_feature};
  71. } else {
  72. if (exists $use_feature->{$feature}) {
  73. $use_feature->{$feature} = $value;
  74. } else {
  75. warning(_g('unknown %s feature: %s'), $area, $feature);
  76. }
  77. }
  78. } else {
  79. warning(_g('incorrect value in %s option of ' .
  80. 'DEB_BUILD_MAINT_OPTIONS: %s'), $area, $feature);
  81. }
  82. }
  83. }
  84. sub add_hardening_flags {
  85. my ($self, $flags) = @_;
  86. my $arch = get_host_arch();
  87. my ($abi, $os, $cpu) = debarch_to_debtriplet($arch);
  88. unless (defined $abi and defined $os and defined $cpu) {
  89. warning(_g("unknown host architecture '%s'"), $arch);
  90. ($abi, $os, $cpu) = ('', '', '');
  91. }
  92. # Features enabled by default for all builds.
  93. my %use_feature = (
  94. pie => 0,
  95. stackprotector => 1,
  96. stackprotectorstrong => 1,
  97. fortify => 1,
  98. format => 1,
  99. relro => 1,
  100. bindnow => 0,
  101. );
  102. # Adjust features based on Maintainer's desires.
  103. $self->_parse_feature_area('hardening', \%use_feature);
  104. # Mask features that are not available on certain architectures.
  105. if ($os !~ /^(?:linux|knetbsd|hurd)$/ or
  106. $cpu =~ /^(?:hppa|mips|mipsel|avr32)$/) {
  107. # Disabled on non-linux/knetbsd/hurd (see #430455 and #586215).
  108. # Disabled on hppa, mips/mipsel (#532821), avr32
  109. # (#574716).
  110. $use_feature{pie} = 0;
  111. }
  112. if ($cpu =~ /^(?:ia64|alpha|mips|mipsel|hppa)$/ or $arch eq 'arm') {
  113. # Stack protector disabled on ia64, alpha, mips, mipsel, hppa.
  114. # "warning: -fstack-protector not supported for this target"
  115. # Stack protector disabled on arm (ok on armel).
  116. # compiler supports it incorrectly (leads to SEGV)
  117. $use_feature{stackprotector} = 0;
  118. }
  119. if ($cpu =~ /^(?:ia64|hppa|avr32)$/) {
  120. # relro not implemented on ia64, hppa, avr32.
  121. $use_feature{relro} = 0;
  122. }
  123. # Mask features that might be influenced by other flags.
  124. if ($flags->{build_options}->has('noopt')) {
  125. # glibc 2.16 and later warn when using -O0 and _FORTIFY_SOURCE.
  126. $use_feature{fortify} = 0;
  127. }
  128. # Handle logical feature interactions.
  129. if ($use_feature{relro} == 0) {
  130. # Disable bindnow if relro is not enabled, since it has no
  131. # hardening ability without relro and may incur load penalties.
  132. $use_feature{bindnow} = 0;
  133. }
  134. if ($use_feature{stackprotector} == 0) {
  135. # Disable stackprotectorstrong if stackprotector is disabled.
  136. $use_feature{stackprotectorstrong} = 0;
  137. }
  138. # PIE
  139. if ($use_feature{pie}) {
  140. my $flag = '-fPIE';
  141. $flags->append('CFLAGS', $flag);
  142. $flags->append('OBJCFLAGS', $flag);
  143. $flags->append('OBJCXXFLAGS', $flag);
  144. $flags->append('FFLAGS', $flag);
  145. $flags->append('FCFLAGS', $flag);
  146. $flags->append('CXXFLAGS', $flag);
  147. $flags->append('GCJFLAGS', $flag);
  148. $flags->append('LDFLAGS', '-fPIE -pie');
  149. }
  150. # Stack protector
  151. if ($use_feature{stackprotectorstrong}) {
  152. my $flag = '-fstack-protector-strong';
  153. $flags->append('CFLAGS', $flag);
  154. $flags->append('OBJCFLAGS', $flag);
  155. $flags->append('OBJCXXFLAGS', $flag);
  156. $flags->append('FFLAGS', $flag);
  157. $flags->append('FCFLAGS', $flag);
  158. $flags->append('CXXFLAGS', $flag);
  159. $flags->append('GCJFLAGS', $flag);
  160. } elsif ($use_feature{stackprotector}) {
  161. my $flag = '-fstack-protector --param=ssp-buffer-size=4';
  162. $flags->append('CFLAGS', $flag);
  163. $flags->append('OBJCFLAGS', $flag);
  164. $flags->append('OBJCXXFLAGS', $flag);
  165. $flags->append('FFLAGS', $flag);
  166. $flags->append('FCFLAGS', $flag);
  167. $flags->append('CXXFLAGS', $flag);
  168. $flags->append('GCJFLAGS', $flag);
  169. }
  170. # Fortify Source
  171. if ($use_feature{fortify}) {
  172. $flags->append('CPPFLAGS', '-D_FORTIFY_SOURCE=2');
  173. }
  174. # Format Security
  175. if ($use_feature{format}) {
  176. my $flag = '-Wformat -Werror=format-security';
  177. $flags->append('CFLAGS', $flag);
  178. $flags->append('CXXFLAGS', $flag);
  179. $flags->append('OBJCFLAGS', $flag);
  180. $flags->append('OBJCXXFLAGS', $flag);
  181. }
  182. # Read-only Relocations
  183. if ($use_feature{relro}) {
  184. $flags->append('LDFLAGS', '-Wl,-z,relro');
  185. }
  186. # Bindnow
  187. if ($use_feature{bindnow}) {
  188. $flags->append('LDFLAGS', '-Wl,-z,now');
  189. }
  190. # Store the feature usage.
  191. while (my ($feature, $enabled) = each %use_feature) {
  192. $flags->set_feature('hardening', $feature, $enabled);
  193. }
  194. }
  195. 1;