This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Use of the Uid_t_sign and casting instead of Uid_t_f.
[perl5.git] / taint.c
CommitLineData
d6376244
JH
1/* taint.c
2 *
663f364b 3 * Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
1129b882 4 * 2002, 2003, 2004, 2005, 2006, 2007, 2008 by Larry Wall and others
d6376244
JH
5 *
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Artistic License, as specified in the README file.
8 *
9 */
10
a0d0e21e 11/*
4ac71550
TC
12 * '...we will have peace, when you and all your works have perished--and
13 * the works of your dark master to whom you would deliver us. You are a
cdad3b53 14 * liar, Saruman, and a corrupter of men's hearts.' --Théoden
4ac71550
TC
15 *
16 * [p.580 of _The Lord of the Rings_, III/x: "The Voice of Saruman"]
a0d0e21e
LW
17 */
18
166f8a29
DM
19/* This file contains a few functions for handling data tainting in Perl
20 */
21
463ee0b2 22#include "EXTERN.h"
864dbfa3 23#define PERL_IN_TAINT_C
463ee0b2
LW
24#include "perl.h"
25
26void
e1c41806 27Perl_taint_proper(pTHX_ const char *f, const char *const s)
79072805 28{
26e623cf 29#if defined(HAS_SETEUID) && defined(DEBUGGING)
97aff369 30 dVAR;
7918f24d
NC
31
32 PERL_ARGS_ASSERT_TAINT_PROPER;
33
26e623cf 34 {
dfff4baf
BF
35 const Uid_t uid = PerlProc_getuid();
36 const Uid_t euid = PerlProc_geteuid();
26e623cf 37
8b225b9c 38#if Uid_t_sign == 1 /* uid_t is unsigned. */
8772537c 39 DEBUG_u(PerlIO_printf(Perl_debug_log,
8b225b9c
JH
40 "%s %d %"UVuf" %"UVuf"\n",
41 s, TAINT_get, (UV)uid, (UV)euid));
42#else /* uid_t is signed (Uid_t_sign == -1), or don't know. */
43 DEBUG_u(PerlIO_printf(Perl_debug_log,
44 "%s %d %"IVdf" %"IVdf"\n",
45 s, TAINT_get, (IV)uid, (IV)euid));
46#endif
26e623cf 47 }
a52cb5f7 48#endif
1e422769 49
284167a5 50 if (TAINT_get) {
8772537c
AL
51 const char *ug;
52
22c35a8c
GS
53 if (!f)
54 f = PL_no_security;
985213f2 55 if (PerlProc_getuid() != PerlProc_geteuid())
bbce6d69 56 ug = " while running setuid";
985213f2 57 else if (PerlProc_getgid() != PerlProc_getegid())
bbce6d69 58 ug = " while running setgid";
284167a5 59 else if (TAINT_WARN_get)
6537fe72
MS
60 ug = " while running with -t switch";
61 else
bbce6d69 62 ug = " while running with -T switch";
5d37acd6 63
6fd06953
DM
64 /* XXX because taint_proper adds extra format args, we can't
65 * get the caller to check properly; o we just silence the warning
66 * and hope the callers aren't naughty */
67 GCC_DIAG_IGNORE(-Wformat-nonliteral);
284167a5 68 if (PL_unsafe || TAINT_WARN_get) {
9b387841 69 Perl_ck_warner_d(aTHX_ packWARN(WARN_TAINT), f, s, ug);
6537fe72
MS
70 }
71 else {
72 Perl_croak(aTHX_ f, s, ug);
73 }
5d37acd6
DM
74 GCC_DIAG_RESTORE;
75
79072805
LW
76 }
77}
78
79void
864dbfa3 80Perl_taint_env(pTHX)
79072805 81{
97aff369 82 dVAR;
79072805 83 SV** svp;
7bac28a0 84 MAGIC* mg;
27da23d5
JH
85 const char* const *e;
86 static const char* const misc_env[] = {
7bac28a0 87 "IFS", /* most shells' inter-field separators */
c90c0ff4 88 "CDPATH", /* ksh dain bramage #1 */
89 "ENV", /* ksh dain bramage #2 */
90 "BASH_ENV", /* bash dain bramage -- I guess it's contagious */
e62a6770
RGS
91#ifdef WIN32
92 "PERL5SHELL", /* used for system() on Windows */
93#endif
7bac28a0 94 NULL
95 };
1e422769 96
c038024b
RGS
97 /* Don't bother if there's no *ENV glob */
98 if (!PL_envgv)
142393a6 99 return;
284167a5 100 /* If there's no %ENV hash or if it's not magical, croak, because
c038024b
RGS
101 * it probably doesn't reflect the actual environment */
102 if (!GvHV(PL_envgv) || !(SvRMAGICAL(GvHV(PL_envgv))
daba3364 103 && mg_find((const SV *)GvHV(PL_envgv), PERL_MAGIC_env))) {
284167a5 104 const bool was_tainted = TAINT_get;
8772537c 105 const char * const name = GvENAME(PL_envgv);
284167a5 106 TAINT;
c038024b
RGS
107 if (strEQ(name,"ENV"))
108 /* hash alias */
109 taint_proper("%%ENV is aliased to %s%s", "another variable");
110 else
111 /* glob alias: report it in the error message */
112 taint_proper("%%ENV is aliased to %%%s%s", name);
113 /* this statement is reached under -t or -U */
284167a5 114 TAINT_set(was_tainted);
9a9b5ec9
DM
115#ifdef NO_TAINT_SUPPORT
116 PERL_UNUSED_VAR(was_tainted);
117#endif
c038024b 118 }
142393a6 119
1e422769 120#ifdef VMS
82f1aded 121 {
1e422769 122 int i = 0;
0dc443ab 123 char name[10 + TYPE_DIGITS(int)] = "DCL$PATH";
86c11942 124 STRLEN len = 8; /* strlen(name) */
1e422769 125
126 while (1) {
127 if (i)
86c11942
NC
128 len = my_sprintf(name,"DCL$PATH;%d", i);
129 svp = hv_fetch(GvHVn(PL_envgv), name, len, FALSE);
6b88bc9c 130 if (!svp || *svp == &PL_sv_undef)
1e422769 131 break;
132 if (SvTAINTED(*svp)) {
133 TAINT;
134 taint_proper("Insecure %s%s", "$ENV{DCL$PATH}");
135 }
c038024b 136 if ((mg = mg_find(*svp, PERL_MAGIC_envelem)) && MgTAINTEDDIR(mg)) {
1e422769 137 TAINT;
138 taint_proper("Insecure directory in %s%s", "$ENV{DCL$PATH}");
139 }
140 i++;
141 }
82f1aded 142 }
1e422769 143#endif /* VMS */
79072805 144
a4fc7abc 145 svp = hv_fetchs(GvHVn(PL_envgv),"PATH",FALSE);
1e422769 146 if (svp && *svp) {
147 if (SvTAINTED(*svp)) {
148 TAINT;
bbce6d69 149 taint_proper("Insecure %s%s", "$ENV{PATH}");
1e422769 150 }
c038024b 151 if ((mg = mg_find(*svp, PERL_MAGIC_envelem)) && MgTAINTEDDIR(mg)) {
1e422769 152 TAINT;
153 taint_proper("Insecure directory in %s%s", "$ENV{PATH}");
154 }
79072805 155 }
79072805 156
c90c0ff4 157#ifndef VMS
158 /* tainted $TERM is okay if it contains no metachars */
a4fc7abc 159 svp = hv_fetchs(GvHVn(PL_envgv),"TERM",FALSE);
c90c0ff4 160 if (svp && *svp && SvTAINTED(*svp)) {
8b6b16e7 161 STRLEN len;
284167a5 162 const bool was_tainted = TAINT_get;
cfd0369c 163 const char *t = SvPV_const(*svp, len);
8772537c 164 const char * const e = t + len;
9a9b5ec9 165
284167a5 166 TAINT_set(was_tainted);
9a9b5ec9
DM
167#ifdef NO_TAINT_SUPPORT
168 PERL_UNUSED_VAR(was_tainted);
169#endif
0eb30aeb 170 if (t < e && isWORDCHAR(*t))
c90c0ff4 171 t++;
0eb30aeb 172 while (t < e && (isWORDCHAR(*t) || strchr("-_.+", *t)))
c90c0ff4 173 t++;
174 if (t < e) {
175 TAINT;
176 taint_proper("Insecure $ENV{%s}%s", "TERM");
177 }
178 }
179#endif /* !VMS */
180
7bac28a0 181 for (e = misc_env; *e; e++) {
0bd48802 182 SV * const * const svp = hv_fetch(GvHVn(PL_envgv), *e, strlen(*e), FALSE);
3280af22 183 if (svp && *svp != &PL_sv_undef && SvTAINTED(*svp)) {
7bac28a0 184 TAINT;
185 taint_proper("Insecure $ENV{%s}%s", *e);
186 }
bbce6d69 187 }
188}
66610fdd
RGS
189
190/*
191 * Local variables:
192 * c-indentation-style: bsd
193 * c-basic-offset: 4
14d04a33 194 * indent-tabs-mode: nil
66610fdd
RGS
195 * End:
196 *
14d04a33 197 * ex: set ts=8 sts=4 sw=4 et:
37442d52 198 */