idpriv.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /* Dropping uid/gid privileges of the current process.
  2. Copyright (C) 2009-2015 Free Software Foundation, Inc.
  3. This program 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 of the License, or
  6. (at your option) any later version.
  7. This program 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. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. #ifndef _IDPRIV_H
  14. #define _IDPRIV_H
  15. #ifdef __cplusplus
  16. extern "C" {
  17. #endif
  18. /* This module allows programs which are installed with setuid or setgid bit
  19. (and which therefore initially run with an effective user id or group id
  20. different from the one of the current user) to drop their uid or gid
  21. privilege, either permanently or temporarily.
  22. It is absolutely necessary to minimize the amount of code that is running
  23. with escalated privileges (e.g. with effective uid = root). The reason is
  24. that any bug or exploit in a part of a program that is running with
  25. escalated privileges is a security vulnerability that - upon discovery -
  26. puts the users in danger and requires immediate fixing. Then consider that
  27. there's a bug every 10 or 20 lines of code on average...
  28. For programs that temporarily drop privileges but have the ability to
  29. restore them later, there are additionally the dangers that
  30. - Any bug in the non-privileged part of the program may be used to
  31. create invalid data structures that will trigger security
  32. vulnerabilities in the privileged part of the program.
  33. - Code execution exploits in the non-privileged part of the program may
  34. be used to invoke the function that restores high privileges and then
  35. execute additional arbitrary code.
  36. 1) The usual, and reasonably safe, way to minimize the amount of code
  37. running with privileges is to create a separate executable, with setuid
  38. or setgid bit, that contains only code for the tasks that require
  39. privileges (and,of course, strict checking of the arguments, so that the
  40. program cannot be abused). The main program is installed without setuid
  41. or setgid bit.
  42. 2) A less safe way is to do some privileged tasks at the beginning of the
  43. program's run, and drop privileges permanently as soon as possible.
  44. Note: There may still be security issues if the privileged task puts
  45. sensitive data into the process memory or opens communication channels
  46. to restricted facilities.
  47. 3) The most unsafe way is to drop privileges temporarily for most of the
  48. main program but to re-enable them for the duration of privileged tasks.
  49. As explained above, this approach has uncontrollable dangers for
  50. security.
  51. This approach is normally not usable in multithreaded programs, because
  52. you cannot know what kind of system calls the other threads could be
  53. doing during the time the privileges are enabled.
  54. With approach 1, you don't need gnulib modules.
  55. With approach 2, you need the gnulib module 'idpriv-drop'.
  56. With approach 3, you need the gnulib module 'idpriv-droptemp'. But really,
  57. you should better stay away from this approach.
  58. */
  59. /* For more in-depth discussion of these topics, see the papers/articles
  60. * Hao Chen, David Wagner, Drew Dean: Setuid Demystified
  61. <http://www.usenix.org/events/sec02/full_papers/chen/chen.pdf>
  62. * Dan Tsafrir, Dilma da Silva, David Wagner: The Murky Issue of Changing
  63. Process Identity: Revising "Setuid Demystified"
  64. <http://www.eecs.berkeley.edu/~daw/papers/setuid-login08b.pdf>
  65. <http://code.google.com/p/change-process-identity/>
  66. * Dhruv Mohindra: Observe correct revocation order while relinquishing
  67. privileges
  68. <https://www.securecoding.cert.org/confluence/display/seccode/POS36-C.+Observe+correct+revocation+order+while+relinquishing+privileges>
  69. */
  70. /* For approach 2. */
  71. /* Drop the uid and gid privileges of the current process.
  72. Return 0 if successful, or -1 with errno set upon failure. The recommended
  73. handling of failure is to terminate the process. */
  74. extern int idpriv_drop (void);
  75. /* For approach 3. */
  76. /* Drop the uid and gid privileges of the current process in a way that allows
  77. them to be restored later.
  78. Return 0 if successful, or -1 with errno set upon failure. The recommended
  79. handling of failure is to terminate the process. */
  80. extern int idpriv_temp_drop (void);
  81. /* Restore the uid and gid privileges of the current process.
  82. Return 0 if successful, or -1 with errno set upon failure. The recommended
  83. handling of failure is to not perform the actions that require the escalated
  84. privileges. */
  85. extern int idpriv_temp_restore (void);
  86. #ifdef __cplusplus
  87. }
  88. #endif
  89. #endif /* _IDPRIV_H */