xref: /freebsd/crypto/krb5/src/util/support/secure_getenv.c (revision 7f2fe78b9dd5f51c821d771b63d2e096f6fd49e9)
1*7f2fe78bSCy Schubert /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2*7f2fe78bSCy Schubert /* util/support/secure_getenv.c - secure_getenv() portability support */
3*7f2fe78bSCy Schubert /*
4*7f2fe78bSCy Schubert  * Copyright (C) 2019 by the Massachusetts Institute of Technology.
5*7f2fe78bSCy Schubert  * All rights reserved.
6*7f2fe78bSCy Schubert  *
7*7f2fe78bSCy Schubert  * Redistribution and use in source and binary forms, with or without
8*7f2fe78bSCy Schubert  * modification, are permitted provided that the following conditions
9*7f2fe78bSCy Schubert  * are met:
10*7f2fe78bSCy Schubert  *
11*7f2fe78bSCy Schubert  * * Redistributions of source code must retain the above copyright
12*7f2fe78bSCy Schubert  *   notice, this list of conditions and the following disclaimer.
13*7f2fe78bSCy Schubert  *
14*7f2fe78bSCy Schubert  * * Redistributions in binary form must reproduce the above copyright
15*7f2fe78bSCy Schubert  *   notice, this list of conditions and the following disclaimer in
16*7f2fe78bSCy Schubert  *   the documentation and/or other materials provided with the
17*7f2fe78bSCy Schubert  *   distribution.
18*7f2fe78bSCy Schubert  *
19*7f2fe78bSCy Schubert  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20*7f2fe78bSCy Schubert  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21*7f2fe78bSCy Schubert  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22*7f2fe78bSCy Schubert  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23*7f2fe78bSCy Schubert  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
24*7f2fe78bSCy Schubert  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25*7f2fe78bSCy Schubert  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26*7f2fe78bSCy Schubert  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27*7f2fe78bSCy Schubert  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28*7f2fe78bSCy Schubert  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29*7f2fe78bSCy Schubert  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
30*7f2fe78bSCy Schubert  * OF THE POSSIBILITY OF SUCH DAMAGE.
31*7f2fe78bSCy Schubert  */
32*7f2fe78bSCy Schubert 
33*7f2fe78bSCy Schubert /*
34*7f2fe78bSCy Schubert  * This file contains the fallback implementation for secure_getenv(), which is
35*7f2fe78bSCy Schubert  * currently only provided by glibc 2.17 and later.  The goal is to ignore the
36*7f2fe78bSCy Schubert  * environment if this process is (or previously was) running at elevated
37*7f2fe78bSCy Schubert  * privilege compared to the calling process.
38*7f2fe78bSCy Schubert  *
39*7f2fe78bSCy Schubert  * In this fallback version we compare the real and effective uid/gid, and also
40*7f2fe78bSCy Schubert  * compare the saved uid/gid if possible.  These comparisons detect a setuid or
41*7f2fe78bSCy Schubert  * setgid process which is still running with elevated privilege; if we can
42*7f2fe78bSCy Schubert  * fetch the saved uid/gid, we also detect a process which has temporarily
43*7f2fe78bSCy Schubert  * dropped privilege with seteuid() or setegid().  These comparisons do not
44*7f2fe78bSCy Schubert  * detect the case where a setuid or setgid process has permanently dropped
45*7f2fe78bSCy Schubert  * privilege before the library initializer ran; this is not ideal because such
46*7f2fe78bSCy Schubert  * a process may possess a privileged resource or have privileged information
47*7f2fe78bSCy Schubert  * in its address space.
48*7f2fe78bSCy Schubert  *
49*7f2fe78bSCy Schubert  * Heimdal also looks at the ELF aux vector in /proc/self/auxv to determine the
50*7f2fe78bSCy Schubert  * starting uid/euid/gid/euid on Solaris/Illumos and NetBSD.  On FreeBSD this
51*7f2fe78bSCy Schubert  * approach can determine the executable path to do a stat() check.  We do not
52*7f2fe78bSCy Schubert  * go to this length due to the amount of code required.
53*7f2fe78bSCy Schubert  *
54*7f2fe78bSCy Schubert  * The BSDs and Solaris provide issetugid(), but the FreeBSD and NetBSD
55*7f2fe78bSCy Schubert  * versions are not useful; they return true if a non-setuid/setgid executable
56*7f2fe78bSCy Schubert  * is run by root and drops privilege, such as Apache httpd.  We do not want to
57*7f2fe78bSCy Schubert  * ignore the process environment in this case.
58*7f2fe78bSCy Schubert  *
59*7f2fe78bSCy Schubert  * On some platforms a process may have elevated privilege via mechanisms other
60*7f2fe78bSCy Schubert  * than setuid/setgid.  glibc's secure_getenv() should detect these cases on
61*7f2fe78bSCy Schubert  * Linux; we do not detect them in this fallback version.
62*7f2fe78bSCy Schubert  */
63*7f2fe78bSCy Schubert 
64*7f2fe78bSCy Schubert #include "k5-platform.h"
65*7f2fe78bSCy Schubert 
66*7f2fe78bSCy Schubert static int elevated_privilege = 0;
67*7f2fe78bSCy Schubert 
68*7f2fe78bSCy Schubert MAKE_INIT_FUNCTION(k5_secure_getenv_init);
69*7f2fe78bSCy Schubert 
70*7f2fe78bSCy Schubert int
k5_secure_getenv_init()71*7f2fe78bSCy Schubert k5_secure_getenv_init()
72*7f2fe78bSCy Schubert {
73*7f2fe78bSCy Schubert     int saved_errno = errno;
74*7f2fe78bSCy Schubert 
75*7f2fe78bSCy Schubert #ifdef HAVE_GETRESUID
76*7f2fe78bSCy Schubert     {
77*7f2fe78bSCy Schubert         uid_t r, e, s;
78*7f2fe78bSCy Schubert         if (getresuid(&r, &e, &s) == 0) {
79*7f2fe78bSCy Schubert             if (r != e || r != s)
80*7f2fe78bSCy Schubert                 elevated_privilege = 1;
81*7f2fe78bSCy Schubert         }
82*7f2fe78bSCy Schubert     }
83*7f2fe78bSCy Schubert #else
84*7f2fe78bSCy Schubert     if (getuid() != geteuid())
85*7f2fe78bSCy Schubert         elevated_privilege = 1;
86*7f2fe78bSCy Schubert #endif
87*7f2fe78bSCy Schubert 
88*7f2fe78bSCy Schubert #ifdef HAVE_GETRESGID
89*7f2fe78bSCy Schubert     {
90*7f2fe78bSCy Schubert         gid_t r, e, s;
91*7f2fe78bSCy Schubert         if (!elevated_privilege && getresgid(&r, &e, &s) == 0) {
92*7f2fe78bSCy Schubert             if (r != e || r != s)
93*7f2fe78bSCy Schubert                 elevated_privilege = 1;
94*7f2fe78bSCy Schubert         }
95*7f2fe78bSCy Schubert     }
96*7f2fe78bSCy Schubert #else
97*7f2fe78bSCy Schubert     if (!elevated_privilege && getgid() != getegid())
98*7f2fe78bSCy Schubert         elevated_privilege = 1;
99*7f2fe78bSCy Schubert #endif
100*7f2fe78bSCy Schubert 
101*7f2fe78bSCy Schubert     errno = saved_errno;
102*7f2fe78bSCy Schubert     return 0;
103*7f2fe78bSCy Schubert }
104*7f2fe78bSCy Schubert 
105*7f2fe78bSCy Schubert char *
k5_secure_getenv(const char * name)106*7f2fe78bSCy Schubert k5_secure_getenv(const char *name)
107*7f2fe78bSCy Schubert {
108*7f2fe78bSCy Schubert     if (CALL_INIT_FUNCTION(k5_secure_getenv_init) != 0)
109*7f2fe78bSCy Schubert         return NULL;
110*7f2fe78bSCy Schubert     return elevated_privilege ? NULL : getenv(name);
111*7f2fe78bSCy Schubert }
112