<?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"
	>

<channel>
	<title>Useful Security</title>
	<atom:link href="http://www.usefulsecurity.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.usefulsecurity.com</link>
	<description>Solving real security problems that matter to real users</description>
	<pubDate>Fri, 09 Nov 2007 01:16:17 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5</generator>
	<language>en</language>
			<item>
		<title>Apple Sandboxes Part 2</title>
		<link>http://www.usefulsecurity.com/2007/11/apple-sandboxes-part-2/</link>
		<comments>http://www.usefulsecurity.com/2007/11/apple-sandboxes-part-2/#comments</comments>
		<pubDate>Fri, 09 Nov 2007 01:14:01 +0000</pubDate>
		<dc:creator>c-had</dc:creator>
		
		<category><![CDATA[Mac OS X]]></category>

		<guid isPermaLink="false">http://www.usefulsecurity.com/?p=22</guid>
		<description><![CDATA[Here are a couple of examples of using sandboxes in Leopard. Both examples involve confining a test application that needs to read a file, but should not be able to write that file. I realize that this could be easily implemented with standard Discretionary Access Control (DAC) mechanisms, but this example is solely for the [...]]]></description>
			<content:encoded><![CDATA[<p>Here are a couple of examples of using sandboxes in Leopard. Both examples involve confining a test application that needs to read a file, but should not be able to write that file. I realize that this could be easily implemented with standard <a href="http://en.wikipedia.org/wiki/Discretionary_access_control">Discretionary Access Control (DAC)</a> mechanisms, but this example is solely for the purposes of demonstrating the mechanism. Perhaps later I&#8217;ll show off some more useful examples.
<p>
<strong>Example 1</strong><br />
This example uses <code>sandbox_init()</code> with one of the predefined policies Apple provides. In this case, we&#8217;re going to use the <code>kSBXProfileNoWrite</code> policy, which prevents the process from writing any files to disk. The example program is:</p>
<table>
<tr>
<td width=30></td>
<td>
<pre><code>
#include &lt;sandbox.h&gt;
#include &lt;unistd.h&gt;
#include &lt;stdio.h&gt;
#include &lt;fcntl.h&gt;
#include &lt;string.h&gt;

int main(int argc, char *argv[])
{
	int ret, fd;
	char **errbuf;
	char buf[50], buf2[] = "blah blah\n";

	ret = sandbox_init(kSBXProfileNoWrite, SANDBOX_NAMED, errbuf);
	if (ret != 0) {
		printf("Error initializing sandbox\n");
		return 1;
	}
	fd = open("temp", O_RDONLY);
	if (fd < 0)
		perror("Error reading temp");
	else {
		read(fd, buf, 50);
		close(fd);
	}
	fd = open("temp", O_WRONLY);
	if (fd < 0)
		perror("Error writing temp");
	else {
		write(fd, buf2, strlen(buf));
		close(fd);
	}
	return ret;
}
</code></pre>
</td>
</tr>
</table>
<p>This code simply initializes a sandbox with the chosen profile, then attempts to open a temp file for reading, then again for writing. Running this yields the following result:</p>
<table>
<tr>
<td width=30></td>
<td>
<pre><code>
chadsbook:leopard chad$ ./test1
Error writing temp: Operation not permitted
</code></pre>
</td>
</tr>
</table>
<p>As expected, the test program can read the file, but not write to it. Looking in <code>/var/log/system.log</code> shows no messages, so apparently the <code>kSBXProfileNoWrite</code> policy does not log denials.
<p><span id="more-22"></span><br />
<strong>Example 2</strong><br />
This example uses the <code>sandbox-exec</code> command-line utility to place a test program in a sandbox that we create. Note that this is clearly marked as a private API that is subject to change in the header files, so try this with care. The test program is the same as the above without the <code>sandbox_init()</code> part (available <a href="http://www.usefulsecurity.com/wp-content/uploads/2007/11/test2.c" title="test2.c">here</a> if you don&#8217;t trust me). The policy, written in the seatbelt Scheme-like language is as follows:</p>
<table>
<tr>
<td width=30></td>
<td>
<pre><code>
;;
;; Test seatbelt sandbox policy
;;
(version 1)

;; Turn on debugging for denials
(debug deny)

;; Import basic rules from bsd policy
(import "/usr/share/sandbox/bsd.sb")

;; Allow all process perms
(allow process*)

;; Allow reading a dylib that isn't part of bsd.sb for some reason
(allow file-read-data
  (regex "^/private/var/db/dyld/dyld_shared_cache_i386$"))

;; Allow reading sysctls
(allow sysctl-read)

;; Allow reading test data, but not writing
(allow file-read-data
  (regex "^/Users/chad/Tresys/svn/leopard.*"))
</code></pre>
</td>
</tr>
</table>
<p>This policy has comments explaining what it does. Note that I have a <code>(debug deny)</code> message at the beginning, telling the kernel to log denials. This allowed me to look in <code>/var/log/system.log</code> to see what was being denied and allow it if necessary. That&#8217;s how I can up with several of these allow rules. Running the test program with this policy yields:</p>
<table>
<tr>
<td width=30></td>
<td>
<pre><code>
chadsbook:leopard chad$ sandbox-exec -f test2.sb ./test2
Error writing temp: Permission denied
</code></pre>
</td>
</tr>
</table>
<p>Again, the result we expect. This time, though, we&#8217;ve got the kernel logging denials. So, looking in <code>/var/log/system.log</code> yields:</p>
<table>
<tr>
<td width=30></td>
<td>
<pre><code>
Nov  8 16:09:50 chadsbook kernel[0]: test2 4511 FS_WRITE_DATA SBF /Users/chad/Tresys/svn/leopard/temp 13 (seatbelt)
</code></pre>
</td>
</tr>
</table>
<p>Here we see that indeed seatbelt blocked our test program from writing to our file, confirming what we&#8217;ve seen.
<p>
At some point, I&#8217;ll try to talk about useful things you can do with Apple&#8217;s sandbox mechanism, as well as potentially compare/contrast it with SELinux. For now, though, just know that Apple&#8217;s pursuing stronger security mechanisms as well. That said, the list of applications actually sandboxed by default on Leopard is very small, so don&#8217;t think you&#8217;re getting a lot of protection from this today. This is more of an indicator of the good things to come as well as a mechanism you can use to protect your apps today.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.usefulsecurity.com/2007/11/apple-sandboxes-part-2/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Apple Sandboxes Part 1</title>
		<link>http://www.usefulsecurity.com/2007/11/apple-sandboxes-part-1/</link>
		<comments>http://www.usefulsecurity.com/2007/11/apple-sandboxes-part-1/#comments</comments>
		<pubDate>Thu, 08 Nov 2007 19:37:08 +0000</pubDate>
		<dc:creator>c-had</dc:creator>
		
		<category><![CDATA[Mac OS X]]></category>

		<guid isPermaLink="false">http://www.usefulsecurity.com/?p=21</guid>
		<description><![CDATA[Linux isn&#8217;t the only operating system with activity in the enhanced access control department. Apple recently released Mac OS X 10.5 Leopard, which includes a new feature called a sandbox (or seatbelt, depending on where you&#8217;re looking) as well. I delved into the sandbox mechanisms a bit and wanted to report my findings.
First, let me [...]]]></description>
			<content:encoded><![CDATA[<p>Linux isn&#8217;t the only operating system with activity in the enhanced access control department. <a href="http://www.apple.com/">Apple</a> recently released <a href="http://www.apple.com/macosx/">Mac OS X 10.5 Leopard</a>, which includes a new feature called a <a href="http://www.apple.com/macosx/technology/security.html">sandbox</a> (or seatbelt, depending on where you&#8217;re looking) as well. I delved into the sandbox mechanisms a bit and wanted to report my findings.</p>
<p>First, let me say that much of this is gleaned or inferred from looking at a running system and the <a href="http://www.kernelthread.com/mac/osx/arch_xnu.html">XNU</a> <a href="http://www.opensource.apple.com/darwinsource/10.5/xnu-1228/">source code</a>. There is a bit of documentation on this feature, but it is limited. In addition, much of what is possible is labeled as &#8220;Apple System Private Interface and are subject to change at any time and without notice&#8221; so I wouldn&#8217;t plan my corporate infrastructure around using it just yet.</p>
<p>Apple has implemented the sandbox mechanism by utilizing the <a href="http://www.trustedbsd.org/mac.html">MAC Framework</a> developed by <a href="http://www.trustedbsd.org/">the TrustedBSD project</a>. This is a kernel framework similar to the <a href="http://en.wikipedia.org/wiki/Linux_Security_Modules">Linux Security Module (LSM)</a> framework that SELinux uses to hook into the Linux kernel. Note that TrustedBSD&#8217;s <a href="http://www.trustedbsd.org/sedarwin.html">SEDarwin project</a> also ported the SELinux policy decision engine to Darwin, but Apple decided to implement their own policy decision engine instead of using SEDarwin&#8217;s. That engine is called seatbelt, and it&#8217;s a kernel extension (found in /System/Library/Extensions/seatbelt.kext).</p>
<p>Seatbelt does not appear to be released as source, so I can only surmise how it works from attempts to use it. It appears to provide pathname-based access control (<a href="http://securityblog.org/brindle/2006/04/19/security-anti-pattern-path-based-access-control/">not my favorite</a>). Additionally, processes are not confined by seatbelt by default. Instead, a process must opt-in (by calling sandbox_init()) to be confined. Once confined, all children are also confined by the same policy, meaning that you can have a wrapper call sandbox_init() and then execute the program you really want to confine. In fact, it looks like Apple thought this might be useful as they included a command-line utility called sandbox-exec which appears to do just that.</p>
<p>A process selects which policy will confine it at sandbox_init() time. There are two ways to do this. The first way, which is the only one officially supported by Apple according to the documentation, is to choose a policy that is statically compiled into the kernel. Apple <a href="http://developer.apple.com/documentation/Darwin/Reference/ManPages/man3/sandbox_init.3.html">documented</a> five of these, each with a high-level goal. These goals include no internet, no networking, no file writing, no file writing except temporary files, and pure computation.</p>
<p>The second way of selecting a policy, which is clearly marked as a private API that is subject to change, is to provide a pathname to a policy file. While there is no documentation of the format of the policy file, there are several examples available in /usr/share/sandbox which give clues to what a policy can do. These policies seem to use a Scheme-like syntax and provide abilities to restrict file access (based on pathname), restrict interprocess communication (IPC) such as shared memory, restrict network access, restrict signals, restrict many process-related actions, restrict sysctls, and more.</p>
<p>Stay tuned for some examples of using sandboxes on Leopard.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.usefulsecurity.com/2007/11/apple-sandboxes-part-1/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Montavista Vision 2007</title>
		<link>http://www.usefulsecurity.com/2007/10/montavista-vision-2007/</link>
		<comments>http://www.usefulsecurity.com/2007/10/montavista-vision-2007/#comments</comments>
		<pubDate>Wed, 10 Oct 2007 23:08:29 +0000</pubDate>
		<dc:creator>c-had</dc:creator>
		
		<category><![CDATA[SELinux]]></category>

		<guid isPermaLink="false">http://www.usefulsecurity.com/?p=19</guid>
		<description><![CDATA[I had the distinct opportunity to speak at the Montavista Vision conference about SELinux in embedded devices. I&#8217;d like to say thanks to all who attended my talk. A copy of the slides is available here. Thanks for all the questions. I hope I answered them to your satisfaction.
Some of you may be scratching your [...]]]></description>
			<content:encoded><![CDATA[<p>I had the distinct opportunity to speak at the <a href="http://www.mvista.com/vision/">Montavista Vision conference</a> about SELinux in embedded devices. I&#8217;d like to say thanks to all who attended <a href="http://www.mvista.com/vision/sessions.html#sellers">my talk</a>. A copy of the slides is available <a href="http://www.usefulsecurity.com/wp-content/uploads/2007/10/utilizing-selinux-in-embedded-devices.pdf">here</a>. Thanks for all the questions. I hope I answered them to your satisfaction.</p>
<p>Some of you may be scratching your head seeing SELinux, Montavista, and embedded all together. Yes, SELinux can be used beyond server systems. Linux is growing incredibly quickly in embedded devices, being used in cell phones, set-top boxes, cameras, televisions, and even hard disks (yeah, that shocked me too). And many of these devices have lots of security issues. Which is why Montavista has <a href="http://www.mvista.com/press_release_detail.php?fid=news/2007/mob5.html">announced</a> support for SELinux in their upcoming release. I look forward to buying my first cell phone with SELinux protecting my personal data. Maybe if Apple had Mandatory Access Controls in their embedded OS they could be more comfortable with allowing 3rd party application that wouldn&#8217;t break the rest of the system.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.usefulsecurity.com/2007/10/montavista-vision-2007/feed/</wfw:commentRss>
		</item>
		<item>
		<title>LinuxWorld 07</title>
		<link>http://www.usefulsecurity.com/2007/08/linuxworld-07/</link>
		<comments>http://www.usefulsecurity.com/2007/08/linuxworld-07/#comments</comments>
		<pubDate>Sat, 11 Aug 2007 23:48:42 +0000</pubDate>
		<dc:creator>c-had</dc:creator>
		
		<category><![CDATA[SELinux]]></category>

		<guid isPermaLink="false">http://www.usefulsecurity.com/?p=16</guid>
		<description><![CDATA[I&#8217;d like to say thanks to all who attended my talk at LinuxWorld. A copy of the slides is available here. I hope you enjoyed it and got a lot out of it. I just posted the second demo in full here, and I&#8217;ll try to get the last one up in the near future. [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;d like to say thanks to all who attended <a href="http://www.linuxworldexpo.com/live/12/events/12SFO07A/conference/tracksessions/Security/QMONYB00129X">my talk at LinuxWorld</a>. A copy of the slides is available <a href="http://www.usefulsecurity.com/wp-content/uploads/2007/08/linuxworld-07.pdf" title="LinuxWorld 07 Slides">here</a>. I hope you enjoyed it and got a lot out of it. I just posted the second demo in full here, and I&#8217;ll try to get the last one up in the near future. If you have further questions, don&#8217;t hesitate to drop me an email or post a comment here. Thanks again.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.usefulsecurity.com/2007/08/linuxworld-07/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Preventing Disclosure</title>
		<link>http://www.usefulsecurity.com/2007/08/preventing-disclosure/</link>
		<comments>http://www.usefulsecurity.com/2007/08/preventing-disclosure/#comments</comments>
		<pubDate>Sat, 11 Aug 2007 23:48:33 +0000</pubDate>
		<dc:creator>c-had</dc:creator>
		
		<category><![CDATA[SELinux]]></category>

		<guid isPermaLink="false">http://www.usefulsecurity.com/?p=15</guid>
		<description><![CDATA[Problem
While it is always preferable to keep confidential information such as customer records away from a website, that is often not feasible. This is especially true in ecommerce, but is true in other areas as well. In order to protect this information, most people will employ some sort of authentication application to ensure that people [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Problem</strong><br />
While it is always preferable to keep confidential information such as customer records away from a website, that is often not feasible. This is especially true in ecommerce, but is true in other areas as well. In order to protect this information, most people will employ some sort of authentication application to ensure that people can only access the data for which they are authorized. Despite this, attackers frequently find ways through or more often around this authentication mechanism.</p>
<p><strong>Goal</strong><br />
Prevent an attacker from accessing customer records on a web site.</p>
<p><strong>Approach</strong><br />
Solving confidentiality problems generally involves two activities. The first entails ensuring that whatever has to access the confidential data is not exploitable. This is a tall order, but is not impossible. The best strategy to take here is to make that program that has to access the confidential data as small and analyzable as possible. You can take great care in writing it and in analyzing it to ensure its correctness. So, I need to make my &#8220;guard&#8221; application that grants access to the data as small as possible. It should only contain code to do what it needs to do to authenticate the user and give back the data that user is authorized to see.<br />
After ensuring that the application that needs access to the data is solid, the second activity in solving this problem is to ensure that nothing else can access the data. This is where I&#8217;ll use SELinux access controls. I&#8217;ll build an SELinux domain for the authentication application to run in as well as a type for the confidential records on disk. I&#8217;ll then allow only the authenitication application to access the confidential records. By doing this, I can prevent other things on the website (as well as the rest of the system) from accessing the records. An attacker may be able to compromise another part of my website, but he will not be able to get to my confidential records.<sup>1</sup><br />
<span id="more-15"></span><br />
<strong>Details</strong><br />
Example system</p>
<ul>
<li><a href="http://fedoraproject.org/">Fedora 7</a></li>
<li>Targeted SELinux policy<sup>2</sup></li>
<li><a href="http://httpd.apache.org/">Apache</a></li>
</ul>
<p>I wrote a quick python script to use as my example application. It simply receives authentication data and displays the contents of the confidential records if the authentication is successful. This is admittedly an overly simple illustration, but those are often the best examples. I also wrote another simple script for demonstration purposes to serve as a vulnerable application also hosted on my website. I&#8217;ll have Apache execute these scripts as a cgi. Note that this will not work if I was using mod_python, as mod_python executes the script with the apache process. Consequently I&#8217;d be forced to try to confine apache itself, and I clearly can&#8217;t place all these restrictions on apache.</p>
<p>I created my SELinux policy with <a href="http://oss.tresys.com/projects/slide">SLIDE</a>, which filled in much of the details for me through a basic wizard. This wizard created a type for my application (records_t) and its corresponding executable (records_exec_t) and granted some basic accesses that almost everything needs (like access to shared libraries and locale). I then really only needed to add four things to make the application work.</p>
<ol>
<li>
<dl>
<dt><code>type record_data_t;<br />
files_type(record_data_t)</code> </dt>
<dd>This creates a type for the confidential records, which separates it from the other files on the website so we can protect it.</dd>
</dl>
</li>
<li>
<dl>
<dt><code>corecmd_exec_bin(records_t)</code> </dt>
<dd>This simply gives the authentication application the ability to execute common binaries on the system (mostly in /bin and /usr/bin). In particular, this is necessary to execute python.</dd>
</dl>
</li>
<li>
<dl>
<dt><code>allow records_t record_data_t:file read_file_perms;</code> </dt>
<dd>This allows the application to read the confidential records. Note that the important part here is that I have not granted anything else the ability to read these records. Also, note that this is an allow rule rather than an interface call. Interface calls are used for interfacing with other modules within the policy. Here we are not interfacing with other policy modules, but are rather just allowing access internal to our policy module.</dd>
</dl>
</li>
<li>
<dl>
<dt><code>apache_cgi_domain(records_t, records_exec_t)<sup>2</sup></code> </dt>
<dd>This does a couple of things. First, it ensures that when Apache executes our web app, the web app runs in the proper domain (records_t). Secondly, it allows our web app to talk to Apache. </dd>
</dl>
</li>
</ol>
<p>The full source code of the policy and the test applications are available <a href="http://www.usefulsecurity.com/wp-content/uploads/2007/08/records.tgz" title="Full source of example">here</a>.</p>
<p><strong>Results</strong></p>
<ol>
<li>
<table>
<tr>
<td><a href="http://www.usefulsecurity.com/wp-content/uploads/2007/08/records_1.jpg" title="Authentication Application"><img src="http://www.usefulsecurity.com/wp-content/uploads/2007/08/records_1.thumbnail.jpg" alt="Authentication Application" /></a></td>
<td><a href="http://www.usefulsecurity.com/wp-content/uploads/2007/08/records_2.jpg" title="Access to records"><img src="http://www.usefulsecurity.com/wp-content/uploads/2007/08/records_2.thumbnail.jpg" alt="Access to records" /></a></td>
</tr>
</table>
<p>This is the authentication applicaiton granting access to the records after I enter proper credentials. This shows how things are supposed to work, as well as that our SELinux policy is letting the proper workflow occur without problems.</li>
<li>
<table>
<tr>
<td><center>SELinux disabled</center></td>
<td><center>SELinux enabled</center></td>
</tr>
<tr>
<td><a href="http://www.usefulsecurity.com/wp-content/uploads/2007/08/records_3.jpg" title="SELinux disabled"><img src="http://www.usefulsecurity.com/wp-content/uploads/2007/08/records_3.thumbnail.jpg" alt="SELinux disabled" /></a></td>
<td><a href="http://www.usefulsecurity.com/wp-content/uploads/2007/08/records_4.jpg" title="SELinux enabled"><img src="http://www.usefulsecurity.com/wp-content/uploads/2007/08/records_4.thumbnail.jpg" alt="SELinux enabled" /></a></td>
</tr>
</table>
<p>This is a view of utilizing the vulnerable application (hacked.py) to get to the customer records. This application had no right to access this data, but without access controls stopping it, it can be used to bypass authentication. On the contrary, with SELinux enabled this program is denied access to the records.</li>
</ol>
<p><sup>1</sup> Note that this example assumes that the confidential data is stored in a file that will be directly read by the web application. Another common scenario is that the confidential data is instead stored in a database which the web application accesses by talking to a database server. The operating system has no visibility into the database server, so SELinux can only control which web applications can talk to the database server, not what data the database server can give them. Thankfully, database server developers have thought of this already. In fact, there is a project to create a <a href="http://code.google.com/p/sepgsql/">security enhanced postgresql server</a> which cooperates with SELinux to enforce very powerful fine-grained mandatory access control over databases. With this, it&#8217;s possible to enforce access control over the individual entries in the database. Other databases, such as <a href="http://www.mysql.com/">mysql</a>, at least have a coarse-grained discretionary access control usually implemented as authentication credentials required for accessing a database. If this is a username and password stored in the web application&#8217;s config file, then I can use SELinux to protect that config file in the same way that I used SELinux to protect the records themselves above. So, it&#8217;s at least possible to indirectly control who can access the records stored in the database.<br />
<sup>2</sup> This policy is built on the upstream Fedora 7 targeted SELinux policy with one exception. The apache_cgi_domain() interface does not exist there yet. When I started working on this blog entry, this interface did not exist. So, I wrote it and upstreamed it to the <a href="http://oss.tresys.com/projects/refpolicy">Reference Policy project</a> (which serves as upstream for most distros including Fedora). As of today, this interface hasn&#8217;t made it into Fedora yet, but it shouldn&#8217;t be long. For the above example, I backported this interface into the current Fedora policy.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.usefulsecurity.com/2007/08/preventing-disclosure/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Vulnerable web applications</title>
		<link>http://www.usefulsecurity.com/2007/08/vulnerable-web-applications/</link>
		<comments>http://www.usefulsecurity.com/2007/08/vulnerable-web-applications/#comments</comments>
		<pubDate>Fri, 03 Aug 2007 19:01:24 +0000</pubDate>
		<dc:creator>c-had</dc:creator>
		
		<category><![CDATA[SELinux]]></category>

		<guid isPermaLink="false">http://www.usefulsecurity.com/?p=6</guid>
		<description><![CDATA[Problem
Web applications can be a source of very frequent vulnerabilities. These vulnerabilities can stem from bugs in the program itself as well as the libraries and frameworks upon which it depends. These vulnerabilities are often used as the entry-point for an attacker to upload malicious software onto a system. This software can contain root-kits, bots, [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Problem</strong><br />
Web applications can be a source of very frequent vulnerabilities. These vulnerabilities can stem from bugs in the program itself as well as the libraries and frameworks upon which it depends. These vulnerabilities are often used as the entry-point for an attacker to upload malicious software onto a system. This software can contain root-kits, bots, data collection agents, or other malicious applications.</p>
<p><strong>Goal</strong><br />
Prevent an attacker from uploading and executing malicious software through vulnerabilities in the web application.</p>
<p><strong>Approach</strong><br />
I&#8217;ll use SELinux to confine the web application. This will involve building an SELinux domain for the web application to run in that cannot execute anything it writes to disk or access the network directly. The strictest policy I could write would not allow the domain to write anything to disk at all, but that is often not feasible for web apps, as part of their function often involves receiving uploads. By eliminating the ability to execute something that has been uploaded, I can significantly reduce the attackers ability to utilize previously generated malicious code such as a root-kit. However, the attacker may still be able to send malicious code by taking over the web app process itself. While I cannot prevent this without having a bullet-proof web app, I can limit what the attacker can do by preventing all direct access to the network. This prevents the attacker from using a compromised web app process as a bot. Additionally, I&#8217;ll limit the access that the web app has to only what is necessary for it to function in order to prevent the compromised app from scouring the system for information or further vulnerabilities.<span id="more-6"></span></p>
<p><strong>Details</strong><br />
Example system</p>
<ul>
<li><a href="http://fedoraproject.org/">Fedora 7</a></li>
<li>Targeted SELinux policy<sup>1</sup></li>
<li><a href="http://httpd.apache.org/">Apache</a></li>
</ul>
<p>I wrote a quick python script to use as my example application. It basically just receives uploads and stores them on disk. It also includes a couple of attacks for testing if my policy is working. I&#8217;ll have Apache execute this script as a cgi. Note that this will not work if I was using mod_python, as mod_python executes the script with the apache process. Consequently I&#8217;d be forced to try to confine apache itself, and I clearly can&#8217;t place all these restrictions on apache.</p>
<p>I created my SELinux policy with <a href="http://oss.tresys.com/projects/slide">SLIDE</a>, which filled in much of the details for me through a basic wizard. This wizard created a type for my application (bb_t) and its corresponding executable (bb_exec_t) and granted some basic accesses that almost everything needs (like access to shared libraries and locale). I then really only needed to add four things to make the application work.</p>
<ol>
<li>
<dl>
<dt><code>type bb_uploads_t;<br />
files_type(bb_uploads_t)</code> </dt>
<dd>This creates a type for the uploads directory and all the files that are uploaded. This type cannot be executed by the web app or apache.</dd>
</dl>
</li>
<li>
<dl>
<dt><code>corecmd_exec_bin(bb_t)</code> </dt>
<dd>This simply gives the web app the ability to execute common binaries on the system (mostly in /bin and /usr/bin). In particular, this is necessary to execute python.</dd>
</dl>
</li>
<li>
<dl>
<dt><code>manage_files_pattern(bb_t, bb_uploads_t, bb_uploads_t)</code> </dt>
<dd>This allows the web app the ability to create, read, write, delete, etc. (but not execute) files and directories in the uploads directory.</dd>
</dl>
</li>
<li>
<dl>
<dt><code>apache_cgi_domain(bb_t, bb_exec_t)<sup>1</sup></code> </dt>
<dd>This does a couple of things. First, it ensures that when Apache executes our web app, the web app runs in the proper domain (bb_t). Secondly, it allows our web app to talk to Apache. </dd>
</dl>
</li>
</ol>
<p>The full source code of the policy and the test web app are available <a href="http://www.usefulsecurity.com/wp-content/uploads/2007/08/bb.tgz" title="Full source of example">here</a>.</p>
<p><strong>Results</strong><br />
My example python script includes two exploits. The first allows an attacker to execute something that&#8217;s been uploaded. The second assumes that the attacker has taken over the web app itself, and attempts to bind to a network port to await instructions from the attacker. Below, I execute both attacks twice - once with SELinux disabled and once with SELinux enabled.</p>
<ol>
<li>
<table>
<tr>
<td><center>SELinux disabled</center></td>
<td><center>SELinux enabled</center></td>
</tr>
<tr>
<td><a href="http://www.usefulsecurity.com/wp-content/uploads/2007/08/bb_1.jpg" title="With SELinux disabled"><img src="http://www.usefulsecurity.com/wp-content/uploads/2007/08/bb_1.thumbnail.jpg" alt="With SELinux disabled" /></a></td>
<td><a href="http://www.usefulsecurity.com/wp-content/uploads/2007/08/bb_2.jpg" title="With SELinux enabled"><img src="http://www.usefulsecurity.com/wp-content/uploads/2007/08/bb_2.thumbnail.jpg" alt="With SELinux enabled" /></a></td>
</tr>
</table>
<p>To test executing uploaded files, I upload a script that simply prints &#8220;You have been hacked!&#8221; Note that the SELinux policy prevents this from being executed.</li>
<li>
<table>
<tr>
<td><center>SELinux disabled</center></td>
<td><center>SELinux enabled</center></td>
</tr>
<tr>
<td><a href="http://www.usefulsecurity.com/wp-content/uploads/2007/08/bb_3.jpg" title="With SELinux disabled"><img src="http://www.usefulsecurity.com/wp-content/uploads/2007/08/bb_3.thumbnail.jpg" alt="With SELinux disabled" /></a></td>
<td><a href="http://www.usefulsecurity.com/wp-content/uploads/2007/08/bb_4.jpg" title="With SELinux enabled"><img src="http://www.usefulsecurity.com/wp-content/uploads/2007/08/bb_4.thumbnail.jpg" alt="With SELinux enabled" /></a></td>
</tr>
</table>
<p>To test accessing the network, my web app attempts to bind to tcp port 1337 and responds on that port to anything that connects to it. Note again that SELinux policy prevents this access, as well as any other network access it may attempt.</li>
</ol>
<p><sup>1</sup> This policy is built on the upstream Fedora 7 targeted SELinux policy with one exception. The apache_cgi_domain() interface does not exist there yet. When I started working on this blog entry, this interface did not exist. So, I wrote it and upstreamed it to the <a href="http://oss.tresys.com/projects/refpolicy">Reference Policy project</a> (which serves as upstream for most distros including Fedora). As of today, this interface hasn&#8217;t made it into Fedora yet, but it shouldn&#8217;t be long. For the above example, I backported this interface into the current Fedora policy.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.usefulsecurity.com/2007/08/vulnerable-web-applications/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Useful Security</title>
		<link>http://www.usefulsecurity.com/2007/08/useful-security/</link>
		<comments>http://www.usefulsecurity.com/2007/08/useful-security/#comments</comments>
		<pubDate>Thu, 02 Aug 2007 19:12:04 +0000</pubDate>
		<dc:creator>c-had</dc:creator>
		
		<category><![CDATA[SELinux]]></category>

		<guid isPermaLink="false">http://www.usefulsecurity.com/?p=5</guid>
		<description><![CDATA[Welcome to usefulsecurity.com. This blog is dedicated to providing tips, pointers, howtos, and other information on solving real security problems. Too often today security professionals come up with &#8220;solutions&#8221; that don&#8217;t really help users solve their security problems. This stems from many reasons, including not understanding the problem correctly, not understanding the available technologies, and [...]]]></description>
			<content:encoded><![CDATA[<p>Welcome to usefulsecurity.com. This blog is dedicated to providing tips, pointers, howtos, and other information on solving real security problems. Too often today security professionals come up with &#8220;solutions&#8221; that don&#8217;t really help users solve their security problems. This stems from many reasons, including not understanding the problem correctly, not understanding the available technologies, and a desire to sell a solution that may not make sense in a particular environment.</p>
<p>This blog will follow a pattern of choosing a security problem and looking at how to solve that problem. As often as possible, this will come in the form of technical examples including code and policy snippets. This blog will often utilize SELinux in some way to solve those problems. There are 2 reasons for this. First, I&#8217;m heavily involved in SELinux in my day job at Tresys, so the problems I see are frequently addressable by SELinux. Second, SELinux is a very flexible security framework, so it actually does address a very large number of security problems. That said, I&#8217;m going do my best to focus on solving the problem and utilizing appropriate technology to do it.</p>
<p>So, sit back and enjoy. Hopefully you&#8217;ll find solutions to some of the security problems you&#8217;re facing today. If not, feel free to let me know what those problems are so I can try to focus on them in future posts.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.usefulsecurity.com/2007/08/useful-security/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
