Add a test for [perl #17753].
[p5sagit/p5-mst-13.2.git] / taint.c
CommitLineData
d6376244 1/* taint.c
2 *
3f774658 3 * Copyright (c) 1997-2002, Larry Wall
d6376244 4 *
5 * You may distribute under the terms of either the GNU General Public
6 * License or the Artistic License, as specified in the README file.
7 *
8 */
9
a0d0e21e 10/*
11 * "...we will have peace, when you and all your works have perished--and
12 * the works of your dark master to whom you would deliver us. You are a
13 * liar, Saruman, and a corrupter of men's hearts." --Theoden
14 */
15
463ee0b2 16#include "EXTERN.h"
864dbfa3 17#define PERL_IN_TAINT_C
463ee0b2 18#include "perl.h"
19
20void
dff6d3cd 21Perl_taint_proper(pTHX_ const char *f, const char *s)
79072805 22{
bbce6d69 23 char *ug;
24
26e623cf 25#if defined(HAS_SETEUID) && defined(DEBUGGING)
26# if Uid_t_size == 1
27 {
28 UV uid = PL_uid;
29 UV euid = PL_euid;
30
31 DEBUG_u(PerlIO_printf(Perl_debug_log,
32 "%s %d %"UVuf" %"UVuf"\n",
33 s, PL_tainted, uid, euid));
34 }
35# else
36 {
37 IV uid = PL_uid;
38 IV euid = PL_euid;
39
40 DEBUG_u(PerlIO_printf(Perl_debug_log,
41 "%s %d %"IVdf" %"IVdf"\n",
42 s, PL_tainted, uid, euid));
43 }
44# endif
a52cb5f7 45#endif
1e422769 46
3280af22 47 if (PL_tainted) {
22c35a8c 48 if (!f)
49 f = PL_no_security;
3280af22 50 if (PL_euid != PL_uid)
bbce6d69 51 ug = " while running setuid";
3280af22 52 else if (PL_egid != PL_gid)
bbce6d69 53 ug = " while running setgid";
6537fe72 54 else if (PL_taint_warn)
55 ug = " while running with -t switch";
56 else
bbce6d69 57 ug = " while running with -T switch";
6537fe72 58 if (PL_unsafe || PL_taint_warn) {
59 if(ckWARN(WARN_TAINT))
9014280d 60 Perl_warner(aTHX_ packWARN(WARN_TAINT), f, s, ug);
6537fe72 61 }
62 else {
63 Perl_croak(aTHX_ f, s, ug);
64 }
79072805 65 }
66}
67
68void
864dbfa3 69Perl_taint_env(pTHX)
79072805 70{
71 SV** svp;
7bac28a0 72 MAGIC* mg;
73 char** e;
74 static char* misc_env[] = {
75 "IFS", /* most shells' inter-field separators */
c90c0ff4 76 "CDPATH", /* ksh dain bramage #1 */
77 "ENV", /* ksh dain bramage #2 */
78 "BASH_ENV", /* bash dain bramage -- I guess it's contagious */
7bac28a0 79 NULL
80 };
1e422769 81
142393a6 82 if (!PL_envgv)
83 return;
84
1e422769 85#ifdef VMS
82f1aded 86 {
1e422769 87 int i = 0;
0dc443ab 88 char name[10 + TYPE_DIGITS(int)] = "DCL$PATH";
1e422769 89
90 while (1) {
91 if (i)
92 (void)sprintf(name,"DCL$PATH;%d", i);
6b88bc9c 93 svp = hv_fetch(GvHVn(PL_envgv), name, strlen(name), FALSE);
94 if (!svp || *svp == &PL_sv_undef)
1e422769 95 break;
96 if (SvTAINTED(*svp)) {
97 TAINT;
98 taint_proper("Insecure %s%s", "$ENV{DCL$PATH}");
99 }
14befaf4 100 if ((mg = mg_find(*svp, PERL_MAGIC_envelem)) && MgTAINTEDDIR(mg)) {
1e422769 101 TAINT;
102 taint_proper("Insecure directory in %s%s", "$ENV{DCL$PATH}");
103 }
104 i++;
105 }
82f1aded 106 }
1e422769 107#endif /* VMS */
79072805 108
3280af22 109 svp = hv_fetch(GvHVn(PL_envgv),"PATH",4,FALSE);
1e422769 110 if (svp && *svp) {
111 if (SvTAINTED(*svp)) {
112 TAINT;
bbce6d69 113 taint_proper("Insecure %s%s", "$ENV{PATH}");
1e422769 114 }
14befaf4 115 if ((mg = mg_find(*svp, PERL_MAGIC_envelem)) && MgTAINTEDDIR(mg)) {
1e422769 116 TAINT;
117 taint_proper("Insecure directory in %s%s", "$ENV{PATH}");
118 }
79072805 119 }
79072805 120
c90c0ff4 121#ifndef VMS
122 /* tainted $TERM is okay if it contains no metachars */
3280af22 123 svp = hv_fetch(GvHVn(PL_envgv),"TERM",4,FALSE);
c90c0ff4 124 if (svp && *svp && SvTAINTED(*svp)) {
2d8e6c8d 125 STRLEN n_a;
3280af22 126 bool was_tainted = PL_tainted;
2d8e6c8d 127 char *t = SvPV(*svp, n_a);
128 char *e = t + n_a;
3280af22 129 PL_tainted = was_tainted;
c90c0ff4 130 if (t < e && isALNUM(*t))
131 t++;
d085cc71 132 while (t < e && (isALNUM(*t) || strchr("-_.+", *t)))
c90c0ff4 133 t++;
134 if (t < e) {
135 TAINT;
136 taint_proper("Insecure $ENV{%s}%s", "TERM");
137 }
138 }
139#endif /* !VMS */
140
7bac28a0 141 for (e = misc_env; *e; e++) {
3280af22 142 svp = hv_fetch(GvHVn(PL_envgv), *e, strlen(*e), FALSE);
143 if (svp && *svp != &PL_sv_undef && SvTAINTED(*svp)) {
7bac28a0 144 TAINT;
145 taint_proper("Insecure $ENV{%s}%s", *e);
146 }
bbce6d69 147 }
148}