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