Blame gl/secure_getenv.c

Packit aea12f
/* Look up an environment variable, returning NULL in insecure situations.
Packit aea12f
Packit Service 991b93
   Copyright 2013-2020 Free Software Foundation, Inc.
Packit aea12f
Packit aea12f
   This program is free software: you can redistribute it and/or modify it
Packit Service 991b93
   under the terms of the GNU Lesser General Public License as published
Packit Service 991b93
   by the Free Software Foundation; either version 2.1 of the License, or
Packit aea12f
   (at your option) any later version.
Packit aea12f
Packit aea12f
   This program is distributed in the hope that it will be useful,
Packit aea12f
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit aea12f
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service 991b93
   Lesser General Public License for more details.
Packit aea12f
Packit Service 991b93
   You should have received a copy of the GNU Lesser General Public License
Packit aea12f
   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
Packit aea12f
Packit aea12f
#include <config.h>
Packit aea12f
Packit aea12f
#include <stdlib.h>
Packit aea12f
Packit aea12f
#if !HAVE___SECURE_GETENV
Packit aea12f
# if HAVE_ISSETUGID || (HAVE_GETUID && HAVE_GETEUID && HAVE_GETGID && HAVE_GETEGID)
Packit aea12f
#  include <unistd.h>
Packit aea12f
# endif
Packit aea12f
#endif
Packit aea12f
Packit aea12f
char *
Packit aea12f
secure_getenv (char const *name)
Packit aea12f
{
Packit aea12f
#if HAVE___SECURE_GETENV /* glibc */
Packit aea12f
  return __secure_getenv (name);
Packit aea12f
#elif HAVE_ISSETUGID /* OS X, FreeBSD, NetBSD, OpenBSD */
Packit aea12f
  if (issetugid ())
Packit aea12f
    return NULL;
Packit aea12f
  return getenv (name);
Packit aea12f
#elif HAVE_GETUID && HAVE_GETEUID && HAVE_GETGID && HAVE_GETEGID /* other Unix */
Packit aea12f
  if (geteuid () != getuid () || getegid () != getgid ())
Packit aea12f
    return NULL;
Packit aea12f
  return getenv (name);
Packit aea12f
#elif defined _WIN32 && ! defined __CYGWIN__ /* native Windows */
Packit aea12f
  /* On native Windows, there is no such concept as setuid or setgid binaries.
Packit aea12f
     - Programs launched as system services have high privileges, but they don't
Packit aea12f
       inherit environment variables from a user.
Packit aea12f
     - Programs launched by a user with "Run as Administrator" have high
Packit aea12f
       privileges and use the environment variables, but the user has been asked
Packit aea12f
       whether he agrees.
Packit aea12f
     - Programs launched by a user without "Run as Administrator" cannot gain
Packit aea12f
       high privileges, therefore there is no risk. */
Packit aea12f
  return getenv (name);
Packit aea12f
#else
Packit aea12f
  return NULL;
Packit aea12f
#endif
Packit aea12f
}