Debian.pm 10 KB

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