secure_getenv.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /* Copyright (C) 2017-2022 Free Software Foundation, Inc.
  2. This file is part of GCC.
  3. GCC is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3, or (at your option)
  6. any later version.
  7. GCC is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. Under Section 7 of GPL version 3, you are granted additional
  12. permissions described in the GCC Runtime Library Exception, version
  13. 3.1, as published by the Free Software Foundation.
  14. You should have received a copy of the GNU General Public License and
  15. a copy of the GCC Runtime Library Exception along with this program;
  16. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  17. <http://www.gnu.org/licenses/>. */
  18. #ifndef _SECURE_GETENV_H
  19. #define _SECURE_GETENV_H 1
  20. /* Secure getenv() which returns NULL if running as SUID/SGID. */
  21. #ifndef HAVE_SECURE_GETENV
  22. #if defined (HAVE_UNISTD_H) && defined (HAVE_GETUID) \
  23. && defined (HAVE_GETEUID) && defined (HAVE_GETGID) \
  24. && defined (HAVE_GETEGID)
  25. #include <unistd.h>
  26. #if SUPPORTS_WEAKREF && defined (HAVE___SECURE_GETENV)
  27. static char* weak_secure_getenv (const char*)
  28. __attribute__((__weakref__("__secure_getenv")));
  29. #endif
  30. /* Implementation of secure_getenv() for targets where it is not provided but
  31. we have at least means to test real and effective IDs. */
  32. static inline char *
  33. secure_getenv (const char *name)
  34. {
  35. #if SUPPORTS_WEAKREF && defined (HAVE___SECURE_GETENV)
  36. if (weak_secure_getenv)
  37. return weak_secure_getenv (name);
  38. #endif
  39. if ((getuid () == geteuid ()) && (getgid () == getegid ()))
  40. return getenv (name);
  41. else
  42. return NULL;
  43. }
  44. #else
  45. #define secure_getenv getenv
  46. #endif
  47. #endif
  48. #endif /* _SECURE_GETENV_H. */