Debian.pm 11 KB

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