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