This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
perldelta for 7ef30830/#114018
[perl5.git] / pod / perlsec.pod
index 2fb6877..634024d 100644 (file)
@@ -12,6 +12,18 @@ with fewer hidden snags.  Additionally, because the language has more
 builtin functionality, it can rely less upon external (and possibly
 untrustworthy) programs to accomplish its purposes.
 
+=head1 SECURITY VULNERABILITY CONTACT INFORMATION
+
+If you believe you have found a security vulnerability in Perl, please email
+perl5-security-report@perl.org with details.  This points to a closed
+subscription, unarchived mailing list.  Please only use this address for
+security issues in the Perl core, not for modules independently distributed on
+CPAN.
+
+=head1 SECURITY MECHANISMS AND CONCERNS
+
+=head2 Taint mode
+
 Perl automatically enables a set of special security checks, called I<taint
 mode>, when it detects its program running with differing real and effective
 user or group IDs.  The setuid bit in Unix permissions is mode 04000, the
@@ -63,6 +75,10 @@ you carefully limit what these symbolic values are, people are able
 to call functions B<outside> your Perl code, such as POSIX::system,
 in which case they are able to run arbitrary external code.
 
+=item *
+
+Hash keys are B<never> tainted.
+
 =back
 
 For efficiency reasons, Perl takes a conservative view of
@@ -72,7 +88,7 @@ of the subexpression is not itself affected by the tainted data.
 
 Because taintedness is associated with each scalar value, some
 elements of an array or hash can be tainted and others not.
-The keys of a hash are never tainted.
+The keys of a hash are B<never> tainted.
 
 For example:
 
@@ -118,10 +134,8 @@ For example:
     @files = <*.c>;            # insecure (uses readdir() or similar)
     @files = glob('*.c');      # insecure (uses readdir() or similar)
 
-    # In Perl releases older than 5.6.0 the <*.c> and glob('*.c') would
-    # have used an external program to do the filename expansion; but in
-    # either case the result is tainted since the list of filenames comes
-    # from outside of the program.
+    # In either case, the results of glob are tainted, since the list of
+    # filenames comes from outside of the program.
 
     $bad = ($arg, 23);         # $bad will be tainted
     $arg, `true`;              # Insecure (although it isn't really)
@@ -154,6 +168,7 @@ nearby CPAN mirror, and included in Perl starting from the release 5.8.0.
 Or you may be able to use the following C<is_tainted()> function.
 
     sub is_tainted {
+        local $@;   # Don't pollute caller's value.
         return ! eval { eval("#" . substr(join("", @_), 0, 0)); 1 };
     }
 
@@ -234,18 +249,23 @@ The benefit of using C<-Mlib=/foo> over C<-I/foo>, is that the former
 will automagically remove any duplicated directories, while the later
 will not.
 
+Note that if a tainted string is added to C<@INC>, the following
+problem will be reported:
+
+  Insecure dependency in require while running with -T switch
+
 =head2 Cleaning Up Your Path
 
-For "Insecure C<$ENV{PATH}>" messages, you need to set C<$ENV{'PATH'}> to a
-known value, and each directory in the path must be 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 I<not>
-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.
+For "Insecure C<$ENV{PATH}>" messages, you need to set C<$ENV{'PATH'}> 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 I<not> 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.
 
 The PATH isn't the only environment variable which can cause problems.
 Because some shells may use the variables IFS, CDPATH, ENV, and
@@ -325,10 +345,15 @@ programs launched on someone else's behalf, like CGI programs.
 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
+run this."  For that kind of safety, you might want to 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.
+are trapped and namespace access is carefully controlled.  Safe should
+not be considered bullet-proof, though: it will not prevent the foreign
+code to set up infinite loops, allocate gigabytes of memory, or even
+abusing perl bugs to make the host interpreter crash or behave in
+unpredictable ways. In any case it's better avoided completely if you're
+really concerned about security.
 
 =head2 Security Bugs
 
@@ -343,11 +368,7 @@ changed, especially if you have symbolic links on your system.
 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 F<suidperl> that is automatically
-invoked for you if it's needed.
+Alternately, it can simply ignore the set-id bits on scripts.
 
 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
@@ -378,9 +399,6 @@ 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.
 
-Prior to release 5.6.1 of Perl, bugs in the code of F<suidperl> could
-introduce a security hole.
-
 =head2 Protecting Your Programs
 
 There are a number of ways to hide the source to your Perl programs,
@@ -411,11 +429,11 @@ code, but none can definitively conceal it (this is true of every
 language, not just Perl).
 
 If you're concerned about people profiting from your code, then the
-bottom line is that nothing but a restrictive licence will give you
+bottom line is that nothing but a restrictive license 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
+blah."  You should see a lawyer to be sure your license's wording will
 stand up in court.
 
 =head2 Unicode
@@ -448,13 +466,16 @@ the hash function is randomly perturbed by a pseudorandom seed which
 makes generating such naughty hash keys harder.
 See L<perlrun/PERL_HASH_SEED> for more information.
 
-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).
+In Perl 5.8.1 the random perturbation was done by default, but as of
+5.8.2 it is only used on individual hashes if the internals detect the
+insertion of pathological data. If one wants for some reason emulate the
+old behaviour (and expose oneself to DoS attacks) one can set the
+environment variable PERL_HASH_SEED to zero to disable the protection
+(or any other integer to force a known perturbation, rather than random). 
+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 longer identical).
 
 B<Perl has never guaranteed any ordering of the hash keys>, and the
 ordering has already changed several times during the lifetime of
@@ -471,9 +492,9 @@ Algorithm::FastPermute), or for any cryptographic applications.
 
 =item *
 
-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 expressions - Perl's regular expression engine is so called NFA
+(Non-deterministic 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
@@ -484,15 +505,14 @@ Perl running out of memory.
 
 Sorting - the quicksort algorithm used in Perls before 5.8.0 to
 implement the sort() 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.
+so that it consumes a lot of time.  Starting from Perl 5.8.0 a different
+sorting algorithm, mergesort, is used by default.  Mergesort cannot
+misbehave on any input.
 
 =back
 
 See L<http://www.cs.rice.edu/~scrosby/hash/> for more information,
-and any computer science text book on the algorithmic complexity.
+and any computer science textbook on algorithmic complexity.
 
 =head1 SEE ALSO