This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Promote v5.36 usage and feature bundles doc
[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
a5aac01f
KW
26/*
27=for apidoc taint_proper
28
29Implements the L</TAINT_PROPER> macro, which you should generally use instead.
30
31=cut
32*/
33
463ee0b2 34void
e1c41806 35Perl_taint_proper(pTHX_ const char *f, const char *const s)
79072805 36{
f50027fc
KW
37 /* Don't use directly; instead use TAINT_PROPER
38 *
39 * Output a tainting violation, croaking unless we're just to warn.
bc2f1ca1
KW
40 * '_proper' is just to throw you off the scent */
41
26e623cf 42#if defined(HAS_SETEUID) && defined(DEBUGGING)
7918f24d
NC
43 PERL_ARGS_ASSERT_TAINT_PROPER;
44
26e623cf 45 {
1604cfb0
MS
46 const Uid_t uid = PerlProc_getuid();
47 const Uid_t euid = PerlProc_geteuid();
26e623cf 48
8b225b9c 49#if Uid_t_sign == 1 /* uid_t is unsigned. */
1604cfb0 50 DEBUG_u(PerlIO_printf(Perl_debug_log,
147e3846 51 "%s %d %" UVuf " %" UVuf "\n",
8b225b9c
JH
52 s, TAINT_get, (UV)uid, (UV)euid));
53#else /* uid_t is signed (Uid_t_sign == -1), or don't know. */
1604cfb0 54 DEBUG_u(PerlIO_printf(Perl_debug_log,
147e3846 55 "%s %d %" IVdf " %" IVdf "\n",
8b225b9c
JH
56 s, TAINT_get, (IV)uid, (IV)euid));
57#endif
26e623cf 58 }
a52cb5f7 59#endif
1e422769 60
284167a5 61 if (TAINT_get) {
1604cfb0
MS
62 const char *ug;
63
64 if (!f)
65 f = PL_no_security;
66 if (PerlProc_getuid() != PerlProc_geteuid())
67 ug = " while running setuid";
68 else if (PerlProc_getgid() != PerlProc_getegid())
69 ug = " while running setgid";
70 else if (TAINT_WARN_get)
6537fe72
MS
71 ug = " while running with -t switch";
72 else
1604cfb0 73 ug = " while running with -T switch";
5d37acd6 74
6fd06953 75 /* XXX because taint_proper adds extra format args, we can't
bc2f1ca1 76 * get the caller to check properly; so we just silence the warning
6fd06953 77 * and hope the callers aren't naughty */
7347ee54 78 GCC_DIAG_IGNORE_STMT(-Wformat-nonliteral);
1604cfb0
MS
79 if (PL_unsafe || TAINT_WARN_get) {
80 Perl_ck_warner_d(aTHX_ packWARN(WARN_TAINT), f, s, ug);
6537fe72
MS
81 }
82 else {
83 Perl_croak(aTHX_ f, s, ug);
84 }
7347ee54 85 GCC_DIAG_RESTORE_STMT;
5d37acd6 86
79072805
LW
87 }
88}
89
a5aac01f
KW
90/*
91=for apidoc taint_env
92
93Implements the L</TAINT_ENV> macro, which you should generally use instead.
94
95=cut
96*/
79072805 97void
864dbfa3 98Perl_taint_env(pTHX)
79072805 99{
f50027fc
KW
100 /* Don't use directly; instead use TAINT_ENV */
101
79072805 102 SV** svp;
27da23d5
JH
103 const char* const *e;
104 static const char* const misc_env[] = {
1604cfb0
MS
105 "IFS", /* most shells' inter-field separators */
106 "CDPATH", /* ksh dain bramage #1 */
107 "ENV", /* ksh dain bramage #2 */
108 "BASH_ENV", /* bash dain bramage -- I guess it's contagious */
e62a6770 109#ifdef WIN32
1604cfb0 110 "PERL5SHELL", /* used for system() on Windows */
e62a6770 111#endif
1604cfb0 112 NULL
7bac28a0 113 };
1e422769 114
c038024b
RGS
115 /* Don't bother if there's no *ENV glob */
116 if (!PL_envgv)
1604cfb0 117 return;
284167a5 118 /* If there's no %ENV hash or if it's not magical, croak, because
c038024b
RGS
119 * it probably doesn't reflect the actual environment */
120 if (!GvHV(PL_envgv) || !(SvRMAGICAL(GvHV(PL_envgv))
1604cfb0
MS
121 && mg_find((const SV *)GvHV(PL_envgv), PERL_MAGIC_env))) {
122 const bool was_tainted = TAINT_get;
123 const char * const name = GvENAME(PL_envgv);
124 TAINT;
125 if (strEQ(name,"ENV"))
126 /* hash alias */
127 taint_proper("%%ENV is aliased to %s%s", "another variable");
128 else
129 /* glob alias: report it in the error message */
130 taint_proper("%%ENV is aliased to %%%s%s", name);
131 /* this statement is reached under -t or -U */
132 TAINT_set(was_tainted);
9a9b5ec9
DM
133#ifdef NO_TAINT_SUPPORT
134 PERL_UNUSED_VAR(was_tainted);
135#endif
c038024b 136 }
142393a6 137
1e422769 138#ifdef VMS
82f1aded 139 {
1e422769 140 int i = 0;
0dc443ab 141 char name[10 + TYPE_DIGITS(int)] = "DCL$PATH";
86c11942 142 STRLEN len = 8; /* strlen(name) */
1e422769 143
144 while (1) {
19742f39 145 MAGIC* mg;
1604cfb0
MS
146 if (i)
147 len = my_snprintf(name, sizeof name, "DCL$PATH;%d", i);
148 svp = hv_fetch(GvHVn(PL_envgv), name, len, FALSE);
149 if (!svp || *svp == &PL_sv_undef)
150 break;
151 if (SvTAINTED(*svp)) {
152 TAINT;
153 taint_proper("Insecure %s%s", "$ENV{DCL$PATH}");
154 }
155 if ((mg = mg_find(*svp, PERL_MAGIC_envelem)) && MgTAINTEDDIR(mg)) {
156 TAINT;
157 taint_proper("Insecure directory in %s%s", "$ENV{DCL$PATH}");
158 }
159 i++;
1e422769 160 }
82f1aded 161 }
1e422769 162#endif /* VMS */
79072805 163
a4fc7abc 164 svp = hv_fetchs(GvHVn(PL_envgv),"PATH",FALSE);
1e422769 165 if (svp && *svp) {
19742f39 166 MAGIC* mg;
1604cfb0
MS
167 if (SvTAINTED(*svp)) {
168 TAINT;
169 taint_proper("Insecure %s%s", "$ENV{PATH}");
170 }
171 if ((mg = mg_find(*svp, PERL_MAGIC_envelem)) && MgTAINTEDDIR(mg)) {
172 TAINT;
173 taint_proper("Insecure directory in %s%s", "$ENV{PATH}");
174 }
79072805 175 }
79072805 176
c90c0ff4 177#ifndef VMS
178 /* tainted $TERM is okay if it contains no metachars */
a4fc7abc 179 svp = hv_fetchs(GvHVn(PL_envgv),"TERM",FALSE);
c90c0ff4 180 if (svp && *svp && SvTAINTED(*svp)) {
1604cfb0
MS
181 STRLEN len;
182 const bool was_tainted = TAINT_get;
183 const char *t = SvPV_const(*svp, len);
184 const char * const e = t + len;
9a9b5ec9 185
1604cfb0 186 TAINT_set(was_tainted);
9a9b5ec9
DM
187#ifdef NO_TAINT_SUPPORT
188 PERL_UNUSED_VAR(was_tainted);
189#endif
1604cfb0
MS
190 if (t < e && isWORDCHAR(*t))
191 t++;
192 while (t < e && (isWORDCHAR(*t) || memCHRs("-_.+", *t)))
193 t++;
194 if (t < e) {
195 TAINT;
196 taint_proper("Insecure $ENV{%s}%s", "TERM");
197 }
c90c0ff4 198 }
199#endif /* !VMS */
200
7bac28a0 201 for (e = misc_env; *e; e++) {
1604cfb0
MS
202 SV * const * const svp = hv_fetch(GvHVn(PL_envgv), *e, strlen(*e), FALSE);
203 if (svp && *svp != &PL_sv_undef && SvTAINTED(*svp)) {
204 TAINT;
205 taint_proper("Insecure $ENV{%s}%s", *e);
206 }
bbce6d69 207 }
208}
66610fdd
RGS
209
210/*
14d04a33 211 * ex: set ts=8 sts=4 sw=4 et:
37442d52 212 */