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