Blame gl/secure_getenv.c

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