b8876f
From 9a4826e0881f8c5498a0fd5f24ed2a0fefb771b7 Mon Sep 17 00:00:00 2001
b8876f
From: Tony Cook <tony@develop-help.com>
b8876f
Date: Thu, 2 Nov 2017 20:18:56 +0000
b8876f
Subject: [PATCH] (perl #131895) fail stat on names with \0 embedded
b8876f
MIME-Version: 1.0
b8876f
Content-Type: text/plain; charset=UTF-8
b8876f
Content-Transfer-Encoding: 8bit
b8876f
b8876f
Also lstat() and the file test ops.
b8876f
b8876f
Petr Písař: Port to 5.24.3.
b8876f
b8876f
Signed-off-by: Petr Písař <ppisar@redhat.com>
b8876f
---
b8876f
 doio.c                | 21 ++++++++++++++++-----
b8876f
 pp_sys.c              | 29 +++++++++++++++++++++++------
b8876f
 t/lib/warnings/pp_sys | 14 ++++++++++++++
b8876f
 t/op/filetest.t       | 10 +++++++++-
b8876f
 t/op/stat.t           | 12 +++++++++++-
b8876f
 5 files changed, 73 insertions(+), 13 deletions(-)
b8876f
b8876f
diff --git a/doio.c b/doio.c
b8876f
index 6704862..2792c66 100644
b8876f
--- a/doio.c
b8876f
+++ b/doio.c
b8876f
@@ -1458,7 +1458,7 @@ Perl_my_stat_flags(pTHX_ const U32 flags)
b8876f
 	return PL_laststatval;
b8876f
     else {
b8876f
 	SV* const sv = TOPs;
b8876f
-	const char *s;
b8876f
+	const char *s, *d;
b8876f
 	STRLEN len;
b8876f
 	if ((gv = MAYBE_DEREF_GV_flags(sv,flags))) {
b8876f
 	    goto do_fstat;
b8876f
@@ -1472,9 +1472,14 @@ Perl_my_stat_flags(pTHX_ const U32 flags)
b8876f
 	s = SvPV_flags_const(sv, len, flags);
b8876f
 	PL_statgv = NULL;
b8876f
 	sv_setpvn(PL_statname, s, len);
b8876f
-	s = SvPVX_const(PL_statname);		/* s now NUL-terminated */
b8876f
+	d = SvPVX_const(PL_statname);		/* s now NUL-terminated */
b8876f
 	PL_laststype = OP_STAT;
b8876f
-	PL_laststatval = PerlLIO_stat(s, &PL_statcache);
b8876f
+        if (!IS_SAFE_PATHNAME(s, len, OP_NAME(PL_op))) {
b8876f
+            PL_laststatval = -1;
b8876f
+        }
b8876f
+        else {
b8876f
+            PL_laststatval = PerlLIO_stat(d, &PL_statcache);
b8876f
+        }
b8876f
 	if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && should_warn_nl(s)) {
b8876f
             GCC_DIAG_IGNORE(-Wformat-nonliteral); /* PL_warn_nl is constant */
b8876f
 	    Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "stat");
b8876f
@@ -1491,6 +1496,7 @@ Perl_my_lstat_flags(pTHX_ const U32 flags)
b8876f
     static const char* const no_prev_lstat = "The stat preceding -l _ wasn't an lstat";
b8876f
     dSP;
b8876f
     const char *file;
b8876f
+    STRLEN len;
b8876f
     SV* const sv = TOPs;
b8876f
     bool isio = FALSE;
b8876f
     if (PL_op->op_flags & OPf_REF) {
b8876f
@@ -1534,9 +1540,14 @@ Perl_my_lstat_flags(pTHX_ const U32 flags)
b8876f
                               HEKfARG(GvENAME_HEK((const GV *)
b8876f
                                           (SvROK(sv) ? SvRV(sv) : sv))));
b8876f
     }
b8876f
-    file = SvPV_flags_const_nolen(sv, flags);
b8876f
+    file = SvPV_flags_const(sv, len, flags);
b8876f
     sv_setpv(PL_statname,file);
b8876f
-    PL_laststatval = PerlLIO_lstat(file,&PL_statcache);
b8876f
+    if (!IS_SAFE_PATHNAME(file, len, OP_NAME(PL_op))) {
b8876f
+        PL_laststatval = -1;
b8876f
+    }
b8876f
+    else {
b8876f
+        PL_laststatval = PerlLIO_lstat(file,&PL_statcache);
b8876f
+    }
b8876f
     if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && should_warn_nl(file)) {
b8876f
         GCC_DIAG_IGNORE(-Wformat-nonliteral); /* PL_warn_nl is constant */
b8876f
         Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "lstat");
b8876f
diff --git a/pp_sys.c b/pp_sys.c
b8876f
index bd55043..1a72e60 100644
b8876f
--- a/pp_sys.c
b8876f
+++ b/pp_sys.c
b8876f
@@ -2927,19 +2927,24 @@ PP(pp_stat)
b8876f
     }
b8876f
     else {
b8876f
         const char *file;
b8876f
+        const char *temp;
b8876f
+        STRLEN len;
b8876f
 	if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVIO) { 
b8876f
             io = MUTABLE_IO(SvRV(sv));
b8876f
             if (PL_op->op_type == OP_LSTAT)
b8876f
                 goto do_fstat_warning_check;
b8876f
             goto do_fstat_have_io; 
b8876f
         }
b8876f
-        
b8876f
 	SvTAINTED_off(PL_statname); /* previous tainting irrelevant */
b8876f
-	sv_setpv(PL_statname, SvPV_nomg_const_nolen(sv));
b8876f
+        temp = SvPV_nomg_const(sv, len);
b8876f
+	sv_setpv(PL_statname, temp);
b8876f
 	PL_statgv = NULL;
b8876f
 	PL_laststype = PL_op->op_type;
b8876f
         file = SvPV_nolen_const(PL_statname);
b8876f
-	if (PL_op->op_type == OP_LSTAT)
b8876f
+        if (!IS_SAFE_PATHNAME(temp, len, OP_NAME(PL_op))) {
b8876f
+            PL_laststatval = -1;
b8876f
+        }
b8876f
+	else if (PL_op->op_type == OP_LSTAT)
b8876f
 	    PL_laststatval = PerlLIO_lstat(file, &PL_statcache);
b8876f
 	else
b8876f
 	    PL_laststatval = PerlLIO_stat(file, &PL_statcache);
b8876f
@@ -3175,8 +3180,12 @@ PP(pp_ftrread)
b8876f
 
b8876f
     if (use_access) {
b8876f
 #if defined(HAS_ACCESS) || defined (PERL_EFF_ACCESS)
b8876f
-	const char *name = SvPV_nolen(*PL_stack_sp);
b8876f
-	if (effective) {
b8876f
+        STRLEN len;
b8876f
+	const char *name = SvPV(*PL_stack_sp, len);
b8876f
+        if (!IS_SAFE_PATHNAME(name, len, OP_NAME(PL_op))) {
b8876f
+            result = -1;
b8876f
+        }
b8876f
+	else if (effective) {
b8876f
 #  ifdef PERL_EFF_ACCESS
b8876f
 	    result = PERL_EFF_ACCESS(name, access_mode);
b8876f
 #  else
b8876f
@@ -3501,10 +3510,18 @@ PP(pp_fttext)
b8876f
     }
b8876f
     else {
b8876f
         const char *file;
b8876f
+        const char *temp;
b8876f
+        STRLEN temp_len;
b8876f
         int fd; 
b8876f
 
b8876f
         assert(sv);
b8876f
-	sv_setpv(PL_statname, SvPV_nomg_const_nolen(sv));
b8876f
+        temp = SvPV_nomg_const(sv, temp_len);
b8876f
+	sv_setpv(PL_statname, temp);
b8876f
+        if (!IS_SAFE_PATHNAME(temp, temp_len, OP_NAME(PL_op))) {
b8876f
+            PL_laststatval = -1;
b8876f
+            PL_laststype = OP_STAT;
b8876f
+            FT_RETURNUNDEF;
b8876f
+        }
b8876f
       really_filename:
b8876f
         file = SvPVX_const(PL_statname);
b8876f
 	PL_statgv = NULL;
b8876f
diff --git a/t/lib/warnings/pp_sys b/t/lib/warnings/pp_sys
b8876f
index 6338964..ded5d7d 100644
b8876f
--- a/t/lib/warnings/pp_sys
b8876f
+++ b/t/lib/warnings/pp_sys
b8876f
@@ -962,3 +962,17 @@ close $fh;
b8876f
 unlink $file;
b8876f
 EXPECT
b8876f
 syswrite() is deprecated on :utf8 handles at - line 6.
b8876f
+########
b8876f
+# NAME stat on name with \0
b8876f
+use warnings;
b8876f
+my @x = stat("./\0-");
b8876f
+my @y = lstat("./\0-");
b8876f
+-T ".\0-";
b8876f
+-x ".\0-";
b8876f
+-l ".\0-";
b8876f
+EXPECT
b8876f
+Invalid \0 character in pathname for stat: ./\0- at - line 2.
b8876f
+Invalid \0 character in pathname for lstat: ./\0- at - line 3.
b8876f
+Invalid \0 character in pathname for fttext: .\0- at - line 4.
b8876f
+Invalid \0 character in pathname for fteexec: .\0- at - line 5.
b8876f
+Invalid \0 character in pathname for ftlink: .\0- at - line 6.
b8876f
diff --git a/t/op/filetest.t b/t/op/filetest.t
b8876f
index 8883381..bd1d08c 100644
b8876f
--- a/t/op/filetest.t
b8876f
+++ b/t/op/filetest.t
b8876f
@@ -9,7 +9,7 @@ BEGIN {
b8876f
     set_up_inc(qw '../lib ../cpan/Perl-OSType/lib');
b8876f
 }
b8876f
 
b8876f
-plan(tests => 53 + 27*14);
b8876f
+plan(tests => 57 + 27*14);
b8876f
 
b8876f
 if ($^O =~ /MSWin32|cygwin|msys/ && !is_miniperl) {
b8876f
   require Win32; # for IsAdminUser()
b8876f
@@ -393,3 +393,11 @@ SKIP: {
b8876f
     is $failed_stat2, $failed_stat1,
b8876f
 	'failed -r($gv_with_io_but_no_fp) with and w/out fatal warnings';
b8876f
 } 
b8876f
+
b8876f
+{
b8876f
+    # [perl #131895] stat() doesn't fail on filenames containing \0 / NUL
b8876f
+    ok(!-T "TEST\0-", '-T on name with \0');
b8876f
+    ok(!-B "TEST\0-", '-B on name with \0');
b8876f
+    ok(!-f "TEST\0-", '-f on name with \0');
b8876f
+    ok(!-r "TEST\0-", '-r on name with \0');
b8876f
+}
b8876f
diff --git a/t/op/stat.t b/t/op/stat.t
b8876f
index 637a902..71193ad 100644
b8876f
--- a/t/op/stat.t
b8876f
+++ b/t/op/stat.t
b8876f
@@ -25,7 +25,7 @@ if ($^O eq 'MSWin32') {
b8876f
     ${^WIN32_SLOPPY_STAT} = 0;
b8876f
 }
b8876f
 
b8876f
-plan tests => 118;
b8876f
+plan tests => 120;
b8876f
 
b8876f
 my $Perl = which_perl();
b8876f
 
b8876f
@@ -651,6 +651,16 @@ SKIP:
b8876f
       'stat on an array of valid paths should return ENOENT';
b8876f
 }
b8876f
 
b8876f
+# [perl #131895] stat() doesn't fail on filenames containing \0 / NUL
b8876f
+ok !stat("TEST\0-"), 'stat on filename with \0';
b8876f
+SKIP: {
b8876f
+    my $link = "TEST.symlink.$$";
b8876f
+    my $can_symlink = eval { symlink "TEST", $link };
b8876f
+    skip "cannot symlink", 1 unless $can_symlink;
b8876f
+    ok !lstat("$link\0-"), 'lstat on filename with \0';
b8876f
+    unlink $link;
b8876f
+}
b8876f
+
b8876f
 END {
b8876f
     chmod 0666, $tmpfile;
b8876f
     unlink_all $tmpfile;
b8876f
-- 
b8876f
2.13.6
b8876f