Blame lib/secure_getenv.c

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