Commit | Line | Data |
---|---|---|
d6376244 JH |
1 | /* taint.c |
2 | * | |
e6906430 | 3 | * Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, |
ae53e38e | 4 | * 2000, 2001, 2002, 2003, 2004, 2005, 2006, by Larry Wall and others |
d6376244 JH |
5 | * |
6 | * You may distribute under the terms of either the GNU General Public | |
7 | * License or the Artistic License, as specified in the README file. | |
8 | * | |
9 | */ | |
10 | ||
a0d0e21e LW |
11 | /* |
12 | * "...we will have peace, when you and all your works have perished--and | |
13 | * the works of your dark master to whom you would deliver us. You are a | |
14 | * liar, Saruman, and a corrupter of men's hearts." --Theoden | |
15 | */ | |
16 | ||
40d34c0d SB |
17 | /* This file contains a few functions for handling data tainting in Perl |
18 | */ | |
19 | ||
463ee0b2 | 20 | #include "EXTERN.h" |
864dbfa3 | 21 | #define PERL_IN_TAINT_C |
463ee0b2 LW |
22 | #include "perl.h" |
23 | ||
24 | void | |
dff6d3cd | 25 | Perl_taint_proper(pTHX_ const char *f, const char *s) |
79072805 | 26 | { |
26e623cf JH |
27 | #if defined(HAS_SETEUID) && defined(DEBUGGING) |
28 | # if Uid_t_size == 1 | |
29 | { | |
4f1e9d25 AL |
30 | const UV uid = PL_uid; |
31 | const UV euid = PL_euid; | |
26e623cf | 32 | |
4f1e9d25 | 33 | DEBUG_u(PerlIO_printf(Perl_debug_log, |
26e623cf JH |
34 | "%s %d %"UVuf" %"UVuf"\n", |
35 | s, PL_tainted, uid, euid)); | |
36 | } | |
37 | # else | |
38 | { | |
4f1e9d25 AL |
39 | const IV uid = PL_uid; |
40 | const IV euid = PL_euid; | |
26e623cf | 41 | |
4f1e9d25 | 42 | DEBUG_u(PerlIO_printf(Perl_debug_log, |
26e623cf JH |
43 | "%s %d %"IVdf" %"IVdf"\n", |
44 | s, PL_tainted, uid, euid)); | |
45 | } | |
46 | # endif | |
a52cb5f7 | 47 | #endif |
1e422769 | 48 | |
3280af22 | 49 | if (PL_tainted) { |
4f1e9d25 AL |
50 | const char *ug; |
51 | ||
22c35a8c GS |
52 | if (!f) |
53 | f = PL_no_security; | |
3280af22 | 54 | if (PL_euid != PL_uid) |
bbce6d69 | 55 | ug = " while running setuid"; |
3280af22 | 56 | else if (PL_egid != PL_gid) |
bbce6d69 | 57 | ug = " while running setgid"; |
6537fe72 MS |
58 | else if (PL_taint_warn) |
59 | ug = " while running with -t switch"; | |
60 | else | |
bbce6d69 | 61 | ug = " while running with -T switch"; |
6537fe72 MS |
62 | if (PL_unsafe || PL_taint_warn) { |
63 | if(ckWARN(WARN_TAINT)) | |
9014280d | 64 | Perl_warner(aTHX_ packWARN(WARN_TAINT), f, s, ug); |
6537fe72 MS |
65 | } |
66 | else { | |
67 | Perl_croak(aTHX_ f, s, ug); | |
68 | } | |
79072805 LW |
69 | } |
70 | } | |
71 | ||
72 | void | |
864dbfa3 | 73 | Perl_taint_env(pTHX) |
79072805 LW |
74 | { |
75 | SV** svp; | |
7bac28a0 | 76 | MAGIC* mg; |
fe20fd30 JH |
77 | const char* const *e; |
78 | static const char* const misc_env[] = { | |
7bac28a0 | 79 | "IFS", /* most shells' inter-field separators */ |
c90c0ff4 | 80 | "CDPATH", /* ksh dain bramage #1 */ |
81 | "ENV", /* ksh dain bramage #2 */ | |
82 | "BASH_ENV", /* bash dain bramage -- I guess it's contagious */ | |
7bac28a0 | 83 | NULL |
84 | }; | |
1e422769 | 85 | |
c038024b | 86 | /* Don't bother if there's no *ENV glob */ |
142393a6 TM |
87 | if (!PL_envgv) |
88 | return; | |
c038024b RGS |
89 | /* If there's no %ENV hash of if it's not magical, croak, because |
90 | * it probably doesn't reflect the actual environment */ | |
91 | if (!GvHV(PL_envgv) || !(SvRMAGICAL(GvHV(PL_envgv)) | |
92 | && mg_find((SV*)GvHV(PL_envgv), PERL_MAGIC_env))) { | |
c05e0e2f | 93 | const bool was_tainted = PL_tainted; |
4f1e9d25 | 94 | const char * const name = GvENAME(PL_envgv); |
c038024b RGS |
95 | PL_tainted = TRUE; |
96 | if (strEQ(name,"ENV")) | |
97 | /* hash alias */ | |
98 | taint_proper("%%ENV is aliased to %s%s", "another variable"); | |
99 | else | |
100 | /* glob alias: report it in the error message */ | |
101 | taint_proper("%%ENV is aliased to %%%s%s", name); | |
102 | /* this statement is reached under -t or -U */ | |
103 | PL_tainted = was_tainted; | |
104 | } | |
142393a6 | 105 | |
1e422769 | 106 | #ifdef VMS |
82f1aded | 107 | { |
1e422769 | 108 | int i = 0; |
0dc443ab | 109 | char name[10 + TYPE_DIGITS(int)] = "DCL$PATH"; |
37724321 | 110 | STRLEN len = 8; /* strlen(name) */ |
1e422769 | 111 | |
112 | while (1) { | |
113 | if (i) | |
37724321 NC |
114 | len = my_sprintf(name,"DCL$PATH;%d", i); |
115 | svp = hv_fetch(GvHVn(PL_envgv), name, len, FALSE); | |
6b88bc9c | 116 | if (!svp || *svp == &PL_sv_undef) |
1e422769 | 117 | break; |
118 | if (SvTAINTED(*svp)) { | |
119 | TAINT; | |
120 | taint_proper("Insecure %s%s", "$ENV{DCL$PATH}"); | |
121 | } | |
14befaf4 | 122 | if ((mg = mg_find(*svp, PERL_MAGIC_envelem)) && MgTAINTEDDIR(mg)) { |
1e422769 | 123 | TAINT; |
124 | taint_proper("Insecure directory in %s%s", "$ENV{DCL$PATH}"); | |
125 | } | |
126 | i++; | |
127 | } | |
82f1aded | 128 | } |
1e422769 | 129 | #endif /* VMS */ |
79072805 | 130 | |
d7559646 | 131 | svp = hv_fetchs(GvHVn(PL_envgv),"PATH",FALSE); |
1e422769 | 132 | if (svp && *svp) { |
133 | if (SvTAINTED(*svp)) { | |
134 | TAINT; | |
bbce6d69 | 135 | taint_proper("Insecure %s%s", "$ENV{PATH}"); |
1e422769 | 136 | } |
14befaf4 | 137 | if ((mg = mg_find(*svp, PERL_MAGIC_envelem)) && MgTAINTEDDIR(mg)) { |
1e422769 | 138 | TAINT; |
139 | taint_proper("Insecure directory in %s%s", "$ENV{PATH}"); | |
140 | } | |
79072805 | 141 | } |
79072805 | 142 | |
c90c0ff4 | 143 | #ifndef VMS |
144 | /* tainted $TERM is okay if it contains no metachars */ | |
d7559646 | 145 | svp = hv_fetchs(GvHVn(PL_envgv),"TERM",FALSE); |
c90c0ff4 | 146 | if (svp && *svp && SvTAINTED(*svp)) { |
db1c9db1 | 147 | STRLEN len; |
e2b56717 | 148 | const bool was_tainted = PL_tainted; |
71a0dd65 | 149 | const char *t = SvPV_const(*svp, len); |
4f1e9d25 | 150 | const char * const e = t + len; |
3280af22 | 151 | PL_tainted = was_tainted; |
c90c0ff4 | 152 | if (t < e && isALNUM(*t)) |
153 | t++; | |
bda19f49 | 154 | while (t < e && (isALNUM(*t) || strchr("-_.+", *t))) |
c90c0ff4 | 155 | t++; |
156 | if (t < e) { | |
157 | TAINT; | |
158 | taint_proper("Insecure $ENV{%s}%s", "TERM"); | |
159 | } | |
160 | } | |
161 | #endif /* !VMS */ | |
162 | ||
7bac28a0 | 163 | for (e = misc_env; *e; e++) { |
8c89da26 | 164 | SV * const * const svp = hv_fetch(GvHVn(PL_envgv), *e, strlen(*e), FALSE); |
3280af22 | 165 | if (svp && *svp != &PL_sv_undef && SvTAINTED(*svp)) { |
7bac28a0 | 166 | TAINT; |
167 | taint_proper("Insecure $ENV{%s}%s", *e); | |
168 | } | |
bbce6d69 | 169 | } |
170 | } | |
d8294a4d NC |
171 | |
172 | /* | |
173 | * Local variables: | |
174 | * c-indentation-style: bsd | |
175 | * c-basic-offset: 4 | |
176 | * indent-tabs-mode: t | |
177 | * End: | |
178 | * | |
179 | * ex: set ts=8 sts=4 sw=4 noet: | |
180 | */ |