Debian.pm 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. # Copyright © 2009-2011 Raphaël Hertzog <hertzog@debian.org>
  2. #
  3. # Hardening build flags handling derived from work of:
  4. # Copyright © 2009-2011 Kees Cook <kees@debian.org>
  5. # Copyright © 2007-2008 Canonical, Ltd.
  6. #
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 2 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. package Dpkg::Vendor::Debian;
  20. use strict;
  21. use warnings;
  22. our $VERSION = '0.01';
  23. use Dpkg::Gettext;
  24. use Dpkg::ErrorHandling;
  25. use Dpkg::Control::Types;
  26. use Dpkg::BuildOptions;
  27. use Dpkg::Arch qw(get_host_arch debarch_to_debtriplet);
  28. use parent qw(Dpkg::Vendor::Default);
  29. =encoding utf8
  30. =head1 NAME
  31. Dpkg::Vendor::Debian - Debian vendor object
  32. =head1 DESCRIPTION
  33. This vendor object customize the behaviour of dpkg scripts
  34. for Debian specific actions.
  35. =cut
  36. sub run_hook {
  37. my ($self, $hook, @params) = @_;
  38. if ($hook eq 'keyrings') {
  39. return ('/usr/share/keyrings/debian-keyring.gpg',
  40. '/usr/share/keyrings/debian-maintainers.gpg');
  41. } elsif ($hook eq 'register-custom-fields') {
  42. } elsif ($hook eq 'extend-patch-header') {
  43. my ($textref, $ch_info) = @params;
  44. if ($ch_info->{'Closes'}) {
  45. foreach my $bug (split(/\s+/, $ch_info->{'Closes'})) {
  46. $$textref .= "Bug-Debian: https://bugs.debian.org/$bug\n";
  47. }
  48. }
  49. # XXX: Layer violation...
  50. require Dpkg::Vendor::Ubuntu;
  51. my $b = Dpkg::Vendor::Ubuntu::find_launchpad_closes($ch_info->{'Changes'});
  52. foreach my $bug (@$b) {
  53. $$textref .= "Bug-Ubuntu: https://bugs.launchpad.net/bugs/$bug\n";
  54. }
  55. } elsif ($hook eq 'update-buildflags') {
  56. $self->_add_qa_flags(@params);
  57. $self->_add_reproducible_flags(@params);
  58. $self->_add_sanitize_flags(@params);
  59. $self->_add_hardening_flags(@params);
  60. } else {
  61. return $self->SUPER::run_hook($hook, @params);
  62. }
  63. }
  64. sub _parse_build_options {
  65. my ($self, $variable, $area, $use_feature) = @_;
  66. # Adjust features based on user or maintainer's desires.
  67. my $opts = Dpkg::BuildOptions->new(envvar => $variable);
  68. foreach my $feature (split(/,/, $opts->get($area) // '')) {
  69. $feature = lc($feature);
  70. if ($feature =~ s/^([+-])//) {
  71. my $value = ($1 eq '+') ? 1 : 0;
  72. if ($feature eq 'all') {
  73. $use_feature->{$_} = $value foreach keys %{$use_feature};
  74. } else {
  75. if (exists $use_feature->{$feature}) {
  76. $use_feature->{$feature} = $value;
  77. } else {
  78. warning(g_('unknown %s feature in %s variable: %s'),
  79. $area, $variable, $feature);
  80. }
  81. }
  82. } else {
  83. warning(g_('incorrect value in %s option of %s variable: %s'),
  84. $area, $variable, $feature);
  85. }
  86. }
  87. }
  88. sub _parse_feature_area {
  89. my ($self, $area, $use_feature) = @_;
  90. $self->_parse_build_options('DEB_BUILD_OPTIONS', $area, $use_feature);
  91. $self->_parse_build_options('DEB_BUILD_MAINT_OPTIONS', $area, $use_feature);
  92. }
  93. sub _add_qa_flags {
  94. my ($self, $flags) = @_;
  95. # Default feature states.
  96. my %use_feature = (
  97. bug => 0,
  98. canary => 0,
  99. );
  100. # Adjust features based on user or maintainer's desires.
  101. $self->_parse_feature_area('qa', \%use_feature);
  102. # Warnings that detect actual bugs.
  103. if ($use_feature{bug}) {
  104. foreach my $warnflag (qw(array-bounds clobbered volatile-register-var
  105. implicit-function-declaration)) {
  106. $flags->append('CFLAGS', "-Werror=$warnflag");
  107. $flags->append('CXXFLAGS', "-Werror=$warnflag");
  108. }
  109. }
  110. # Inject dummy canary options to detect issues with build flag propagation.
  111. if ($use_feature{canary}) {
  112. require Digest::MD5;
  113. my $id = Digest::MD5::md5_hex(int rand 4096);
  114. foreach my $flag (qw(CPPFLAGS CFLAGS OBJCFLAGS CXXFLAGS OBJCXXFLAGS)) {
  115. $flags->append($flag, "-D__DEB_CANARY_${flag}_${id}__");
  116. }
  117. $flags->append('LDFLAGS', "-Wl,-z,deb-canary-${id}");
  118. }
  119. # Store the feature usage.
  120. while (my ($feature, $enabled) = each %use_feature) {
  121. $flags->set_feature('qa', $feature, $enabled);
  122. }
  123. }
  124. sub _add_reproducible_flags {
  125. my ($self, $flags) = @_;
  126. # Default feature states.
  127. my %use_feature = (
  128. timeless => 0,
  129. );
  130. # Adjust features based on user or maintainer's desires.
  131. $self->_parse_feature_area('reproducible', \%use_feature);
  132. # Warn when the __TIME__, __DATE__ and __TIMESTAMP__ macros are used.
  133. if ($use_feature{timeless}) {
  134. $flags->append('CPPFLAGS', '-Wdate-time');
  135. }
  136. # Store the feature usage.
  137. while (my ($feature, $enabled) = each %use_feature) {
  138. $flags->set_feature('reproducible', $feature, $enabled);
  139. }
  140. }
  141. sub _add_sanitize_flags {
  142. my ($self, $flags) = @_;
  143. # Default feature states.
  144. my %use_feature = (
  145. address => 0,
  146. thread => 0,
  147. leak => 0,
  148. undefined => 0,
  149. );
  150. # Adjust features based on user or maintainer's desires.
  151. $self->_parse_feature_area('sanitize', \%use_feature);
  152. # Handle logical feature interactions.
  153. if ($use_feature{address} or $use_feature{thread}) {
  154. # Disable leak sanitizer, it is implied by the address or thread ones.
  155. $use_feature{leak} = 0;
  156. }
  157. if ($use_feature{address}) {
  158. my $flag = '-fsanitize=address -fno-omit-frame-pointer';
  159. $flags->append('CFLAGS', $flag);
  160. $flags->append('CXXFLAGS', $flag);
  161. $flags->append('LDFLAGS', '-fsanitize=address');
  162. }
  163. if ($use_feature{thread}) {
  164. my $flag = '-fsanitize=thread';
  165. $flags->append('CFLAGS', $flag);
  166. $flags->append('CXXFLAGS', $flag);
  167. $flags->append('LDFLAGS', $flag);
  168. }
  169. if ($use_feature{leak}) {
  170. $flags->append('LDFLAGS', '-fsanitize=leak');
  171. }
  172. if ($use_feature{undefined}) {
  173. my $flag = '-fsanitize=undefined';
  174. $flags->append('CFLAGS', $flag);
  175. $flags->append('CXXFLAGS', $flag);
  176. $flags->append('LDFLAGS', $flag);
  177. }
  178. # Store the feature usage.
  179. while (my ($feature, $enabled) = each %use_feature) {
  180. $flags->set_feature('sanitize', $feature, $enabled);
  181. }
  182. }
  183. sub _add_hardening_flags {
  184. my ($self, $flags) = @_;
  185. my $arch = get_host_arch();
  186. my ($abi, $os, $cpu) = debarch_to_debtriplet($arch);
  187. unless (defined $abi and defined $os and defined $cpu) {
  188. warning(g_("unknown host architecture '%s'"), $arch);
  189. ($abi, $os, $cpu) = ('', '', '');
  190. }
  191. # Default feature states.
  192. my %use_feature = (
  193. pie => 0,
  194. stackprotector => 1,
  195. stackprotectorstrong => 1,
  196. fortify => 1,
  197. format => 1,
  198. relro => 1,
  199. bindnow => 0,
  200. );
  201. # Adjust features based on user or maintainer's desires.
  202. $self->_parse_feature_area('hardening', \%use_feature);
  203. # Mask features that are not available on certain architectures.
  204. if ($os !~ /^(?:linux|knetbsd|hurd)$/ or
  205. $cpu =~ /^(?:hppa|avr32)$/) {
  206. # Disabled on non-linux/knetbsd/hurd (see #430455 and #586215).
  207. # Disabled on hppa, avr32
  208. # (#574716).
  209. $use_feature{pie} = 0;
  210. }
  211. if ($cpu =~ /^(?:ia64|alpha|hppa)$/ or $arch eq 'arm') {
  212. # Stack protector disabled on ia64, alpha, hppa.
  213. # "warning: -fstack-protector not supported for this target"
  214. # Stack protector disabled on arm (ok on armel).
  215. # compiler supports it incorrectly (leads to SEGV)
  216. $use_feature{stackprotector} = 0;
  217. }
  218. if ($cpu =~ /^(?:ia64|hppa|avr32)$/) {
  219. # relro not implemented on ia64, hppa, avr32.
  220. $use_feature{relro} = 0;
  221. }
  222. # Mask features that might be influenced by other flags.
  223. if ($flags->{build_options}->has('noopt')) {
  224. # glibc 2.16 and later warn when using -O0 and _FORTIFY_SOURCE.
  225. $use_feature{fortify} = 0;
  226. }
  227. # Handle logical feature interactions.
  228. if ($use_feature{relro} == 0) {
  229. # Disable bindnow if relro is not enabled, since it has no
  230. # hardening ability without relro and may incur load penalties.
  231. $use_feature{bindnow} = 0;
  232. }
  233. if ($use_feature{stackprotector} == 0) {
  234. # Disable stackprotectorstrong if stackprotector is disabled.
  235. $use_feature{stackprotectorstrong} = 0;
  236. }
  237. # PIE
  238. if ($use_feature{pie}) {
  239. my $flag = '-fPIE';
  240. $flags->append('CFLAGS', $flag);
  241. $flags->append('OBJCFLAGS', $flag);
  242. $flags->append('OBJCXXFLAGS', $flag);
  243. $flags->append('FFLAGS', $flag);
  244. $flags->append('FCFLAGS', $flag);
  245. $flags->append('CXXFLAGS', $flag);
  246. $flags->append('GCJFLAGS', $flag);
  247. $flags->append('LDFLAGS', '-fPIE -pie');
  248. }
  249. # Stack protector
  250. if ($use_feature{stackprotectorstrong}) {
  251. my $flag = '-fstack-protector-strong';
  252. $flags->append('CFLAGS', $flag);
  253. $flags->append('OBJCFLAGS', $flag);
  254. $flags->append('OBJCXXFLAGS', $flag);
  255. $flags->append('FFLAGS', $flag);
  256. $flags->append('FCFLAGS', $flag);
  257. $flags->append('CXXFLAGS', $flag);
  258. $flags->append('GCJFLAGS', $flag);
  259. } elsif ($use_feature{stackprotector}) {
  260. my $flag = '-fstack-protector --param=ssp-buffer-size=4';
  261. $flags->append('CFLAGS', $flag);
  262. $flags->append('OBJCFLAGS', $flag);
  263. $flags->append('OBJCXXFLAGS', $flag);
  264. $flags->append('FFLAGS', $flag);
  265. $flags->append('FCFLAGS', $flag);
  266. $flags->append('CXXFLAGS', $flag);
  267. $flags->append('GCJFLAGS', $flag);
  268. }
  269. # Fortify Source
  270. if ($use_feature{fortify}) {
  271. $flags->append('CPPFLAGS', '-D_FORTIFY_SOURCE=2');
  272. }
  273. # Format Security
  274. if ($use_feature{format}) {
  275. my $flag = '-Wformat -Werror=format-security';
  276. $flags->append('CFLAGS', $flag);
  277. $flags->append('CXXFLAGS', $flag);
  278. $flags->append('OBJCFLAGS', $flag);
  279. $flags->append('OBJCXXFLAGS', $flag);
  280. }
  281. # Read-only Relocations
  282. if ($use_feature{relro}) {
  283. $flags->append('LDFLAGS', '-Wl,-z,relro');
  284. }
  285. # Bindnow
  286. if ($use_feature{bindnow}) {
  287. $flags->append('LDFLAGS', '-Wl,-z,now');
  288. }
  289. # Store the feature usage.
  290. while (my ($feature, $enabled) = each %use_feature) {
  291. $flags->set_feature('hardening', $feature, $enabled);
  292. }
  293. }
  294. =head1 CHANGES
  295. =head2 Version 0.xx
  296. This is a private module.
  297. =cut
  298. 1;