Debian.pm 12 KB

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