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