perlsec.html
来自「perl教程」· HTML 代码 · 共 526 行 · 第 1/3 页
HTML
526 行
<span class="comment"># Consider sanitizing the environment even more.</span>
<span class="keyword">exec</span> <span class="string">'myprog'</span><span class="operator">,</span> <span class="string">'arg1'</span><span class="operator">,</span> <span class="string">'arg2'</span>
<span class="keyword">or</span> <span class="keyword">die</span> <span class="string">"can't exec myprog: $!"</span><span class="operator">;</span>
<span class="operator">}</span>
</pre>
<p>A similar strategy would work for wildcard expansion via <a href="../../lib/Pod/perlfunc.html#item_glob"><code>glob</code></a>, although
you can use <a href="../../lib/Pod/perlfunc.html#item_readdir"><code>readdir</code></a> instead.</p>
<p>Taint checking is most useful when although you trust yourself not to have
written a program to give away the farm, you don't necessarily trust those
who end up using it not to try to trick it into doing something bad. This
is the kind of security checking that's useful for set-id programs and
programs launched on someone else's behalf, like CGI programs.</p>
<p>This is quite different, however, from not even trusting the writer of the
code not to try to do something evil. That's the kind of trust needed
when someone hands you a program you've never seen before and says, "Here,
run this." For that kind of safety, check out the Safe module,
included standard in the Perl distribution. This module allows the
programmer to set up special compartments in which all system operations
are trapped and namespace access is carefully controlled.</p>
<p>
</p>
<h2><a name="security_bugs">Security Bugs</a></h2>
<p>Beyond the obvious problems that stem from giving special privileges to
systems as flexible as scripts, on many versions of Unix, set-id scripts
are inherently insecure right from the start. The problem is a race
condition in the kernel. Between the time the kernel opens the file to
see which interpreter to run and when the (now-set-id) interpreter turns
around and reopens the file to interpret it, the file in question may have
changed, especially if you have symbolic links on your system.</p>
<p>Fortunately, sometimes this kernel "feature" can be disabled.
Unfortunately, there are two ways to disable it. The system can simply
outlaw scripts with any set-id bit set, which doesn't help much.
Alternately, it can simply ignore the set-id bits on scripts. If the
latter is true, Perl can emulate the setuid and setgid mechanism when it
notices the otherwise useless setuid/gid bits on Perl scripts. It does
this via a special executable called <em>suidperl</em> that is automatically
invoked for you if it's needed.</p>
<p>However, if the kernel set-id script feature isn't disabled, Perl will
complain loudly that your set-id script is insecure. You'll need to
either disable the kernel set-id script feature, or put a C wrapper around
the script. A C wrapper is just a compiled program that does nothing
except call your Perl program. Compiled programs are not subject to the
kernel bug that plagues set-id scripts. Here's a simple wrapper, written
in C:</p>
<pre>
<span class="comment">#define REAL_PATH "/path/to/script"</span>
<span class="variable">main</span><span class="operator">(</span><span class="variable">ac</span><span class="operator">,</span> <span class="variable">av</span><span class="operator">)</span>
<span class="variable">char</span> <span class="operator">**</span><span class="variable">av</span><span class="operator">;</span>
<span class="operator">{</span>
<span class="variable">execv</span><span class="operator">(</span><span class="variable">REAL_PATH</span><span class="operator">,</span> <span class="variable">av</span><span class="operator">);</span>
<span class="operator">}</span>
</pre>
<p>Compile this wrapper into a binary executable and then make <em>it</em> rather
than your script setuid or setgid.</p>
<p>In recent years, vendors have begun to supply systems free of this
inherent security bug. On such systems, when the kernel passes the name
of the set-id script to open to the interpreter, rather than using a
pathname subject to meddling, it instead passes <em>/dev/fd/3</em>. This is a
special file already opened on the script, so that there can be no race
condition for evil scripts to exploit. On these systems, Perl should be
compiled with <code>-DSETUID_SCRIPTS_ARE_SECURE_NOW</code>. The <em>Configure</em>
program that builds Perl tries to figure this out for itself, so you
should never have to specify this yourself. Most modern releases of
SysVr4 and BSD 4.4 use this approach to avoid the kernel race condition.</p>
<p>Prior to release 5.6.1 of Perl, bugs in the code of <em>suidperl</em> could
introduce a security hole.</p>
<p>
</p>
<h2><a name="protecting_your_programs">Protecting Your Programs</a></h2>
<p>There are a number of ways to hide the source to your Perl programs,
with varying levels of "security".</p>
<p>First of all, however, you <em>can't</em> take away read permission, because
the source code has to be readable in order to be compiled and
interpreted. (That doesn't mean that a CGI script's source is
readable by people on the web, though.) So you have to leave the
permissions at the socially friendly 0755 level. This lets
people on your local system only see your source.</p>
<p>Some people mistakenly regard this as a security problem. If your program does
insecure things, and relies on people not knowing how to exploit those
insecurities, it is not secure. It is often possible for someone to
determine the insecure things and exploit them without viewing the
source. Security through obscurity, the name for hiding your bugs
instead of fixing them, is little security indeed.</p>
<p>You can try using encryption via source filters (Filter::* from CPAN,
or Filter::Util::Call and Filter::Simple since Perl 5.8).
But crackers might be able to decrypt it. You can try using the byte
code compiler and interpreter described below, but crackers might be
able to de-compile it. You can try using the native-code compiler
described below, but crackers might be able to disassemble it. These
pose varying degrees of difficulty to people wanting to get at your
code, but none can definitively conceal it (this is true of every
language, not just Perl).</p>
<p>If you're concerned about people profiting from your code, then the
bottom line is that nothing but a restrictive licence will give you
legal security. License your software and pepper it with threatening
statements like "This is unpublished proprietary software of XYZ Corp.
Your access to it does not give you permission to use it blah blah
blah." You should see a lawyer to be sure your licence's wording will
stand up in court.</p>
<p>
</p>
<h2><a name="unicode">Unicode</a></h2>
<p>Unicode is a new and complex technology and one may easily overlook
certain security pitfalls. See <a href="../../lib/Pod/perluniintro.html">the perluniintro manpage</a> for an overview and
<a href="../../lib/Pod/perlunicode.html">the perlunicode manpage</a> for details, and <a href="../../lib/Pod/perlunicode.html#security_implications_of_unicode">Security Implications of Unicode in the perlunicode manpage</a> for security implications in particular.</p>
<p>
</p>
<h2><a name="algorithmic_complexity_attacks">Algorithmic Complexity Attacks</a></h2>
<p>Certain internal algorithms used in the implementation of Perl can
be attacked by choosing the input carefully to consume large amounts
of either time or space or both. This can lead into the so-called
<em>Denial of Service</em> (DoS) attacks.</p>
<ul>
<li>
<p>Hash Function - the algorithm used to "order" hash elements has been
changed several times during the development of Perl, mainly to be
reasonably fast. In Perl 5.8.1 also the security aspect was taken
into account.</p>
<p>In Perls before 5.8.1 one could rather easily generate data that as
hash keys would cause Perl to consume large amounts of time because
internal structure of hashes would badly degenerate. In Perl 5.8.1
the hash function is randomly perturbed by a pseudorandom seed which
makes generating such naughty hash keys harder.
See <a href="../../lib/Pod/perlrun.html#perl_hash_seed">PERL_HASH_SEED in the perlrun manpage</a> for more information.</p>
<p>The random perturbation is done by default but if one wants for some
reason emulate the old behaviour one can set the environment variable
PERL_HASH_SEED to zero (or any other integer). One possible reason
for wanting to emulate the old behaviour is that in the new behaviour
consecutive runs of Perl will order hash keys differently, which may
confuse some applications (like Data::Dumper: the outputs of two
different runs are no more identical).</p>
<p><strong>Perl has never guaranteed any ordering of the hash keys</strong>, and the
ordering has already changed several times during the lifetime of
Perl 5. Also, the ordering of hash keys has always been, and
continues to be, affected by the insertion order.</p>
<p>Also note that while the order of the hash elements might be
randomised, this "pseudoordering" should <strong>not</strong> be used for
applications like shuffling a list randomly (use List::Util::shuffle()
for that, see <a href="../../lib/List/Util.html">the List::Util manpage</a>, a standard core module since Perl 5.8.0;
or the CPAN module Algorithm::Numerical::Shuffle), or for generating
permutations (use e.g. the CPAN modules Algorithm::Permute or
Algorithm::FastPermute), or for any cryptographic applications.</p>
</li>
<li>
<p>Regular expressions - Perl's regular expression engine is so called
NFA (Non-Finite Automaton), which among other things means that it can
rather easily consume large amounts of both time and space if the
regular expression may match in several ways. Careful crafting of the
regular expressions can help but quite often there really isn't much
one can do (the book "Mastering Regular Expressions" is required
reading, see <a href="../../lib/Pod/perlfaq2.html">the perlfaq2 manpage</a>). Running out of space manifests itself by
Perl running out of memory.</p>
</li>
<li>
<p>Sorting - the quicksort algorithm used in Perls before 5.8.0 to
implement the <a href="../../lib/Pod/perlfunc.html#item_sort"><code>sort()</code></a> function is very easy to trick into misbehaving
so that it consumes a lot of time. Nothing more is required than
resorting a list already sorted. Starting from Perl 5.8.0 a different
sorting algorithm, mergesort, is used. Mergesort is insensitive to
its input data, so it cannot be similarly fooled.</p>
</li>
</ul>
<p>See <a href="http://www.cs.rice.edu/~scrosby/hash/">http://www.cs.rice.edu/~scrosby/hash/</a> for more information,
and any computer science text book on the algorithmic complexity.</p>
<p>
</p>
<hr />
<h1><a name="see_also">SEE ALSO</a></h1>
<p><a href="../../lib/Pod/perlrun.html">the perlrun manpage</a> for its description of cleaning up environment variables.</p>
</body>
</html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?