This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Copyright++. (Not all the toplevel *.h have one, it seems.)
[perl5.git] / taint.c
CommitLineData
a0d0e21e
LW
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
463ee0b2 7#include "EXTERN.h"
864dbfa3 8#define PERL_IN_TAINT_C
463ee0b2
LW
9#include "perl.h"
10
11void
dff6d3cd 12Perl_taint_proper(pTHX_ const char *f, const char *s)
79072805 13{
bbce6d69
PP
14 char *ug;
15
26e623cf
JH
16#if defined(HAS_SETEUID) && defined(DEBUGGING)
17# if Uid_t_size == 1
18 {
19 UV uid = PL_uid;
20 UV euid = PL_euid;
21
22 DEBUG_u(PerlIO_printf(Perl_debug_log,
23 "%s %d %"UVuf" %"UVuf"\n",
24 s, PL_tainted, uid, euid));
25 }
26# else
27 {
28 IV uid = PL_uid;
29 IV euid = PL_euid;
30
31 DEBUG_u(PerlIO_printf(Perl_debug_log,
32 "%s %d %"IVdf" %"IVdf"\n",
33 s, PL_tainted, uid, euid));
34 }
35# endif
a52cb5f7 36#endif
1e422769 37
3280af22 38 if (PL_tainted) {
22c35a8c
GS
39 if (!f)
40 f = PL_no_security;
3280af22 41 if (PL_euid != PL_uid)
bbce6d69 42 ug = " while running setuid";
3280af22 43 else if (PL_egid != PL_gid)
bbce6d69 44 ug = " while running setgid";
6537fe72
MS
45 else if (PL_taint_warn)
46 ug = " while running with -t switch";
47 else
bbce6d69 48 ug = " while running with -T switch";
6537fe72
MS
49 if (PL_unsafe || PL_taint_warn) {
50 if(ckWARN(WARN_TAINT))
51 Perl_warner(aTHX_ WARN_TAINT, f, s, ug);
52 }
53 else {
54 Perl_croak(aTHX_ f, s, ug);
55 }
79072805
LW
56 }
57}
58
59void
864dbfa3 60Perl_taint_env(pTHX)
79072805
LW
61{
62 SV** svp;
7bac28a0
PP
63 MAGIC* mg;
64 char** e;
65 static char* misc_env[] = {
66 "IFS", /* most shells' inter-field separators */
c90c0ff4
PP
67 "CDPATH", /* ksh dain bramage #1 */
68 "ENV", /* ksh dain bramage #2 */
69 "BASH_ENV", /* bash dain bramage -- I guess it's contagious */
7bac28a0
PP
70 NULL
71 };
1e422769 72
142393a6
TM
73 if (!PL_envgv)
74 return;
75
1e422769 76#ifdef VMS
82f1aded 77 {
1e422769 78 int i = 0;
0dc443ab 79 char name[10 + TYPE_DIGITS(int)] = "DCL$PATH";
1e422769
PP
80
81 while (1) {
82 if (i)
83 (void)sprintf(name,"DCL$PATH;%d", i);
6b88bc9c
GS
84 svp = hv_fetch(GvHVn(PL_envgv), name, strlen(name), FALSE);
85 if (!svp || *svp == &PL_sv_undef)
1e422769
PP
86 break;
87 if (SvTAINTED(*svp)) {
88 TAINT;
89 taint_proper("Insecure %s%s", "$ENV{DCL$PATH}");
90 }
14befaf4 91 if ((mg = mg_find(*svp, PERL_MAGIC_envelem)) && MgTAINTEDDIR(mg)) {
1e422769
PP
92 TAINT;
93 taint_proper("Insecure directory in %s%s", "$ENV{DCL$PATH}");
94 }
95 i++;
96 }
82f1aded 97 }
1e422769 98#endif /* VMS */
79072805 99
3280af22 100 svp = hv_fetch(GvHVn(PL_envgv),"PATH",4,FALSE);
1e422769
PP
101 if (svp && *svp) {
102 if (SvTAINTED(*svp)) {
103 TAINT;
bbce6d69 104 taint_proper("Insecure %s%s", "$ENV{PATH}");
1e422769 105 }
14befaf4 106 if ((mg = mg_find(*svp, PERL_MAGIC_envelem)) && MgTAINTEDDIR(mg)) {
1e422769
PP
107 TAINT;
108 taint_proper("Insecure directory in %s%s", "$ENV{PATH}");
109 }
79072805 110 }
79072805 111
c90c0ff4
PP
112#ifndef VMS
113 /* tainted $TERM is okay if it contains no metachars */
3280af22 114 svp = hv_fetch(GvHVn(PL_envgv),"TERM",4,FALSE);
c90c0ff4 115 if (svp && *svp && SvTAINTED(*svp)) {
2d8e6c8d 116 STRLEN n_a;
3280af22 117 bool was_tainted = PL_tainted;
2d8e6c8d
GS
118 char *t = SvPV(*svp, n_a);
119 char *e = t + n_a;
3280af22 120 PL_tainted = was_tainted;
c90c0ff4
PP
121 if (t < e && isALNUM(*t))
122 t++;
123 while (t < e && (isALNUM(*t) || *t == '-' || *t == ':'))
124 t++;
125 if (t < e) {
126 TAINT;
127 taint_proper("Insecure $ENV{%s}%s", "TERM");
128 }
129 }
130#endif /* !VMS */
131
7bac28a0 132 for (e = misc_env; *e; e++) {
3280af22
NIS
133 svp = hv_fetch(GvHVn(PL_envgv), *e, strlen(*e), FALSE);
134 if (svp && *svp != &PL_sv_undef && SvTAINTED(*svp)) {
7bac28a0
PP
135 TAINT;
136 taint_proper("Insecure $ENV{%s}%s", *e);
137 }
bbce6d69
PP
138 }
139}