<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="/stylesheets/rss.css" type="text/css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Tom Moertel's Weblog: A bright future: security and modern type systems</title>
    <link>http://blog.moertel.com/articles/2007/08/15/a-bright-future-security-and-modern-type-systems</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Quality rants on programming theory and stuff geeks like</description>
    <item>
      <title>A bright future: security and modern type systems</title>
      <description>&lt;p&gt;The &lt;a href="http://it.slashdot.org/it/07/08/12/1550254.shtml"&gt;recent defacement of the United Nations web
site&lt;/a&gt; is a prime
example of why we programmers shouldn&amp;#8217;t trust ourselves to write
secure code &amp;#8211; at least not without our computers&amp;#8217; help.  The U.N. web
site, according to Slashdot&amp;#8217;s coverage of the incident, was defaced by
way of a common, well-known attack: &lt;a href="http://en.wikipedia.org/wiki/SQL_injection"&gt;&lt;span class="caps"&gt;SQL&lt;/span&gt;
injection&lt;/a&gt;.  What&amp;#8217;s
interesting is that programmers can render this attack harmless by
employing simple, readily available programming tools such as
placeholders and prepared statements.  Why, then, are so many web sites,
including the UN site apparently, still vulnerable?&lt;/p&gt;


	&lt;p&gt;Some say it&amp;#8217;s because the programmers of these sites are incompetent,
but that argument ignores that programmers are
human, while the security tools we give them offer meaningful
protection only if wielded with inhuman perfection.  Having the tools
to plug security holes, even if the tools are simple to use and
readily available, is not enough to ensure that every single security
hole will be identified, let alone plugged.  Even the most experienced
programmer can be expected to overlook a hole now and then.
Unfortunately, one hole is all it takes.&lt;/p&gt;


	&lt;p&gt;That&amp;#8217;s because security is not like other software-quality challenges:
its costs are fundamentally asymmetric.  For the attacker, the bad
guy, the challenge is to find just a single exploitable hole.  For
us, the good guys, the challenge is to achieve perfection: to plug
&lt;em&gt;all&lt;/em&gt; of the holes in our code, every single one.  That&amp;#8217;s because
attackers, unlike regular users, can be expected to probe our code
until they find a hole to exploit.&lt;/p&gt;


	&lt;p&gt;How then do we ensure that we have plugged every single hole in our
code?  Testing isn&amp;#8217;t sufficient: we can easily overlook holes
when writing tests &amp;#8211; a perfectly human error.  We could supplement
testing with code reviews, painstakingly searching for remaining holes
while enforcing the use of hole-preventing best practices, but reviews
are expensive and, again, subject to human error.  A better approach,
both less costly and more reliable, is to delegate this burden to our
computers, which can do the job correctly, every single time.&lt;/p&gt;


	&lt;p&gt;This kind of delegation is possible today with modern static type systems.
For example, in &lt;a href="http://blog.moertel.com/articles/2006/10/18/a-type-based-solution-to-the-strings-problem"&gt;A Type-Based Solution to the Strings
Problem&lt;/a&gt;,
I offered a tiny &amp;#8220;safe strings&amp;#8221; library for the &lt;a href="http://haskell.org/"&gt;Haskell programming
language&lt;/a&gt;.  The library takes advantage of
Haskell&amp;#8217;s powerful type system to detect unsafe string interactions at
compile time. If we faithfully build our code on top of the library, and our
code compiles without error, we can be assured that our code is
free &amp;#8211; completely free &amp;#8211; of &lt;span class="caps"&gt;SQL&lt;/span&gt;-injection (and &lt;a href="http://en.wikipedia.org/wiki/Cross-site_scripting"&gt;&lt;span class="caps"&gt;XSS&lt;/span&gt;&lt;/a&gt;) holes.&lt;/p&gt;


	&lt;p&gt;While this result is indeed quite beautiful, it certainly isn&amp;#8217;t novel.
Researchers have been proving interesting
properties via type systems for a long time.  As Oleg Kiselyov and Chung-chieh Shan pointed out in &lt;a href="http://blog.moertel.com/articles/2006/10/18/a-type-based-solution-to-the-strings-problem#comment-204"&gt;a comment on my earlier article&lt;/a&gt;, the foundational idea is over three decades old.&lt;/p&gt;


	&lt;p&gt;More recently, Kiselyov and Shan have extended the
idea to guarantee more-interesting properties using a trusted kernel and types that represent
&lt;a href="http://okmij.org/ftp/papers/lightweight-static-capabilities.pdf"&gt;lightweight static
capabilities&lt;/a&gt;.
The kernel, which is small enough to be reasoned about and formally
verified, carefully hands out capabilities to untrusted application
code.  The untrusted code, in turn, presents the capabilities back to
the kernel to invoke operations, which, thanks to the kernel&amp;#8217;s
trustworthiness, are guaranteed to be safe.  (My safe-string library
can be seen as a trivial implementation of this programming style.)&lt;/p&gt;


	&lt;p&gt;When static type systems are used in this way, they don&amp;#8217;t merely catch
typos and bugs that good testing would have caught as a matter of
course, but offer programmers guarantees that would have been
impractical to obtain any other way.&lt;sup&gt;&lt;a href="#un-sql-fn1"&gt;1&lt;/a&gt;&lt;/sup&gt; If
you consider security important, you might bear this fact in mind when
choosing languages for your next project.&lt;/p&gt;


	&lt;p&gt;Going further, the security benefits of rich static type systems are only now
starting to trickle into mainstream industry.  As libraries like &amp;#8220;safe
strings&amp;#8221; and idioms like static capabilities become more familiar and
get woven into future generations of development frameworks, we can
expect marked improvements in the security and robustness of our
applications.&lt;/p&gt;


	&lt;p&gt;In the not-too-distant future, perhaps, we might look
back in amazement at the days when important security properties were
neither free nor guaranteed but expensive and uncertain, underwritten
only by the heroic efforts of individual programmers, struggling
against impossible odds to achieve inhuman perfection.&lt;/p&gt;


	&lt;p&gt;Then again, it sure took garbage collection a long time to catch on.&lt;/p&gt;


&lt;hr/&gt;

&lt;div class="footnote"&gt;

	&lt;p&gt;&lt;a name="un-sql-fn1"&gt;1.&lt;/a&gt; How, for example, could you eliminate the
possibility of &lt;span class="caps"&gt;SQL&lt;/span&gt;-injection and &lt;span class="caps"&gt;XSS&lt;/span&gt; holes via testing?&lt;/p&gt;


	&lt;p&gt;I suppose you could do it if you worked at
it hard enough. You could augment your string data
structures with run-time information about what they represent:
&lt;em&gt;this string represents &lt;span class="caps"&gt;SQL&lt;/span&gt;, this string represents plain-old
text&lt;/em&gt;, and so on.  Then you could redefine your string operations
and template interpolation systems to assert that their string inputs
were compatible.  Of course, if these assertions ever failed, they
would do so only at run time, when it would be too late to do anything
but die rather ungracefully.  So you would be forced to augment your
code-coverage tools to ensure that every string-path was covered
during testing.  That way you could catch all potential run-time
string failures &amp;#8211; indicating holes &amp;#8211; during testing and eliminate the
holes (and the subsequent need to fail at run time) before you
deployed your application for real.&lt;/p&gt;


	&lt;p&gt;So, yes, you could do it.  But to do so would require you, in effect,
to write a crude, single-purpose type system that checks types at test
time.  That says something, doesn&amp;#8217;t it?&lt;/p&gt;


&lt;/div&gt;</description>
      <pubDate>Wed, 15 Aug 2007 16:07:00 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:fd522245-d8b7-4270-814d-5237a26bb0b9</guid>
      <author>Tom Moertel</author>
      <link>http://blog.moertel.com/articles/2007/08/15/a-bright-future-security-and-modern-type-systems</link>
      <category>programming</category>
      <category>web development</category>
      <category>security</category>
      <category>types</category>
      <category>security</category>
      <category>capabilities</category>
      <category>safestrings</category>
      <category>sqlinjection</category>
      <category>xss</category>
      <trackback:ping>http://blog.moertel.com/articles/trackback/524</trackback:ping>
    </item>
    <item>
      <title>"A bright future: security and modern type systems" by osuszacze</title>
      <description>&lt;p&gt;I got to agree with Poul Johnson, he`s right. Btw. Great blog!&lt;/p&gt;</description>
      <pubDate>Wed, 17 Oct 2007 03:11:40 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:603a5ea9-9777-4a92-bab1-f2504362e1e1</guid>
      <link>http://blog.moertel.com/articles/2007/08/15/a-bright-future-security-and-modern-type-systems#comment-597</link>
    </item>
    <item>
      <title>"A bright future: security and modern type systems" by Tom Moertel</title>
      <description>&lt;p&gt;Mike:  I&amp;#8217;ll respond to your comment because I think you have confused me (Tom Moertel, the guy who wrote this article) for Tom Clancy, the guy who posted a comment earlier.&lt;/p&gt;


	&lt;blockquote&gt;
		&lt;p&gt;Your solution generates regular “code and data together” SQL and will fail when someone manages to escape something or play encoding tricks in a way which your type system didn’t foresee.&lt;/p&gt;
	&lt;/blockquote&gt;


	&lt;p&gt;Just how is somebody going to &amp;#8220;escape something&amp;#8221; or &amp;#8220;play encoding tricks&amp;#8221; in a way that evades the type system?  Can you show me an example (that doesn&amp;#8217;t assume the programmer is pathological)?&lt;/p&gt;


	&lt;p&gt;Assume we have the typical newbie web programmer: well-meaning but possibly forgetful and lacking knowledge of security best practices (or even fundamentals).  Now, drop him into a programming environment where something like the safe-strings system has been baked into the web-app framework.  Every string this programmer gets or gives to the framework will have the right type already associated with it &lt;em&gt;at compile time&lt;/em&gt;.  Now, tell me, just how is this programmer, through anything but an outright attempt at subversion, going to evade the type system?  How is he going to manage to write code that stuffs a string that the type system thinks is &amp;#8220;plain old text&amp;#8221; into a template that represents SQL or XHTML or a URL or &lt;em&gt;anything else&lt;/em&gt; without the string being properly escaped?&lt;/p&gt;


	&lt;blockquote&gt;
		&lt;p&gt;[E]ven if we could agree that using a language with advanced static typing and inference would solve security issues, that’s not a realistic solution for web sites that have already been built nor for [..] the near future [...]&lt;/p&gt;
	&lt;/blockquote&gt;


	&lt;p&gt;True.  But if we assume that advanced static typing will someday work its way into the mainstream &amp;#8211; a reasonable assumption &amp;#8211; then we might at least want to start thinking about that future today.&lt;/p&gt;


	&lt;p&gt;Thanks for your comment.&lt;/p&gt;


	&lt;p&gt;Cheers,&lt;br /&gt;
Tom&lt;/p&gt;</description>
      <pubDate>Tue, 21 Aug 2007 17:28:16 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:00bf8687-1b9f-4266-90ae-403bfdd93313</guid>
      <link>http://blog.moertel.com/articles/2007/08/15/a-bright-future-security-and-modern-type-systems#comment-543</link>
    </item>
    <item>
      <title>"A bright future: security and modern type systems" by Mike</title>
      <description>&lt;p&gt;Tom Clancy:&lt;/p&gt;


	&lt;p&gt;I don&amp;#8217;t think you understood Thomas&amp;#8217; point.&lt;/p&gt;


	&lt;p&gt;Your solution does not separate code from data at a database level. A SQL prepared statement expects the SQL code and the data to be fed to the database separately. Only a bug in the database will allow you to exploit a prepared statement.&lt;/p&gt;


	&lt;p&gt;Your solution generates regular &amp;#8220;code and data together&amp;#8221; SQL and will fail when someone manages to escape something or play encoding tricks in a way which your type system didn&amp;#8217;t foresee. You also need to remember that different databases are subtly different and moving targets as it comes to escaping and encoding rules.&lt;/p&gt;


	&lt;p&gt;The fact that most web sites are exploitable doesn&amp;#8217;t mean that the solutions Thomas talks about are inefficient, it just means that web sites are not using them. It&amp;#8217;s not the solutions that are not doing their job, it&amp;#8217;s the managers and programmers. Surely, it requires discipline and has a price, but it works.&lt;/p&gt;


	&lt;p&gt;I personally dislike static typing and even if we could agree that using a language with advanced static typing and inference would solve security issues, that&amp;#8217;s not a realistic solution for web sites that have already been built nor for many that will be built by many organizations in the near future for a myraid of reasons.&lt;/p&gt;</description>
      <pubDate>Tue, 21 Aug 2007 08:32:16 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:95202303-e513-438c-9d69-4da9e223352a</guid>
      <link>http://blog.moertel.com/articles/2007/08/15/a-bright-future-security-and-modern-type-systems#comment-542</link>
    </item>
    <item>
      <title>"A bright future: security and modern type systems" by Tom Clancy</title>
      <description>&lt;p&gt;If people get nothing out of this except the part about the game being asymmetric, that&amp;#8217;s enough. My awakening came during a testing of a CMS we built. The client was a major IT Consultancy and one thing they offer are security audits. Seeing the results, I finally got the point that thinking like a developer will never plug all the holes. Meta-answers (like your type system) that eliminate a whole class of mistakes from the realm of possibility are what is needed.&lt;/p&gt;


	&lt;p&gt;Rather than being terribly clever like I (developer) imagined, the security audit was brute force: they spidered thousands of pages to disk, ran scripts looking for form inputs and crafted fake posts to all those urls with noise for data. And blew up old legacy things no one had thought about or used in years. I don&amp;#8217;t know if a human ever had to be involved beyond pointing the script at the initial url.&lt;/p&gt;


	&lt;p&gt;The best analogy I can come up with is the last time I got electrocuted*: the instant previous, my hand was controlled by a series of tiny, intentional, carefully crafted electrical impulses. Then it was taken over by a flood of huge impulses coming through every nerve ending I have (had). There&amp;#8217;s no way to control that, you just have to avoid it.&lt;/p&gt;


	&lt;ul&gt;
	&lt;li&gt;Yes, I&amp;#8217;m that bright in my day-to-day life as well, but this was my father&amp;#8217;s fault.&lt;/li&gt;
	&lt;/ul&gt;</description>
      <pubDate>Thu, 16 Aug 2007 11:52:53 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:7ed003b1-a7d6-4123-8aa7-62c3021c433a</guid>
      <link>http://blog.moertel.com/articles/2007/08/15/a-bright-future-security-and-modern-type-systems#comment-530</link>
    </item>
    <item>
      <title>"A bright future: security and modern type systems" by Paul Johnson</title>
      <description>&lt;p&gt;Your footnote about a &amp;#8220;crude single-purpose type system&amp;#8221; accurately describes the Perl tainting approach.&lt;/p&gt;</description>
      <pubDate>Thu, 16 Aug 2007 03:43:44 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:638bce67-8b26-4424-bd32-91c52add1981</guid>
      <link>http://blog.moertel.com/articles/2007/08/15/a-bright-future-security-and-modern-type-systems#comment-529</link>
    </item>
    <item>
      <title>"A bright future: security and modern type systems" by Tom Moertel</title>
      <description>&lt;p&gt;@Thomas H. Ptacek:  First, thanks for your comments.  I can see that you&amp;#8217;re passionate about this topic, so I appreciate your taking the time to share your views.&lt;/p&gt;


	&lt;p&gt;Second, I would ask that you please give my article a closer read because, for example, you seemed not to notice the first paragraph, which acknowledges the &amp;#8220;almost universal&amp;#8221; solutions to SQL injection that you mentioned and points out that they aren&amp;#8217;t getting the job done: &lt;em&gt;lots&lt;/em&gt; of sites are still vulnerable.  It&amp;#8217;s this state of affairs that the article attempts to address.&lt;/p&gt;


	&lt;p&gt;Third, I don&amp;#8217;t think you understand what my &amp;#8220;evil&amp;#8221; safe-strings solution actually does.  You wrote that the right solution is to &amp;#8220;segregate user inputs from parseable code,&amp;#8221; but that&amp;#8217;s what my solution does.  My solution performs the segregation at the type level, however, and if you&amp;#8217;re not expecting it, I guess it&amp;#8217;s easy to overlook.  But that&amp;#8217;s how it works.  (And if you think it&amp;#8217;s anything like mod_security, you&amp;#8217;re really off the mark.  For starters, mod_security doesn&amp;#8217;t provide its assurances at compile time.)&lt;/p&gt;


	&lt;p&gt;Thanks again for your comments.  If you take a closer look at my safe-strings library, I would be interested in your feedback.&lt;/p&gt;


	&lt;p&gt;Cheers,&lt;br /&gt;
Tom&lt;/p&gt;</description>
      <pubDate>Thu, 16 Aug 2007 01:29:34 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:6af2426d-7f8a-4bcc-a138-d4990fff4a16</guid>
      <link>http://blog.moertel.com/articles/2007/08/15/a-bright-future-security-and-modern-type-systems#comment-528</link>
    </item>
    <item>
      <title>"A bright future: security and modern type systems" by Thomas H. Ptacek</title>
      <description>&lt;p&gt;After reading this article, I&amp;#8217;m not sure you know what an SQL Injection vulnerability is.&lt;/p&gt;


	&lt;p&gt;You can construe a myriad of coding errors as &amp;#8220;type system failures&amp;#8221;, for sufficiently broad definitions of &amp;#8220;type system&amp;#8221;. But in conventional (read: non-Haskell) web applications, SQL injection isn&amp;#8217;t caused by inadequate string types.&lt;/p&gt;


	&lt;p&gt;SQL Injection happens because web application developers cons up SQL queries on the fly, inside of strings, into which user input is interpolated, and then present them to a database hoping for the best. The solution to this problem isn&amp;#8217;t a smarter string type that can ensure safer interpolation; the solution, almost universally adopted by the industry, is to seperate code from data using prepared/parameterized statements or stored procedures, either of which parse statements seperately from user-controlled data.&lt;/p&gt;


	&lt;p&gt;Your solution to this problem is evil. Instead of segregating user inputs from parseable code, you try to employ transparent escaping to ensure safe interpolation. &amp;#8220;A-ha!&amp;#8221;, you say, &amp;#8220;there is no way in my type system to evade my escaping, ergo my strings are safe&amp;#8221;. Until you realize you forgot about internationalization, or whatever else Jeremiah Grossman or Alex Stamos thinks up next year.&lt;/p&gt;


	&lt;p&gt;This is simply mod_security, provided at a library level. As a general approach, it has a poor track record.&lt;/p&gt;</description>
      <pubDate>Wed, 15 Aug 2007 22:52:05 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:9b234b2d-a859-4fd9-ac40-2635cb28c779</guid>
      <link>http://blog.moertel.com/articles/2007/08/15/a-bright-future-security-and-modern-type-systems#comment-527</link>
    </item>
    <item>
      <title>"A bright future: security and modern type systems" by Joseph Huang</title>
      <description>&lt;p&gt;You could use randomized testing like quickcheck. Of course, this wouldn&amp;#8217;t ensure that all holes were found.&lt;/p&gt;</description>
      <pubDate>Wed, 15 Aug 2007 21:37:33 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:ca6997d5-a26f-41f8-8417-af01cb957f9a</guid>
      <link>http://blog.moertel.com/articles/2007/08/15/a-bright-future-security-and-modern-type-systems#comment-526</link>
    </item>
    <item>
      <title>"A bright future: security and modern type systems" by mrd</title>
      <description>&lt;p&gt;&lt;a href="http://www.cs.cmu.edu/~fp/papers/manifest07.pdf"&gt;http://www.cs.cmu.edu/~fp/papers/manifest07.pdf&lt;/a&gt;&lt;/p&gt;


	&lt;p&gt;another recent proposal.&lt;/p&gt;</description>
      <pubDate>Wed, 15 Aug 2007 19:53:55 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:49b56b84-e611-430e-a26f-ec3a9feb663f</guid>
      <link>http://blog.moertel.com/articles/2007/08/15/a-bright-future-security-and-modern-type-systems#comment-525</link>
    </item>
  </channel>
</rss>
