Debian.pm 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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 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 'keyrings') {
  40. return ('/usr/share/keyrings/debian-keyring.gpg',
  41. '/usr/share/keyrings/debian-maintainers.gpg');
  42. } elsif ($hook eq 'builtin-build-depends') {
  43. return qw(build-essential:native);
  44. } elsif ($hook eq 'builtin-build-conflicts') {
  45. return ();
  46. } elsif ($hook eq 'register-custom-fields') {
  47. } elsif ($hook eq 'extend-patch-header') {
  48. my ($textref, $ch_info) = @params;
  49. if ($ch_info->{'Closes'}) {
  50. foreach my $bug (split(/\s+/, $ch_info->{'Closes'})) {
  51. $$textref .= "Bug-Debian: https://bugs.debian.org/$bug\n";
  52. }
  53. }
  54. # XXX: Layer violation...
  55. require Dpkg::Vendor::Ubuntu;
  56. my $b = Dpkg::Vendor::Ubuntu::find_launchpad_closes($ch_info->{'Changes'});
  57. foreach my $bug (@$b) {
  58. $$textref .= "Bug-Ubuntu: https://bugs.launchpad.net/bugs/$bug\n";
  59. }
  60. } elsif ($hook eq 'update-buildflags') {
  61. $self->_add_qa_flags(@params);
  62. $self->_add_reproducible_flags(@params);
  63. $self->_add_sanitize_flags(@params);
  64. $self->_add_hardening_flags(@params);
  65. } else {
  66. return $self->SUPER::run_hook($hook, @params);
  67. }
  68. }
  69. sub _parse_build_options {
  70. my ($self, $variable, $area, $use_feature) = @_;
  71. # Adjust features based on user or maintainer's desires.
  72. my $opts = Dpkg::BuildOptions->new(envvar => $variable);
  73. foreach my $feature (split(/,/, $opts->get($area) // '')) {
  74. $feature = lc($feature);
  75. if ($feature =~ s/^([+-])//) {
  76. my $value = ($1 eq '+') ? 1 : 0;
  77. if ($feature eq 'all') {
  78. $use_feature->{$_} = $value foreach keys %{$use_feature};
  79. } else {
  80. if (exists $use_feature->{$feature}) {
  81. $use_feature->{$feature} = $value;
  82. } else {
  83. warning(g_('unknown %s feature in %s variable: %s'),
  84. $area, $variable, $feature);
  85. }
  86. }
  87. } else {
  88. warning(g_('incorrect value in %s option of %s variable: %s'),
  89. $area, $variable, $feature);
  90. }
  91. }
  92. }
  93. sub _parse_feature_area {
  94. my ($self, $area, $use_feature) = @_;
  95. $self->_parse_build_options('DEB_BUILD_OPTIONS', $area, $use_feature);
  96. $self->_parse_build_options('DEB_BUILD_MAINT_OPTIONS', $area, $use_feature);
  97. }
  98. sub _add_qa_flags {
  99. my ($self, $flags) = @_;
  100. # Default feature states.
  101. my %use_feature = (
  102. bug => 0,
  103. canary => 0,
  104. );
  105. # Adjust features based on user or maintainer's desires.
  106. $self->_parse_feature_area('qa', \%use_feature);
  107. # Warnings that detect actual bugs.
  108. if ($use_feature{bug}) {
  109. foreach my $warnflag (qw(array-bounds clobbered volatile-register-var
  110. implicit-function-declaration)) {
  111. $flags->append('CFLAGS', "-Werror=$warnflag");
  112. $flags->append('CXXFLAGS', "-Werror=$warnflag");
  113. }
  114. }
  115. # Inject dummy canary options to detect issues with build flag propagation.
  116. if ($use_feature{canary}) {
  117. require Digest::MD5;
  118. my $id = Digest::MD5::md5_hex(int rand 4096);
  119. foreach my $flag (qw(CPPFLAGS CFLAGS OBJCFLAGS CXXFLAGS OBJCXXFLAGS)) {
  120. $flags->append($flag, "-D__DEB_CANARY_${flag}_${id}__");
  121. }
  122. $flags->append('LDFLAGS', "-Wl,-z,deb-canary-${id}");
  123. }
  124. # Store the feature usage.
  125. while (my ($feature, $enabled) = each %use_feature) {
  126. $flags->set_feature('qa', $feature, $enabled);
  127. }
  128. }
  129. sub _add_reproducible_flags {
  130. my ($self, $flags) = @_;
  131. # Default feature states.
  132. my %use_feature = (
  133. timeless => 1,
  134. fixdebugpath => 0,
  135. );
  136. # Adjust features based on user or maintainer's desires.
  137. $self->_parse_feature_area('reproducible', \%use_feature);
  138. # Warn when the __TIME__, __DATE__ and __TIMESTAMP__ macros are used.
  139. if ($use_feature{timeless}) {
  140. $flags->append('CPPFLAGS', '-Wdate-time');
  141. }
  142. # Avoid storing the build path in the debug symbols.
  143. if ($use_feature{fixdebugpath}) {
  144. require Cwd;
  145. my $map = '-fdebug-prefix-map=' . Cwd::cwd() . '=.';
  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, $os, $cpu) = debarch_to_debtriplet($arch);
  210. unless (defined $abi 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. pie => 0,
  217. stackprotector => 1,
  218. stackprotectorstrong => 1,
  219. fortify => 1,
  220. format => 1,
  221. relro => 1,
  222. bindnow => 0,
  223. );
  224. # Adjust features based on user or maintainer's desires.
  225. $self->_parse_feature_area('hardening', \%use_feature);
  226. # Mask features that are not available on certain architectures.
  227. if ($os !~ /^(?:linux|kfreebsd|knetbsd|hurd)$/ or
  228. $cpu =~ /^(?:hppa|avr32)$/) {
  229. # Disabled on non-(linux/kfreebsd/knetbsd/hurd).
  230. # Disabled on hppa, avr32
  231. # (#574716).
  232. $use_feature{pie} = 0;
  233. }
  234. if ($cpu =~ /^(?:ia64|alpha|hppa|nios2)$/ or $arch eq 'arm') {
  235. # Stack protector disabled on ia64, alpha, hppa, nios2.
  236. # "warning: -fstack-protector not supported for this target"
  237. # Stack protector disabled on arm (ok on armel).
  238. # compiler supports it incorrectly (leads to SEGV)
  239. $use_feature{stackprotector} = 0;
  240. }
  241. if ($cpu =~ /^(?:ia64|hppa|avr32)$/) {
  242. # relro not implemented on ia64, hppa, avr32.
  243. $use_feature{relro} = 0;
  244. }
  245. # Mask features that might be influenced by other flags.
  246. if ($flags->{build_options}->has('noopt')) {
  247. # glibc 2.16 and later warn when using -O0 and _FORTIFY_SOURCE.
  248. $use_feature{fortify} = 0;
  249. }
  250. # Handle logical feature interactions.
  251. if ($use_feature{relro} == 0) {
  252. # Disable bindnow if relro is not enabled, since it has no
  253. # hardening ability without relro and may incur load penalties.
  254. $use_feature{bindnow} = 0;
  255. }
  256. if ($use_feature{stackprotector} == 0) {
  257. # Disable stackprotectorstrong if stackprotector is disabled.
  258. $use_feature{stackprotectorstrong} = 0;
  259. }
  260. # PIE
  261. if ($use_feature{pie}) {
  262. my $flag = '-fPIE';
  263. $flags->append('CFLAGS', $flag);
  264. $flags->append('OBJCFLAGS', $flag);
  265. $flags->append('OBJCXXFLAGS', $flag);
  266. $flags->append('FFLAGS', $flag);
  267. $flags->append('FCFLAGS', $flag);
  268. $flags->append('CXXFLAGS', $flag);
  269. $flags->append('GCJFLAGS', $flag);
  270. $flags->append('LDFLAGS', '-fPIE -pie');
  271. }
  272. # Stack protector
  273. if ($use_feature{stackprotectorstrong}) {
  274. my $flag = '-fstack-protector-strong';
  275. $flags->append('CFLAGS', $flag);
  276. $flags->append('OBJCFLAGS', $flag);
  277. $flags->append('OBJCXXFLAGS', $flag);
  278. $flags->append('FFLAGS', $flag);
  279. $flags->append('FCFLAGS', $flag);
  280. $flags->append('CXXFLAGS', $flag);
  281. $flags->append('GCJFLAGS', $flag);
  282. } elsif ($use_feature{stackprotector}) {
  283. my $flag = '-fstack-protector --param=ssp-buffer-size=4';
  284. $flags->append('CFLAGS', $flag);
  285. $flags->append('OBJCFLAGS', $flag);
  286. $flags->append('OBJCXXFLAGS', $flag);
  287. $flags->append('FFLAGS', $flag);
  288. $flags->append('FCFLAGS', $flag);
  289. $flags->append('CXXFLAGS', $flag);
  290. $flags->append('GCJFLAGS', $flag);
  291. }
  292. # Fortify Source
  293. if ($use_feature{fortify}) {
  294. $flags->append('CPPFLAGS', '-D_FORTIFY_SOURCE=2');
  295. }
  296. # Format Security
  297. if ($use_feature{format}) {
  298. my $flag = '-Wformat -Werror=format-security';
  299. $flags->append('CFLAGS', $flag);
  300. $flags->append('CXXFLAGS', $flag);
  301. $flags->append('OBJCFLAGS', $flag);
  302. $flags->append('OBJCXXFLAGS', $flag);
  303. }
  304. # Read-only Relocations
  305. if ($use_feature{relro}) {
  306. $flags->append('LDFLAGS', '-Wl,-z,relro');
  307. }
  308. # Bindnow
  309. if ($use_feature{bindnow}) {
  310. $flags->append('LDFLAGS', '-Wl,-z,now');
  311. }
  312. # Store the feature usage.
  313. while (my ($feature, $enabled) = each %use_feature) {
  314. $flags->set_feature('hardening', $feature, $enabled);
  315. }
  316. }
  317. =head1 CHANGES
  318. =head2 Version 0.xx
  319. This is a private module.
  320. =cut
  321. 1;