<?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>~clay &#187; setuid</title>
	<atom:link href="http://daemons.net/~clay/tag/setuid/feed/" rel="self" type="application/rss+xml" />
	<link>http://daemons.net/~clay</link>
	<description>merely my musings</description>
	<lastBuildDate>Mon, 10 May 2010 17:48:58 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>setuid() ate my CSS</title>
		<link>http://daemons.net/~clay/2009/05/02/setuid-ate-my-css/</link>
		<comments>http://daemons.net/~clay/2009/05/02/setuid-ate-my-css/#comments</comments>
		<pubDate>Sat, 02 May 2009 10:15:35 +0000</pubDate>
		<dc:creator>clay</dc:creator>
				<category><![CDATA[Engineering]]></category>
		<category><![CDATA[Systems Management]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[setuid]]></category>

		<guid isPermaLink="false">http://daemons.net/~clay/?p=244</guid>
		<description><![CDATA[We ran into an interesting problem while testing a new version of our code deployment tool tonight. By all appearances, the tool was happily deploying code and launching our Java applications, but one of our QA engineers noticed missing CSS on some pages in our test environment. Could that possibly be related to the code deployment tool, which essentially just untars an archive and forks off a little ruby script to start the application?]]></description>
			<content:encoded><![CDATA[<p>We ran into an interesting problem while testing a new version of our code deployment tool tonight. By all appearances, the tool was happily deploying code and launching our Java applications, but one of our QA engineers noticed missing CSS on some pages in our test environment. Could that possibly be related to the code deployment tool, which essentially just untars an archive and forks off a little ruby script to start the application?</p>
<p>Tracing the application&#8217;s system calls with truss revealed that the process was getting EPERM errors while trying to read the CSS files, which live on NFS. One of our more clever engineers decided to start up the application manually, not via the code deployment tool, and found that the CSS loaded just fine when the Java process was invoked directly from the shell. He compared user and group ids, as reported by ps, of JVMs started by our tool and those started manually and found no differences. Hmm.</p>
<p>When looking at the processes&#8217; <code>/proc/&lt;pid&gt;/cred</code> files, however, some differences were apparent. The <code>cred</code> file contains binary data and is best viewed with <code>od</code>:</p>
<p><code><br />
$ od -X /proc/$$/cred<br />
0000000 00002716 00002716 00002716 0000000a<br />
0000020 0000000a 0000000a 00000002 0000000a<br />
0000040 0000000e<br />
0000044<br />
</code></p>
<p>The file consists of a sequence of 32-bit id values in the following order:</p>
<p>* uid<br />
* euid<br />
* suid<br />
* gid<br />
* egid<br />
* sgid<br />
* supplemental group ids &#8230;</p>
<p>You can see how that maps to decimal ids by comparing with <code>id</code> output:</p>
<p><code><br />
$ id -a<br />
uid=10006(clay) gid=10(staff) groups=10(staff),14(sysadmin)<br />
</code></p>
<p>[Solaris geek aside: remember when you wanted to be a member of the sysadmin group so you could run the handy-dandy admintool?]</p>
<p>So what we noticed was that while the manually started JVM and the JVM launched via our code deployment tool had identical uid/euid/sgid and gid/egid/sgid values, they had different supplemental group id lists. Notably, the JVM running under the code deployment tool still had a gid of 0 in its supplemental group list. Letting our Java application servers traipse around the filesystem with elevated privileges is perhaps not the best &#8220;feature&#8221; we&#8217;ve ever implemented.</p>
<p>Trust but verify might be a good foreign policy, but our NFS server wasn&#8217;t having any of it. It thoroughly distrusted the Java app servers claiming to have elevated privileges, and rewarded them with EPERMs for their trouble. Root squash is, after all, a pretty common NFS security measure.</p>
<p>As it turns out, I had implemented a new feature in the code deployment agent to make it switch user id on startup. Previously we handled the user switch by launching the tool under <code>su</code>, but that approach prevented the tool from writing its pid file to the root-owned /var/run directory. The solution, I thought, was just to call <code>setgid()</code> followed by <code>setuid()</code>. We tested that code by verifying the user and group ids with <code>ps</code>, and it seemed to work just great.</p>
<p>Quick: what&#8217;s wrong with this?</p>
<pre class="brush: ruby;">
    def HostUtils.switch_user user
      pwent = Etc::getpwnam(user)
      Process::GID::change_privilege(pwent.gid)
      Process::UID::change_privilege(pwent.uid)
    end
</pre>
<p>Maybe several things, but certainly one thing is that I&#8217;ve completely neglected supplemental group ids. I should have written:</p>
<pre class="brush: ruby;">
    def HostUtils.switch_user user
      pwent = Etc::getpwnam(user)
      Process::initgroups(user, pwent.gid)
      Process::GID::change_privilege(pwent.gid)
      Process::UID::change_privilege(pwent.uid)
    end
</pre>
<p>That call to <a href="http://www.ruby-doc.org/core/classes/Process.html#M003208">Process::initgroups</a> makes all the difference. After making the change, the apps could access NFS and our test site looked all pretty again. Good thing we caught it when we did!</p>
<p>Turns out this is a fairly <a href="http://www.ruby-forum.com/topic/110492">common problem</a>, and I feel especially dumb for overlooking something so obvious. Live and learn.</p>
]]></content:encoded>
			<wfw:commentRss>http://daemons.net/~clay/2009/05/02/setuid-ate-my-css/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
