<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>coupleprogramming</title>
	<atom:link href="http://coupleprogramming.eu/blog/?feed=rss2" rel="self" type="application/rss+xml" />
	<link>http://coupleprogramming.eu/blog</link>
	<description>...has nothing in common with pair programming ;)</description>
	<lastBuildDate>Tue, 26 Apr 2011 19:10:44 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>rulegenerator for firewalls or what I&#8217;ve been up to</title>
		<link>http://coupleprogramming.eu/blog/?p=219</link>
		<comments>http://coupleprogramming.eu/blog/?p=219#comments</comments>
		<pubDate>Sun, 27 Feb 2011 17:02:15 +0000</pubDate>
		<dc:creator>constanze</dc:creator>
				<category><![CDATA[firewall]]></category>
		<category><![CDATA[life]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://coupleprogramming.eu/blog/?p=219</guid>
		<description><![CDATA[Long time no see :). Some of you may have wondered or not, where I disappeared, I&#8217;m going to tell you anyway. I&#8217;ve been writing my bachelor thesis and now I&#8217;m finally finished :). And I actually developed something useful, &#8230;<p class="read-more"><a href="http://coupleprogramming.eu/blog/?p=219">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<p>Long time no see :). Some of you may have wondered or not, where I disappeared, I&#8217;m going to tell you anyway.</p>
<p>I&#8217;ve been writing my bachelor thesis and now I&#8217;m finally finished :). And I actually developed something useful, or at least, I think it&#8217;s useful.</p>
<p>My thesis&#8217; subject is: <strong>Design and Prototypic Implementation of a Rule–Generator for Packetfilter–Firewalls with Integrated Detection of Malicious Traffic</strong>. </p>
<p>It&#8217;s in German, so most of you can&#8217;t read it, which you probably wouldn&#8217;t want anyway. So I&#8217;m going to tell what&#8217;s in it.</p>
<p>With your standard personal firewall you have a mechanism, which presents you potential rules, based on connections programs want to establish. This may be unnerving on a windows machine with windows popping up every 5 minutes, but the principle is kind of useful.<br />
What I wanted to do was: Port this mechanism to packetfilter-firewalls. And I did it, it&#8217;s not exactly the same, but more or less similar. </p>
<p>I wrote a python program, which can read a traffic dump in <tt>pcap</tt> format and combines the traffic to rules. The rules can be printed out in <tt>pf</tt>- and <tt>netfilter</tt>-syntax. If you wonder: <tt>pf</tt> is the <tt>OpenBSD</tt> packetfilter.</p>
<p>Since that was not enough for a bachelor thesis, I also implemented simple portscan and ddos detection algorithms and a mechanism to filter out unwanted rules before printing out the ruleset.</p>
<p>It&#8217;s only a prototype and it has problems with traffic-dense dumps (it consumes all your memory and never comes to an end :( ). But all in all it works kind of good, at least in my tests :).</p>
<p>So, you may ask, can I test this rulegenerator-thingy? Yes, you can :D.<br />
I put it on github [1]. You have to have dpkt [2] installed, a &#8220;python packet creation / parsing library&#8221;, which is used to read the dumps. Don&#8217;t forget to read the README :).</p>
<p>And, so you can see how it works right now, I included some examples here. I used the output in <tt>pf</tt>-syntax, because it&#8217;s more compact.</p>
<ul>
<li>rulegeneration with normal dump<br />
<blockquote><p>Reading pcap file</p>
<p>Checking for DDoS<br />
Filtering Rules with existing ruleset<br />
Detecting Portscans</p>
<p>Generating TCP-Ruleset<br />
Generating UDP-Ruleset<br />
Generating ICMP-Ruleset</p>
<p>1 rule for protocol tcp from 4 tcp connection(s)<br />
pass none-outer proto tcp from 192.168.1.1 port random to 192.168.1.2 port { 21, 22, 23, 80 }</p>
<p>5 rules for protocol udp from 12 udp connections<br />
pass none-outer proto udp from 192.168.1.2 port 513 to 192.168.1.255 port 513<br />
pass none-outer proto udp from 192.168.1.1 port 53 to 192.168.1.2<br />
pass none-outer proto udp from 192.168.1.2 port 138 to 192.168.1.255 port 138<br />
pass none-outer proto udp from 192.168.1.2 port random to 192.168.1.1 port 53<br />
pass none-outer proto udp from 192.168.1.0/24 port 137 to 192.168.1.255 port 137</p>
<p>No connections for protocol icmp</p></blockquote>
</li>
<li>rulegeneration with dump containing a portscan<br />
<blockquote><p>Reading pcap file</p>
<p>Checking for DDoS<br />
Detecting Portscans<br />
Detecting infected hosts</p>
<p>Generating TCP-Ruleset<br />
Generating UDP-Ruleset<br />
Generating ICMP-Ruleset</p>
<p>No connections for protocol tcp</p>
<p>1 rule for protocol udp from 1 udp connection(s)<br />
pass none-outer proto udp from 192.168.2.105 port random to 224.0.0.251 port 5353</p>
<p>No connections for protocol icmp</p>
<p>1 rule which results from traffic which looks like a portscan<br />
1000 ports were scanned in range: 1:65389<br />
block none-outer proto tcp from 192.168.2.101 port random to 192.168.2.105<br />
1 IP(s) scanned: 1 IP(s)<br />
192.168.2.101 scanned 192.168.2.105</p></blockquote>
<li>rulegeneration with dump containing a ddos-attack<br />
<blockquote><p>Reading pcap file</p>
<p>Checking for DDoS<br />
There might be an tcp (D)DoS Attack. The weighted moving average is 5.41770376331, which is below the given border 400.<br />
Detecting Portscans</p>
<p>Generating TCP-Ruleset<br />
Generating UDP-Ruleset<br />
Generating ICMP-Ruleset</p>
<p>1 rule for protocol tcp from 4000 tcp connection(s)<br />
pass in proto tcp from any port random to 10.0.0.1 port 22</p>
<p>No connections for protocol udp</p>
<p>No connections for protocol icmp</p></blockquote>
<p>The portscan detection mechanism is a simple &#8220;if there are more than x connection/connection-attemps from one host to another host/different ports or other hosts/one port or other hosts/different ports&#8221; test.<br />
The ddos detection mechanism is a little bit more complicated. I took it from a paper on ddos-detection [3].</p>
<p>I would be happy if the rulegenerator is of some use to someone out there. If you look at the code, remember it&#8217;s just a prototype. If you have suggestions or want to make the rulegenerator better, tell me. Maybe, with a little work, it can be more than just a prototype ;).</p>
<p>[1] <a href="https://github.com/constanze/rulegen">https://github.com/constanze/rulegen</a><br />
[2] <a href="http://code.google.com/p/dpkt/">http://code.google.com/p/dpkt/</a><br />
[3] <a href="http://www.springerlink.com/content/k6436394vn52275h/">http://www.springerlink.com/content/k6436394vn52275h/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://coupleprogramming.eu/blog/?feed=rss2&#038;p=219</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>The state of Scala on Gentoo</title>
		<link>http://coupleprogramming.eu/blog/?p=211</link>
		<comments>http://coupleprogramming.eu/blog/?p=211#comments</comments>
		<pubDate>Sat, 22 Jan 2011 14:45:17 +0000</pubDate>
		<dc:creator>ntoythi</dc:creator>
				<category><![CDATA[gentoo]]></category>
		<category><![CDATA[java / scala]]></category>

		<guid isPermaLink="false">http://coupleprogramming.eu/blog/?p=211</guid>
		<description><![CDATA[Scala is an interesting programming language for the Java VM which combines object-oriented with functional programming-style. It&#8217;s feature-set is especially attractive for erlang developers (e.g. immutability, functional-style, higher-order-functions) as well as for c++ / boost developers (e.g. closures, first-order-functions, multiple-inheritance &#8230;<p class="read-more"><a href="http://coupleprogramming.eu/blog/?p=211">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<p><a title="Scala" href="http://www.scala-lang.org">Scala</a> is an interesting programming language for the Java VM which combines object-oriented with functional programming-style. It&#8217;s feature-set is especially attractive for erlang developers (e.g. immutability, functional-style, higher-order-functions) as well as for c++ / boost developers (e.g. closures, first-order-functions, multiple-inheritance via traits). User votes in different java-magazines on future JVM languages clearly see Scala as the language with the highest potential. So it&#8217;s worth a look for sure :-)</p>
<p>The current state of Scala on Gentoo Linux is:</p>
<ul>
<li>There is a dev-lang/scala ebuild for Scala 2.7.7, more than 1 year old (added 30 Nov 2009; current is 2.8.1, ebuild available in <a title="Bug 328291" href="https://bugs.gentoo.org/show_bug.cgi?id=328291">Bug 328291</a>)</li>
<li>There is also a dev-java/scala-bin, which is kinda ancient (added 02 Aug 2004, Version 1.2.0.1). I think this ebuild should be removed or updated to 2.8.1.</li>
</ul>
<p>If we look at the tooling- and library-front it&#8217;s even more frustrating. None of these are currently available as an ebuild:</p>
<ul>
<li>Testing: <a title="ScalaTest" href="http://www.scalatest.org/">ScalaTest</a>, <a title="ScalaCheck" href="http://code.google.com/p/scalacheck/">ScalaCheck</a>, <a title="Specs" href="http://code.google.com/p/specs/">Specs</a></li>
<li>Concurrency / Actor framework: <a title="Akka" href="http://akka.io/">Akka</a></li>
<li>Web-Framework: <a title="Lift" href="http://liftweb.net/">Lift</a></li>
</ul>
<p>I think it&#8217;s kinda sad that Scala doesn&#8217;t play the role it deserves on Gentoo. The version bump request is open since 14 Jul 2010 and in the meanwhile even the summary is outdated (request was once filed for 2.8.0). I&#8217;ll keep posting ebuilds on bugzilla, but I think the community&#8217;s interest on Scala is high enough to justify an &#8220;official&#8221; stable version of a current Scala version in the tree (or does anyone know of such a version in any overlay?)</p>
<p>I know the Gentoo Java Team is understaffed and there are many issues requiring their attention, but I think this would help keeping a lot Java-/Scala developers staying with Gentoo as a development platform. If someone would like to proxy-maintain me I&#8217;d be happy to help.</p>
]]></content:encoded>
			<wfw:commentRss>http://coupleprogramming.eu/blog/?feed=rss2&#038;p=211</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Progress Report #9 for POSIX-Capabilities Project</title>
		<link>http://coupleprogramming.eu/blog/?p=190</link>
		<comments>http://coupleprogramming.eu/blog/?p=190#comments</comments>
		<pubDate>Thu, 12 Aug 2010 18:51:27 +0000</pubDate>
		<dc:creator>constanze</dc:creator>
				<category><![CDATA[gentoo]]></category>
		<category><![CDATA[gsoc2010]]></category>

		<guid isPermaLink="false">http://coupleprogramming.eu/blog/?p=190</guid>
		<description><![CDATA[So, this is the last progress report of this years GSoC. That doesn&#8217;t mean that this project is done, but GSoC has come to an end. To wrap things up, the version, which will hopefully be added to the tree &#8230;<p class="read-more"><a href="http://coupleprogramming.eu/blog/?p=190">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<p>So, this is the last progress report of this years GSoC. That doesn&#8217;t mean that this project is done, but GSoC has come to an end.</p>
<p>To wrap things up, the version, which will hopefully be added to the tree soon is the one which sets the caps directly in the <tt>livefs</tt>. Why? Because I still haven&#8217;t figured out a good fallback-mechanism, if the <tt>livefs</tt> doesn&#8217;t support caps and the caps can&#8217;t be reapplied after the copy.<br />
Don&#8217;t worry, I&#8217;m working on that, but it won&#8217;t be finished by Aug 16th, which is the Pencils Down Date of GSoC. So flameeyes and me decided, that it would be good, to see this version as a final result.</p>
<p>Thanks you all for trying my stuff and commenting. This was the best summer of my life so far, I learned a lot and had a great time. Thanks to my mentor Diego E. “Flameeyes” Pettenò for giving me feedback and encouraging me, when I wasn&#8217;t so sure I&#8217;m on the right path.</p>
<p>As for my involvement with Gentoo, there is already a recruitment bug for me in bugzilla ;).</p>
]]></content:encoded>
			<wfw:commentRss>http://coupleprogramming.eu/blog/?feed=rss2&#038;p=190</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Progress Report #8 for POSIX-Capabilities Project</title>
		<link>http://coupleprogramming.eu/blog/?p=171</link>
		<comments>http://coupleprogramming.eu/blog/?p=171#comments</comments>
		<pubDate>Sat, 31 Jul 2010 12:05:15 +0000</pubDate>
		<dc:creator>constanze</dc:creator>
				<category><![CDATA[gentoo]]></category>
		<category><![CDATA[gsoc2010]]></category>

		<guid isPermaLink="false">http://coupleprogramming.eu/blog/?p=171</guid>
		<description><![CDATA[Again, it has been two weeks since my last report, shame on me. But my exams are all written by now (and passed :D) and I have some exciting new stuff to tell you :). What did I do this &#8230;<p class="read-more"><a href="http://coupleprogramming.eu/blog/?p=171">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<p>Again, it has been two weeks since my last report, shame on me. But my exams are all written by now (and passed :D) and I have some exciting new stuff to tell you :).</p>
<p><strong>What did I do this week (and the last):</strong><br />
The next step in this project was to find out why and where the caps get lost, when they are set in <tt>src_install</tt> (that means on the binary in the sandbox and not on the binary which is already in the livefs).<br />
After a long and sometimes frustrating journey through the <tt>portage</tt> sources and with a lot help from <tt>pdb</tt> I found out why.<br />
Normally (e.g. when the source and the destination are on the same filesystem) <tt>os.rename</tt> is used to move the binary from sandbox to livefs. This happens in <tt>portage/pym/portage/util/movefile.py</tt> and is <strong>not</strong> were the caps are lost.<br />
They are lost earlier, when the binaries are stripped. So I added a preserving-mechanism to <tt>portage/bin/ebuild-helpers/prepstrip</tt>, where the stripping happens. And it worked :). I could set the caps in <tt>src_install</tt> and they did not get lost.<br />
But there was another place where the caps could get lost. If <tt>os.rename</tt> in <tt>movefile.py</tt> fails or <tt>src</tt> and <tt>dest</tt> are not on the same filesystem, <tt>shutil.copy</tt> is used before <tt>os.rename</tt> and the binarys mode/ownership are restored after. Of course the extended attributes, like caps, have to be restored, too. Using <tt>pyxattr</tt> I extracted the caps before the copy and applied them back after the binary is copied and its mode/ownership are restored. The downside is, that <tt>portage</tt> now needs <tt>pyxattr</tt>.<br />
With that now working, I changed all ebuilds I have patched so far, so the caps are set in <tt>src_install</tt>.<br />
I added the changed ebuilds and the changed <tt>portage</tt>-files to a new branch in my repository. I used the current stable version (2.1.8.3) of <tt>portage</tt>, just so you know :).</p>
<p>That&#8217;s it for today, tell me what you think in the comments :).</p>
]]></content:encoded>
			<wfw:commentRss>http://coupleprogramming.eu/blog/?feed=rss2&#038;p=171</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Progress Report #7 for POSIX-Capabilities Project</title>
		<link>http://coupleprogramming.eu/blog/?p=148</link>
		<comments>http://coupleprogramming.eu/blog/?p=148#comments</comments>
		<pubDate>Fri, 16 Jul 2010 20:00:12 +0000</pubDate>
		<dc:creator>constanze</dc:creator>
				<category><![CDATA[gentoo]]></category>
		<category><![CDATA[gsoc2010]]></category>

		<guid isPermaLink="false">http://coupleprogramming.eu/blog/?p=148</guid>
		<description><![CDATA[This report is a little late, but I&#8217;m in my exams period and had to do a lot of studying. I had three exams already and have two to go next week, so I will be back full time, after &#8230;<p class="read-more"><a href="http://coupleprogramming.eu/blog/?p=148">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<p>This report is a little late, but I&#8217;m in my exams period and had to do a lot of studying. I had three exams already and have two to go next week, so I will be back full time, after that. This report is about this and the last week, enjoy :D.</p>
<p><strong>What did I do this week (and the last):</strong><br />
Since some &#8220;easy&#8221; ebuilds have been patched, flameeyes suggested, I could take a look at <tt>tcpdump</tt>. While <tt>tcpdump</tt> has no <tt>suid-bit</tt> set, it can only be run as <tt>root</tt>, which is inconvenient. It would be nice if &#8220;special&#8221; users could run it to sniff (without using <tt>sudo</tt>) and normal users can use it to view dumps. Then it could also be moved to <tt>/usr/bin/</tt>. So how could you realize that with caps?<br />
There is a <tt>pam</tt> module called <tt>pam_cap</tt>, which allows you to put caps in the users inherited set.<br />
Then there are two options:</p>
<ol>
<li>Is your application capability-aware?<br />
You can put the caps in the permitted set and/or inherited set.</li>
<li>Is your application not capability-aware?<br />
You will have to put the caps in the inherited and effective set.</li>
</ol>
<p> Only capability-aware application can populate their effective set themselves, the kernel has to do it for the others. And the kernel knows, if the effective set is not empty, the application is not capability-aware [0].</p>
<p>So, how do you use <tt>pam_cap</tt>?</p>
<ol>
<li>You need to have <tt>libcap</tt> installed. (Obviously :))</li>
<li>You need to include <tt>pam_cap.so</tt> in your <tt>/etc/pam.d/system-auth</tt>.<br />
Something like this:<br />
<code>auth required pam_env.so<br />
auth required pam_cap.so<br />
...</code></li>
<li>Add caps to users in <tt>/etc/security/capability.conf</tt>.<br />
Like this:<br />
<code>#netadmin inherits cap_net_admin,cap_net_raw<br />
cap_net_admin,cap_net_raw netadmin<br />
#every other user inherits nothing<br />
none *</code><br />
The last line is very important, otherwise all other users inherit all caps, which would be not what we wanted.</li>
<li>Logout/Login</li>
<li>It should work now, but maybe you need to reboot.</li>
</ol>
<p>Now, that you can grant users certain caps, you can put the caps on your binaries in <tt>ei</tt> or <tt>p/pi</tt> instead of <tt>ep</tt> and only the users, who have the right caps in their set can execute these binaries correctly. To try it out you can emerge <tt>tcpdump</tt> from my overlay, which has <tt>cap_net_raw</tt> in its effective and inherited set. Only a user who has at least <tt>cap_net_raw</tt> in his/her set can sniff with <tt>tcpdump</tt>.</p>
<p>You want to see this as an example, before you try it out on your system? Here you go:<br />
This is my <tt>/etc/pam.d/system-auth</tt>:</p>
<blockquote><p>auth        required    pam_env.so<br />
auth        required    pam_cap.so<br />
auth        required    pam_unix.so try_first_pass likeauth nullok</p>
<p>account     required    pam_unix.so</p>
<p>password    required    pam_cracklib.so difok=2 minlen=8 dcredit=2 ocredit=2 retry=3<br />
password    required    pam_unix.so try_first_pass use_authtok nullok sha512 shadow<br />
session     required    pam_limits.so<br />
session     required    pam_env.so<br />
session     required    pam_unix.so<br />
session     optional    pam_permit.so</p></blockquote>
<p>This is my <tt>/etc/security/capabiliy.conf</tt></p>
<blockquote><p>#<br />
# /etc/security/capability.conf<br />
#<br />
# this is a sample capability file (to be used in conjunction with<br />
# the pam_cap.so module)<br />
#<br />
# In order to use this module, it must have been linked with libcap<br />
# and thus you&#8217;ll know about Linux&#8217;s capability support.<br />
# [If you don't know about libcap, the sources for it are here:<br />
#<br />
#   http://linux.kernel.org/pub/linux/libs/security/linux-privs/<br />
#<br />
# .]<br />
#<br />
# Here are some sample lines (remove the preceding &#8216;#&#8217; if you want to<br />
# use them</p>
<p>## user &#8216;morgan&#8217; gets the CAP_SETFCAP inheritable capability<br />
##cap_setfcap       morgan</p>
<p>cap_net_admin,cap_net_raw netadmin</p>
<p>## &#8216;everyone else&#8217; gets no inheritable capabilities<br />
none  *</p>
<p>## if there is no &#8216;*&#8217; entry, all users not explicitly mentioned will<br />
## get all available capabilities. This is a permissive default, and<br />
## probably not what you want&#8230;</p></blockquote>
<p><tt>tcpdump</tt> is now in <tt>/usr/bin/</tt>, let&#8217;s look at its caps.<code><br />
superkfr@totoro ~ $ sudo getcap /usr/bin/tcpdump<br />
/usr/bin/tcpdump = cap_net_raw+ei</code><br />
It has <tt>cap_net_raw</tt> in the effective and inherited set, good so far.</p>
<p>Let&#8217;s try to execute <tt>tcpdump</tt> with the user <tt>netadmin</tt>, who has the required caps.<br />
<code>superkfr@totoro ~ $ su - netadmin<br />
Password:<br />
netadmin@totoro ~ $ tcpdump -i wlan0<br />
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode<br />
listening on wlan0, link-type EN10MB (Ethernet), capture size 65535 bytes<br />
...<br />
netadmin@totoro ~ $ exit<br />
logout</code><br />
Works just fine.</p>
<p>Let&#8217;s try with user <tt>test</tt>, who has no caps inherited.<br />
<code>superkfr@totoro ~ $ su - test<br />
Password:<br />
test@totoro ~ $ tcpdump -i wlan0<br />
tcpdump: wlan0: You don't have permission to capture on that device<br />
(socket: Operation not permitted)<br />
test@totoro ~ $ tcpdump -r /tmp/dump<br />
reading from file /tmp/dump, link-type EN10MB (Ethernet)<br />
...<br />
test@totoro ~ $ exit<br />
logout<br />
</code><br />
Like we expected, user <tt>test</tt> does not have the right permissions to sniff, but can view dumps.</p>
<p>If you have trouble getting <tt>pam_cap</tt> to work (believe me, I&#8217;ve been there), try this:<br />
Look at the output of <tt>/sbin/getpcaps $$</tt> in the shell of the user, whom you want to have the caps.</p>
<ul>
<li>If the output looks something like this:<br />
<code>test@totoro ~ $ /sbin/getpcaps $$<br />
Capabilities for `8219': =</code><br />
The users cap set is empty and something didn&#8217;t work. Try rebooting, if you haven&#8217;t done this already.</li>
<li>If the output looks something like this:<br />
<code>netadmin@totoro ~ $ /sbin/getpcaps $$<br />
Capabilities for `13976': = cap_net_admin,cap_net_raw+i</code><br />
It worked.</li>
</ul>
<p>So that&#8217;s it for this week. Hope to hear from you in the comments :D.</p>
<p><a href="http://www.linuxjournal.com/magazine/making-root-unprivileged">[0] http://www.linuxjournal.com/magazine/making-root-unprivileged</a></p>
]]></content:encoded>
			<wfw:commentRss>http://coupleprogramming.eu/blog/?feed=rss2&#038;p=148</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Detecting needed capabilities with capable_probe</title>
		<link>http://coupleprogramming.eu/blog/?p=128</link>
		<comments>http://coupleprogramming.eu/blog/?p=128#comments</comments>
		<pubDate>Mon, 05 Jul 2010 11:24:29 +0000</pubDate>
		<dc:creator>constanze</dc:creator>
				<category><![CDATA[gentoo]]></category>

		<guid isPermaLink="false">http://coupleprogramming.eu/blog/?p=128</guid>
		<description><![CDATA[If you want to find out which capabilities a binary needs, you have several options: You use strace and determine them from the output You use capable_probe The first method is quite simple, but you have to deduct the caps &#8230;<p class="read-more"><a href="http://coupleprogramming.eu/blog/?p=128">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<p>If you want to find out which capabilities a binary needs, you have several options:</p>
<ol>
<li>You use <tt>strace</tt> and determine them from the output</li>
<li>You use <tt>capable_probe</tt></li>
</ol>
<p>The first method is quite simple, but you have to deduct the caps from the error messages, which can be a bit difficult.<br />
The second method is more convenient and simple, so I&#8217;m going to describe this method further.<br />
<tt>capable_probe</tt> is a loadable kernel module developed by Serge Hallyn [0]. It uses the <tt>kprobe</tt> kernel mechanism [1] to insert a <tt>Jprobe</tt>.  And what does it do exactly?</p>
<blockquote><p>When this kernel module is inserted, any calls to <tt>cap_capable()</tt> are replaced by a call to the <tt>cr_capable()</tt> function. This function prints the  name of  the program that requires capabilities and the capability being checked. It then continues executing the actual <tt>cap_capable()</tt> call through the call to <tt>jprobe_return()</tt>. [0]</p></blockquote>
<p>The signature of <tt>cr_capable</tt> has to be the same as the signature of <tt>cap_capable</tt>. The signature changed from<br />
<code>int cap_capable (struct task_struct *tsk, int cap)</code><br />
to<br />
<code>int cr_capable (struct task_struct *tsk, const struct cred *cred, int cap, int audit)</code>.<br />
So to get <tt>capable_probe</tt> to work you need to change the signature or use the one from my repository [2].</p>
<p>There is a good README by Chris Friedhoff [3], which describes how to install and use it.</p>
<p><strong>Important</strong>: Don&#8217;t forget to unload capable_probe after you determined the caps, as it will spam your <tt>/var/log/messages</tt> extremely.</p>
<p>To see <tt>capable_probe</tt> in action here is how to determine the caps for <tt>passwd</tt>:</p>
<ul>
<li>Remove suid<br />
<code>chown -s /bin/passwd</code></li>
<li><code>tail -f /var/log/messages | grep passwd</code></li>
<li><code>modprobe capable_probe</code></li>
<li>Try to change your password</li>
<li>This is what you get from <tt>passwd</tt>:<br />
<code>superkfr@totoro ~ $ passwd<br />
Changing password for superkfr.<br />
(current) UNIX password:<br />
Password:<br />
Retype new password:<br />
passwd: Authentication token lock busy<br />
passwd: password unchanged<br />
</code> Not very informative.</li>
<li><code>modprobe -r capable_probe</code></li>
<li>Let&#8217;s see what we got from <tt>capable_probe</tt>:<br />
<code>Jul  5 15:20:54 totoro kernel: cr_capable: asking for capability 1 for passwd<br />
Jul  5 15:20:54 totoro kernel: cr_capable: asking for capability 2 for passwd<br />
Jul  5 15:20:54 totoro kernel: cr_capable: asking for capability 1 for passwd<br />
Jul  5 15:20:54 totoro kernel: cr_capable: asking for capability 2 for passwd<br />
Jul  5 15:20:54 totoro kernel: cr_capable: asking for capability 24 for passwd<br />
</code></li>
<li>What does this all mean?<br />
The numbers stand for different caps, you can look them up in <tt>/usr/include/linux/capability.h</tt>.</p>
<blockquote><p><tt>/* Override all DAC access, including ACL execute access if<br />
[_POSIX_ACL] is defined. Excluding DAC access covered by<br />
CAP_LINUX_IMMUTABLE. */</tt></p>
<p><tt>#define CAP_DAC_OVERRIDE     1</tt></p>
<p><tt>/* Overrides all DAC restrictions regarding read and search on files<br />
and directories, including ACL restrictions if [_POSIX_ACL] is<br />
defined. Excluding DAC access covered by CAP_LINUX_IMMUTABLE. */</tt></p>
<p><tt>#define CAP_DAC_READ_SEARCH  2<br />
...</tt></p>
<p><tt>/* Override resource limits. Set resource limits. */<br />
/* Override quota limits. */<br />
/* Override reserved space on ext2 filesystem */<br />
/* Modify data journaling mode on ext3 filesystem (uses journaling<br />
resources) */<br />
/* NOTE: ext2 honors fsuid when checking for resource overrides, so<br />
you can override using fsuid too */<br />
/* Override size restrictions on IPC message queues */<br />
/* Allow more than 64hz interrupts from the real-time clock */<br />
/* Override max number of consoles on console allocation */<br />
/* Override max number of keymaps */</tt></p>
<p><tt>#define CAP_SYS_RESOURCE     24</tt></p></blockquote>
</li>
<li>Now, which caps seem plausible? <tt>passwd</tt> needs to read and write /etc/shadow so it needs <tt>CAP_DAC_OVERRIDE</tt>, <tt>CAP_DAC_READ_SEARCH</tt> is a subset, is does not have to be set additionally. There is no indication, that <tt>passwd</tt> could need CAP_SYS_RESOURCE. So we try it out with CAP_DAC_OVERRIDE.</li>
<li><code>sudo setcap cap_dac_override=ep /bin/passwd<br />
sudo modprobe capable_probe<br />
passwd</code><br />
And it fails again.</li>
<li><code>Jul  5 16:02:50 totoro kernel: cr_capable: asking for capability 1 for passwd<br />
Jul  5 16:02:50 totoro kernel: cr_capable: asking for capability 2 for passwd<br />
Jul  5 16:02:50 totoro kernel: cr_capable: asking for capability 1 for passwd<br />
Jul  5 16:02:50 totoro kernel: cr_capable: asking for capability 24 for passwd<br />
Jul  5 16:03:01 totoro kernel: cr_capable: asking for capability 1 for passwd<br />
Jul  5 16:03:01 totoro kernel: cr_capable: asking for capability 1 for passwd<br />
Jul  5 16:03:01 totoro kernel: cr_capable: asking for capability 1 for passwd<br />
Jul  5 16:03:01 totoro kernel: cr_capable: asking for capability 0 for passwd<br />
Jul  5 16:03:01 totoro kernel: cr_capable: asking for capability 1 for passwd<br />
</code>It also asks for <tt>CAP_CHOWN(0)</tt> now, that seems also plausible, so grant it and repeat.</li>
<li>It fails again, what is still missing?<br />
<code>Jul  5 16:03:56 totoro kernel: cr_capable: asking for capability 1 for passwd<br />
Jul  5 16:03:56 totoro kernel: cr_capable: asking for capability 2 for passwd<br />
Jul  5 16:03:56 totoro kernel: cr_capable: asking for capability 1 for passwd<br />
Jul  5 16:03:56 totoro kernel: cr_capable: asking for capability 24 for passwd<br />
Jul  5 16:04:06 totoro kernel: cr_capable: asking for capability 1 for passwd<br />
Jul  5 16:04:06 totoro kernel: cr_capable: asking for capability 1 for passwd<br />
Jul  5 16:04:06 totoro kernel: cr_capable: asking for capability 1 for passwd<br />
Jul  5 16:04:06 totoro kernel: cr_capable: asking for capability 0 for passwd<br />
Jul  5 16:04:06 totoro kernel: cr_capable: asking for capability 0 for passwd<br />
Jul  5 16:04:06 totoro kernel: cr_capable: asking for capability 3 for passwd<br />
Jul  5 16:04:06 totoro kernel: cr_capable: asking for capability 1 for passwd<br />
</code>It also wants <tt>CAP_FOWNER(3)</tt></li>
<li>Add that to the set and it works.</li>
<li>Don&#8217;t forget:<br />
<code>modprobe -r capable_probe</code></li>
</ul>
<p>This example is a little more complicated than the standard <tt>ping</tt>-example, but I wanted to do a different one. Hope that helps :).</p>
<p><tt><a href="http://www.ibm.com/developerworks/library/l-posixcap.html">[0] http://www.ibm.com/developerworks/library/l-posixcap.html</a><br />
[1] You need <tt>CONFIG_KPROBES=y</tt> in your kernel<br />
<a href="http://github.com/constanze/GSoC2010_Gentoo_Capabilities/tree/master/capable_probe/">[2] http://github.com/constanze/GSoC2010_Gentoo_Capabilities/tree/master/capable_probe/</a><br />
<a href="http://www.friedhoff.org/posixfilecaps.html">[3] http://www.friedhoff.org/posixfilecaps.html</a></tt></p>
]]></content:encoded>
			<wfw:commentRss>http://coupleprogramming.eu/blog/?feed=rss2&#038;p=128</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Progress Report #6 for POSIX-Capabilities Project</title>
		<link>http://coupleprogramming.eu/blog/?p=103</link>
		<comments>http://coupleprogramming.eu/blog/?p=103#comments</comments>
		<pubDate>Sun, 04 Jul 2010 18:27:22 +0000</pubDate>
		<dc:creator>constanze</dc:creator>
				<category><![CDATA[gentoo]]></category>
		<category><![CDATA[gsoc2010]]></category>

		<guid isPermaLink="false">http://coupleprogramming.eu/blog/?p=103</guid>
		<description><![CDATA[In spite of my exams-period beginning next week, I managed to do something useful for GSoC :). What did I do this week: In order to do some better testing I wanted to patch some ebuilds, as I said last &#8230;<p class="read-more"><a href="http://coupleprogramming.eu/blog/?p=103">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<p>In spite of my exams-period beginning next week, I managed to do something useful for GSoC :).</p>
<p><strong>What did I do this week:</strong><br />
In order to do some better testing I wanted to patch some ebuilds, as I said last week. The question there is, how do you find out which caps the file needs to function? I consulted Friedhoffs page [0] again and downloaded <tt>capable_probe</tt>. This neat loadable kernel module detects when a program asks for caps and prints it in <tt>/var/log/messages</tt>. All you need is <tt>CONFIG_KPROBES=y</tt> in your kernel.<br />
I tried it out and unfortunately it didn&#8217;t work. I remembered vaguely, that the caps-format had been changed recently, but I was busy with learning so I didn&#8217;t bother to look for the problem (but I came back later :)).<br />
Friedhoff has some example caps-sets on his page and there are also some on [1], so I used these and patched shadow, pam and util-linux.<br />
The patched binaries are:</p>
<ul>
<li><tt>passwd - CAP_CHOWN, CAP_DAC_OVERRIDE, CAP_FOWNER</tt></li>
<li><tt>chsh - CAP_CHOWN, CAP_DAC_READ_SEARCH, CAP_FSETID, CAP_SETUID</tt></li>
<li><tt>chfn - CAP_CHOWN, CAP_DAC_READ_SEARCH, CAP_FSETID, CAP_SETUID</tt></li>
<li><tt>chage - CAP_DAC_READ_SEARCH</tt></li>
<li><tt>expiry - CAP_DAC_OVERRIDE, CAP_SETGID</tt></li>
<li><tt>gpasswd - CAP_CHOWN, CAP_DAC_OVERRIDE, CAP_SETUID</tt></li>
<li><tt>newgrp - CAP_DAC_OVERRIDE, CAP_SETGID</tt></li>
<li><tt>su - CAP_SETGID, CAP_SETUID</tt> (this only seems to work, when unix_chkpwd has its cap)</li>
<li><tt>mount - CAP_DAC_OVERRIDE, CAP_SYS_ADMIN</tt></li>
<li><tt>umount - CAP_DAC_OVERRIDE, CAP_SYS_ADMIN, (CAP_CHOWN)</tt></li>
<li><tt>unix_chkpwd - CAP_DAC_OVERRIDE</tt></li>
</ul>
<p>After testing them, all worked except <tt>umount</tt>. So to investigate what was missing I turned back to <tt>capable_probe</tt>. After a bit of digging through kernel docs and sources, the problem was quite clear: The signature of <tt>cap_capable</tt> had changed (the method which was used to place the Jprobe-hook on). After adapting the signature it worked!</p>
<p>Using <tt>capable_probe</tt> I detected that <tt>umount</tt> was asking for <tt>CAP_CHOWN</tt>, which was not in its set. Because <tt>umount</tt> failed when trying to move <tt>mtab.tmp</tt> back to <tt>mtab</tt>, it seemed reasonable, that <tt>CAP_CHOWN</tt> could help. And it did :).</p>
<p>All in all I&#8217;m very happy today and will write a separate post on <tt>capable_probe</tt>. I added the patched version to my git-repo [2].</p>
<p>I would be even happier :D if someone would try my patched ebuilds and tell me if all is okay or if there is something missing.</p>
<p><a href="http://www.friedhoff.org/posixfilecaps.html">[0] http://www.friedhoff.org/posixfilecaps.html</a><br />
<a href="http://wiki.archlinux.org/index.php/Using_File_Capabilities_Instead_Of_Setuid">[1] http://wiki.archlinux.org/index.php/Using_File_Capabilities_Instead_Of_Setuid</a><br />
<a href="http://github.com/constanze/GSoC2010_Gentoo_Capabilities/tree/master/capable_probe/"> [2] http://github.com/constanze/GSoC2010_Gentoo_Capabilities/tree/master/capable_probe/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://coupleprogramming.eu/blog/?feed=rss2&#038;p=103</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Progress Report #5 for POSIX-Capabilities Project</title>
		<link>http://coupleprogramming.eu/blog/?p=92</link>
		<comments>http://coupleprogramming.eu/blog/?p=92#comments</comments>
		<pubDate>Sun, 27 Jun 2010 17:17:32 +0000</pubDate>
		<dc:creator>constanze</dc:creator>
				<category><![CDATA[gentoo]]></category>
		<category><![CDATA[gsoc2010]]></category>

		<guid isPermaLink="false">http://coupleprogramming.eu/blog/?p=92</guid>
		<description><![CDATA[This week was kind of slow GSoC-wise due to my sister giving birth to my beautiful niece :D. Yes I&#8217;m an aunt now. Moving on to the interesting part &#8211; What did I do this week: I looked into the &#8230;<p class="read-more"><a href="http://coupleprogramming.eu/blog/?p=92">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<p>This week was kind of slow GSoC-wise due to my sister giving birth to my beautiful niece :D. Yes I&#8217;m an aunt now.<br />
Moving on to the interesting part &#8211; <strong>What did I do this week</strong>:<br />
I looked into the eclass/tests folder and tried to make a similar &#8220;test-suite&#8221; for the fcaps eclass.<br />
It contains 4 tests:</p>
<ol>
<li>Unknown capability</li>
<li>Known capability</li>
<li>Unknown user/group</li>
<li>Unknown file-mode</li>
</ol>
<p>I also compiled a test-kernel without XATTR, to check that possibility and tested if libcap is correctly emerged, if it is not installed, yet. All worked fine.</p>
<p>My second mile-stone, which is due next week looks like this:</p>
<blockquote><p>June 21th to July 4th<br />
In this period I will work on the USE-Flag  support. A lot of testing/integration-testing will be needed here, to  make sure the setting of the Capabilities really works. I also want to  find out where Capabilities cannot be used (Does it work on all file-systems? Can every setuid be replaced? Which Kernel-Versions can be  used?). The user needs to be informed if Capabilities cannot be used on  his/her system.</p></blockquote>
<p>I added a USE-Flag and I did some testing. I also already found out which kernel-settings need to be set and which file-systems support capabilities. The user gets an ewarn if the capabilities could not be set. What I need to find out, but what is not trivial, is if all setuid can be replaced. I think this is something which I have to investigate in, when I begin patching some other ebuild than net-misc/iputils.</p>
<p><strong>In the next week</strong> I want to patch some other ebuilds, so I can do better tests with fcaps.</p>
<p>That&#8217;s it for today, have a nice week :).</p>
]]></content:encoded>
			<wfw:commentRss>http://coupleprogramming.eu/blog/?feed=rss2&#038;p=92</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Progress Report #4 for POSIX-Capabilities Project</title>
		<link>http://coupleprogramming.eu/blog/?p=78</link>
		<comments>http://coupleprogramming.eu/blog/?p=78#comments</comments>
		<pubDate>Sun, 20 Jun 2010 18:26:58 +0000</pubDate>
		<dc:creator>constanze</dc:creator>
				<category><![CDATA[gentoo]]></category>
		<category><![CDATA[gsoc2010]]></category>

		<guid isPermaLink="false">http://coupleprogramming.eu/blog/?p=78</guid>
		<description><![CDATA[Maybe it&#8217;s called summer of code, but summer definitely did not arrive in Munich, yet. We had a lot of rain and temperatures about 13°C all week. But there may be a good side to it, I had no desire &#8230;<p class="read-more"><a href="http://coupleprogramming.eu/blog/?p=78">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<p>Maybe it&#8217;s called <strong>summer</strong> of code, but summer definitely did not arrive in Munich, yet. We had a lot of rain and temperatures about 13°C all week. But there may be a good side to it, I had no desire to go out &#8211; more time to study and code :D.</p>
<p><strong>What did I do this week:<br />
</strong>After discussing the further progress of this project with my mentor (flameeyes), we decided, that it would be better to put the fcaps functionality in an eclass, for now. We decided this, because a new eclass does not require an EAPI bump and it would be easier to get fcaps out there ;).<br />
So that&#8217;s what I did:</p>
<ul>
<li>moving the fcaps stuff to an eclass</li>
<li>some minor changes considering failure-handling</li>
<li>adding a USE-flag (filecaps)</li>
<li>applied an overlay-structure, for easier testing and because it makes more sense to use one</li>
</ul>
<p><strong>In the next week</strong> I will finally do more testing of fcaps.</p>
<p>That’s it for today.<br />
Feel free to write me a comment or mail :).</p>
]]></content:encoded>
			<wfw:commentRss>http://coupleprogramming.eu/blog/?feed=rss2&#038;p=78</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Progress Report #3 for POSIX-Capabilities Project</title>
		<link>http://coupleprogramming.eu/blog/?p=60</link>
		<comments>http://coupleprogramming.eu/blog/?p=60#comments</comments>
		<pubDate>Sun, 13 Jun 2010 19:53:58 +0000</pubDate>
		<dc:creator>constanze</dc:creator>
				<category><![CDATA[gentoo]]></category>
		<category><![CDATA[gsoc2010]]></category>

		<guid isPermaLink="false">http://coupleprogramming.eu/blog/?p=60</guid>
		<description><![CDATA[The next progress report is due and forces me to review what I&#8217;ve done. First I have to admit I have put a lot less time into GSoC than I planned to do. This has to stop. I will do &#8230;<p class="read-more"><a href="http://coupleprogramming.eu/blog/?p=60">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<p>The next progress report is due and forces me to review what I&#8217;ve done.<br />
First I have to admit I have put a lot less time into GSoC than I planned to do. This has to stop. I will do a different schedule for the coming week, let&#8217;s see how that turns out.</p>
<p><strong>What did I do this week:</strong><br />
I had a conversation with ferringb about the placement of the fcaps call. As you may know, I was very uncertain about putting it into pkg_postinst and thus messing with the users livesystem. It is evil, but as flameeyes, my mentor put it, not exceedingly evil :D.<br />
So what did we discuss: ferringb made the suggestion to write the info from fcaps to a temp file in src_install, which would then be evaluated in post_pkg_postinst, as a temporary solution until portage supports preserving xattrs/caps. It would still be a hack, but fcaps could be called from src_install.<br />
I looked into this approach and on the way, tried to learn more about how portage handles an emerge. I didn&#8217;t got that far, but I hope I understand things a little better now (in any case I know now, that I have a really long road ahead :), thus the definite need to spend much more time on this). I&#8217;m very eager to get to know portage better, however I find it a little difficult to get familiar with it. (Any tips?)<br />
On the way of my investigation I also found out that my version of coreutils has a bug [1], so I switched to the unstable one. What I gained is, that now I can copy files and preserve their caps (with &#8211;preserve=xattr).<br />
As you can see in my git-repo I didn&#8217;t implement ferringbs suggestion. I discussed this approach with flameeyes.</p>
<p>This is what we concluded:</p>
<ol>
<li>This approach would still require logic in postinst to make sure the caps didn&#8217;t get lost and the destination file-system supports caps.</li>
<li>Handling the caps totally on portage side requires a lot of change and there are a lot of dependencies which might lead to problems.<br />
To clarify: tools like tar might or might not support preserving the caps, the same goes for coreutils and if I understood correctly python (without add-ons) does not support xattrs altogether. (Please correct me, if you know otherwise)</li>
<li>To increase the chance of a new feature to get accepted and used, it is imported to do small steps, which require little change, so it gets out there and doesn&#8217;t stay in the development-phase for years.</li>
</ol>
<p>Personally I think it might be good to keep the hack where you can see it (i.e. in the ebuild) as long as it&#8217;s a hack. I get that it would be convenient, if the ebuild could have the fcaps call in the correct place right now, so there is no change required later. But this might also give the false impression, that it&#8217;s a clean solution already when it&#8217;s still just a hack.</p>
<p>I also added something to fcaps. It now sets owner and file-mode on every file, not just as fallback, but the file-mode with -s of course ;).<br />
This has been added because some binaries like wireshark require other than default owners and/or file-modes.</p>
<p><strong>In the next week</strong> I will do more testing of fcaps, which is now in its alpha-version-state (if I forgot to test something, please tell me).<br />
I will also look into how FEATURES are used and implemented as the caps-support might better be triggered by a FEATURE than a use-flag. What I also want to look into is the EAPI-Process, as I&#8217;m not sure I got that all correctly. (If you have some links/documentation for me please mail or comment :))</p>
<p>That&#8217;s it for today.<br />
Feel free to tell me what you think about fcaps and the process so far and  to make suggestions for a better approach.</p>
<p><a href="http://lists.gnu.org/archive/html/bug-coreutils/2010-03/msg00136.html">[1] http://lists.gnu.org/archive/html/bug-coreutils/2010-03/msg00136.html</a></p>
]]></content:encoded>
			<wfw:commentRss>http://coupleprogramming.eu/blog/?feed=rss2&#038;p=60</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

