"perl -T -P" dumps core on OpenBSD and Linux
[p5sagit/p5-mst-13.2.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 (!f)
21             f = PL_no_security;
22         if (PL_euid != PL_uid)
23             ug = " while running setuid";
24         else if (PL_egid != PL_gid)
25             ug = " while running setgid";
26         else
27             ug = " while running with -T switch";
28         if (!PL_unsafe)
29             croak(f, s, ug);
30         else if (ckWARN(WARN_TAINT))
31             warner(WARN_TAINT, f, s, ug);
32     }
33 }
34
35 void
36 taint_env(void)
37 {
38     SV** svp;
39     MAGIC* mg;
40     char** e;
41     static char* misc_env[] = {
42         "IFS",          /* most shells' inter-field separators */
43         "CDPATH",       /* ksh dain bramage #1 */
44         "ENV",          /* ksh dain bramage #2 */
45         "BASH_ENV",     /* bash dain bramage -- I guess it's contagious */
46         NULL
47     };
48
49     if (!PL_envgv)
50         return;
51
52 #ifdef VMS
53     int i = 0;
54     char name[10 + TYPE_DIGITS(int)] = "DCL$PATH";
55
56     while (1) {
57         if (i)
58             (void)sprintf(name,"DCL$PATH;%d", i);
59         svp = hv_fetch(GvHVn(PL_envgv), name, strlen(name), FALSE);
60         if (!svp || *svp == &PL_sv_undef)
61             break;
62         if (SvTAINTED(*svp)) {
63             dTHR;
64             TAINT;
65             taint_proper("Insecure %s%s", "$ENV{DCL$PATH}");
66         }
67         if ((mg = mg_find(*svp, 'e')) && MgTAINTEDDIR(mg)) {
68             dTHR;
69             TAINT;
70             taint_proper("Insecure directory in %s%s", "$ENV{DCL$PATH}");
71         }
72         i++;
73     }
74 #endif /* VMS */
75
76     svp = hv_fetch(GvHVn(PL_envgv),"PATH",4,FALSE);
77     if (svp && *svp) {
78         if (SvTAINTED(*svp)) {
79             dTHR;
80             TAINT;
81             taint_proper("Insecure %s%s", "$ENV{PATH}");
82         }
83         if ((mg = mg_find(*svp, 'e')) && MgTAINTEDDIR(mg)) {
84             dTHR;
85             TAINT;
86             taint_proper("Insecure directory in %s%s", "$ENV{PATH}");
87         }
88     }
89
90 #ifndef VMS
91     /* tainted $TERM is okay if it contains no metachars */
92     svp = hv_fetch(GvHVn(PL_envgv),"TERM",4,FALSE);
93     if (svp && *svp && SvTAINTED(*svp)) {
94         dTHR;   /* just for taint */
95         STRLEN n_a;
96         bool was_tainted = PL_tainted;
97         char *t = SvPV(*svp, n_a);
98         char *e = t + n_a;
99         PL_tainted = was_tainted;
100         if (t < e && isALNUM(*t))
101             t++;
102         while (t < e && (isALNUM(*t) || *t == '-' || *t == ':'))
103             t++;
104         if (t < e) {
105             TAINT;
106             taint_proper("Insecure $ENV{%s}%s", "TERM");
107         }
108     }
109 #endif /* !VMS */
110
111     for (e = misc_env; *e; e++) {
112         svp = hv_fetch(GvHVn(PL_envgv), *e, strlen(*e), FALSE);
113         if (svp && *svp != &PL_sv_undef && SvTAINTED(*svp)) {
114             dTHR;       /* just for taint */
115             TAINT;
116             taint_proper("Insecure $ENV{%s}%s", *e);
117         }
118     }
119 }