diff options
author | Tony Cook <tony@develop-help.com> | 2013-08-26 11:26:19 +1000 |
---|---|---|
committer | Tony Cook <tony@develop-help.com> | 2013-08-26 14:06:16 +1000 |
commit | c8028aa68dedb3c7683abb0bcf0fdba782a1190e (patch) | |
tree | 0c1acb4263f2d3d1b08e2e42d1ad18b2686617d8 /inline.h | |
parent | 5f7c1602dfa694a4a6761e9e4fc077ce794f7ff0 (diff) | |
download | perl-c8028aa68dedb3c7683abb0bcf0fdba782a1190e.tar.gz |
[perl #117265] safesyscalls: check embedded nul in syscall args
Check for the nul char in pathnames and string arguments to
syscalls, return undef and set errno to ENOENT.
Added to the io warnings category syscalls.
Strings with embedded \0 chars were prev. ignored in the syscall but
kept in perl. The hidden payloads in these invalid string args may cause
unnoticed security problems, as they are hard to detect, ignored by
the syscalls but kept around in perl PVs.
Allow an ending \0 though, as several modules add a \0 to
such strings without adjusting the length.
This is based on a change originally by Reini Urban, but pretty much
all of the code has been replaced.
Diffstat (limited to 'inline.h')
-rw-r--r-- | inline.h | 50 |
1 files changed, 50 insertions, 0 deletions
@@ -221,3 +221,53 @@ S_isALNUM_lazy(pTHX_ const char* p) return isALNUM_lazy_if(p,1); } + +/* ------------------------------- perl.h ----------------------------- */ + +/* +=for apidoc AiR|bool|is_safe_syscall|SV *pv|const char *what|const char *op_name + +Test that the given C<pv> doesn't contain any internal NUL characters. +If it does, set C<errno> to ENOENT, optionally warn, and return FALSE. + +Return TRUE if the name is safe. + +Used by the IS_SAFE_SYSCALL() macro. + +=cut +*/ + +PERL_STATIC_INLINE bool +S_is_safe_syscall(pTHX_ SV *pv, const char *what, const char *op_name) { + /* While the Windows CE API provides only UCS-16 (or UTF-16) APIs + * perl itself uses xce*() functions which accept 8-bit strings. + */ + + PERL_ARGS_ASSERT_IS_SAFE_SYSCALL; + + if (SvPOK(pv) && SvCUR(pv) >= 1) { + char *p = SvPVX(pv); + char *null_at; + if (UNLIKELY((null_at = (char *)memchr(p, 0, SvCUR(pv)-1)) != NULL)) { + SETERRNO(ENOENT, LIB_INVARG); + if (ckWARN(WARN_SYSCALLS)) { + Perl_ck_warner(aTHX_ packWARN(WARN_SYSCALLS), + "Invalid \\0 character in %s for %s: %s\\0%s", + what, op_name, p, null_at+1); + } + return FALSE; + } + } + + return TRUE; +} + +/* + * Local variables: + * c-indentation-style: bsd + * c-basic-offset: 4 + * indent-tabs-mode: nil + * End: + * + * ex: set ts=8 sts=4 sw=4 et: + */ |