Path: blob/main/crypto/krb5/src/util/support/secure_getenv.c
34889 views
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */1/* util/support/secure_getenv.c - secure_getenv() portability support */2/*3* Copyright (C) 2019 by the Massachusetts Institute of Technology.4* All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9*10* * Redistributions of source code must retain the above copyright11* notice, this list of conditions and the following disclaimer.12*13* * Redistributions in binary form must reproduce the above copyright14* notice, this list of conditions and the following disclaimer in15* the documentation and/or other materials provided with the16* distribution.17*18* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS19* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT20* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS21* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE22* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,23* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES24* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR25* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)26* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,27* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)28* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED29* OF THE POSSIBILITY OF SUCH DAMAGE.30*/3132/*33* This file contains the fallback implementation for secure_getenv(), which is34* currently only provided by glibc 2.17 and later. The goal is to ignore the35* environment if this process is (or previously was) running at elevated36* privilege compared to the calling process.37*38* In this fallback version we compare the real and effective uid/gid, and also39* compare the saved uid/gid if possible. These comparisons detect a setuid or40* setgid process which is still running with elevated privilege; if we can41* fetch the saved uid/gid, we also detect a process which has temporarily42* dropped privilege with seteuid() or setegid(). These comparisons do not43* detect the case where a setuid or setgid process has permanently dropped44* privilege before the library initializer ran; this is not ideal because such45* a process may possess a privileged resource or have privileged information46* in its address space.47*48* Heimdal also looks at the ELF aux vector in /proc/self/auxv to determine the49* starting uid/euid/gid/euid on Solaris/Illumos and NetBSD. On FreeBSD this50* approach can determine the executable path to do a stat() check. We do not51* go to this length due to the amount of code required.52*53* The BSDs and Solaris provide issetugid(), but the FreeBSD and NetBSD54* versions are not useful; they return true if a non-setuid/setgid executable55* is run by root and drops privilege, such as Apache httpd. We do not want to56* ignore the process environment in this case.57*58* On some platforms a process may have elevated privilege via mechanisms other59* than setuid/setgid. glibc's secure_getenv() should detect these cases on60* Linux; we do not detect them in this fallback version.61*/6263#include "k5-platform.h"6465static int elevated_privilege = 0;6667MAKE_INIT_FUNCTION(k5_secure_getenv_init);6869int70k5_secure_getenv_init(void)71{72int saved_errno = errno;7374#ifdef HAVE_GETRESUID75{76uid_t r, e, s;77if (getresuid(&r, &e, &s) == 0) {78if (r != e || r != s)79elevated_privilege = 1;80}81}82#else83if (getuid() != geteuid())84elevated_privilege = 1;85#endif8687#ifdef HAVE_GETRESGID88{89gid_t r, e, s;90if (!elevated_privilege && getresgid(&r, &e, &s) == 0) {91if (r != e || r != s)92elevated_privilege = 1;93}94}95#else96if (!elevated_privilege && getgid() != getegid())97elevated_privilege = 1;98#endif99100errno = saved_errno;101return 0;102}103104char *105k5_secure_getenv(const char *name)106{107if (CALL_INIT_FUNCTION(k5_secure_getenv_init) != 0)108return NULL;109return elevated_privilege ? NULL : getenv(name);110}111112113