Setuid_linux.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. #define PERMITTED_MASK CAP_TO_MASK(CAP_NET_ADMIN)
  29. static inline int capSet(cap_user_header_t hdr, cap_user_data_t data)
  30. {
  31. int capset(cap_user_header_t hdr, cap_user_data_t data);
  32. return capset(hdr, data);
  33. }
  34. static inline int capGet(cap_user_header_t hdr, cap_user_data_t data)
  35. {
  36. int capget(cap_user_header_t hdr, cap_user_data_t data);
  37. return capget(hdr, data);
  38. }
  39. void Setuid_preSetuid(struct Allocator* alloc, struct Except* eh)
  40. {
  41. cap_user_header_t hdr = Allocator_calloc(alloc, sizeof(*hdr), 1);
  42. cap_user_data_t data = Allocator_calloc(alloc, sizeof(*data), 1);
  43. hdr->version = _LINUX_CAPABILITY_VERSION;
  44. hdr->pid = 0;
  45. if (capGet(hdr, data)) {
  46. Except_throw(eh, "Error getting capabilities: [errno:%d (%s)]", errno, strerror(errno));
  47. }
  48. data->permitted &= PERMITTED_MASK | CAP_TO_MASK(CAP_SETUID) | CAP_TO_MASK(CAP_SETGID);
  49. data->effective = data->permitted;
  50. data->inheritable = 0;
  51. if (capSet(hdr, data)) {
  52. Except_throw(eh, "Error setting capabilities: [errno:%d (%s)]", errno, strerror(errno));
  53. }
  54. if (prctl(PR_SET_KEEPCAPS, 1)) {
  55. Except_throw(eh, "Error keeping capabilities: [errno:%d (%s)]", errno, strerror(errno));
  56. }
  57. }
  58. void Setuid_postSetuid(struct Allocator* alloc, struct Except* eh)
  59. {
  60. cap_user_header_t hdr = Allocator_calloc(alloc, sizeof(*hdr), 1);
  61. cap_user_data_t data = Allocator_calloc(alloc, sizeof(*data), 1);
  62. hdr->version = _LINUX_CAPABILITY_VERSION;
  63. hdr->pid = 0;
  64. if (capGet(hdr, data)) {
  65. Except_throw(eh, "Error getting capabilities (post-setuid): [errno:%d (%s)]",
  66. errno, strerror(errno));
  67. }
  68. data->permitted &= PERMITTED_MASK;
  69. data->effective = data->permitted;
  70. data->inheritable = 0;
  71. if (capSet(hdr, data)) {
  72. Except_throw(eh, "Error setting capabilities (post-setuid): [errno:%d (%s)]",
  73. errno, strerror(errno));
  74. }
  75. if (prctl(PR_SET_KEEPCAPS, 0)) {
  76. Except_throw(eh, "Error un-keeping capabilities (post-setuid): [errno:%d (%s)]",
  77. errno, strerror(errno));
  78. }
  79. }