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