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