This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
[perl #117531] Deparse elements of %# correctly
[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
JH
34# if Uid_t_size == 1
35 {
985213f2
AB
36 const UV uid = PerlProc_getuid();
37 const UV euid = PerlProc_geteuid();
26e623cf 38
8772537c 39 DEBUG_u(PerlIO_printf(Perl_debug_log,
26e623cf 40 "%s %d %"UVuf" %"UVuf"\n",
284167a5 41 s, TAINT_get, uid, euid));
26e623cf
JH
42 }
43# else
44 {
985213f2
AB
45 const IV uid = PerlProc_getuid();
46 const IV euid = PerlProc_geteuid();
26e623cf 47
8772537c 48 DEBUG_u(PerlIO_printf(Perl_debug_log,
26e623cf 49 "%s %d %"IVdf" %"IVdf"\n",
284167a5 50 s, TAINT_get, uid, euid));
26e623cf
JH
51 }
52# endif
a52cb5f7 53#endif
1e422769 54
284167a5 55 if (TAINT_get) {
8772537c
AL
56 const char *ug;
57
22c35a8c
GS
58 if (!f)
59 f = PL_no_security;
985213f2 60 if (PerlProc_getuid() != PerlProc_geteuid())
bbce6d69 61 ug = " while running setuid";
985213f2 62 else if (PerlProc_getgid() != PerlProc_getegid())
bbce6d69 63 ug = " while running setgid";
284167a5 64 else if (TAINT_WARN_get)
6537fe72
MS
65 ug = " while running with -t switch";
66 else
bbce6d69 67 ug = " while running with -T switch";
284167a5 68 if (PL_unsafe || TAINT_WARN_get) {
9b387841 69 Perl_ck_warner_d(aTHX_ packWARN(WARN_TAINT), f, s, ug);
6537fe72
MS
70 }
71 else {
72 Perl_croak(aTHX_ f, s, ug);
73 }
79072805
LW
74 }
75}
76
77void
864dbfa3 78Perl_taint_env(pTHX)
79072805 79{
97aff369 80 dVAR;
79072805 81 SV** svp;
7bac28a0 82 MAGIC* mg;
27da23d5
JH
83 const char* const *e;
84 static const char* const misc_env[] = {
7bac28a0 85 "IFS", /* most shells' inter-field separators */
c90c0ff4
PP
86 "CDPATH", /* ksh dain bramage #1 */
87 "ENV", /* ksh dain bramage #2 */
88 "BASH_ENV", /* bash dain bramage -- I guess it's contagious */
e62a6770
RGS
89#ifdef WIN32
90 "PERL5SHELL", /* used for system() on Windows */
91#endif
7bac28a0
PP
92 NULL
93 };
1e422769 94
c038024b
RGS
95 /* Don't bother if there's no *ENV glob */
96 if (!PL_envgv)
142393a6 97 return;
284167a5 98 /* If there's no %ENV hash or if it's not magical, croak, because
c038024b
RGS
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))) {
284167a5 102 const bool was_tainted = TAINT_get;
8772537c 103 const char * const name = GvENAME(PL_envgv);
284167a5 104 TAINT;
c038024b
RGS
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 */
284167a5 112 TAINT_set(was_tainted);
9a9b5ec9
DM
113#ifdef NO_TAINT_SUPPORT
114 PERL_UNUSED_VAR(was_tainted);
115#endif
c038024b 116 }
142393a6 117
1e422769 118#ifdef VMS
82f1aded 119 {
1e422769 120 int i = 0;
0dc443ab 121 char name[10 + TYPE_DIGITS(int)] = "DCL$PATH";
86c11942 122 STRLEN len = 8; /* strlen(name) */
1e422769
PP
123
124 while (1) {
125 if (i)
86c11942
NC
126 len = my_sprintf(name,"DCL$PATH;%d", i);
127 svp = hv_fetch(GvHVn(PL_envgv), name, len, FALSE);
6b88bc9c 128 if (!svp || *svp == &PL_sv_undef)
1e422769
PP
129 break;
130 if (SvTAINTED(*svp)) {
131 TAINT;
132 taint_proper("Insecure %s%s", "$ENV{DCL$PATH}");
133 }
c038024b 134 if ((mg = mg_find(*svp, PERL_MAGIC_envelem)) && MgTAINTEDDIR(mg)) {
1e422769
PP
135 TAINT;
136 taint_proper("Insecure directory in %s%s", "$ENV{DCL$PATH}");
137 }
138 i++;
139 }
82f1aded 140 }
1e422769 141#endif /* VMS */
79072805 142
a4fc7abc 143 svp = hv_fetchs(GvHVn(PL_envgv),"PATH",FALSE);
1e422769
PP
144 if (svp && *svp) {
145 if (SvTAINTED(*svp)) {
146 TAINT;
bbce6d69 147 taint_proper("Insecure %s%s", "$ENV{PATH}");
1e422769 148 }
c038024b 149 if ((mg = mg_find(*svp, PERL_MAGIC_envelem)) && MgTAINTEDDIR(mg)) {
1e422769
PP
150 TAINT;
151 taint_proper("Insecure directory in %s%s", "$ENV{PATH}");
152 }
79072805 153 }
79072805 154
c90c0ff4
PP
155#ifndef VMS
156 /* tainted $TERM is okay if it contains no metachars */
a4fc7abc 157 svp = hv_fetchs(GvHVn(PL_envgv),"TERM",FALSE);
c90c0ff4 158 if (svp && *svp && SvTAINTED(*svp)) {
8b6b16e7 159 STRLEN len;
284167a5 160 const bool was_tainted = TAINT_get;
cfd0369c 161 const char *t = SvPV_const(*svp, len);
8772537c 162 const char * const e = t + len;
9a9b5ec9 163
284167a5 164 TAINT_set(was_tainted);
9a9b5ec9
DM
165#ifdef NO_TAINT_SUPPORT
166 PERL_UNUSED_VAR(was_tainted);
167#endif
0eb30aeb 168 if (t < e && isWORDCHAR(*t))
c90c0ff4 169 t++;
0eb30aeb 170 while (t < e && (isWORDCHAR(*t) || strchr("-_.+", *t)))
c90c0ff4
PP
171 t++;
172 if (t < e) {
173 TAINT;
174 taint_proper("Insecure $ENV{%s}%s", "TERM");
175 }
176 }
177#endif /* !VMS */
178
7bac28a0 179 for (e = misc_env; *e; e++) {
0bd48802 180 SV * const * const svp = hv_fetch(GvHVn(PL_envgv), *e, strlen(*e), FALSE);
3280af22 181 if (svp && *svp != &PL_sv_undef && SvTAINTED(*svp)) {
7bac28a0
PP
182 TAINT;
183 taint_proper("Insecure $ENV{%s}%s", *e);
184 }
bbce6d69
PP
185 }
186}
66610fdd
RGS
187
188/*
189 * Local variables:
190 * c-indentation-style: bsd
191 * c-basic-offset: 4
14d04a33 192 * indent-tabs-mode: nil
66610fdd
RGS
193 * End:
194 *
14d04a33 195 * ex: set ts=8 sts=4 sw=4 et:
37442d52 196 */