This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Trivial patch for HP-UX 11 and shared libperl
[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 (!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     {
54     int i = 0;
55     char name[10 + TYPE_DIGITS(int)] = "DCL$PATH";
56
57     while (1) {
58         if (i)
59             (void)sprintf(name,"DCL$PATH;%d", i);
60         svp = hv_fetch(GvHVn(PL_envgv), name, strlen(name), FALSE);
61         if (!svp || *svp == &PL_sv_undef)
62             break;
63         if (SvTAINTED(*svp)) {
64             dTHR;
65             TAINT;
66             taint_proper("Insecure %s%s", "$ENV{DCL$PATH}");
67         }
68         if ((mg = mg_find(*svp, 'e')) && MgTAINTEDDIR(mg)) {
69             dTHR;
70             TAINT;
71             taint_proper("Insecure directory in %s%s", "$ENV{DCL$PATH}");
72         }
73         i++;
74     }
75   }
76 #endif /* VMS */
77
78     svp = hv_fetch(GvHVn(PL_envgv),"PATH",4,FALSE);
79     if (svp && *svp) {
80         if (SvTAINTED(*svp)) {
81             dTHR;
82             TAINT;
83             taint_proper("Insecure %s%s", "$ENV{PATH}");
84         }
85         if ((mg = mg_find(*svp, 'e')) && MgTAINTEDDIR(mg)) {
86             dTHR;
87             TAINT;
88             taint_proper("Insecure directory in %s%s", "$ENV{PATH}");
89         }
90     }
91
92 #ifndef VMS
93     /* tainted $TERM is okay if it contains no metachars */
94     svp = hv_fetch(GvHVn(PL_envgv),"TERM",4,FALSE);
95     if (svp && *svp && SvTAINTED(*svp)) {
96         dTHR;   /* just for taint */
97         STRLEN n_a;
98         bool was_tainted = PL_tainted;
99         char *t = SvPV(*svp, n_a);
100         char *e = t + n_a;
101         PL_tainted = was_tainted;
102         if (t < e && isALNUM(*t))
103             t++;
104         while (t < e && (isALNUM(*t) || *t == '-' || *t == ':'))
105             t++;
106         if (t < e) {
107             TAINT;
108             taint_proper("Insecure $ENV{%s}%s", "TERM");
109         }
110     }
111 #endif /* !VMS */
112
113     for (e = misc_env; *e; e++) {
114         svp = hv_fetch(GvHVn(PL_envgv), *e, strlen(*e), FALSE);
115         if (svp && *svp != &PL_sv_undef && SvTAINTED(*svp)) {
116             dTHR;       /* just for taint */
117             TAINT;
118             taint_proper("Insecure $ENV{%s}%s", *e);
119         }
120     }
121 }