perlsec.html
来自「perl教程」· HTML 代码 · 共 526 行 · 第 1/3 页
HTML
526 行
<p>If you try to do something insecure, you will get a fatal error saying
something like "Insecure dependency" or "Insecure $ENV{PATH}".</p>
<p>The exception to the principle of "one tainted value taints the whole
expression" is with the ternary conditional operator <code>?:</code>. Since code
with a ternary conditional</p>
<pre>
<span class="variable">$result</span> <span class="operator">=</span> <span class="variable">$tainted_value</span> <span class="operator">?</span> <span class="string">"Untainted"</span> <span class="operator">:</span> <span class="string">"Also untainted"</span><span class="operator">;</span>
</pre>
<p>is effectively</p>
<pre>
<span class="keyword">if</span> <span class="operator">(</span> <span class="variable">$tainted_value</span> <span class="operator">)</span> <span class="operator">{</span>
<span class="variable">$result</span> <span class="operator">=</span> <span class="string">"Untainted"</span><span class="operator">;</span>
<span class="operator">}</span> <span class="keyword">else</span> <span class="operator">{</span>
<span class="variable">$result</span> <span class="operator">=</span> <span class="string">"Also untainted"</span><span class="operator">;</span>
<span class="operator">}</span>
</pre>
<p>it doesn't make sense for <code>$result</code> to be tainted.</p>
<p>
</p>
<h2><a name="laundering_and_detecting_tainted_data">Laundering and Detecting Tainted Data</a></h2>
<p>To test whether a variable contains tainted data, and whose use would
thus trigger an "Insecure dependency" message, you can use the
<code>tainted()</code> function of the Scalar::Util module, available in your
nearby CPAN mirror, and included in Perl starting from the release 5.8.0.
Or you may be able to use the following <code>is_tainted()</code> function.</p>
<pre>
<span class="keyword">sub</span><span class="variable"> is_tainted </span><span class="operator">{</span>
<span class="keyword">return</span> <span class="operator">!</span> <span class="keyword">eval</span> <span class="operator">{</span> <span class="keyword">eval</span><span class="operator">(</span><span class="string">"#"</span> <span class="operator">.</span> <span class="keyword">substr</span><span class="operator">(</span><span class="keyword">join</span><span class="operator">(</span><span class="string">""</span><span class="operator">,</span> <span class="variable">@_</span><span class="operator">),</span> <span class="number">0</span><span class="operator">,</span> <span class="number">0</span><span class="operator">));</span> <span class="number">1</span> <span class="operator">};</span>
<span class="operator">}</span>
</pre>
<p>This function makes use of the fact that the presence of tainted data
anywhere within an expression renders the entire expression tainted. It
would be inefficient for every operator to test every argument for
taintedness. Instead, the slightly more efficient and conservative
approach is used that if any tainted value has been accessed within the
same expression, the whole expression is considered tainted.</p>
<p>But testing for taintedness gets you only so far. Sometimes you have just
to clear your data's taintedness. Values may be untainted by using them
as keys in a hash; otherwise the only way to bypass the tainting
mechanism is by referencing subpatterns from a regular expression match.
Perl presumes that if you reference a substring using $1, $2, etc., that
you knew what you were doing when you wrote the pattern. That means using
a bit of thought--don't just blindly untaint anything, or you defeat the
entire mechanism. It's better to verify that the variable has only good
characters (for certain values of "good") rather than checking whether it
has any bad characters. That's because it's far too easy to miss bad
characters that you never thought of.</p>
<p>Here's a test to make sure that the data contains nothing but "word"
characters (alphabetics, numerics, and underscores), a hyphen, an at sign,
or a dot.</p>
<pre>
<span class="keyword">if</span> <span class="operator">(</span><span class="variable">$data</span> <span class="operator">=~</span> <span class="regex">/^([-\@\w.]+)$/</span><span class="operator">)</span> <span class="operator">{</span>
<span class="variable">$data</span> <span class="operator">=</span> <span class="variable">$1</span><span class="operator">;</span> <span class="comment"># $data now untainted</span>
<span class="operator">}</span> <span class="keyword">else</span> <span class="operator">{</span>
<span class="keyword">die</span> <span class="string">"Bad data in '$data'"</span><span class="operator">;</span> <span class="comment"># log this somewhere</span>
<span class="operator">}</span>
</pre>
<p>This is fairly secure because <code>/\w+/</code> doesn't normally match shell
metacharacters, nor are dot, dash, or at going to mean something special
to the shell. Use of <code>/.+/</code> would have been insecure in theory because
it lets everything through, but Perl doesn't check for that. The lesson
is that when untainting, you must be exceedingly careful with your patterns.
Laundering data using regular expression is the <em>only</em> mechanism for
untainting dirty data, unless you use the strategy detailed below to fork
a child of lesser privilege.</p>
<p>The example does not untaint <code>$data</code> if <code>use locale</code> is in effect,
because the characters matched by <code>\w</code> are determined by the locale.
Perl considers that locale definitions are untrustworthy because they
contain data from outside the program. If you are writing a
locale-aware program, and want to launder data with a regular expression
containing <code>\w</code>, put <code>no locale</code> ahead of the expression in the same
block. See <a href="../../lib/Pod/perllocale.html#security">SECURITY in the perllocale manpage</a> for further discussion and examples.</p>
<p>
</p>
<h2><a name="switches_on_the____line">Switches On the "#!" Line</a></h2>
<p>When you make a script executable, in order to make it usable as a
command, the system will pass switches to perl from the script's #!
line. Perl checks that any command line switches given to a setuid
(or setgid) script actually match the ones set on the #! line. Some
Unix and Unix-like environments impose a one-switch limit on the #!
line, so you may need to use something like <code>-wU</code> instead of <code>-w -U</code>
under such systems. (This issue should arise only in Unix or
Unix-like environments that support #! and setuid or setgid scripts.)</p>
<p>
</p>
<h2><a name="taint_mode_and__inc">Taint mode and @INC</a></h2>
<p>When the taint mode (<a href="../../lib/Pod/perlrun.html#item__2dt"><code>-T</code></a>) is in effect, the "." directory is removed
from <a href="../../lib/Pod/perlvar.html#item__inc"><code>@INC</code></a>, and the environment variables <a href="../../lib/Pod/perlrun.html#item_perl5lib"><code>PERL5LIB</code></a> and <a href="../../lib/Pod/perlrun.html#item_perllib"><code>PERLLIB</code></a>
are ignored by Perl. You can still adjust <a href="../../lib/Pod/perlvar.html#item__inc"><code>@INC</code></a> from outside the
program by using the <code>-I</code> command line option as explained in
<a href="../../lib/Pod/perlrun.html">the perlrun manpage</a>. The two environment variables are ignored because
they are obscured, and a user running a program could be unaware that
they are set, whereas the <code>-I</code> option is clearly visible and
therefore permitted.</p>
<p>Another way to modify <a href="../../lib/Pod/perlvar.html#item__inc"><code>@INC</code></a> without modifying the program, is to use
the <code>lib</code> pragma, e.g.:</p>
<pre>
perl -Mlib=/foo program</pre>
<p>The benefit of using <code>-Mlib=/foo</code> over <code>-I/foo</code>, is that the former
will automagically remove any duplicated directories, while the later
will not.</p>
<p>Note that if a tainted string is added to <a href="../../lib/Pod/perlvar.html#item__inc"><code>@INC</code></a>, the following
problem will be reported:</p>
<pre>
Insecure dependency in require while running with -T switch</pre>
<p>
</p>
<h2><a name="cleaning_up_your_path">Cleaning Up Your Path</a></h2>
<p>For "Insecure <code>$ENV{PATH}</code>" messages, you need to set <code>$ENV{'PATH'}</code> to
a known value, and each directory in the path must be absolute and
non-writable by others than its owner and group. You may be surprised to
get this message even if the pathname to your executable is fully
qualified. This is <em>not</em> generated because you didn't supply a full path
to the program; instead, it's generated because you never set your PATH
environment variable, or you didn't set it to something that was safe.
Because Perl can't guarantee that the executable in question isn't itself
going to turn around and execute some other program that is dependent on
your PATH, it makes sure you set the PATH.</p>
<p>The PATH isn't the only environment variable which can cause problems.
Because some shells may use the variables IFS, CDPATH, ENV, and
BASH_ENV, Perl checks that those are either empty or untainted when
starting subprocesses. You may wish to add something like this to your
setid and taint-checking scripts.</p>
<pre>
<span class="keyword">delete</span> <span class="variable">@ENV</span><span class="operator">{</span><span class="string">qw(IFS CDPATH ENV BASH_ENV)</span><span class="operator">}</span><span class="operator">;</span> <span class="comment"># Make %ENV safer</span>
</pre>
<p>It's also possible to get into trouble with other operations that don't
care whether they use tainted values. Make judicious use of the file
tests in dealing with any user-supplied filenames. When possible, do
opens and such <strong>after</strong> properly dropping any special user (or group!)
privileges. Perl doesn't prevent you from opening tainted filenames for reading,
so be careful what you print out. The tainting mechanism is intended to
prevent stupid mistakes, not to remove the need for thought.</p>
<p>Perl does not call the shell to expand wild cards when you pass <a href="../../lib/Pod/perlfunc.html#item_system"><code>system</code></a>
and <a href="../../lib/Pod/perlfunc.html#item_exec"><code>exec</code></a> explicit parameter lists instead of strings with possible shell
wildcards in them. Unfortunately, the <a href="../../lib/Pod/perlfunc.html#item_open"><code>open</code></a>, <a href="../../lib/Pod/perlfunc.html#item_glob"><code>glob</code></a>, and
backtick functions provide no such alternate calling convention, so more
subterfuge will be required.</p>
<p>Perl provides a reasonably safe way to open a file or pipe from a setuid
or setgid program: just create a child process with reduced privilege who
does the dirty work for you. First, fork a child using the special
<a href="../../lib/Pod/perlfunc.html#item_open"><code>open</code></a> syntax that connects the parent and child by a pipe. Now the
child resets its ID set and any other per-process attributes, like
environment variables, umasks, current working directories, back to the
originals or known safe values. Then the child process, which no longer
has any special permissions, does the <a href="../../lib/Pod/perlfunc.html#item_open"><code>open</code></a> or other system call.
Finally, the child passes the data it managed to access back to the
parent. Because the file or pipe was opened in the child while running
under less privilege than the parent, it's not apt to be tricked into
doing something it shouldn't.</p>
<p>Here's a way to do backticks reasonably safely. Notice how the <a href="../../lib/Pod/perlfunc.html#item_exec"><code>exec</code></a> is
not called with a string that the shell could expand. This is by far the
best way to call something that might be subjected to shell escapes: just
never call the shell at all.</p>
<pre>
<span class="keyword">use</span> <span class="variable">English</span> <span class="string">'-no_match_vars'</span><span class="operator">;</span>
<span class="keyword">die</span> <span class="string">"Can't fork: $!"</span> <span class="keyword">unless</span> <span class="keyword">defined</span><span class="operator">(</span><span class="variable">$pid</span> <span class="operator">=</span> <span class="keyword">open</span><span class="operator">(</span><span class="variable">KID</span><span class="operator">,</span> <span class="string">"-|"</span><span class="operator">));</span>
<span class="keyword">if</span> <span class="operator">(</span><span class="variable">$pid</span><span class="operator">)</span> <span class="operator">{</span> <span class="comment"># parent</span>
<span class="keyword">while</span> <span class="operator">(<</span><span class="variable">KID</span><span class="operator">>)</span> <span class="operator">{</span>
<span class="comment"># do something</span>
<span class="operator">}</span>
<span class="keyword">close</span> <span class="variable">KID</span><span class="operator">;</span>
<span class="operator">}</span> <span class="keyword">else</span> <span class="operator">{</span>
<span class="keyword">my</span> <span class="variable">@temp</span> <span class="operator">=</span> <span class="operator">(</span><span class="variable">$EUID</span><span class="operator">,</span> <span class="variable">$EGID</span><span class="operator">);</span>
<span class="keyword">my</span> <span class="variable">$orig_uid</span> <span class="operator">=</span> <span class="variable">$UID</span><span class="operator">;</span>
<span class="keyword">my</span> <span class="variable">$orig_gid</span> <span class="operator">=</span> <span class="variable">$GID</span><span class="operator">;</span>
<span class="variable">$EUID</span> <span class="operator">=</span> <span class="variable">$UID</span><span class="operator">;</span>
<span class="variable">$EGID</span> <span class="operator">=</span> <span class="variable">$GID</span><span class="operator">;</span>
<span class="comment"># Drop privileges</span>
<span class="variable">$UID</span> <span class="operator">=</span> <span class="variable">$orig_uid</span><span class="operator">;</span>
<span class="variable">$GID</span> <span class="operator">=</span> <span class="variable">$orig_gid</span><span class="operator">;</span>
<span class="comment"># Make sure privs are really gone</span>
<span class="operator">(</span><span class="variable">$EUID</span><span class="operator">,</span> <span class="variable">$EGID</span><span class="operator">)</span> <span class="operator">=</span> <span class="variable">@temp</span><span class="operator">;</span>
<span class="keyword">die</span> <span class="string">"Can't drop privileges"</span>
<span class="keyword">unless</span> <span class="variable">$UID</span> <span class="operator">==</span> <span class="variable">$EUID</span> <span class="operator">&&</span> <span class="variable">$GID</span> <span class="keyword">eq</span> <span class="variable">$EGID</span><span class="operator">;</span>
<span class="variable">$ENV</span><span class="operator">{</span><span class="string">PATH</span><span class="operator">}</span> <span class="operator">=</span> <span class="string">"/bin:/usr/bin"</span><span class="operator">;</span> <span class="comment"># Minimal PATH.</span>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?