Blame lib/secure_getenv.c

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