This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Upgrade to MakeMaker 6.53_02
[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
14 * liar, Saruman, and a corrupter of men's hearts.' --Théoden
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
JH
34# if Uid_t_size == 1
35 {
8772537c
AL
36 const UV uid = PL_uid;
37 const UV euid = PL_euid;
26e623cf 38
8772537c 39 DEBUG_u(PerlIO_printf(Perl_debug_log,
26e623cf
JH
40 "%s %d %"UVuf" %"UVuf"\n",
41 s, PL_tainted, uid, euid));
42 }
43# else
44 {
8772537c
AL
45 const IV uid = PL_uid;
46 const IV euid = PL_euid;
26e623cf 47
8772537c 48 DEBUG_u(PerlIO_printf(Perl_debug_log,
26e623cf
JH
49 "%s %d %"IVdf" %"IVdf"\n",
50 s, PL_tainted, uid, euid));
51 }
52# endif
a52cb5f7 53#endif
1e422769 54
3280af22 55 if (PL_tainted) {
8772537c
AL
56 const char *ug;
57
22c35a8c
GS
58 if (!f)
59 f = PL_no_security;
3280af22 60 if (PL_euid != PL_uid)
bbce6d69 61 ug = " while running setuid";
3280af22 62 else if (PL_egid != PL_gid)
bbce6d69 63 ug = " while running setgid";
6537fe72
MS
64 else if (PL_taint_warn)
65 ug = " while running with -t switch";
66 else
bbce6d69 67 ug = " while running with -T switch";
6537fe72 68 if (PL_unsafe || PL_taint_warn) {
b7bf404b 69 if(ckWARN_d(WARN_TAINT))
9014280d 70 Perl_warner(aTHX_ packWARN(WARN_TAINT), f, s, ug);
6537fe72
MS
71 }
72 else {
73 Perl_croak(aTHX_ f, s, ug);
74 }
79072805
LW
75 }
76}
77
78void
864dbfa3 79Perl_taint_env(pTHX)
79072805 80{
97aff369 81 dVAR;
79072805 82 SV** svp;
7bac28a0 83 MAGIC* mg;
27da23d5
JH
84 const char* const *e;
85 static const char* const misc_env[] = {
7bac28a0 86 "IFS", /* most shells' inter-field separators */
c90c0ff4 87 "CDPATH", /* ksh dain bramage #1 */
88 "ENV", /* ksh dain bramage #2 */
89 "BASH_ENV", /* bash dain bramage -- I guess it's contagious */
e62a6770
RGS
90#ifdef WIN32
91 "PERL5SHELL", /* used for system() on Windows */
92#endif
7bac28a0 93 NULL
94 };
1e422769 95
c038024b
RGS
96 /* Don't bother if there's no *ENV glob */
97 if (!PL_envgv)
142393a6 98 return;
c038024b
RGS
99 /* If there's no %ENV hash of if it's not magical, croak, because
100 * it probably doesn't reflect the actual environment */
101 if (!GvHV(PL_envgv) || !(SvRMAGICAL(GvHV(PL_envgv))
daba3364 102 && mg_find((const SV *)GvHV(PL_envgv), PERL_MAGIC_env))) {
e1ec3a88 103 const bool was_tainted = PL_tainted;
8772537c 104 const char * const name = GvENAME(PL_envgv);
c038024b
RGS
105 PL_tainted = TRUE;
106 if (strEQ(name,"ENV"))
107 /* hash alias */
108 taint_proper("%%ENV is aliased to %s%s", "another variable");
109 else
110 /* glob alias: report it in the error message */
111 taint_proper("%%ENV is aliased to %%%s%s", name);
112 /* this statement is reached under -t or -U */
113 PL_tainted = was_tainted;
114 }
142393a6 115
1e422769 116#ifdef VMS
82f1aded 117 {
1e422769 118 int i = 0;
0dc443ab 119 char name[10 + TYPE_DIGITS(int)] = "DCL$PATH";
86c11942 120 STRLEN len = 8; /* strlen(name) */
1e422769 121
122 while (1) {
123 if (i)
86c11942
NC
124 len = my_sprintf(name,"DCL$PATH;%d", i);
125 svp = hv_fetch(GvHVn(PL_envgv), name, len, FALSE);
6b88bc9c 126 if (!svp || *svp == &PL_sv_undef)
1e422769 127 break;
128 if (SvTAINTED(*svp)) {
129 TAINT;
130 taint_proper("Insecure %s%s", "$ENV{DCL$PATH}");
131 }
c038024b 132 if ((mg = mg_find(*svp, PERL_MAGIC_envelem)) && MgTAINTEDDIR(mg)) {
1e422769 133 TAINT;
134 taint_proper("Insecure directory in %s%s", "$ENV{DCL$PATH}");
135 }
136 i++;
137 }
82f1aded 138 }
1e422769 139#endif /* VMS */
79072805 140
a4fc7abc 141 svp = hv_fetchs(GvHVn(PL_envgv),"PATH",FALSE);
1e422769 142 if (svp && *svp) {
143 if (SvTAINTED(*svp)) {
144 TAINT;
bbce6d69 145 taint_proper("Insecure %s%s", "$ENV{PATH}");
1e422769 146 }
c038024b 147 if ((mg = mg_find(*svp, PERL_MAGIC_envelem)) && MgTAINTEDDIR(mg)) {
1e422769 148 TAINT;
149 taint_proper("Insecure directory in %s%s", "$ENV{PATH}");
150 }
79072805 151 }
79072805 152
c90c0ff4 153#ifndef VMS
154 /* tainted $TERM is okay if it contains no metachars */
a4fc7abc 155 svp = hv_fetchs(GvHVn(PL_envgv),"TERM",FALSE);
c90c0ff4 156 if (svp && *svp && SvTAINTED(*svp)) {
8b6b16e7 157 STRLEN len;
73d840c0 158 const bool was_tainted = PL_tainted;
cfd0369c 159 const char *t = SvPV_const(*svp, len);
8772537c 160 const char * const e = t + len;
3280af22 161 PL_tainted = was_tainted;
c90c0ff4 162 if (t < e && isALNUM(*t))
163 t++;
d085cc71 164 while (t < e && (isALNUM(*t) || strchr("-_.+", *t)))
c90c0ff4 165 t++;
166 if (t < e) {
167 TAINT;
168 taint_proper("Insecure $ENV{%s}%s", "TERM");
169 }
170 }
171#endif /* !VMS */
172
7bac28a0 173 for (e = misc_env; *e; e++) {
0bd48802 174 SV * const * const svp = hv_fetch(GvHVn(PL_envgv), *e, strlen(*e), FALSE);
3280af22 175 if (svp && *svp != &PL_sv_undef && SvTAINTED(*svp)) {
7bac28a0 176 TAINT;
177 taint_proper("Insecure $ENV{%s}%s", *e);
178 }
bbce6d69 179 }
180}
66610fdd
RGS
181
182/*
183 * Local variables:
184 * c-indentation-style: bsd
185 * c-basic-offset: 4
186 * indent-tabs-mode: t
187 * End:
188 *
37442d52
RGS
189 * ex: set ts=8 sts=4 sw=4 noet:
190 */