Debian.pm 11 KB

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