Setuid_linux.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  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. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. #include "util/Setuid.h"
  16. #include "memory/Allocator.h"
  17. #include <stdlib.h>
  18. #include <unistd.h>
  19. #include <string.h>
  20. #include <errno.h>
  21. #include <sys/socket.h>
  22. #include <netinet/in.h>
  23. #include <arpa/inet.h>
  24. #include <linux/capability.h>
  25. #include <sys/prctl.h>
  26. #ifndef CAP_TO_MASK
  27. #define CAP_TO_MASK(X) (1 << ((X) & 31))
  28. #endif
  29. #ifndef _LINUX_CAPABILITY_VERSION_3
  30. #define _LINUX_CAPABILITY_VERSION_3 0x20080522
  31. #endif
  32. #define PERMITTED_MASK CAP_TO_MASK(CAP_NET_ADMIN)
  33. static inline int capSet(cap_user_header_t hdr, cap_user_data_t data)
  34. {
  35. int capset(cap_user_header_t hdr, cap_user_data_t data);
  36. return capset(hdr, data);
  37. }
  38. static inline int capGet(cap_user_header_t hdr, cap_user_data_t data)
  39. {
  40. int capget(cap_user_header_t hdr, cap_user_data_t data);
  41. return capget(hdr, data);
  42. }
  43. Err_DEFUN Setuid_preSetuid(struct Allocator* alloc)
  44. {
  45. cap_user_header_t hdr = Allocator_calloc(alloc, sizeof(*hdr), 1);
  46. cap_user_data_t data = Allocator_calloc(alloc, sizeof(*data), 2);
  47. hdr->version = _LINUX_CAPABILITY_VERSION_3;
  48. hdr->pid = 0;
  49. if (capGet(hdr, data)) {
  50. Err_raise(alloc, "Error getting capabilities: [errno:%d (%s)]", errno, strerror(errno));
  51. }
  52. data->permitted &= PERMITTED_MASK | CAP_TO_MASK(CAP_SETUID) | CAP_TO_MASK(CAP_SETGID);
  53. data->effective = data->permitted;
  54. data->inheritable = 0;
  55. if (capSet(hdr, data)) {
  56. Err_raise(alloc, "Error setting capabilities: [errno:%d (%s)]", errno, strerror(errno));
  57. }
  58. if (prctl(PR_SET_KEEPCAPS, 1)) {
  59. Err_raise(alloc, "Error keeping capabilities: [errno:%d (%s)]", errno, strerror(errno));
  60. }
  61. return NULL;
  62. }
  63. Err_DEFUN Setuid_postSetuid(struct Allocator* alloc)
  64. {
  65. cap_user_header_t hdr = Allocator_calloc(alloc, sizeof(*hdr), 1);
  66. cap_user_data_t data = Allocator_calloc(alloc, sizeof(*data), 2);
  67. hdr->version = _LINUX_CAPABILITY_VERSION_3;
  68. hdr->pid = 0;
  69. if (capGet(hdr, data)) {
  70. Err_raise(alloc, "Error getting capabilities (post-setuid): [errno:%d (%s)]",
  71. errno, strerror(errno));
  72. }
  73. data->permitted &= PERMITTED_MASK;
  74. data->effective = data->permitted;
  75. data->inheritable = 0;
  76. if (capSet(hdr, data)) {
  77. Err_raise(alloc, "Error setting capabilities (post-setuid): [errno:%d (%s)]",
  78. errno, strerror(errno));
  79. }
  80. if (prctl(PR_SET_KEEPCAPS, 0)) {
  81. Err_raise(alloc, "Error un-keeping capabilities (post-setuid): [errno:%d (%s)]",
  82. errno, strerror(errno));
  83. }
  84. return NULL;
  85. }