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