Debian.pm 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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} and $use_feature{thread}) {
  155. # Disable the thread sanitizer when the address one is active, they
  156. # are mutually incompatible.
  157. $use_feature{thread} = 0;
  158. }
  159. if ($use_feature{address} or $use_feature{thread}) {
  160. # Disable leak sanitizer, it is implied by the address or thread ones.
  161. $use_feature{leak} = 0;
  162. }
  163. if ($use_feature{address}) {
  164. my $flag = '-fsanitize=address -fno-omit-frame-pointer';
  165. $flags->append('CFLAGS', $flag);
  166. $flags->append('CXXFLAGS', $flag);
  167. $flags->append('LDFLAGS', '-fsanitize=address');
  168. }
  169. if ($use_feature{thread}) {
  170. my $flag = '-fsanitize=thread';
  171. $flags->append('CFLAGS', $flag);
  172. $flags->append('CXXFLAGS', $flag);
  173. $flags->append('LDFLAGS', $flag);
  174. }
  175. if ($use_feature{leak}) {
  176. $flags->append('LDFLAGS', '-fsanitize=leak');
  177. }
  178. if ($use_feature{undefined}) {
  179. my $flag = '-fsanitize=undefined';
  180. $flags->append('CFLAGS', $flag);
  181. $flags->append('CXXFLAGS', $flag);
  182. $flags->append('LDFLAGS', $flag);
  183. }
  184. # Store the feature usage.
  185. while (my ($feature, $enabled) = each %use_feature) {
  186. $flags->set_feature('sanitize', $feature, $enabled);
  187. }
  188. }
  189. sub _add_hardening_flags {
  190. my ($self, $flags) = @_;
  191. my $arch = get_host_arch();
  192. my ($abi, $os, $cpu) = debarch_to_debtriplet($arch);
  193. unless (defined $abi and defined $os and defined $cpu) {
  194. warning(g_("unknown host architecture '%s'"), $arch);
  195. ($abi, $os, $cpu) = ('', '', '');
  196. }
  197. # Default feature states.
  198. my %use_feature = (
  199. pie => 0,
  200. stackprotector => 1,
  201. stackprotectorstrong => 1,
  202. fortify => 1,
  203. format => 1,
  204. relro => 1,
  205. bindnow => 0,
  206. );
  207. # Adjust features based on user or maintainer's desires.
  208. $self->_parse_feature_area('hardening', \%use_feature);
  209. # Mask features that are not available on certain architectures.
  210. if ($os !~ /^(?:linux|knetbsd|hurd)$/ or
  211. $cpu =~ /^(?:hppa|avr32)$/) {
  212. # Disabled on non-linux/knetbsd/hurd (see #430455 and #586215).
  213. # Disabled on hppa, avr32
  214. # (#574716).
  215. $use_feature{pie} = 0;
  216. }
  217. if ($cpu =~ /^(?:ia64|alpha|hppa)$/ or $arch eq 'arm') {
  218. # Stack protector disabled on ia64, alpha, hppa.
  219. # "warning: -fstack-protector not supported for this target"
  220. # Stack protector disabled on arm (ok on armel).
  221. # compiler supports it incorrectly (leads to SEGV)
  222. $use_feature{stackprotector} = 0;
  223. }
  224. if ($cpu =~ /^(?:ia64|hppa|avr32)$/) {
  225. # relro not implemented on ia64, hppa, avr32.
  226. $use_feature{relro} = 0;
  227. }
  228. # Mask features that might be influenced by other flags.
  229. if ($flags->{build_options}->has('noopt')) {
  230. # glibc 2.16 and later warn when using -O0 and _FORTIFY_SOURCE.
  231. $use_feature{fortify} = 0;
  232. }
  233. # Handle logical feature interactions.
  234. if ($use_feature{relro} == 0) {
  235. # Disable bindnow if relro is not enabled, since it has no
  236. # hardening ability without relro and may incur load penalties.
  237. $use_feature{bindnow} = 0;
  238. }
  239. if ($use_feature{stackprotector} == 0) {
  240. # Disable stackprotectorstrong if stackprotector is disabled.
  241. $use_feature{stackprotectorstrong} = 0;
  242. }
  243. # PIE
  244. if ($use_feature{pie}) {
  245. my $flag = '-fPIE';
  246. $flags->append('CFLAGS', $flag);
  247. $flags->append('OBJCFLAGS', $flag);
  248. $flags->append('OBJCXXFLAGS', $flag);
  249. $flags->append('FFLAGS', $flag);
  250. $flags->append('FCFLAGS', $flag);
  251. $flags->append('CXXFLAGS', $flag);
  252. $flags->append('GCJFLAGS', $flag);
  253. $flags->append('LDFLAGS', '-fPIE -pie');
  254. }
  255. # Stack protector
  256. if ($use_feature{stackprotectorstrong}) {
  257. my $flag = '-fstack-protector-strong';
  258. $flags->append('CFLAGS', $flag);
  259. $flags->append('OBJCFLAGS', $flag);
  260. $flags->append('OBJCXXFLAGS', $flag);
  261. $flags->append('FFLAGS', $flag);
  262. $flags->append('FCFLAGS', $flag);
  263. $flags->append('CXXFLAGS', $flag);
  264. $flags->append('GCJFLAGS', $flag);
  265. } elsif ($use_feature{stackprotector}) {
  266. my $flag = '-fstack-protector --param=ssp-buffer-size=4';
  267. $flags->append('CFLAGS', $flag);
  268. $flags->append('OBJCFLAGS', $flag);
  269. $flags->append('OBJCXXFLAGS', $flag);
  270. $flags->append('FFLAGS', $flag);
  271. $flags->append('FCFLAGS', $flag);
  272. $flags->append('CXXFLAGS', $flag);
  273. $flags->append('GCJFLAGS', $flag);
  274. }
  275. # Fortify Source
  276. if ($use_feature{fortify}) {
  277. $flags->append('CPPFLAGS', '-D_FORTIFY_SOURCE=2');
  278. }
  279. # Format Security
  280. if ($use_feature{format}) {
  281. my $flag = '-Wformat -Werror=format-security';
  282. $flags->append('CFLAGS', $flag);
  283. $flags->append('CXXFLAGS', $flag);
  284. $flags->append('OBJCFLAGS', $flag);
  285. $flags->append('OBJCXXFLAGS', $flag);
  286. }
  287. # Read-only Relocations
  288. if ($use_feature{relro}) {
  289. $flags->append('LDFLAGS', '-Wl,-z,relro');
  290. }
  291. # Bindnow
  292. if ($use_feature{bindnow}) {
  293. $flags->append('LDFLAGS', '-Wl,-z,now');
  294. }
  295. # Store the feature usage.
  296. while (my ($feature, $enabled) = each %use_feature) {
  297. $flags->set_feature('hardening', $feature, $enabled);
  298. }
  299. }
  300. =head1 CHANGES
  301. =head2 Version 0.xx
  302. This is a private module.
  303. =cut
  304. 1;