Debian.pm 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 <http://www.gnu.org/licenses/>.
  19. package Dpkg::Vendor::Debian;
  20. use strict;
  21. use warnings;
  22. our $VERSION = "0.01";
  23. use base qw(Dpkg::Vendor::Default);
  24. use Dpkg::Gettext;
  25. use Dpkg::ErrorHandling;
  26. use Dpkg::Control::Types;
  27. use Dpkg::Vendor::Ubuntu;
  28. use Dpkg::BuildOptions;
  29. use Dpkg::Arch qw(get_host_arch debarch_to_debtriplet);
  30. =encoding utf8
  31. =head1 NAME
  32. Dpkg::Vendor::Debian - Debian vendor object
  33. =head1 DESCRIPTION
  34. This vendor object customize the behaviour of dpkg scripts
  35. for Debian specific actions.
  36. =cut
  37. sub run_hook {
  38. my ($self, $hook, @params) = @_;
  39. if ($hook eq "keyrings") {
  40. return ('/usr/share/keyrings/debian-keyring.gpg',
  41. '/usr/share/keyrings/debian-maintainers.gpg');
  42. } elsif ($hook eq "register-custom-fields") {
  43. } elsif ($hook eq "extend-patch-header") {
  44. my ($textref, $ch_info) = @params;
  45. if ($ch_info->{'Closes'}) {
  46. foreach my $bug (split(/\s+/, $ch_info->{'Closes'})) {
  47. $$textref .= "Bug-Debian: http://bugs.debian.org/$bug\n";
  48. }
  49. }
  50. my $b = Dpkg::Vendor::Ubuntu::find_launchpad_closes($ch_info->{'Changes'});
  51. foreach my $bug (@$b) {
  52. $$textref .= "Bug-Ubuntu: https://bugs.launchpad.net/bugs/$bug\n";
  53. }
  54. } elsif ($hook eq "update-buildflags") {
  55. $self->add_hardening_flags(@params);
  56. } else {
  57. return $self->SUPER::run_hook($hook, @params);
  58. }
  59. }
  60. sub add_hardening_flags {
  61. my ($self, $flags) = @_;
  62. my $arch = get_host_arch();
  63. my ($abi, $os, $cpu) = debarch_to_debtriplet($arch);
  64. unless (defined $abi and defined $os and defined $cpu) {
  65. warning(_g("unknown host architecture '%s'"), $arch);
  66. ($abi, $os, $cpu) = ("", "", "");
  67. }
  68. # Features enabled by default for all builds.
  69. my %use_feature = (
  70. "pie" => 0,
  71. "stackprotector" => 1,
  72. "fortify" => 1,
  73. "format" => 1,
  74. "relro" => 1,
  75. "bindnow" => 0
  76. );
  77. # Adjust features based on Maintainer's desires.
  78. my $opts = Dpkg::BuildOptions->new(envvar => "DEB_BUILD_MAINT_OPTIONS");
  79. foreach my $feature (split(",", $opts->get("hardening") // "")) {
  80. $feature = lc($feature);
  81. if ($feature =~ s/^([+-])//) {
  82. my $value = ($1 eq "+") ? 1 : 0;
  83. if ($feature eq "all") {
  84. $use_feature{$_} = $value foreach keys %use_feature;
  85. } else {
  86. if (exists $use_feature{$feature}) {
  87. $use_feature{$feature} = $value;
  88. } else {
  89. warning(_g("unknown hardening feature: %s"), $feature);
  90. }
  91. }
  92. } else {
  93. warning(_g("incorrect value in hardening option of " .
  94. "DEB_BUILD_MAINT_OPTIONS: %s"), $feature);
  95. }
  96. }
  97. # Mask features that are not available on certain architectures.
  98. if ($os !~ /^(linux|knetbsd|hurd)$/ or
  99. $cpu =~ /^(hppa|mips|mipsel|avr32)$/) {
  100. # Disabled on non-linux/knetbsd/hurd (see #430455 and #586215).
  101. # Disabled on hppa, mips/mipsel (#532821), avr32
  102. # (#574716).
  103. $use_feature{"pie"} = 0;
  104. }
  105. if ($cpu =~ /^(ia64|alpha|mips|mipsel|hppa)$/ or $arch eq "arm") {
  106. # Stack protector disabled on ia64, alpha, mips, mipsel, hppa.
  107. # "warning: -fstack-protector not supported for this target"
  108. # Stack protector disabled on arm (ok on armel).
  109. # compiler supports it incorrectly (leads to SEGV)
  110. $use_feature{"stackprotector"} = 0;
  111. }
  112. if ($cpu =~ /^(ia64|hppa|avr32)$/) {
  113. # relro not implemented on ia64, hppa, avr32.
  114. $use_feature{"relro"} = 0;
  115. }
  116. # Mask features that might be influenced by other flags.
  117. if ($flags->{'build-options'}->has('noopt')) {
  118. # glibc 2.16 and later warn when using -O0 and _FORTIFY_SOURCE.
  119. $use_feature{'fortify'} = 0;
  120. }
  121. # Handle logical feature interactions.
  122. if ($use_feature{"relro"} == 0) {
  123. # Disable bindnow if relro is not enabled, since it has no
  124. # hardening ability without relro and may incur load penalties.
  125. $use_feature{"bindnow"} = 0;
  126. }
  127. # PIE
  128. if ($use_feature{"pie"}) {
  129. $flags->append("CFLAGS", "-fPIE");
  130. $flags->append("CXXFLAGS", "-fPIE");
  131. $flags->append("LDFLAGS", "-fPIE -pie");
  132. }
  133. # Stack protector
  134. if ($use_feature{"stackprotector"}) {
  135. $flags->append("CFLAGS", "-fstack-protector --param=ssp-buffer-size=4");
  136. $flags->append("CXXFLAGS", "-fstack-protector --param=ssp-buffer-size=4");
  137. }
  138. # Fortify Source
  139. if ($use_feature{"fortify"}) {
  140. $flags->append("CPPFLAGS", "-D_FORTIFY_SOURCE=2");
  141. }
  142. # Format Security
  143. if ($use_feature{"format"}) {
  144. $flags->append("CFLAGS", "-Wformat -Werror=format-security");
  145. $flags->append("CXXFLAGS", "-Wformat -Werror=format-security");
  146. }
  147. # Read-only Relocations
  148. if ($use_feature{"relro"}) {
  149. $flags->append("LDFLAGS", "-Wl,-z,relro");
  150. }
  151. # Bindnow
  152. if ($use_feature{"bindnow"}) {
  153. $flags->append("LDFLAGS", "-Wl,-z,now");
  154. }
  155. # Store the feature usage.
  156. while (my ($feature, $enabled) = each %use_feature) {
  157. $flags->set_feature("hardening", $feature, $enabled);
  158. }
  159. }
  160. 1;