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