Debian.pm 12 KB

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