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