This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Configure update.
[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
49#ifdef VMS
50 int i = 0;
0dc443ab 51 char name[10 + TYPE_DIGITS(int)] = "DCL$PATH";
1e422769 52
53 while (1) {
54 if (i)
55 (void)sprintf(name,"DCL$PATH;%d", i);
6b88bc9c
GS
56 svp = hv_fetch(GvHVn(PL_envgv), name, strlen(name), FALSE);
57 if (!svp || *svp == &PL_sv_undef)
1e422769 58 break;
59 if (SvTAINTED(*svp)) {
61bb5906 60 dTHR;
1e422769 61 TAINT;
62 taint_proper("Insecure %s%s", "$ENV{DCL$PATH}");
63 }
64 if ((mg = mg_find(*svp, 'e')) && MgTAINTEDDIR(mg)) {
61bb5906 65 dTHR;
1e422769 66 TAINT;
67 taint_proper("Insecure directory in %s%s", "$ENV{DCL$PATH}");
68 }
69 i++;
70 }
71#endif /* VMS */
79072805 72
3280af22 73 svp = hv_fetch(GvHVn(PL_envgv),"PATH",4,FALSE);
1e422769 74 if (svp && *svp) {
75 if (SvTAINTED(*svp)) {
aeea060c 76 dTHR;
1e422769 77 TAINT;
bbce6d69 78 taint_proper("Insecure %s%s", "$ENV{PATH}");
1e422769 79 }
80 if ((mg = mg_find(*svp, 'e')) && MgTAINTEDDIR(mg)) {
aeea060c 81 dTHR;
1e422769 82 TAINT;
83 taint_proper("Insecure directory in %s%s", "$ENV{PATH}");
84 }
79072805 85 }
79072805 86
c90c0ff4 87#ifndef VMS
88 /* tainted $TERM is okay if it contains no metachars */
3280af22 89 svp = hv_fetch(GvHVn(PL_envgv),"TERM",4,FALSE);
c90c0ff4 90 if (svp && *svp && SvTAINTED(*svp)) {
aeea060c 91 dTHR; /* just for taint */
3280af22
NIS
92 bool was_tainted = PL_tainted;
93 char *t = SvPV(*svp, PL_na);
94 char *e = t + PL_na;
95 PL_tainted = was_tainted;
c90c0ff4 96 if (t < e && isALNUM(*t))
97 t++;
98 while (t < e && (isALNUM(*t) || *t == '-' || *t == ':'))
99 t++;
100 if (t < e) {
101 TAINT;
102 taint_proper("Insecure $ENV{%s}%s", "TERM");
103 }
104 }
105#endif /* !VMS */
106
7bac28a0 107 for (e = misc_env; *e; e++) {
3280af22
NIS
108 svp = hv_fetch(GvHVn(PL_envgv), *e, strlen(*e), FALSE);
109 if (svp && *svp != &PL_sv_undef && SvTAINTED(*svp)) {
aeea060c 110 dTHR; /* just for taint */
7bac28a0 111 TAINT;
112 taint_proper("Insecure $ENV{%s}%s", *e);
113 }
bbce6d69 114 }
115}