Setuid_linux.c 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 <http://www.gnu.org/licenses/>.
  14. */
  15. #include "util/Setuid.h"
  16. #include "memory/Allocator.h"
  17. #include "exception/Except.h"
  18. //#include <stdio.h>
  19. #include <stdlib.h>
  20. #include <unistd.h>
  21. #include <string.h>
  22. #include <errno.h>
  23. #include <sys/socket.h>
  24. #include <netinet/in.h>
  25. #include <arpa/inet.h>
  26. #include <linux/capability.h>
  27. #include <sys/prctl.h>
  28. #ifndef CAP_TO_MASK
  29. #define CAP_TO_MASK(X) (1 << ((X) & 31))
  30. #endif
  31. #define PERMITTED_MASK CAP_TO_MASK(CAP_NET_ADMIN)
  32. static inline int capSet(cap_user_header_t hdr, cap_user_data_t data)
  33. {
  34. int capset(cap_user_header_t hdr, cap_user_data_t data);
  35. return capset(hdr, data);
  36. }
  37. static inline int capGet(cap_user_header_t hdr, cap_user_data_t data)
  38. {
  39. int capget(cap_user_header_t hdr, cap_user_data_t data);
  40. return capget(hdr, data);
  41. }
  42. void Setuid_preSetuid(struct Allocator* alloc, struct Except* eh)
  43. {
  44. cap_user_header_t hdr = Allocator_calloc(alloc, sizeof(*hdr), 1);
  45. cap_user_data_t data = Allocator_calloc(alloc, sizeof(*data), 1);
  46. hdr->version = _LINUX_CAPABILITY_VERSION;
  47. hdr->pid = 0;
  48. if (capGet(hdr, data)) {
  49. Except_throw(eh, "Error getting capabilities: [errno:%d (%s)]", errno, strerror(errno));
  50. }
  51. data->permitted &= PERMITTED_MASK | CAP_TO_MASK(CAP_SETUID) | CAP_TO_MASK(CAP_SETGID);
  52. data->effective = data->permitted;
  53. data->inheritable = 0;
  54. if (capSet(hdr, data)) {
  55. Except_throw(eh, "Error setting capabilities: [errno:%d (%s)]", errno, strerror(errno));
  56. }
  57. if (prctl(PR_SET_KEEPCAPS, 1)) {
  58. Except_throw(eh, "Error keeping capabilities: [errno:%d (%s)]", errno, strerror(errno));
  59. }
  60. }
  61. void Setuid_postSetuid(struct Allocator* alloc, struct Except* eh)
  62. {
  63. cap_user_header_t hdr = Allocator_calloc(alloc, sizeof(*hdr), 1);
  64. cap_user_data_t data = Allocator_calloc(alloc, sizeof(*data), 1);
  65. hdr->version = _LINUX_CAPABILITY_VERSION;
  66. hdr->pid = 0;
  67. if (capGet(hdr, data)) {
  68. Except_throw(eh, "Error getting capabilities (post-setuid): [errno:%d (%s)]",
  69. errno, strerror(errno));
  70. }
  71. data->permitted &= PERMITTED_MASK;
  72. data->effective = data->permitted;
  73. data->inheritable = 0;
  74. if (capSet(hdr, data)) {
  75. Except_throw(eh, "Error setting capabilities (post-setuid): [errno:%d (%s)]",
  76. errno, strerror(errno));
  77. }
  78. if (prctl(PR_SET_KEEPCAPS, 0)) {
  79. Except_throw(eh, "Error un-keeping capabilities (post-setuid): [errno:%d (%s)]",
  80. errno, strerror(errno));
  81. }
  82. }