1
0

Security.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. static const unsigned long cfgMaxMemoryBytes = 100000000;
  37. Dict* Security_getUser(char* userName, struct Allocator* retAlloc)
  38. {
  39. struct passwd* pw;
  40. if (userName) {
  41. pw = getpwnam(userName);
  42. } else {
  43. pw = getpwuid(getuid());
  44. }
  45. Dict* ret = Dict_new(retAlloc);
  46. if (!pw) {
  47. Dict_putString(ret, String_new("error", retAlloc),
  48. String_printf(retAlloc, "Could not find user [%s]", strerror(errno)),
  49. retAlloc);
  50. return ret;
  51. }
  52. Dict_putString(ret, String_new("error", retAlloc), String_new("none", retAlloc), retAlloc);
  53. Dict_putString(ret, String_new("name", retAlloc), String_new(pw->pw_name, retAlloc), retAlloc);
  54. Dict_putInt(ret, String_new("uid", retAlloc), pw->pw_uid, retAlloc);
  55. return ret;
  56. }
  57. void Security_setUser(int uid,
  58. bool keepNetAdmin,
  59. struct Log* logger,
  60. struct Except* eh,
  61. struct Allocator* alloc)
  62. {
  63. if (keepNetAdmin) {
  64. Setuid_preSetuid(alloc, eh);
  65. }
  66. int ret = setuid(uid);
  67. if (keepNetAdmin) {
  68. Setuid_postSetuid(alloc, eh);
  69. }
  70. if (ret) {
  71. Except_throw(eh, "Failed to set UID [%s]", strerror(errno));
  72. }
  73. if (uid != (int) getuid()) {
  74. Except_throw(eh, "Failed to set UID but seemed to succeed");
  75. }
  76. }
  77. static int canOpenFiles()
  78. {
  79. int file = dup(0);
  80. close(file);
  81. return file >= 0;
  82. }
  83. void Security_nofiles(struct Except* eh)
  84. {
  85. #if !defined(RLIMIT_NOFILE) && defined(RLIMIT_OFILE)
  86. #define RLIMIT_NOFILE RLIMIT_OFILE
  87. #endif
  88. if (!canOpenFiles()) {
  89. Except_throw(eh, "Unable to dupe stdin");
  90. }
  91. if (setrlimit(RLIMIT_NOFILE, &(struct rlimit){ 0, 0 })) {
  92. Except_throw(eh, "Failed to set open file limit to 0 [%s]", strerror(errno));
  93. }
  94. if (canOpenFiles()) {
  95. Except_throw(eh, "Still able to dupe stdin after setting number of files to 0!");
  96. }
  97. }
  98. void Security_noforks(struct Except* eh)
  99. {
  100. if (setrlimit(RLIMIT_NPROC, &(struct rlimit){ 0, 0 })) {
  101. Except_throw(eh, "Failed to set fork limit to 0 [%s]", strerror(errno));
  102. }
  103. }
  104. void Security_chroot(char* root, struct Except* eh)
  105. {
  106. if (chdir(root)) {
  107. Except_throw(eh, "chdir(%s) -> [%s]", root, strerror(errno));
  108. }
  109. if (chroot(root)) {
  110. Except_throw(eh, "chroot(%s) -> [%s]", root, strerror(errno));
  111. }
  112. }
  113. void Security_seccomp(struct Allocator* tempAlloc, struct Log* logger, struct Except* eh)
  114. {
  115. Seccomp_dropPermissions(tempAlloc, logger, eh);
  116. }
  117. struct Security_pvt
  118. {
  119. struct Security pub;
  120. struct Allocator* setupAlloc;
  121. struct Log* log;
  122. Identity
  123. };
  124. void Security_setupComplete(struct Security* security)
  125. {
  126. struct Security_pvt* sec = Identity_check((struct Security_pvt*) security);
  127. sec->pub.setupComplete = 1;
  128. Allocator_free(sec->setupAlloc);
  129. }
  130. static void fail(void* vSec)
  131. {
  132. struct Security_pvt* sec = Identity_check((struct Security_pvt*) vSec);
  133. Log_critical(sec->log, "Security_setupComplete() not called in time, exiting");
  134. exit(232);
  135. }
  136. struct Security* Security_new(struct Allocator* alloc, struct Log* log, struct EventBase* base)
  137. {
  138. struct Security_pvt* sec = Allocator_calloc(alloc, sizeof(struct Security_pvt), 1);
  139. Identity_set(sec);
  140. sec->setupAlloc = Allocator_child(alloc);
  141. Timeout_setInterval(fail, sec, 20000, base, sec->setupAlloc);
  142. sec->log = log;
  143. return &sec->pub;
  144. }
  145. struct Security_Permissions* Security_checkPermissions(struct Allocator* alloc, struct Except* eh)
  146. {
  147. struct Security_Permissions* out =
  148. Allocator_calloc(alloc, sizeof(struct Security_Permissions), 1);
  149. out->noOpenFiles = !canOpenFiles();
  150. out->seccompExists = Seccomp_exists();
  151. out->seccompEnforcing = Seccomp_isWorking();
  152. out->uid = getuid();
  153. return out;
  154. }