This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Thanks to FindExt::apply_config() we're now able to test dynamic extensions.
[perl5.git] / taint.c
CommitLineData
d6376244
JH
1/* taint.c
2 *
663f364b 3 * Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
1129b882 4 * 2002, 2003, 2004, 2005, 2006, 2007, 2008 by Larry Wall and others
d6376244
JH
5 *
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Artistic License, as specified in the README file.
8 *
9 */
10
a0d0e21e 11/*
4ac71550
TC
12 * '...we will have peace, when you and all your works have perished--and
13 * the works of your dark master to whom you would deliver us. You are a
cdad3b53 14 * liar, Saruman, and a corrupter of men's hearts.' --Théoden
4ac71550
TC
15 *
16 * [p.580 of _The Lord of the Rings_, III/x: "The Voice of Saruman"]
a0d0e21e
LW
17 */
18
166f8a29
DM
19/* This file contains a few functions for handling data tainting in Perl
20 */
21
463ee0b2 22#include "EXTERN.h"
864dbfa3 23#define PERL_IN_TAINT_C
463ee0b2
LW
24#include "perl.h"
25
26void
e1c41806 27Perl_taint_proper(pTHX_ const char *f, const char *const s)
79072805 28{
26e623cf 29#if defined(HAS_SETEUID) && defined(DEBUGGING)
97aff369 30 dVAR;
7918f24d
NC
31
32 PERL_ARGS_ASSERT_TAINT_PROPER;
33
26e623cf 34 {
dfff4baf
BF
35 const Uid_t uid = PerlProc_getuid();
36 const Uid_t euid = PerlProc_geteuid();
26e623cf 37
8772537c 38 DEBUG_u(PerlIO_printf(Perl_debug_log,
dfff4baf 39 "%s %d %"Uid_t_f" %"Uid_t_f"\n",
284167a5 40 s, TAINT_get, uid, euid));
26e623cf 41 }
a52cb5f7 42#endif
1e422769 43
284167a5 44 if (TAINT_get) {
8772537c
AL
45 const char *ug;
46
22c35a8c
GS
47 if (!f)
48 f = PL_no_security;
985213f2 49 if (PerlProc_getuid() != PerlProc_geteuid())
bbce6d69 50 ug = " while running setuid";
985213f2 51 else if (PerlProc_getgid() != PerlProc_getegid())
bbce6d69 52 ug = " while running setgid";
284167a5 53 else if (TAINT_WARN_get)
6537fe72
MS
54 ug = " while running with -t switch";
55 else
bbce6d69 56 ug = " while running with -T switch";
284167a5 57 if (PL_unsafe || TAINT_WARN_get) {
9b387841 58 Perl_ck_warner_d(aTHX_ packWARN(WARN_TAINT), f, s, ug);
6537fe72
MS
59 }
60 else {
61 Perl_croak(aTHX_ f, s, ug);
62 }
79072805
LW
63 }
64}
65
66void
864dbfa3 67Perl_taint_env(pTHX)
79072805 68{
97aff369 69 dVAR;
79072805 70 SV** svp;
7bac28a0 71 MAGIC* mg;
27da23d5
JH
72 const char* const *e;
73 static const char* const misc_env[] = {
7bac28a0 74 "IFS", /* most shells' inter-field separators */
c90c0ff4 75 "CDPATH", /* ksh dain bramage #1 */
76 "ENV", /* ksh dain bramage #2 */
77 "BASH_ENV", /* bash dain bramage -- I guess it's contagious */
e62a6770
RGS
78#ifdef WIN32
79 "PERL5SHELL", /* used for system() on Windows */
80#endif
7bac28a0 81 NULL
82 };
1e422769 83
c038024b
RGS
84 /* Don't bother if there's no *ENV glob */
85 if (!PL_envgv)
142393a6 86 return;
284167a5 87 /* If there's no %ENV hash or if it's not magical, croak, because
c038024b
RGS
88 * it probably doesn't reflect the actual environment */
89 if (!GvHV(PL_envgv) || !(SvRMAGICAL(GvHV(PL_envgv))
daba3364 90 && mg_find((const SV *)GvHV(PL_envgv), PERL_MAGIC_env))) {
284167a5 91 const bool was_tainted = TAINT_get;
8772537c 92 const char * const name = GvENAME(PL_envgv);
284167a5 93 TAINT;
c038024b
RGS
94 if (strEQ(name,"ENV"))
95 /* hash alias */
96 taint_proper("%%ENV is aliased to %s%s", "another variable");
97 else
98 /* glob alias: report it in the error message */
99 taint_proper("%%ENV is aliased to %%%s%s", name);
100 /* this statement is reached under -t or -U */
284167a5 101 TAINT_set(was_tainted);
9a9b5ec9
DM
102#ifdef NO_TAINT_SUPPORT
103 PERL_UNUSED_VAR(was_tainted);
104#endif
c038024b 105 }
142393a6 106
1e422769 107#ifdef VMS
82f1aded 108 {
1e422769 109 int i = 0;
0dc443ab 110 char name[10 + TYPE_DIGITS(int)] = "DCL$PATH";
86c11942 111 STRLEN len = 8; /* strlen(name) */
1e422769 112
113 while (1) {
114 if (i)
86c11942
NC
115 len = my_sprintf(name,"DCL$PATH;%d", i);
116 svp = hv_fetch(GvHVn(PL_envgv), name, len, FALSE);
6b88bc9c 117 if (!svp || *svp == &PL_sv_undef)
1e422769 118 break;
119 if (SvTAINTED(*svp)) {
120 TAINT;
121 taint_proper("Insecure %s%s", "$ENV{DCL$PATH}");
122 }
c038024b 123 if ((mg = mg_find(*svp, PERL_MAGIC_envelem)) && MgTAINTEDDIR(mg)) {
1e422769 124 TAINT;
125 taint_proper("Insecure directory in %s%s", "$ENV{DCL$PATH}");
126 }
127 i++;
128 }
82f1aded 129 }
1e422769 130#endif /* VMS */
79072805 131
a4fc7abc 132 svp = hv_fetchs(GvHVn(PL_envgv),"PATH",FALSE);
1e422769 133 if (svp && *svp) {
134 if (SvTAINTED(*svp)) {
135 TAINT;
bbce6d69 136 taint_proper("Insecure %s%s", "$ENV{PATH}");
1e422769 137 }
c038024b 138 if ((mg = mg_find(*svp, PERL_MAGIC_envelem)) && MgTAINTEDDIR(mg)) {
1e422769 139 TAINT;
140 taint_proper("Insecure directory in %s%s", "$ENV{PATH}");
141 }
79072805 142 }
79072805 143
c90c0ff4 144#ifndef VMS
145 /* tainted $TERM is okay if it contains no metachars */
a4fc7abc 146 svp = hv_fetchs(GvHVn(PL_envgv),"TERM",FALSE);
c90c0ff4 147 if (svp && *svp && SvTAINTED(*svp)) {
8b6b16e7 148 STRLEN len;
284167a5 149 const bool was_tainted = TAINT_get;
cfd0369c 150 const char *t = SvPV_const(*svp, len);
8772537c 151 const char * const e = t + len;
9a9b5ec9 152
284167a5 153 TAINT_set(was_tainted);
9a9b5ec9
DM
154#ifdef NO_TAINT_SUPPORT
155 PERL_UNUSED_VAR(was_tainted);
156#endif
0eb30aeb 157 if (t < e && isWORDCHAR(*t))
c90c0ff4 158 t++;
0eb30aeb 159 while (t < e && (isWORDCHAR(*t) || strchr("-_.+", *t)))
c90c0ff4 160 t++;
161 if (t < e) {
162 TAINT;
163 taint_proper("Insecure $ENV{%s}%s", "TERM");
164 }
165 }
166#endif /* !VMS */
167
7bac28a0 168 for (e = misc_env; *e; e++) {
0bd48802 169 SV * const * const svp = hv_fetch(GvHVn(PL_envgv), *e, strlen(*e), FALSE);
3280af22 170 if (svp && *svp != &PL_sv_undef && SvTAINTED(*svp)) {
7bac28a0 171 TAINT;
172 taint_proper("Insecure $ENV{%s}%s", *e);
173 }
bbce6d69 174 }
175}
66610fdd
RGS
176
177/*
178 * Local variables:
179 * c-indentation-style: bsd
180 * c-basic-offset: 4
14d04a33 181 * indent-tabs-mode: nil
66610fdd
RGS
182 * End:
183 *
14d04a33 184 * ex: set ts=8 sts=4 sw=4 et:
37442d52 185 */