Debian.pm 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. # Copyright © 2009-2011 Raphaël Hertzog <hertzog@debian.org>
  2. # Copyright © 2009, 2011-2017 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;
  25. use Dpkg::Gettext;
  26. use Dpkg::ErrorHandling;
  27. use Dpkg::Control::Types;
  28. use Dpkg::BuildOptions;
  29. use Dpkg::Arch qw(get_host_arch debarch_to_debtuple);
  30. use parent qw(Dpkg::Vendor::Default);
  31. =encoding utf8
  32. =head1 NAME
  33. Dpkg::Vendor::Debian - Debian vendor object
  34. =head1 DESCRIPTION
  35. This vendor object customizes the behaviour of dpkg scripts for Debian
  36. specific behavior and policies.
  37. =cut
  38. sub run_hook {
  39. my ($self, $hook, @params) = @_;
  40. if ($hook eq 'package-keyrings') {
  41. return ('/usr/share/keyrings/debian-keyring.gpg',
  42. '/usr/share/keyrings/debian-maintainers.gpg');
  43. } elsif ($hook eq 'keyrings') {
  44. warnings::warnif('deprecated', 'deprecated keyrings vendor hook');
  45. return $self->run_hook('package-keyrings', @params);
  46. } elsif ($hook eq 'archive-keyrings') {
  47. return ('/usr/share/keyrings/debian-archive-keyring.gpg');
  48. } elsif ($hook eq 'archive-keyrings-historic') {
  49. return ('/usr/share/keyrings/debian-archive-removed-keys.gpg');
  50. } elsif ($hook eq 'builtin-build-depends') {
  51. return qw(build-essential:native);
  52. } elsif ($hook eq 'builtin-build-conflicts') {
  53. return ();
  54. } elsif ($hook eq 'register-custom-fields') {
  55. } elsif ($hook eq 'extend-patch-header') {
  56. my ($textref, $ch_info) = @params;
  57. if ($ch_info->{'Closes'}) {
  58. foreach my $bug (split(/\s+/, $ch_info->{'Closes'})) {
  59. $$textref .= "Bug-Debian: https://bugs.debian.org/$bug\n";
  60. }
  61. }
  62. # XXX: Layer violation...
  63. require Dpkg::Vendor::Ubuntu;
  64. my $b = Dpkg::Vendor::Ubuntu::find_launchpad_closes($ch_info->{'Changes'});
  65. foreach my $bug (@$b) {
  66. $$textref .= "Bug-Ubuntu: https://bugs.launchpad.net/bugs/$bug\n";
  67. }
  68. } elsif ($hook eq 'update-buildflags') {
  69. $self->_add_qa_flags(@params);
  70. $self->_add_reproducible_flags(@params);
  71. $self->_add_sanitize_flags(@params);
  72. $self->_add_hardening_flags(@params);
  73. } elsif ($hook eq 'builtin-system-build-paths') {
  74. return qw(/build/);
  75. } else {
  76. return $self->SUPER::run_hook($hook, @params);
  77. }
  78. }
  79. sub _parse_feature_area {
  80. my ($self, $area, $use_feature) = @_;
  81. # Adjust features based on user or maintainer's desires.
  82. my $opts = Dpkg::BuildOptions->new(envvar => 'DEB_BUILD_OPTIONS');
  83. $opts->parse_features($area, $use_feature);
  84. $opts = Dpkg::BuildOptions->new(envvar => 'DEB_BUILD_MAINT_OPTIONS');
  85. $opts->parse_features($area, $use_feature);
  86. }
  87. sub _add_qa_flags {
  88. my ($self, $flags) = @_;
  89. # Default feature states.
  90. my %use_feature = (
  91. bug => 0,
  92. canary => 0,
  93. );
  94. # Adjust features based on user or maintainer's desires.
  95. $self->_parse_feature_area('qa', \%use_feature);
  96. # Warnings that detect actual bugs.
  97. if ($use_feature{bug}) {
  98. foreach my $warnflag (qw(array-bounds clobbered volatile-register-var
  99. implicit-function-declaration)) {
  100. $flags->append('CFLAGS', "-Werror=$warnflag");
  101. $flags->append('CXXFLAGS', "-Werror=$warnflag");
  102. }
  103. }
  104. # Inject dummy canary options to detect issues with build flag propagation.
  105. if ($use_feature{canary}) {
  106. require Digest::MD5;
  107. my $id = Digest::MD5::md5_hex(int rand 4096);
  108. foreach my $flag (qw(CPPFLAGS CFLAGS OBJCFLAGS CXXFLAGS OBJCXXFLAGS)) {
  109. $flags->append($flag, "-D__DEB_CANARY_${flag}_${id}__");
  110. }
  111. $flags->append('LDFLAGS', "-Wl,-z,deb-canary-${id}");
  112. }
  113. # Store the feature usage.
  114. while (my ($feature, $enabled) = each %use_feature) {
  115. $flags->set_feature('qa', $feature, $enabled);
  116. }
  117. }
  118. sub _add_reproducible_flags {
  119. my ($self, $flags) = @_;
  120. # Default feature states.
  121. my %use_feature = (
  122. timeless => 1,
  123. fixdebugpath => 1,
  124. );
  125. my $build_path;
  126. # Adjust features based on user or maintainer's desires.
  127. $self->_parse_feature_area('reproducible', \%use_feature);
  128. # Mask features that might have an unsafe usage.
  129. if ($use_feature{fixdebugpath}) {
  130. require Cwd;
  131. $build_path = $ENV{DEB_BUILD_PATH} || Cwd::cwd();
  132. # If we have any unsafe character in the path, disable the flag,
  133. # so that we do not need to worry about escaping the characters
  134. # on output.
  135. if ($build_path =~ m/[^-+:.0-9a-zA-Z~\/_]/) {
  136. $use_feature{fixdebugpath} = 0;
  137. }
  138. }
  139. # Warn when the __TIME__, __DATE__ and __TIMESTAMP__ macros are used.
  140. if ($use_feature{timeless}) {
  141. $flags->append('CPPFLAGS', '-Wdate-time');
  142. }
  143. # Avoid storing the build path in the debug symbols.
  144. if ($use_feature{fixdebugpath}) {
  145. my $map = '-fdebug-prefix-map=' . $build_path . '=.';
  146. $flags->append('CFLAGS', $map);
  147. $flags->append('CXXFLAGS', $map);
  148. $flags->append('OBJCFLAGS', $map);
  149. $flags->append('OBJCXXFLAGS', $map);
  150. $flags->append('FFLAGS', $map);
  151. $flags->append('FCFLAGS', $map);
  152. $flags->append('GCJFLAGS', $map);
  153. }
  154. # Store the feature usage.
  155. while (my ($feature, $enabled) = each %use_feature) {
  156. $flags->set_feature('reproducible', $feature, $enabled);
  157. }
  158. }
  159. sub _add_sanitize_flags {
  160. my ($self, $flags) = @_;
  161. # Default feature states.
  162. my %use_feature = (
  163. address => 0,
  164. thread => 0,
  165. leak => 0,
  166. undefined => 0,
  167. );
  168. # Adjust features based on user or maintainer's desires.
  169. $self->_parse_feature_area('sanitize', \%use_feature);
  170. # Handle logical feature interactions.
  171. if ($use_feature{address} and $use_feature{thread}) {
  172. # Disable the thread sanitizer when the address one is active, they
  173. # are mutually incompatible.
  174. $use_feature{thread} = 0;
  175. }
  176. if ($use_feature{address} or $use_feature{thread}) {
  177. # Disable leak sanitizer, it is implied by the address or thread ones.
  178. $use_feature{leak} = 0;
  179. }
  180. if ($use_feature{address}) {
  181. my $flag = '-fsanitize=address -fno-omit-frame-pointer';
  182. $flags->append('CFLAGS', $flag);
  183. $flags->append('CXXFLAGS', $flag);
  184. $flags->append('LDFLAGS', '-fsanitize=address');
  185. }
  186. if ($use_feature{thread}) {
  187. my $flag = '-fsanitize=thread';
  188. $flags->append('CFLAGS', $flag);
  189. $flags->append('CXXFLAGS', $flag);
  190. $flags->append('LDFLAGS', $flag);
  191. }
  192. if ($use_feature{leak}) {
  193. $flags->append('LDFLAGS', '-fsanitize=leak');
  194. }
  195. if ($use_feature{undefined}) {
  196. my $flag = '-fsanitize=undefined';
  197. $flags->append('CFLAGS', $flag);
  198. $flags->append('CXXFLAGS', $flag);
  199. $flags->append('LDFLAGS', $flag);
  200. }
  201. # Store the feature usage.
  202. while (my ($feature, $enabled) = each %use_feature) {
  203. $flags->set_feature('sanitize', $feature, $enabled);
  204. }
  205. }
  206. sub _add_hardening_flags {
  207. my ($self, $flags) = @_;
  208. my $arch = get_host_arch();
  209. my ($abi, $libc, $os, $cpu) = debarch_to_debtuple($arch);
  210. unless (defined $abi and defined $libc and defined $os and defined $cpu) {
  211. warning(g_("unknown host architecture '%s'"), $arch);
  212. ($abi, $os, $cpu) = ('', '', '');
  213. }
  214. # Default feature states.
  215. my %use_feature = (
  216. # XXX: This is set to undef so that we can cope with the brokenness
  217. # of gcc managing this feature builtin.
  218. pie => undef,
  219. stackprotector => 1,
  220. stackprotectorstrong => 1,
  221. fortify => 1,
  222. format => 1,
  223. relro => 1,
  224. bindnow => 0,
  225. );
  226. my %builtin_feature = (
  227. pie => 1,
  228. );
  229. my %builtin_pie_arch = map { $_ => 1 } qw(
  230. amd64 arm64 armel armhf i386 kfreebsd-amd64 kfreebsd-i386
  231. mips mipsel mips64el ppc64el s390x sparc sparc64
  232. );
  233. # Mask builtin features that are not enabled by default in the compiler.
  234. if (not exists $builtin_pie_arch{$arch}) {
  235. $builtin_feature{pie} = 0;
  236. }
  237. # Adjust features based on user or maintainer's desires.
  238. $self->_parse_feature_area('hardening', \%use_feature);
  239. # Mask features that are not available on certain architectures.
  240. if ($os !~ /^(?:linux|kfreebsd|knetbsd|hurd)$/ or
  241. $cpu =~ /^(?:hppa|avr32)$/) {
  242. # Disabled on non-(linux/kfreebsd/knetbsd/hurd).
  243. # Disabled on hppa, avr32
  244. # (#574716).
  245. $use_feature{pie} = 0;
  246. }
  247. if ($cpu =~ /^(?:ia64|alpha|hppa|nios2)$/ or $arch eq 'arm') {
  248. # Stack protector disabled on ia64, alpha, hppa, nios2.
  249. # "warning: -fstack-protector not supported for this target"
  250. # Stack protector disabled on arm (ok on armel).
  251. # compiler supports it incorrectly (leads to SEGV)
  252. $use_feature{stackprotector} = 0;
  253. }
  254. if ($cpu =~ /^(?:ia64|hppa|avr32)$/) {
  255. # relro not implemented on ia64, hppa, avr32.
  256. $use_feature{relro} = 0;
  257. }
  258. # Mask features that might be influenced by other flags.
  259. if ($flags->{build_options}->has('noopt')) {
  260. # glibc 2.16 and later warn when using -O0 and _FORTIFY_SOURCE.
  261. $use_feature{fortify} = 0;
  262. }
  263. # Handle logical feature interactions.
  264. if ($use_feature{relro} == 0) {
  265. # Disable bindnow if relro is not enabled, since it has no
  266. # hardening ability without relro and may incur load penalties.
  267. $use_feature{bindnow} = 0;
  268. }
  269. if ($use_feature{stackprotector} == 0) {
  270. # Disable stackprotectorstrong if stackprotector is disabled.
  271. $use_feature{stackprotectorstrong} = 0;
  272. }
  273. # PIE
  274. if (defined $use_feature{pie} and $use_feature{pie} and
  275. not $builtin_feature{pie}) {
  276. my $flag = "-specs=$Dpkg::DATADIR/pie-compile.specs";
  277. $flags->append('CFLAGS', $flag);
  278. $flags->append('OBJCFLAGS', $flag);
  279. $flags->append('OBJCXXFLAGS', $flag);
  280. $flags->append('FFLAGS', $flag);
  281. $flags->append('FCFLAGS', $flag);
  282. $flags->append('CXXFLAGS', $flag);
  283. $flags->append('GCJFLAGS', $flag);
  284. $flags->append('LDFLAGS', "-specs=$Dpkg::DATADIR/pie-link.specs");
  285. } elsif (defined $use_feature{pie} and not $use_feature{pie} and
  286. $builtin_feature{pie}) {
  287. my $flag = "-specs=$Dpkg::DATADIR/no-pie-compile.specs";
  288. $flags->append('CFLAGS', $flag);
  289. $flags->append('OBJCFLAGS', $flag);
  290. $flags->append('OBJCXXFLAGS', $flag);
  291. $flags->append('FFLAGS', $flag);
  292. $flags->append('FCFLAGS', $flag);
  293. $flags->append('CXXFLAGS', $flag);
  294. $flags->append('GCJFLAGS', $flag);
  295. $flags->append('LDFLAGS', "-specs=$Dpkg::DATADIR/no-pie-link.specs");
  296. }
  297. # Stack protector
  298. if ($use_feature{stackprotectorstrong}) {
  299. my $flag = '-fstack-protector-strong';
  300. $flags->append('CFLAGS', $flag);
  301. $flags->append('OBJCFLAGS', $flag);
  302. $flags->append('OBJCXXFLAGS', $flag);
  303. $flags->append('FFLAGS', $flag);
  304. $flags->append('FCFLAGS', $flag);
  305. $flags->append('CXXFLAGS', $flag);
  306. $flags->append('GCJFLAGS', $flag);
  307. } elsif ($use_feature{stackprotector}) {
  308. my $flag = '-fstack-protector --param=ssp-buffer-size=4';
  309. $flags->append('CFLAGS', $flag);
  310. $flags->append('OBJCFLAGS', $flag);
  311. $flags->append('OBJCXXFLAGS', $flag);
  312. $flags->append('FFLAGS', $flag);
  313. $flags->append('FCFLAGS', $flag);
  314. $flags->append('CXXFLAGS', $flag);
  315. $flags->append('GCJFLAGS', $flag);
  316. }
  317. # Fortify Source
  318. if ($use_feature{fortify}) {
  319. $flags->append('CPPFLAGS', '-D_FORTIFY_SOURCE=2');
  320. }
  321. # Format Security
  322. if ($use_feature{format}) {
  323. my $flag = '-Wformat -Werror=format-security';
  324. $flags->append('CFLAGS', $flag);
  325. $flags->append('CXXFLAGS', $flag);
  326. $flags->append('OBJCFLAGS', $flag);
  327. $flags->append('OBJCXXFLAGS', $flag);
  328. }
  329. # Read-only Relocations
  330. if ($use_feature{relro}) {
  331. $flags->append('LDFLAGS', '-Wl,-z,relro');
  332. }
  333. # Bindnow
  334. if ($use_feature{bindnow}) {
  335. $flags->append('LDFLAGS', '-Wl,-z,now');
  336. }
  337. # Set used features to their builtin setting if unset.
  338. foreach my $feature (keys %builtin_feature) {
  339. $use_feature{$feature} //= $builtin_feature{$feature};
  340. }
  341. # Store the feature usage.
  342. while (my ($feature, $enabled) = each %use_feature) {
  343. $flags->set_feature('hardening', $feature, $enabled);
  344. }
  345. }
  346. =head1 CHANGES
  347. =head2 Version 0.xx
  348. This is a private module.
  349. =cut
  350. 1;