Debian Codegraph @

lib/roken/issuid.c

raw
1 /*
2 * Copyright (c) 1998 - 2001 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 #include <config.h>
35
36 #ifdef HAVE_SYS_AUXV_H
37 #include <sys/auxv.h>
38 #endif
39
40 #include <errno.h>
41
42 #include "roken.h"
43
44 /* NetBSD calls AT_UID AT_RUID. Everyone else calls it AT_UID. */
45 #if defined(AT_EUID) && defined(AT_RUID) && !defined(AT_UID)
46 #define AT_UID AT_RUID
47 #endif
48 #if defined(AT_EGID) && defined(AT_RGID) && !defined(AT_GID)
49 #define AT_GID AT_RGID
50 #endif
51
52 #ifdef __GLIBC__
53 #ifdef __GLIBC_PREREQ
54 #define HAVE_GLIBC_API_VERSION_SUPPORT(maj, min) __GLIBC_PREREQ(maj, min)
55 #else
56 #define HAVE_GLIBC_API_VERSION_SUPPORT(maj, min) \
57 ((__GLIBC << 16) + GLIBC_MINOR >= ((maj) << 16) + (min))
58 #endif
59
60 #if HAVE_GLIBC_API_VERSION_SUPPORT(2, 19)
61 #define GETAUXVAL_SETS_ERRNO
62 #endif
63 #endif
64
65 #ifdef HAVE_GETAUXVAL
66 static unsigned long
67 rk_getauxval(unsigned long type)
68 {
69 errno = 0;
70 #ifdef GETAUXVAL_SETS_ERRNO
71 return getauxval(type);
72 #else
73 unsigned long ret = getauxval(type);
74
75 if (ret == 0)
76 errno = ENOENT;
77 return ret;
78 #endif
79 }
80 #define USE_RK_GETAUXVAL
81 #endif
82
83 /**
84 * Returns non-zero if the caller's process started as set-uid or
85 * set-gid (and therefore the environment cannot be trusted).
86 *
87 * @return Non-zero if the environment is not trusted.
88 */
89 ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
90 issuid(void)
91 {
92 /*
93 * We want to use issetugid(), but issetugid() is not the same on
94 * all OSes.
95 *
96 * On Illumos derivatives, OpenBSD, and Solaris issetugid() returns
97 * true IFF the program exec()ed was set-uid or set-gid.
98 *
99 * On NetBSD and FreeBSD issetugid() returns true if the program
100 * exec()ed was set-uid or set-gid, or if the process has switched
101 * UIDs/GIDs or otherwise changed privileges or is a descendant of
102 * such a process and has not exec()ed since.
103 *
104 * What we want here is to know only if the program exec()ed was
105 * set-uid or set-gid, so we can decide whether to trust the
106 * enviroment variables. We don't care if this was a process that
107 * started as root and later changed UIDs/privs whatever: since it
108 * started out as privileged, it inherited an environment from a
109 * privileged pre-exec self, and so on, so the environment is
110 * trusted.
111 *
112 * Therefore the FreeBSD/NetBSD issetugid() does us no good.
113 *
114 * Linux, meanwhile, has no issetugid() (at least glibc doesn't
115 * anyways).
116 *
117 * Systems that support ELF put an "auxilliary vector" on the stack
118 * prior to starting the RTLD, and this vector includes (optionally)
119 * information about the process' EUID, RUID, EGID, RGID, and so on
120 * at the time of exec(), which we can use to construct proper
121 * issetugid() functionality.
122 *
123 * Where available, we use the ELF auxilliary vector as a fallback
124 * if issetugid() is not available.
125 *
126 * All of this is as of late March 2015, and might become stale in
127 * the future.
128 */
129
130 #ifdef USE_RK_GETAUXVAL
131 /* If we have getauxval(), use that */
132
133 #if (defined(AT_EUID) && defined(AT_UID) || (defined(AT_EGID) && defined(AT_GID)))
134 int seen = 0;
135 #endif
136
137 #if defined(AT_EUID) && defined(AT_UID)
138 {
139 unsigned long euid;
140 unsigned long uid;
141
142 euid = rk_getauxval(AT_EUID);
143 if (errno == 0)
144 seen |= 1;
145 uid = rk_getauxval(AT_UID);
146 if (errno == 0)
147 seen |= 2;
148 if (euid != uid)
149 return 1;
150 }
151 #endif
152 #if defined(AT_EGID) && defined(AT_GID)
153 {
154 unsigned long egid;
155 unsigned long gid;
156
157 egid = rk_getauxval(AT_EGID);
158 if (errno == 0)
159 seen |= 4;
160 gid = rk_getauxval(AT_GID);
161 if (errno == 0)
162 seen |= 8;
163 if (egid != gid)
164 return 2;
165 }
166 #endif
167 #ifdef AT_SECURE
168 /* AT_SECURE is set if the program was set-id. */
169 if (rk_getauxval(AT_SECURE) != 0)
170 return 1;
171 #endif
172
173 #if (defined(AT_EUID) && defined(AT_UID) || (defined(AT_EGID) && defined(AT_GID)))
174 if (seen == 15)
175 return 0;
176 #endif
177
178 /* rk_getauxval() does set errno */
179 if (errno == 0)
180 return 0;
181 /*
182 * Fall through if we have getauxval() but we didn't have (or don't
183 * know if we don't have) the aux entries that we needed.
184 */
185 #endif /* USE_RK_GETAUXVAL */
186
187 #if defined(HAVE_ISSETUGID)
188 /*
189 * If we have issetugid(), use it.
190 *
191 * We may lose on some BSDs. This manifests as, for example,
192 * gss_store_cred() not honoring KRB5CCNAME.
193 */
194 return issetugid();
195 #endif /* USE_RK_GETAUXVAL */
196
197 /*
198 * Paranoia: for extra safety we ought to default to returning 1.
199 * But who knows what that might break where users link statically
200 * and use a.out, say. Also, on Windows we should always return 0.
201 *
202 * For now we stick to returning zero by default.
203 */
204
205 #if defined(HAVE_GETUID) && defined(HAVE_GETEUID)
206 if (getuid() != geteuid())
207 return 1;
208 #endif
209 #if defined(HAVE_GETGID) && defined(HAVE_GETEGID)
210 if (getgid() != getegid())
211 return 2;
212 #endif
213
214 return 0;
215 }