Security.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. #define _GNU_SOURCE // chroot(), MAP_ANONYMOUS
  16. #include "benc/Dict.h"
  17. #include "benc/String.h"
  18. #include "exception/Err.h"
  19. #include "util/log/Log.h"
  20. #include "util/Security.h"
  21. #include "memory/Allocator.h"
  22. #include "util/Setuid.h"
  23. #include "util/events/EventBase.h"
  24. #include "util/events/Timeout.h"
  25. #include <sys/resource.h>
  26. #include <sys/types.h>
  27. #include <pwd.h>
  28. #include <unistd.h>
  29. #include <errno.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <sys/mman.h>
  33. #include <stdio.h>
  34. Dict* Security_getUser(char* userName, struct Allocator* retAlloc)
  35. {
  36. struct passwd* pw;
  37. if (userName) {
  38. pw = getpwnam(userName);
  39. } else {
  40. pw = getpwuid(getuid());
  41. }
  42. Dict* ret = Dict_new(retAlloc);
  43. if (!pw) {
  44. Dict_putString(ret, String_new("error", retAlloc),
  45. String_printf(retAlloc, "Could not find user [%s]", strerror(errno)),
  46. retAlloc);
  47. return ret;
  48. }
  49. Dict_putString(ret, String_new("error", retAlloc), String_new("none", retAlloc), retAlloc);
  50. Dict_putString(ret, String_new("name", retAlloc), String_new(pw->pw_name, retAlloc), retAlloc);
  51. Dict_putInt(ret, String_new("uid", retAlloc), pw->pw_uid, retAlloc);
  52. Dict_putInt(ret, String_new("gid", retAlloc), pw->pw_gid, retAlloc);
  53. return ret;
  54. }
  55. Err_DEFUN Security_setUser(int uid,
  56. int gid,
  57. bool keepNetAdmin,
  58. struct Log* logger,
  59. struct Allocator* alloc)
  60. {
  61. int gidErrno = 0;
  62. int uidErrno = 0;
  63. if (keepNetAdmin) {
  64. Err(Setuid_preSetuid(alloc));
  65. }
  66. if (gid && setgid(gid)) {
  67. gidErrno = errno;
  68. }
  69. if (setuid(uid)) {
  70. // errno is global and could get overwritten by Setuid_postSetuid()
  71. uidErrno = errno;
  72. }
  73. if (keepNetAdmin) {
  74. Err(Setuid_postSetuid(alloc));
  75. }
  76. if (uidErrno > 0) {
  77. Err_raise(alloc, "Failed to set UID [%s]", strerror(uidErrno));
  78. }
  79. if (uid != (int) getuid()) {
  80. Err_raise(alloc, "Failed to set UID but seemed to succeed");
  81. }
  82. if (gidErrno > 0) {
  83. Err_raise(alloc, "Failed to set GID [%s]", strerror(gidErrno));
  84. }
  85. if (gid != (int) getgid()) {
  86. Err_raise(alloc, "Failed to set GID but seemed to succeed");
  87. }
  88. return NULL;
  89. }
  90. static int canOpenFiles()
  91. {
  92. int file = dup(0);
  93. close(file);
  94. return file >= 0;
  95. }
  96. Err_DEFUN Security_nofiles(struct Allocator* errAlloc)
  97. {
  98. #if !defined(RLIMIT_NOFILE) && defined(RLIMIT_OFILE)
  99. #define RLIMIT_NOFILE RLIMIT_OFILE
  100. #endif
  101. if (!canOpenFiles()) {
  102. Err_raise(errAlloc, "Unable to dupe stdin");
  103. }
  104. if (setrlimit(RLIMIT_NOFILE, &(struct rlimit){ 0, 0 })) {
  105. Err_raise(errAlloc, "Failed to set open file limit to 0 [%s]", strerror(errno));
  106. }
  107. if (canOpenFiles()) {
  108. Err_raise(errAlloc, "Still able to dupe stdin after setting number of files to 0!");
  109. }
  110. return NULL;
  111. }
  112. Err_DEFUN Security_noforks(struct Allocator* errAlloc)
  113. {
  114. if (setrlimit(RLIMIT_NPROC, &(struct rlimit){ 0, 0 })) {
  115. Err_raise(errAlloc, "Failed to set fork limit to 0 [%s]", strerror(errno));
  116. }
  117. return NULL;
  118. }
  119. Err_DEFUN Security_chroot(char* root, struct Allocator* errAlloc)
  120. {
  121. if (chdir(root)) {
  122. Err_raise(errAlloc, "chdir(%s) -> [%s]", root, strerror(errno));
  123. }
  124. if (chroot(root)) {
  125. Err_raise(errAlloc, "chroot(%s) -> [%s]", root, strerror(errno));
  126. }
  127. return NULL;
  128. }
  129. struct Security_pvt
  130. {
  131. struct Security pub;
  132. struct Allocator* setupAlloc;
  133. struct Log* log;
  134. Identity
  135. };
  136. void Security_setupComplete(struct Security* security)
  137. {
  138. struct Security_pvt* sec = Identity_check((struct Security_pvt*) security);
  139. sec->pub.setupComplete = 1;
  140. Allocator_free(sec->setupAlloc);
  141. }
  142. static void fail(void* vSec)
  143. {
  144. struct Security_pvt* sec = Identity_check((struct Security_pvt*) vSec);
  145. Log_critical(sec->log, "Security_setupComplete() not called in time, exiting");
  146. exit(232);
  147. }
  148. struct Security* Security_new(struct Allocator* alloc, struct Log* log, EventBase_t* base)
  149. {
  150. struct Security_pvt* sec = Allocator_calloc(alloc, sizeof(struct Security_pvt), 1);
  151. Identity_set(sec);
  152. sec->setupAlloc = Allocator_child(alloc);
  153. Timeout_setInterval(fail, sec, 20000, base, sec->setupAlloc);
  154. sec->log = log;
  155. return &sec->pub;
  156. }
  157. Err_DEFUN Security_checkPermissions(struct Security_Permissions** outP, struct Allocator* alloc)
  158. {
  159. struct Security_Permissions* out =
  160. Allocator_calloc(alloc, sizeof(struct Security_Permissions), 1);
  161. out->noOpenFiles = !canOpenFiles();
  162. out->uid = getuid();
  163. *outP = out;
  164. return NULL;
  165. }