Security.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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/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. Er_DEFUN(void Security_setUser(int uid,
  58. int gid,
  59. bool keepNetAdmin,
  60. struct Log* logger,
  61. struct Allocator* alloc))
  62. {
  63. int gidErrno = 0;
  64. int uidErrno = 0;
  65. if (keepNetAdmin) {
  66. Er(Setuid_preSetuid(alloc));
  67. }
  68. if (gid && setgid(gid)) {
  69. gidErrno = errno;
  70. }
  71. if (setuid(uid)) {
  72. // errno is global and could get overwritten by Setuid_postSetuid()
  73. uidErrno = errno;
  74. }
  75. if (keepNetAdmin) {
  76. Er(Setuid_postSetuid(alloc));
  77. }
  78. if (uidErrno > 0) {
  79. Er_raise(alloc, "Failed to set UID [%s]", strerror(uidErrno));
  80. }
  81. if (uid != (int) getuid()) {
  82. Er_raise(alloc, "Failed to set UID but seemed to succeed");
  83. }
  84. if (gidErrno > 0) {
  85. Er_raise(alloc, "Failed to set GID [%s]", strerror(gidErrno));
  86. }
  87. if (gid != (int) getgid()) {
  88. Er_raise(alloc, "Failed to set GID but seemed to succeed");
  89. }
  90. Er_ret();
  91. }
  92. static int canOpenFiles()
  93. {
  94. int file = dup(0);
  95. close(file);
  96. return file >= 0;
  97. }
  98. Er_DEFUN(void Security_nofiles(struct Allocator* errAlloc))
  99. {
  100. #if !defined(RLIMIT_NOFILE) && defined(RLIMIT_OFILE)
  101. #define RLIMIT_NOFILE RLIMIT_OFILE
  102. #endif
  103. if (!canOpenFiles()) {
  104. Er_raise(errAlloc, "Unable to dupe stdin");
  105. }
  106. if (setrlimit(RLIMIT_NOFILE, &(struct rlimit){ 0, 0 })) {
  107. Er_raise(errAlloc, "Failed to set open file limit to 0 [%s]", strerror(errno));
  108. }
  109. if (canOpenFiles()) {
  110. Er_raise(errAlloc, "Still able to dupe stdin after setting number of files to 0!");
  111. }
  112. Er_ret();
  113. }
  114. Er_DEFUN(void Security_noforks(struct Allocator* errAlloc))
  115. {
  116. if (setrlimit(RLIMIT_NPROC, &(struct rlimit){ 0, 0 })) {
  117. Er_raise(errAlloc, "Failed to set fork limit to 0 [%s]", strerror(errno));
  118. }
  119. Er_ret();
  120. }
  121. Er_DEFUN(void Security_chroot(char* root, struct Allocator* errAlloc))
  122. {
  123. if (chdir(root)) {
  124. Er_raise(errAlloc, "chdir(%s) -> [%s]", root, strerror(errno));
  125. }
  126. if (chroot(root)) {
  127. Er_raise(errAlloc, "chroot(%s) -> [%s]", root, strerror(errno));
  128. }
  129. Er_ret();
  130. }
  131. Er_DEFUN(void Security_seccomp(struct Allocator* tempAlloc, struct Log* logger))
  132. {
  133. Er(Seccomp_dropPermissions(tempAlloc, logger));
  134. Er_ret();
  135. }
  136. struct Security_pvt
  137. {
  138. struct Security pub;
  139. struct Allocator* setupAlloc;
  140. struct Log* log;
  141. Identity
  142. };
  143. void Security_setupComplete(struct Security* security)
  144. {
  145. struct Security_pvt* sec = Identity_check((struct Security_pvt*) security);
  146. sec->pub.setupComplete = 1;
  147. Allocator_free(sec->setupAlloc);
  148. }
  149. static void fail(void* vSec)
  150. {
  151. struct Security_pvt* sec = Identity_check((struct Security_pvt*) vSec);
  152. Log_critical(sec->log, "Security_setupComplete() not called in time, exiting");
  153. exit(232);
  154. }
  155. struct Security* Security_new(struct Allocator* alloc, struct Log* log, struct EventBase* base)
  156. {
  157. struct Security_pvt* sec = Allocator_calloc(alloc, sizeof(struct Security_pvt), 1);
  158. Identity_set(sec);
  159. sec->setupAlloc = Allocator_child(alloc);
  160. Timeout_setInterval(fail, sec, 20000, base, sec->setupAlloc);
  161. sec->log = log;
  162. return &sec->pub;
  163. }
  164. Er_DEFUN(struct Security_Permissions* Security_checkPermissions(struct Allocator* alloc))
  165. {
  166. struct Security_Permissions* out =
  167. Allocator_calloc(alloc, sizeof(struct Security_Permissions), 1);
  168. out->noOpenFiles = !canOpenFiles();
  169. out->seccompExists = Seccomp_exists();
  170. out->seccompEnforcing = Seccomp_isWorking();
  171. out->uid = getuid();
  172. Er_ret(out);
  173. }