Debian.pm 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. return (
  44. [ "register", "Dm-Upload-Allowed",
  45. CTRL_INFO_SRC | CTRL_INDEX_SRC | CTRL_PKG_SRC ],
  46. [ "insert_after", CTRL_INDEX_SRC, "Uploaders", "Dm-Upload-Allowed" ],
  47. [ "insert_after", CTRL_PKG_SRC, "Uploaders", "Dm-Upload-Allowed" ],
  48. );
  49. } elsif ($hook eq "extend-patch-header") {
  50. my ($textref, $ch_info) = @params;
  51. if ($ch_info->{'Closes'}) {
  52. foreach my $bug (split(/\s+/, $ch_info->{'Closes'})) {
  53. $$textref .= "Bug-Debian: http://bugs.debian.org/$bug\n";
  54. }
  55. }
  56. my $b = Dpkg::Vendor::Ubuntu::find_launchpad_closes($ch_info->{'Changes'});
  57. foreach my $bug (@$b) {
  58. $$textref .= "Bug-Ubuntu: https://bugs.launchpad.net/bugs/$bug\n";
  59. }
  60. } elsif ($hook eq "update-buildflags") {
  61. $self->add_hardening_flags(@params);
  62. } else {
  63. return $self->SUPER::run_hook($hook, @params);
  64. }
  65. }
  66. sub add_hardening_flags {
  67. my ($self, $flags) = @_;
  68. my $arch = get_host_arch();
  69. my ($abi, $os, $cpu) = debarch_to_debtriplet($arch);
  70. # Decide what's enabled
  71. my %use_feature = (
  72. "pie" => 0,
  73. "stackprotector" => 1,
  74. "fortify" => 1,
  75. "format" => 1,
  76. "relro" => 1,
  77. "bindnow" => 0
  78. );
  79. my $opts = Dpkg::BuildOptions->new(envvar => "DEB_BUILD_MAINT_OPTIONS");
  80. foreach my $feature (split(",", $opts->get("hardening") // "")) {
  81. $feature = lc($feature);
  82. if ($feature =~ s/^([+-])//) {
  83. my $value = ($1 eq "+") ? 1 : 0;
  84. if ($feature eq "all") {
  85. $use_feature{$_} = $value foreach keys %use_feature;
  86. } else {
  87. if (exists $use_feature{$feature}) {
  88. $use_feature{$feature} = $value;
  89. } else {
  90. warning(_g("unknown hardening feature: %s"), $feature);
  91. }
  92. }
  93. } else {
  94. warning(_g("incorrect value in hardening option of " .
  95. "DEB_BUILD_MAINT_OPTIONS: %s"), $feature);
  96. }
  97. }
  98. # PIE
  99. if ($use_feature{"pie"} and
  100. $os =~ /^(linux|knetbsd|hurd)$/ and
  101. $cpu !~ /^(hppa|m68k|mips|mipsel|avr32)$/) {
  102. # Only on linux/knetbsd/hurd (see #430455 and #586215)
  103. # Disabled on hppa, m68k (#451192), mips/mipsel (#532821), avr32
  104. # (#574716)
  105. $flags->append("CFLAGS", "-fPIE");
  106. $flags->append("CXXFLAGS", "-fPIE");
  107. $flags->append("LDFLAGS", "-fPIE -pie");
  108. }
  109. # Stack protector
  110. if ($use_feature{"stackprotector"} and
  111. $cpu !~ /^(ia64|alpha|mips|mipsel|hppa)$/ and $arch ne "arm") {
  112. # Stack protector disabled on ia64, alpha, mips, mipsel, hppa.
  113. # "warning: -fstack-protector not supported for this target"
  114. # Stack protector disabled on arm (ok on armel).
  115. # compiler supports it incorrectly (leads to SEGV)
  116. $flags->append("CFLAGS", "-fstack-protector --param=ssp-buffer-size=4");
  117. $flags->append("CXXFLAGS", "-fstack-protector --param=ssp-buffer-size=4");
  118. }
  119. # Fortify
  120. if ($use_feature{"fortify"}) {
  121. $flags->append("CPPFLAGS", "-D_FORTIFY_SOURCE=2");
  122. }
  123. # Format
  124. if ($use_feature{"format"}) {
  125. $flags->append("CFLAGS", "-Wformat -Wformat-security -Werror=format-security");
  126. $flags->append("CXXFLAGS", "-Wformat -Wformat-security -Werror=format-security");
  127. }
  128. # Relro
  129. if ($use_feature{"relro"} and $cpu !~ /^(ia64|hppa|avr32)$/) {
  130. $flags->append("LDFLAGS", "-Wl,-z,relro");
  131. } else {
  132. # Disable full relro if relro is not enabled.
  133. $use_feature{"bindnow"} = 0;
  134. }
  135. # Bindnow
  136. if ($use_feature{"bindnow"}) {
  137. $flags->append("LDFLAGS", "-Wl,-z,now");
  138. }
  139. }
  140. 1;