Debian.pm 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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;
  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_build_options {
  80. my ($self, $variable, $area, $use_feature) = @_;
  81. # Adjust features based on user or maintainer's desires.
  82. my $opts = Dpkg::BuildOptions->new(envvar => $variable);
  83. foreach my $feature (split(/,/, $opts->get($area) // '')) {
  84. $feature = lc($feature);
  85. if ($feature =~ s/^([+-])//) {
  86. my $value = ($1 eq '+') ? 1 : 0;
  87. if ($feature eq 'all') {
  88. $use_feature->{$_} = $value foreach keys %{$use_feature};
  89. } else {
  90. if (exists $use_feature->{$feature}) {
  91. $use_feature->{$feature} = $value;
  92. } else {
  93. warning(g_('unknown %s feature in %s variable: %s'),
  94. $area, $variable, $feature);
  95. }
  96. }
  97. } else {
  98. warning(g_('incorrect value in %s option of %s variable: %s'),
  99. $area, $variable, $feature);
  100. }
  101. }
  102. }
  103. sub _parse_feature_area {
  104. my ($self, $area, $use_feature) = @_;
  105. $self->_parse_build_options('DEB_BUILD_OPTIONS', $area, $use_feature);
  106. $self->_parse_build_options('DEB_BUILD_MAINT_OPTIONS', $area, $use_feature);
  107. }
  108. sub _add_qa_flags {
  109. my ($self, $flags) = @_;
  110. # Default feature states.
  111. my %use_feature = (
  112. bug => 0,
  113. canary => 0,
  114. );
  115. # Adjust features based on user or maintainer's desires.
  116. $self->_parse_feature_area('qa', \%use_feature);
  117. # Warnings that detect actual bugs.
  118. if ($use_feature{bug}) {
  119. foreach my $warnflag (qw(array-bounds clobbered volatile-register-var
  120. implicit-function-declaration)) {
  121. $flags->append('CFLAGS', "-Werror=$warnflag");
  122. $flags->append('CXXFLAGS', "-Werror=$warnflag");
  123. }
  124. }
  125. # Inject dummy canary options to detect issues with build flag propagation.
  126. if ($use_feature{canary}) {
  127. require Digest::MD5;
  128. my $id = Digest::MD5::md5_hex(int rand 4096);
  129. foreach my $flag (qw(CPPFLAGS CFLAGS OBJCFLAGS CXXFLAGS OBJCXXFLAGS)) {
  130. $flags->append($flag, "-D__DEB_CANARY_${flag}_${id}__");
  131. }
  132. $flags->append('LDFLAGS', "-Wl,-z,deb-canary-${id}");
  133. }
  134. # Store the feature usage.
  135. while (my ($feature, $enabled) = each %use_feature) {
  136. $flags->set_feature('qa', $feature, $enabled);
  137. }
  138. }
  139. sub _add_reproducible_flags {
  140. my ($self, $flags) = @_;
  141. # Default feature states.
  142. my %use_feature = (
  143. timeless => 1,
  144. fixdebugpath => 1,
  145. );
  146. my $build_path;
  147. # Adjust features based on user or maintainer's desires.
  148. $self->_parse_feature_area('reproducible', \%use_feature);
  149. # Mask features that might have an unsafe usage.
  150. if ($use_feature{fixdebugpath}) {
  151. require Cwd;
  152. $build_path = $ENV{DEB_BUILD_PATH} || Cwd::cwd();
  153. # If we have any unsafe character in the path, disable the flag,
  154. # so that we do not need to worry about escaping the characters
  155. # on output.
  156. if ($build_path =~ m/[^-+:.0-9a-zA-Z~\/_]/) {
  157. $use_feature{fixdebugpath} = 0;
  158. }
  159. }
  160. # Warn when the __TIME__, __DATE__ and __TIMESTAMP__ macros are used.
  161. if ($use_feature{timeless}) {
  162. $flags->append('CPPFLAGS', '-Wdate-time');
  163. }
  164. # Avoid storing the build path in the debug symbols.
  165. if ($use_feature{fixdebugpath}) {
  166. my $map = '-fdebug-prefix-map=' . $build_path . '=.';
  167. $flags->append('CFLAGS', $map);
  168. $flags->append('CXXFLAGS', $map);
  169. $flags->append('OBJCFLAGS', $map);
  170. $flags->append('OBJCXXFLAGS', $map);
  171. $flags->append('FFLAGS', $map);
  172. $flags->append('FCFLAGS', $map);
  173. $flags->append('GCJFLAGS', $map);
  174. }
  175. # Store the feature usage.
  176. while (my ($feature, $enabled) = each %use_feature) {
  177. $flags->set_feature('reproducible', $feature, $enabled);
  178. }
  179. }
  180. sub _add_sanitize_flags {
  181. my ($self, $flags) = @_;
  182. # Default feature states.
  183. my %use_feature = (
  184. address => 0,
  185. thread => 0,
  186. leak => 0,
  187. undefined => 0,
  188. );
  189. # Adjust features based on user or maintainer's desires.
  190. $self->_parse_feature_area('sanitize', \%use_feature);
  191. # Handle logical feature interactions.
  192. if ($use_feature{address} and $use_feature{thread}) {
  193. # Disable the thread sanitizer when the address one is active, they
  194. # are mutually incompatible.
  195. $use_feature{thread} = 0;
  196. }
  197. if ($use_feature{address} or $use_feature{thread}) {
  198. # Disable leak sanitizer, it is implied by the address or thread ones.
  199. $use_feature{leak} = 0;
  200. }
  201. if ($use_feature{address}) {
  202. my $flag = '-fsanitize=address -fno-omit-frame-pointer';
  203. $flags->append('CFLAGS', $flag);
  204. $flags->append('CXXFLAGS', $flag);
  205. $flags->append('LDFLAGS', '-fsanitize=address');
  206. }
  207. if ($use_feature{thread}) {
  208. my $flag = '-fsanitize=thread';
  209. $flags->append('CFLAGS', $flag);
  210. $flags->append('CXXFLAGS', $flag);
  211. $flags->append('LDFLAGS', $flag);
  212. }
  213. if ($use_feature{leak}) {
  214. $flags->append('LDFLAGS', '-fsanitize=leak');
  215. }
  216. if ($use_feature{undefined}) {
  217. my $flag = '-fsanitize=undefined';
  218. $flags->append('CFLAGS', $flag);
  219. $flags->append('CXXFLAGS', $flag);
  220. $flags->append('LDFLAGS', $flag);
  221. }
  222. # Store the feature usage.
  223. while (my ($feature, $enabled) = each %use_feature) {
  224. $flags->set_feature('sanitize', $feature, $enabled);
  225. }
  226. }
  227. sub _add_hardening_flags {
  228. my ($self, $flags) = @_;
  229. my $arch = get_host_arch();
  230. my ($abi, $libc, $os, $cpu) = debarch_to_debtuple($arch);
  231. unless (defined $abi and defined $libc and defined $os and defined $cpu) {
  232. warning(g_("unknown host architecture '%s'"), $arch);
  233. ($abi, $os, $cpu) = ('', '', '');
  234. }
  235. # Default feature states.
  236. my %use_feature = (
  237. pie => 1,
  238. stackprotector => 1,
  239. stackprotectorstrong => 1,
  240. fortify => 1,
  241. format => 1,
  242. relro => 1,
  243. bindnow => 0,
  244. );
  245. my %builtin_feature = (
  246. pie => 1,
  247. );
  248. # Mask builtin features that are not enabled by default in the compiler.
  249. if ($arch !~ /^(?:amd64|arm64|armel|armhf|i386|mips|mipsel|mips64el|ppc64el|s390x)$/) {
  250. $builtin_feature{pie} = 0;
  251. }
  252. # Adjust features based on user or maintainer's desires.
  253. $self->_parse_feature_area('hardening', \%use_feature);
  254. # Mask features that are not available on certain architectures.
  255. if ($os !~ /^(?:linux|kfreebsd|knetbsd|hurd)$/ or
  256. $cpu =~ /^(?:hppa|avr32)$/) {
  257. # Disabled on non-(linux/kfreebsd/knetbsd/hurd).
  258. # Disabled on hppa, avr32
  259. # (#574716).
  260. $use_feature{pie} = 0;
  261. }
  262. if ($cpu =~ /^(?:ia64|alpha|hppa|nios2)$/ or $arch eq 'arm') {
  263. # Stack protector disabled on ia64, alpha, hppa, nios2.
  264. # "warning: -fstack-protector not supported for this target"
  265. # Stack protector disabled on arm (ok on armel).
  266. # compiler supports it incorrectly (leads to SEGV)
  267. $use_feature{stackprotector} = 0;
  268. }
  269. if ($cpu =~ /^(?:ia64|hppa|avr32)$/) {
  270. # relro not implemented on ia64, hppa, avr32.
  271. $use_feature{relro} = 0;
  272. }
  273. # Mask features that might be influenced by other flags.
  274. if ($flags->{build_options}->has('noopt')) {
  275. # glibc 2.16 and later warn when using -O0 and _FORTIFY_SOURCE.
  276. $use_feature{fortify} = 0;
  277. }
  278. # Handle logical feature interactions.
  279. if ($use_feature{relro} == 0) {
  280. # Disable bindnow if relro is not enabled, since it has no
  281. # hardening ability without relro and may incur load penalties.
  282. $use_feature{bindnow} = 0;
  283. }
  284. if ($use_feature{stackprotector} == 0) {
  285. # Disable stackprotectorstrong if stackprotector is disabled.
  286. $use_feature{stackprotectorstrong} = 0;
  287. }
  288. # PIE
  289. if ($use_feature{pie} and not $builtin_feature{pie}) {
  290. my $flag = "-specs=$Dpkg::DATADIR/pie-compile.specs";
  291. $flags->append('CFLAGS', $flag);
  292. $flags->append('OBJCFLAGS', $flag);
  293. $flags->append('OBJCXXFLAGS', $flag);
  294. $flags->append('FFLAGS', $flag);
  295. $flags->append('FCFLAGS', $flag);
  296. $flags->append('CXXFLAGS', $flag);
  297. $flags->append('GCJFLAGS', $flag);
  298. $flags->append('LDFLAGS', "-specs=$Dpkg::DATADIR/pie-link.specs");
  299. } elsif (not $use_feature{pie} and $builtin_feature{pie}) {
  300. my $flag = "-specs=$Dpkg::DATADIR/no-pie-compile.specs";
  301. $flags->append('CFLAGS', $flag);
  302. $flags->append('OBJCFLAGS', $flag);
  303. $flags->append('OBJCXXFLAGS', $flag);
  304. $flags->append('FFLAGS', $flag);
  305. $flags->append('FCFLAGS', $flag);
  306. $flags->append('CXXFLAGS', $flag);
  307. $flags->append('GCJFLAGS', $flag);
  308. $flags->append('LDFLAGS', "-specs=$Dpkg::DATADIR/no-pie-link.specs");
  309. }
  310. # Stack protector
  311. if ($use_feature{stackprotectorstrong}) {
  312. my $flag = '-fstack-protector-strong';
  313. $flags->append('CFLAGS', $flag);
  314. $flags->append('OBJCFLAGS', $flag);
  315. $flags->append('OBJCXXFLAGS', $flag);
  316. $flags->append('FFLAGS', $flag);
  317. $flags->append('FCFLAGS', $flag);
  318. $flags->append('CXXFLAGS', $flag);
  319. $flags->append('GCJFLAGS', $flag);
  320. } elsif ($use_feature{stackprotector}) {
  321. my $flag = '-fstack-protector --param=ssp-buffer-size=4';
  322. $flags->append('CFLAGS', $flag);
  323. $flags->append('OBJCFLAGS', $flag);
  324. $flags->append('OBJCXXFLAGS', $flag);
  325. $flags->append('FFLAGS', $flag);
  326. $flags->append('FCFLAGS', $flag);
  327. $flags->append('CXXFLAGS', $flag);
  328. $flags->append('GCJFLAGS', $flag);
  329. }
  330. # Fortify Source
  331. if ($use_feature{fortify}) {
  332. $flags->append('CPPFLAGS', '-D_FORTIFY_SOURCE=2');
  333. }
  334. # Format Security
  335. if ($use_feature{format}) {
  336. my $flag = '-Wformat -Werror=format-security';
  337. $flags->append('CFLAGS', $flag);
  338. $flags->append('CXXFLAGS', $flag);
  339. $flags->append('OBJCFLAGS', $flag);
  340. $flags->append('OBJCXXFLAGS', $flag);
  341. }
  342. # Read-only Relocations
  343. if ($use_feature{relro}) {
  344. $flags->append('LDFLAGS', '-Wl,-z,relro');
  345. }
  346. # Bindnow
  347. if ($use_feature{bindnow}) {
  348. $flags->append('LDFLAGS', '-Wl,-z,now');
  349. }
  350. # Store the feature usage.
  351. while (my ($feature, $enabled) = each %use_feature) {
  352. $flags->set_feature('hardening', $feature, $enabled);
  353. }
  354. }
  355. =head1 CHANGES
  356. =head2 Version 0.xx
  357. This is a private module.
  358. =cut
  359. 1;