This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Fix a typo in #4036.
[perl5.git] / taint.c
1 /*
2  * "...we will have peace, when you and all your works have perished--and
3  * the works of your dark master to whom you would deliver us.  You are a
4  * liar, Saruman, and a corrupter of men's hearts."  --Theoden
5  */
6
7 #include "EXTERN.h"
8 #define PERL_IN_TAINT_C
9 #include "perl.h"
10
11 void
12 Perl_taint_proper(pTHX_ const char *f, char *s)
13 {
14     dTHR;       /* just for taint */
15     char *ug;
16
17     DEBUG_u(PerlIO_printf(Perl_debug_log,
18             "%s %d %d %d\n", s, PL_tainted, PL_uid, PL_euid));
19
20     if (PL_tainted) {
21         if (!f)
22             f = PL_no_security;
23         if (PL_euid != PL_uid)
24             ug = " while running setuid";
25         else if (PL_egid != PL_gid)
26             ug = " while running setgid";
27         else
28             ug = " while running with -T switch";
29         if (!PL_unsafe)
30             Perl_croak(aTHX_ f, s, ug);
31         else if (ckWARN(WARN_TAINT))
32             Perl_warner(aTHX_ WARN_TAINT, f, s, ug);
33     }
34 }
35
36 void
37 Perl_taint_env(pTHX)
38 {
39     SV** svp;
40     MAGIC* mg;
41     char** e;
42     static char* misc_env[] = {
43         "IFS",          /* most shells' inter-field separators */
44         "CDPATH",       /* ksh dain bramage #1 */
45         "ENV",          /* ksh dain bramage #2 */
46         "BASH_ENV",     /* bash dain bramage -- I guess it's contagious */
47         NULL
48     };
49
50     if (!PL_envgv)
51         return;
52
53 #ifdef VMS
54     {
55     int i = 0;
56     char name[10 + TYPE_DIGITS(int)] = "DCL$PATH";
57
58     while (1) {
59         if (i)
60             (void)sprintf(name,"DCL$PATH;%d", i);
61         svp = hv_fetch(GvHVn(PL_envgv), name, strlen(name), FALSE);
62         if (!svp || *svp == &PL_sv_undef)
63             break;
64         if (SvTAINTED(*svp)) {
65             dTHR;
66             TAINT;
67             taint_proper("Insecure %s%s", "$ENV{DCL$PATH}");
68         }
69         if ((mg = mg_find(*svp, 'e')) && MgTAINTEDDIR(mg)) {
70             dTHR;
71             TAINT;
72             taint_proper("Insecure directory in %s%s", "$ENV{DCL$PATH}");
73         }
74         i++;
75     }
76   }
77 #endif /* VMS */
78
79     svp = hv_fetch(GvHVn(PL_envgv),"PATH",4,FALSE);
80     if (svp && *svp) {
81         if (SvTAINTED(*svp)) {
82             dTHR;
83             TAINT;
84             taint_proper("Insecure %s%s", "$ENV{PATH}");
85         }
86         if ((mg = mg_find(*svp, 'e')) && MgTAINTEDDIR(mg)) {
87             dTHR;
88             TAINT;
89             taint_proper("Insecure directory in %s%s", "$ENV{PATH}");
90         }
91     }
92
93 #ifndef VMS
94     /* tainted $TERM is okay if it contains no metachars */
95     svp = hv_fetch(GvHVn(PL_envgv),"TERM",4,FALSE);
96     if (svp && *svp && SvTAINTED(*svp)) {
97         dTHR;   /* just for taint */
98         STRLEN n_a;
99         bool was_tainted = PL_tainted;
100         char *t = SvPV(*svp, n_a);
101         char *e = t + n_a;
102         PL_tainted = was_tainted;
103         if (t < e && isALNUM(*t))
104             t++;
105         while (t < e && (isALNUM(*t) || *t == '-' || *t == ':'))
106             t++;
107         if (t < e) {
108             TAINT;
109             taint_proper("Insecure $ENV{%s}%s", "TERM");
110         }
111     }
112 #endif /* !VMS */
113
114     for (e = misc_env; *e; e++) {
115         svp = hv_fetch(GvHVn(PL_envgv), *e, strlen(*e), FALSE);
116         if (svp && *svp != &PL_sv_undef && SvTAINTED(*svp)) {
117             dTHR;       /* just for taint */
118             TAINT;
119             taint_proper("Insecure $ENV{%s}%s", *e);
120         }
121     }
122 }