Avoid a possible race condition where a parallel make might
[p5sagit/p5-mst-13.2.git] / taint.c
CommitLineData
d6376244 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 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 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 17 */
18
166f8a29 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 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 31
32 PERL_ARGS_ASSERT_TAINT_PROPER;
33
26e623cf 34# if Uid_t_size == 1
35 {
8772537c 36 const UV uid = PL_uid;
37 const UV euid = PL_euid;
26e623cf 38
8772537c 39 DEBUG_u(PerlIO_printf(Perl_debug_log,
26e623cf 40 "%s %d %"UVuf" %"UVuf"\n",
41 s, PL_tainted, uid, euid));
42 }
43# else
44 {
8772537c 45 const IV uid = PL_uid;
46 const IV euid = PL_euid;
26e623cf 47
8772537c 48 DEBUG_u(PerlIO_printf(Perl_debug_log,
26e623cf 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 56 const char *ug;
57
22c35a8c 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 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) {
9b387841 69 Perl_ck_warner_d(aTHX_ packWARN(WARN_TAINT), f, s, ug);
6537fe72 70 }
71 else {
72 Perl_croak(aTHX_ f, s, ug);
73 }
79072805 74 }
75}
76
77void
864dbfa3 78Perl_taint_env(pTHX)
79072805 79{
97aff369 80 dVAR;
79072805 81 SV** svp;
7bac28a0 82 MAGIC* mg;
27da23d5 83 const char* const *e;
84 static const char* const misc_env[] = {
7bac28a0 85 "IFS", /* most shells' inter-field separators */
c90c0ff4 86 "CDPATH", /* ksh dain bramage #1 */
87 "ENV", /* ksh dain bramage #2 */
88 "BASH_ENV", /* bash dain bramage -- I guess it's contagious */
e62a6770 89#ifdef WIN32
90 "PERL5SHELL", /* used for system() on Windows */
91#endif
7bac28a0 92 NULL
93 };
1e422769 94
c038024b 95 /* Don't bother if there's no *ENV glob */
96 if (!PL_envgv)
142393a6 97 return;
c038024b 98 /* If there's no %ENV hash of if it's not magical, croak, because
99 * it probably doesn't reflect the actual environment */
100 if (!GvHV(PL_envgv) || !(SvRMAGICAL(GvHV(PL_envgv))
daba3364 101 && mg_find((const SV *)GvHV(PL_envgv), PERL_MAGIC_env))) {
e1ec3a88 102 const bool was_tainted = PL_tainted;
8772537c 103 const char * const name = GvENAME(PL_envgv);
c038024b 104 PL_tainted = TRUE;
105 if (strEQ(name,"ENV"))
106 /* hash alias */
107 taint_proper("%%ENV is aliased to %s%s", "another variable");
108 else
109 /* glob alias: report it in the error message */
110 taint_proper("%%ENV is aliased to %%%s%s", name);
111 /* this statement is reached under -t or -U */
112 PL_tainted = was_tainted;
113 }
142393a6 114
1e422769 115#ifdef VMS
82f1aded 116 {
1e422769 117 int i = 0;
0dc443ab 118 char name[10 + TYPE_DIGITS(int)] = "DCL$PATH";
86c11942 119 STRLEN len = 8; /* strlen(name) */
1e422769 120
121 while (1) {
122 if (i)
86c11942 123 len = my_sprintf(name,"DCL$PATH;%d", i);
124 svp = hv_fetch(GvHVn(PL_envgv), name, len, FALSE);
6b88bc9c 125 if (!svp || *svp == &PL_sv_undef)
1e422769 126 break;
127 if (SvTAINTED(*svp)) {
128 TAINT;
129 taint_proper("Insecure %s%s", "$ENV{DCL$PATH}");
130 }
c038024b 131 if ((mg = mg_find(*svp, PERL_MAGIC_envelem)) && MgTAINTEDDIR(mg)) {
1e422769 132 TAINT;
133 taint_proper("Insecure directory in %s%s", "$ENV{DCL$PATH}");
134 }
135 i++;
136 }
82f1aded 137 }
1e422769 138#endif /* VMS */
79072805 139
a4fc7abc 140 svp = hv_fetchs(GvHVn(PL_envgv),"PATH",FALSE);
1e422769 141 if (svp && *svp) {
142 if (SvTAINTED(*svp)) {
143 TAINT;
bbce6d69 144 taint_proper("Insecure %s%s", "$ENV{PATH}");
1e422769 145 }
c038024b 146 if ((mg = mg_find(*svp, PERL_MAGIC_envelem)) && MgTAINTEDDIR(mg)) {
1e422769 147 TAINT;
148 taint_proper("Insecure directory in %s%s", "$ENV{PATH}");
149 }
79072805 150 }
79072805 151
c90c0ff4 152#ifndef VMS
153 /* tainted $TERM is okay if it contains no metachars */
a4fc7abc 154 svp = hv_fetchs(GvHVn(PL_envgv),"TERM",FALSE);
c90c0ff4 155 if (svp && *svp && SvTAINTED(*svp)) {
8b6b16e7 156 STRLEN len;
73d840c0 157 const bool was_tainted = PL_tainted;
cfd0369c 158 const char *t = SvPV_const(*svp, len);
8772537c 159 const char * const e = t + len;
3280af22 160 PL_tainted = was_tainted;
c90c0ff4 161 if (t < e && isALNUM(*t))
162 t++;
d085cc71 163 while (t < e && (isALNUM(*t) || strchr("-_.+", *t)))
c90c0ff4 164 t++;
165 if (t < e) {
166 TAINT;
167 taint_proper("Insecure $ENV{%s}%s", "TERM");
168 }
169 }
170#endif /* !VMS */
171
7bac28a0 172 for (e = misc_env; *e; e++) {
0bd48802 173 SV * const * const svp = hv_fetch(GvHVn(PL_envgv), *e, strlen(*e), FALSE);
3280af22 174 if (svp && *svp != &PL_sv_undef && SvTAINTED(*svp)) {
7bac28a0 175 TAINT;
176 taint_proper("Insecure $ENV{%s}%s", *e);
177 }
bbce6d69 178 }
179}
66610fdd 180
181/*
182 * Local variables:
183 * c-indentation-style: bsd
184 * c-basic-offset: 4
185 * indent-tabs-mode: t
186 * End:
187 *
37442d52 188 * ex: set ts=8 sts=4 sw=4 noet:
189 */