Blame gl/secure_getenv.c

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