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