{"id":2222,"date":"2015-09-07T21:59:26","date_gmt":"2015-09-08T00:59:26","guid":{"rendered":"http:\/\/eriberto.pro.br\/blog\/?p=2222"},"modified":"2017-09-05T15:52:20","modified_gmt":"2017-09-05T18:52:20","slug":"debian-how-to-use-blhc-to-solve-hardening-issues-when-packaging","status":"publish","type":"post","link":"https:\/\/eriberto.pro.br\/blog\/2015\/09\/07\/debian-how-to-use-blhc-to-solve-hardening-issues-when-packaging\/","title":{"rendered":"Debian: how to use blhc to solve hardening issues when packaging"},"content":{"rendered":"<h3><span style=\"color: #ff0000;\"><span style=\"background-color: #ffff00;\">UPDATE: this post was originally published on Sep. 7, 2015. I did a full review on Sep. 5, 2017. This revision is full compliant with Debian 9 and dpkg 1.18.13 or latter.<\/span><\/span><\/h3>\n<h2>Implementing the hardening<\/h2>\n<p>When packaging in Debian, is very common to see some lintian messages as &#8216;<strong>hardening-no-relro<\/strong>&#8216; and &#8216;<strong>hardening-no-fortify-functions<\/strong>&#8216; in softwares written in C or C++. To solve these issues, we can use the &#8216;<strong>blhc<\/strong>&#8216; tool (apt-get install blhc).<\/p>\n<p>Please, get the revision 1.11-9 of the icmpinfo package. You can get this revision from <a href=\"http:\/\/snapshot.debian.org\">http:\/\/snapshot.debian.org<\/a> or from <a href=\"http:\/\/eriberto.pro.br\/debian\/icmpinfo\">http:\/\/eriberto.pro.br\/debian\/icmpinfo<\/a>. As a shortcut, you can use the following command:<\/p>\n<pre>$ dget -u http:\/\/eriberto.pro.br\/debian\/icmpinfo\/icmpinfo_1.11-9.dsc<\/pre>\n<p>The icmpinfo 1.11-9 is almost clean for lintian (in 2015-09-07, Standards-Version 3.9.6). The most relevant problem is:<\/p>\n<pre>W: icmpinfo: hardening-no-relro usr\/sbin\/icmpinfo<\/pre>\n<p>To track the problem I will use <em>blhc<\/em> over the <em>.build<\/em> file:<\/p>\n<pre>$ blhc icmpinfo_1.11-9_amd64.build\r\nLDFLAGS missing (-Wl,-z,relro): cc -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -o icmpinfo recvping.o print.o err.o icmpinfo.o pid.o<\/pre>\n<p>Note that the problem is some missing options (-Wl,-z,relro) for LDFLAGS when building icmpinfo (for newbies, in GCC, -o is used to indicate the name to be used for the final binary after the compilation). If you are using the DebHelper compat 9 (debian\/compat=9) and the DebHelper 9 (debhelper &gt;= 9 in Build-Depends field in d\/control), some variables as CFLAGS, LDFLAGS, CPPFLAGS and CXXFLAGS will be automatically passed during calls to dh_auto_* programs (yes, you should use the new and reduced debian\/rules format &#8211; as an example, see the debian\/rules of the icmpinfo 1.11-9; if you still having doubts, $ man dh).<\/p>\n<p>Now, we need discover the reason why the LDFLAGS is being changed between its generation by the Debian build system and its utilization by the upstream&#8217;s source code. So, we need to check the upstream&#8217;s Makefile.<\/p>\n<p>There is inside Makefile (after a &#8216;quilt push -a&#8217;, to apply all current patches):<\/p>\n<pre>LDFLAGS= $(CFLAGS)\r\n\r\nOBJECTS= recvping.o print.o err.o icmpinfo.o pid.o\r\nTARGET = icmpinfo\r\n\r\n$(TARGET): $(OBJECTS)\r\n $(CC) $(LDFLAGS) -o $@ $(OBJECTS) $(LDLIBS)<\/pre>\n<p>Hummm&#8230; The LDFLAGS content generated by Debian is being dropped by Makefile because it is saying that &#8220;LDFLAGS = CFLAGS content&#8221;. This line is a problem because the upstream Makefile needs to take and use the CFLAGS and LDFLAGS independently. To fix the issue, you can use this patch:<\/p>\n<pre>--- icmpinfo-1.11.orig\/Makefile\r\n+++ icmpinfo-1.11\/Makefile\r\n@@ -20,13 +20,13 @@ VERS = 1.11\r\n \r\n RM = rm -f\r\n \r\n-LDFLAGS= $(CFLAGS)\r\n+#LDFLAGS= $(CFLAGS)\r\n \r\n OBJECTS= recvping.o print.o err.o icmpinfo.o pid.o\r\n TARGET = icmpinfo\r\n \r\n $(TARGET): $(OBJECTS)\r\n- $(CC) $(LDFLAGS) -o $@ $(OBJECTS) $(LDLIBS)\r\n+ $(CC) $(LDFLAGS) $(CFLAGS) -o $@ $(OBJECTS) $(LDLIBS)\r\n \r\n tgz: clean\r\n rm -f CHECKSUMS.asc<\/pre>\n<p>After a &#8216;debuild&#8217;, we have a new lintian:<\/p>\n<pre>I: icmpinfo: hardening-no-bindnow usr\/sbin\/icmpinfo<\/pre>\n<p>There is a simple way to fix this message. We<\/p>\n<p>needed to add the following line to debian\/rules:<\/p>\n<pre>export DEB_BUILD_MAINT_OPTIONS = hardening=+all<\/pre>\n<p>If you still seeing lintians about the hardening, use the following options in blhc (&gt;= 0.07+20170817+gita232d32) to get a deep analysis:<\/p>\n<pre>blhc --all --debian --arch=amd64 ..\/icmpinfo_1.11-9_amd64.build\r\n<\/pre>\n<p>For more details, see the bug #845339 on Debian.<\/p>\n<h2>More examples<\/h2>\n<p>Let me to show other example. I will use the mac-robber 1.02-5 (however, I disabled the Makefile.patch in debian\/patches\/series). After a debuild, the following lintian messages are presented:<\/p>\n<pre>I: mac-robber: hardening-no-fortify-functions usr\/bin\/mac-robber\r\nI: mac-robber: hardening-no-bindnow usr\/bin\/mac-robber<\/pre>\n<p>Using blhc:<\/p>\n<pre>$ blhc ..\/mac-robber_1.02-5_amd64.build \r\nCFLAGS missing (-g -O2 -fstack-protector-strong -Wformat -Werror=format-security): gcc -D_FILE_OFFSET_BITS=64 -o mac-robber mac-robber.c\r\nCPPFLAGS missing (-D_FORTIFY_SOURCE=2): gcc -D_FILE_OFFSET_BITS=64 -o mac-robber mac-robber.c\r\nLDFLAGS missing (-Wl,-z,relro): gcc -D_FILE_OFFSET_BITS=64 -o mac-robber mac-robber.c<\/pre>\n<p>We need to verify what is the problem in Makefile with CFLAGS, CPPFLAGS and LDFLAGS when generating the binary &#8216;mac-robber&#8217; (just recalling, -o mac-robber in GCC command). See:<\/p>\n<pre>linux_notstatic: \r\n $(CC) -D_FILE_OFFSET_BITS=64 -o mac-robber mac-robber.c<\/pre>\n<p>There are no references to CFLAGS, CPPFLAGS and LDFLAGS. To solve the problem, we need patch the Makefile to make this:<\/p>\n<pre>linux_notstatic: \r\n $(CC) $(CFLAGS) $(LDFLAGS) $(CPPFLAGS) -D_FILE_OFFSET_BITS=64 -o mac-robber mac-robber.c<\/pre>\n<p>As last example, is possible that the Makefile is overriding the content sent by DebHelper when building. See this line from a hypothetical Makefile:<\/p>\n<pre><span class=\"pl-smi\">CFLAGS<\/span> = -g -Wall<\/pre>\n<p>As you can see, the Makefile is redefining CFLAGS; consequently, it is discarding any previous content sent by DebHelper. To solve this issue, we can use the following patch:<\/p>\n<pre><span class=\"pl-smi\">-CFLAGS<\/span> = -g -Wall\r\n<span class=\"pl-smi\">+CFLAGS<\/span> += -g -Wall<\/pre>\n<p>So, the content received from DebHelper will be added to &#8216;-g -Wall&#8217;.<\/p>\n<h2>Default parameters<\/h2>\n<p>As curiosity, to see the basic parameters created by DebHelper as hardening, use the command:<\/p>\n<pre>$ dpkg-buildflags<\/pre>\n<p>To see the all parameters, use the command:<\/p>\n<pre>$ DEB_BUILD_MAINT_OPTIONS=hardening=+all dpkg-buildflags<\/pre>\n<h2>More information<\/h2>\n<p>More information about the hardening can be viewed at two places:<\/p>\n<p><a href=\"https:\/\/wiki.debian.org\/Hardening\">https:\/\/wiki.debian.org\/Hardening<\/a><\/p>\n<p><a href=\"https:\/\/wiki.debian.org\/HardeningWalkthrough\">https:\/\/wiki.debian.org\/HardeningWalkthrough<\/a><\/p>\n<p>I hope this help. Enjoy!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>UPDATE: this post was originally published on Sep. 7, 2015. I did a full review on Sep. 5, 2017. This revision is full compliant with Debian 9 and dpkg 1.18.13 or latter. Implementing the hardening When packaging in Debian, is very common to see some lintian messages as &#8216;hardening-no-relro&#8216; and &#8216;hardening-no-fortify-functions&#8216; in softwares written in&hellip;&nbsp;<a href=\"https:\/\/eriberto.pro.br\/blog\/2015\/09\/07\/debian-how-to-use-blhc-to-solve-hardening-issues-when-packaging\/\" rel=\"bookmark\">Continue a ler &raquo;<span class=\"screen-reader-text\">Debian: how to use blhc to solve hardening issues when packaging<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"neve_meta_sidebar":"","neve_meta_container":"","neve_meta_enable_content_width":"","neve_meta_content_width":0,"neve_meta_title_alignment":"","neve_meta_author_avatar":"","neve_post_elements_order":"","neve_meta_disable_header":"","neve_meta_disable_footer":"","neve_meta_disable_title":"","footnotes":""},"categories":[16],"tags":[619,616,615,40,624,620,39,613,612,611,618,617,614,621,41,44,667],"class_list":["post-2222","post","type-post","status-publish","format-standard","hentry","category-debian","tag-blhc","tag-cflags","tag-cppflags","tag-debhelper","tag-debian","tag-dpkg","tag-empacotamento","tag-g","tag-gcc","tag-hardening","tag-hardening-no-fortify-functions","tag-hardening-no-relro","tag-ldflags","tag-linux","tag-packaging","tag-pacote","tag-planet-en"],"_links":{"self":[{"href":"https:\/\/eriberto.pro.br\/blog\/wp-json\/wp\/v2\/posts\/2222","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/eriberto.pro.br\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/eriberto.pro.br\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/eriberto.pro.br\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/eriberto.pro.br\/blog\/wp-json\/wp\/v2\/comments?post=2222"}],"version-history":[{"count":21,"href":"https:\/\/eriberto.pro.br\/blog\/wp-json\/wp\/v2\/posts\/2222\/revisions"}],"predecessor-version":[{"id":2324,"href":"https:\/\/eriberto.pro.br\/blog\/wp-json\/wp\/v2\/posts\/2222\/revisions\/2324"}],"wp:attachment":[{"href":"https:\/\/eriberto.pro.br\/blog\/wp-json\/wp\/v2\/media?parent=2222"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/eriberto.pro.br\/blog\/wp-json\/wp\/v2\/categories?post=2222"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/eriberto.pro.br\/blog\/wp-json\/wp\/v2\/tags?post=2222"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}