3 * Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
4 * 2002, 2003, 2004, 2005, 2006, 2007, by Larry Wall and others
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.
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
14 * liar, Saruman, and a corrupter of men's hearts." --Theoden
17 /* This file contains a few functions for handling data tainting in Perl
21 #define PERL_IN_TAINT_C
25 Perl_taint_proper(pTHX_ const char *f, const char *const s)
27 #if defined(HAS_SETEUID) && defined(DEBUGGING)
30 PERL_ARGS_ASSERT_TAINT_PROPER;
34 const UV uid = PL_uid;
35 const UV euid = PL_euid;
37 DEBUG_u(PerlIO_printf(Perl_debug_log,
38 "%s %d %"UVuf" %"UVuf"\n",
39 s, PL_tainted, uid, euid));
43 const IV uid = PL_uid;
44 const IV euid = PL_euid;
46 DEBUG_u(PerlIO_printf(Perl_debug_log,
47 "%s %d %"IVdf" %"IVdf"\n",
48 s, PL_tainted, uid, euid));
58 if (PL_euid != PL_uid)
59 ug = " while running setuid";
60 else if (PL_egid != PL_gid)
61 ug = " while running setgid";
62 else if (PL_taint_warn)
63 ug = " while running with -t switch";
65 ug = " while running with -T switch";
66 if (PL_unsafe || PL_taint_warn) {
67 if(ckWARN_d(WARN_TAINT))
68 Perl_warner(aTHX_ packWARN(WARN_TAINT), f, s, ug);
71 Perl_croak(aTHX_ f, s, ug);
83 static const char* const misc_env[] = {
84 "IFS", /* most shells' inter-field separators */
85 "CDPATH", /* ksh dain bramage #1 */
86 "ENV", /* ksh dain bramage #2 */
87 "BASH_ENV", /* bash dain bramage -- I guess it's contagious */
89 "PERL5SHELL", /* used for system() on Windows */
94 /* Don't bother if there's no *ENV glob */
97 /* If there's no %ENV hash of if it's not magical, croak, because
98 * it probably doesn't reflect the actual environment */
99 if (!GvHV(PL_envgv) || !(SvRMAGICAL(GvHV(PL_envgv))
100 && mg_find((SV*)GvHV(PL_envgv), PERL_MAGIC_env))) {
101 const bool was_tainted = PL_tainted;
102 const char * const name = GvENAME(PL_envgv);
104 if (strEQ(name,"ENV"))
106 taint_proper("%%ENV is aliased to %s%s", "another variable");
108 /* glob alias: report it in the error message */
109 taint_proper("%%ENV is aliased to %%%s%s", name);
110 /* this statement is reached under -t or -U */
111 PL_tainted = was_tainted;
117 char name[10 + TYPE_DIGITS(int)] = "DCL$PATH";
118 STRLEN len = 8; /* strlen(name) */
122 len = my_sprintf(name,"DCL$PATH;%d", i);
123 svp = hv_fetch(GvHVn(PL_envgv), name, len, FALSE);
124 if (!svp || *svp == &PL_sv_undef)
126 if (SvTAINTED(*svp)) {
128 taint_proper("Insecure %s%s", "$ENV{DCL$PATH}");
130 if ((mg = mg_find(*svp, PERL_MAGIC_envelem)) && MgTAINTEDDIR(mg)) {
132 taint_proper("Insecure directory in %s%s", "$ENV{DCL$PATH}");
139 svp = hv_fetchs(GvHVn(PL_envgv),"PATH",FALSE);
141 if (SvTAINTED(*svp)) {
143 taint_proper("Insecure %s%s", "$ENV{PATH}");
145 if ((mg = mg_find(*svp, PERL_MAGIC_envelem)) && MgTAINTEDDIR(mg)) {
147 taint_proper("Insecure directory in %s%s", "$ENV{PATH}");
152 /* tainted $TERM is okay if it contains no metachars */
153 svp = hv_fetchs(GvHVn(PL_envgv),"TERM",FALSE);
154 if (svp && *svp && SvTAINTED(*svp)) {
156 const bool was_tainted = PL_tainted;
157 const char *t = SvPV_const(*svp, len);
158 const char * const e = t + len;
159 PL_tainted = was_tainted;
160 if (t < e && isALNUM(*t))
162 while (t < e && (isALNUM(*t) || strchr("-_.+", *t)))
166 taint_proper("Insecure $ENV{%s}%s", "TERM");
171 for (e = misc_env; *e; e++) {
172 SV * const * const svp = hv_fetch(GvHVn(PL_envgv), *e, strlen(*e), FALSE);
173 if (svp && *svp != &PL_sv_undef && SvTAINTED(*svp)) {
175 taint_proper("Insecure $ENV{%s}%s", *e);
182 * c-indentation-style: bsd
184 * indent-tabs-mode: t
187 * ex: set ts=8 sts=4 sw=4 noet: