Security.c 5.4 KB

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