Blame SOURCES/perl-5.25.2-Don-t-let-XSLoader-load-relative-paths.patch

276c98
From 08e3451d7b3b714ad63a27f1b9c2a23ee75d15ee Mon Sep 17 00:00:00 2001
276c98
From: Father Chrysostomos <sprout@cpan.org>
276c98
Date: Sat, 2 Jul 2016 22:56:51 -0700
276c98
Subject: [PATCH 1/4] =?UTF-8?q?Don=E2=80=99t=20let=20XSLoader=20load=20rel?=
276c98
 =?UTF-8?q?ative=20paths?=
276c98
MIME-Version: 1.0
276c98
Content-Type: text/plain; charset=UTF-8
276c98
Content-Transfer-Encoding: 8bit
276c98
276c98
[rt.cpan.org #115808]
276c98
276c98
The logic in XSLoader for determining the library goes like this:
276c98
276c98
    my $c = () = split(/::/,$caller,-1);
276c98
    $modlibname =~ s,[\\/][^\\/]+$,, while $c--;    # Q&D basename
276c98
    my $file = "$modlibname/auto/$modpname/$modfname.bundle";
276c98
276c98
(That last line varies by platform.)
276c98
276c98
$caller is the calling package.  $modlibname is the calling file.  It
276c98
removes as many path segments from $modlibname as there are segments
276c98
in $caller.  So if you have Foo/Bar/XS.pm calling XSLoader from the
276c98
Foo::Bar package, the $modlibname will end up containing the path in
276c98
@INC where XS.pm was found, followed by "/Foo".  Usually the fallback
276c98
to Dynaloader::bootstrap_inherit, which does an @INC search, makes
276c98
things Just Work.
276c98
276c98
But if our hypothetical Foo/Bar/XS.pm actually calls
276c98
XSLoader::load from inside a string eval, then path ends up being
276c98
"(eval 1)/auto/Foo/Bar/Bar.bundle".
276c98
276c98
So if someone creates a directory named ‘(eval 1)’ with a naughty
276c98
binary file in it, it will be loaded if a script using Foo::Bar is run
276c98
in the parent directory.
276c98
276c98
This commit makes XSLoader fall back to Dynaloader’s @INC search if
276c98
the calling file has a relative path that is not found in @INC.
276c98
---
276c98
 dist/XSLoader/XSLoader_pm.PL | 25 +++++++++++++++++++++++++
276c98
 dist/XSLoader/t/XSLoader.t   | 27 ++++++++++++++++++++++++++-
276c98
 2 files changed, 51 insertions(+), 1 deletion(-)
276c98
276c98
diff --git a/dist/XSLoader/XSLoader_pm.PL b/dist/XSLoader/XSLoader_pm.PL
276c98
index 8a8852e..749f72d 100644
276c98
--- a/dist/XSLoader/XSLoader_pm.PL
276c98
+++ b/dist/XSLoader/XSLoader_pm.PL
276c98
@@ -91,6 +91,31 @@ print OUT <<'EOT';
276c98
     my $modpname = join('/',@modparts);
276c98
     my $c = () = split(/::/,$caller,-1);
276c98
     $modlibname =~ s,[\\/][^\\/]+$,, while $c--;    # Q&D basename
276c98
+    # Does this look like a relative path?
276c98
+    if ($modlibname !~ m|^[\\/]|) {
276c98
+        # Someone may have a #line directive that changes the file name, or
276c98
+        # may be calling XSLoader::load from inside a string eval.  We cer-
276c98
+        # tainly do not want to go loading some code that is not in @INC,
276c98
+        # as it could be untrusted.
276c98
+        #
276c98
+        # We could just fall back to DynaLoader here, but then the rest of
276c98
+        # this function would go untested in the perl core, since all @INC
276c98
+        # paths are relative during testing.  That would be a time bomb
276c98
+        # waiting to happen, since bugs could be introduced into the code.
276c98
+        #
276c98
+        # So look through @INC to see if $modlibname is in it.  A rela-
276c98
+        # tive $modlibname is not a common occurrence, so this block is
276c98
+        # not hot code.
276c98
+        FOUND: {
276c98
+            for (@INC) {
276c98
+                if ($_ eq $modlibname) {
276c98
+                    last FOUND;
276c98
+                }
276c98
+            }
276c98
+            # Not found.  Fall back to DynaLoader.
276c98
+            goto \&XSLoader::bootstrap_inherit;
276c98
+        }
276c98
+    }
276c98
 EOT
276c98
 
276c98
 my $dl_dlext = quotemeta($Config::Config{'dlext'});
276c98
diff --git a/dist/XSLoader/t/XSLoader.t b/dist/XSLoader/t/XSLoader.t
276c98
index 2ff11fe..1e86faa 100644
276c98
--- a/dist/XSLoader/t/XSLoader.t
276c98
+++ b/dist/XSLoader/t/XSLoader.t
276c98
@@ -33,7 +33,7 @@ my %modules = (
276c98
     'Time::HiRes'=> q| ::can_ok( 'Time::HiRes' => 'usleep'  ) |,  # 5.7.3
276c98
 );
276c98
 
276c98
-plan tests => keys(%modules) * 3 + 9;
276c98
+plan tests => keys(%modules) * 3 + 10;
276c98
 
276c98
 # Try to load the module
276c98
 use_ok( 'XSLoader' );
276c98
@@ -125,3 +125,28 @@ XSLoader::load("Devel::Peek");
276c98
 EOS
276c98
     or ::diag $@;
276c98
 }
276c98
+
276c98
+SKIP: {
276c98
+  skip "File::Path not available", 1
276c98
+    unless eval { require File::Path };
276c98
+  my $name = "phooo$$";
276c98
+  File::Path::make_path("$name/auto/Foo/Bar");
276c98
+  open my $fh,
276c98
+    ">$name/auto/Foo/Bar/Bar.$Config::Config{'dlext'}";
276c98
+  close $fh;
276c98
+  my $fell_back;
276c98
+  local *XSLoader::bootstrap_inherit = sub {
276c98
+    $fell_back++;
276c98
+    # Break out of the calling subs
276c98
+    goto the_test;
276c98
+  };
276c98
+  eval <
276c98
+#line 1 $name
276c98
+package Foo::Bar;
276c98
+XSLoader::load("Foo::Bar");
276c98
+END
276c98
+ the_test:
276c98
+  ok $fell_back,
276c98
+    'XSLoader will not load relative paths based on (caller)[1]';
276c98
+  File::Path::remove_tree($name);
276c98
+}
276c98
-- 
276c98
2.5.5
276c98
276c98
From 5993d6620f29d22b0a72701f4f0fdacff3d25460 Mon Sep 17 00:00:00 2001
276c98
From: Father Chrysostomos <sprout@cpan.org>
276c98
Date: Sat, 2 Jul 2016 22:57:46 -0700
276c98
Subject: [PATCH 2/4] Increase $XSLoader::VERSION to 0.22
276c98
276c98
---
276c98
 dist/XSLoader/XSLoader_pm.PL | 2 +-
276c98
 1 file changed, 1 insertion(+), 1 deletion(-)
276c98
276c98
diff --git a/dist/XSLoader/XSLoader_pm.PL b/dist/XSLoader/XSLoader_pm.PL
276c98
index 749f72d..7e24b83 100644
276c98
--- a/dist/XSLoader/XSLoader_pm.PL
276c98
+++ b/dist/XSLoader/XSLoader_pm.PL
276c98
@@ -11,7 +11,7 @@ print OUT <<'EOT';
276c98
 
276c98
 package XSLoader;
276c98
 
276c98
-$VERSION = "0.21";
276c98
+$VERSION = "0.22";
276c98
 
276c98
 #use strict;
276c98
 
276c98
-- 
276c98
2.5.5
276c98
276c98
From a651dcdf6a9151150dcf0fb6b18849d3e39b0811 Mon Sep 17 00:00:00 2001
276c98
From: Father Chrysostomos <sprout@cpan.org>
276c98
Date: Mon, 4 Jul 2016 08:48:57 -0700
276c98
Subject: [PATCH 3/4] Fix XSLoader to recognize drive letters
276c98
276c98
Commit 08e3451d made XSLoader confirm that the file path it got
276c98
from (caller)[2] was in @INC if it looked like a relative path.
276c98
Not taking drive letters into account, it made that @INC search
276c98
mandatory on Windows and some other systems.  It still worked, but
276c98
was slightly slower.
276c98
---
276c98
 dist/XSLoader/XSLoader_pm.PL | 14 +++++++++++++-
276c98
 1 file changed, 13 insertions(+), 1 deletion(-)
276c98
276c98
diff --git a/dist/XSLoader/XSLoader_pm.PL b/dist/XSLoader/XSLoader_pm.PL
276c98
index 7e24b83..2efb99e 100644
276c98
--- a/dist/XSLoader/XSLoader_pm.PL
276c98
+++ b/dist/XSLoader/XSLoader_pm.PL
276c98
@@ -91,8 +91,20 @@ print OUT <<'EOT';
276c98
     my $modpname = join('/',@modparts);
276c98
     my $c = () = split(/::/,$caller,-1);
276c98
     $modlibname =~ s,[\\/][^\\/]+$,, while $c--;    # Q&D basename
276c98
+EOT
276c98
+
276c98
+my $to_print = <<'EOT';
276c98
     # Does this look like a relative path?
276c98
-    if ($modlibname !~ m|^[\\/]|) {
276c98
+    if ($modlibname !~ m{regexp}) {
276c98
+EOT
276c98
+
276c98
+$to_print =~ s~regexp~
276c98
+    $^O eq 'MSWin32' || $^O eq 'os2' || $^O eq 'cygwin' || $^O eq 'amigaos'
276c98
+        ? '^(?:[A-Za-z]:)?[\\\/]' # Optional drive letter
276c98
+        : '^/'
276c98
+~e;
276c98
+
276c98
+print OUT $to_print, <<'EOT';
276c98
         # Someone may have a #line directive that changes the file name, or
276c98
         # may be calling XSLoader::load from inside a string eval.  We cer-
276c98
         # tainly do not want to go loading some code that is not in @INC,
276c98
-- 
276c98
2.5.5
276c98
276c98
From ae635bbffa4769051671b9832a7472b9d977c198 Mon Sep 17 00:00:00 2001
276c98
From: =?UTF-8?q?S=C3=A9bastien=20Aperghis-Tramoni?= <sebastien@aperghis.net>
276c98
Date: Tue, 5 Jul 2016 14:53:08 -0700
276c98
Subject: [PATCH 4/4] Synchronize blead with CPAN XSLoader 0.22
276c98
276c98
---
276c98
 dist/XSLoader/XSLoader_pm.PL | 2 +-
276c98
 dist/XSLoader/t/XSLoader.t   | 4 ++--
276c98
 2 files changed, 3 insertions(+), 3 deletions(-)
276c98
276c98
diff --git a/dist/XSLoader/XSLoader_pm.PL b/dist/XSLoader/XSLoader_pm.PL
276c98
index 2efb99e..09f9d4b 100644
276c98
--- a/dist/XSLoader/XSLoader_pm.PL
276c98
+++ b/dist/XSLoader/XSLoader_pm.PL
276c98
@@ -255,7 +255,7 @@ XSLoader - Dynamically load C libraries into Perl code
276c98
 
276c98
 =head1 VERSION
276c98
 
276c98
-Version 0.17
276c98
+Version 0.22
276c98
 
276c98
 =head1 SYNOPSIS
276c98
 
276c98
diff --git a/dist/XSLoader/t/XSLoader.t b/dist/XSLoader/t/XSLoader.t
276c98
index 1e86faa..d3538b8 100644
276c98
--- a/dist/XSLoader/t/XSLoader.t
276c98
+++ b/dist/XSLoader/t/XSLoader.t
276c98
@@ -130,7 +130,7 @@ SKIP: {
276c98
   skip "File::Path not available", 1
276c98
     unless eval { require File::Path };
276c98
   my $name = "phooo$$";
276c98
-  File::Path::make_path("$name/auto/Foo/Bar");
276c98
+  File::Path::mkpath("$name/auto/Foo/Bar");
276c98
   open my $fh,
276c98
     ">$name/auto/Foo/Bar/Bar.$Config::Config{'dlext'}";
276c98
   close $fh;
276c98
@@ -148,5 +148,5 @@ END
276c98
  the_test:
276c98
   ok $fell_back,
276c98
     'XSLoader will not load relative paths based on (caller)[1]';
276c98
-  File::Path::remove_tree($name);
276c98
+  File::Path::rmtree($name);
276c98
 }
276c98
-- 
276c98
2.5.5
276c98