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