This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
YIELD more robustly.
[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 #include "perl.h"
9
10 void
11 taint_proper(const char *f, char *s)
12 {
13     dTHR;       /* just for taint */
14     char *ug;
15
16     DEBUG_u(PerlIO_printf(Perl_debug_log,
17             "%s %d %d %d\n", s, PL_tainted, PL_uid, PL_euid));
18
19     if (PL_tainted) {
20         if (PL_euid != PL_uid)
21             ug = " while running setuid";
22         else if (PL_egid != PL_gid)
23             ug = " while running setgid";
24         else
25             ug = " while running with -T switch";
26         if (!PL_unsafe)
27             croak(f, s, ug);
28         else if (ckWARN(WARN_TAINT))
29             warner(WARN_TAINT, f, s, ug);
30     }
31 }
32
33 void
34 taint_env(void)
35 {
36     SV** svp;
37     MAGIC* mg;
38     char** e;
39     static char* misc_env[] = {
40         "IFS",          /* most shells' inter-field separators */
41         "CDPATH",       /* ksh dain bramage #1 */
42         "ENV",          /* ksh dain bramage #2 */
43         "BASH_ENV",     /* bash dain bramage -- I guess it's contagious */
44         NULL
45     };
46
47 #ifdef VMS
48     int i = 0;
49     char name[10 + TYPE_DIGITS(int)] = "DCL$PATH";
50
51     while (1) {
52         if (i)
53             (void)sprintf(name,"DCL$PATH;%d", i);
54         svp = hv_fetch(GvHVn(PL_envgv), name, strlen(name), FALSE);
55         if (!svp || *svp == &PL_sv_undef)
56             break;
57         if (SvTAINTED(*svp)) {
58             dTHR;
59             TAINT;
60             taint_proper("Insecure %s%s", "$ENV{DCL$PATH}");
61         }
62         if ((mg = mg_find(*svp, 'e')) && MgTAINTEDDIR(mg)) {
63             dTHR;
64             TAINT;
65             taint_proper("Insecure directory in %s%s", "$ENV{DCL$PATH}");
66         }
67         i++;
68     }
69 #endif /* VMS */
70
71     svp = hv_fetch(GvHVn(PL_envgv),"PATH",4,FALSE);
72     if (svp && *svp) {
73         if (SvTAINTED(*svp)) {
74             dTHR;
75             TAINT;
76             taint_proper("Insecure %s%s", "$ENV{PATH}");
77         }
78         if ((mg = mg_find(*svp, 'e')) && MgTAINTEDDIR(mg)) {
79             dTHR;
80             TAINT;
81             taint_proper("Insecure directory in %s%s", "$ENV{PATH}");
82         }
83     }
84
85 #ifndef VMS
86     /* tainted $TERM is okay if it contains no metachars */
87     svp = hv_fetch(GvHVn(PL_envgv),"TERM",4,FALSE);
88     if (svp && *svp && SvTAINTED(*svp)) {
89         dTHR;   /* just for taint */
90         bool was_tainted = PL_tainted;
91         char *t = SvPV(*svp, PL_na);
92         char *e = t + PL_na;
93         PL_tainted = was_tainted;
94         if (t < e && isALNUM(*t))
95             t++;
96         while (t < e && (isALNUM(*t) || *t == '-' || *t == ':'))
97             t++;
98         if (t < e) {
99             TAINT;
100             taint_proper("Insecure $ENV{%s}%s", "TERM");
101         }
102     }
103 #endif /* !VMS */
104
105     for (e = misc_env; *e; e++) {
106         svp = hv_fetch(GvHVn(PL_envgv), *e, strlen(*e), FALSE);
107         if (svp && *svp != &PL_sv_undef && SvTAINTED(*svp)) {
108             dTHR;       /* just for taint */
109             TAINT;
110             taint_proper("Insecure $ENV{%s}%s", *e);
111         }
112     }
113 }