c8c6800c46970d6c57b046657f4afd39fe090090
[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     char *ug;
14
15     DEBUG_u(PerlIO_printf(Perl_debug_log,
16             "%s %d %d %d\n", s, tainted, uid, euid));
17
18     if (tainted) {
19         if (euid != uid)
20             ug = " while running setuid";
21         else if (egid != gid)
22             ug = " while running setgid";
23         else
24             ug = " while running with -T switch";
25         if (!unsafe)
26             croak(f, s, ug);
27         else if (dowarn)
28             warn(f, s, ug);
29     }
30 }
31
32 void
33 taint_env(void)
34 {
35     SV** svp;
36     MAGIC* mg;
37     char** e;
38     static char* misc_env[] = {
39         "IFS",          /* most shells' inter-field separators */
40         "CDPATH",       /* ksh dain bramage #1 */
41         "ENV",          /* ksh dain bramage #2 */
42         "BASH_ENV",     /* bash dain bramage -- I guess it's contagious */
43         NULL
44     };
45
46 #ifdef VMS
47     int i = 0;
48     char name[10 + TYPE_DIGITS(int)] = "DCL$PATH";
49
50     while (1) {
51         if (i)
52             (void)sprintf(name,"DCL$PATH;%d", i);
53         svp = hv_fetch(GvHVn(envgv), name, strlen(name), FALSE);
54         if (!svp || *svp == &sv_undef)
55             break;
56         if (SvTAINTED(*svp)) {
57             TAINT;
58             taint_proper("Insecure %s%s", "$ENV{DCL$PATH}");
59         }
60         if ((mg = mg_find(*svp, 'e')) && MgTAINTEDDIR(mg)) {
61             TAINT;
62             taint_proper("Insecure directory in %s%s", "$ENV{DCL$PATH}");
63         }
64         i++;
65     }
66 #endif /* VMS */
67
68     svp = hv_fetch(GvHVn(envgv),"PATH",4,FALSE);
69     if (svp && *svp) {
70         if (SvTAINTED(*svp)) {
71             TAINT;
72             taint_proper("Insecure %s%s", "$ENV{PATH}");
73         }
74         if ((mg = mg_find(*svp, 'e')) && MgTAINTEDDIR(mg)) {
75             TAINT;
76             taint_proper("Insecure directory in %s%s", "$ENV{PATH}");
77         }
78     }
79
80 #ifndef VMS
81     /* tainted $TERM is okay if it contains no metachars */
82     svp = hv_fetch(GvHVn(envgv),"TERM",4,FALSE);
83     if (svp && *svp && SvTAINTED(*svp)) {
84         bool was_tainted = tainted;
85         char *t = SvPV(*svp, na);
86         char *e = t + na;
87         tainted = was_tainted;
88         if (t < e && isALNUM(*t))
89             t++;
90         while (t < e && (isALNUM(*t) || *t == '-' || *t == ':'))
91             t++;
92         if (t < e) {
93             TAINT;
94             taint_proper("Insecure $ENV{%s}%s", "TERM");
95         }
96     }
97 #endif /* !VMS */
98
99     for (e = misc_env; *e; e++) {
100         svp = hv_fetch(GvHVn(envgv), *e, strlen(*e), FALSE);
101         if (svp && *svp != &sv_undef && SvTAINTED(*svp)) {
102             TAINT;
103             taint_proper("Insecure $ENV{%s}%s", *e);
104         }
105     }
106 }