Security.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 "exception/Except.h"
  16. #include "util/log/Log.h"
  17. #include "util/Security.h"
  18. #include "util/Seccomp.h"
  19. #include "memory/Allocator.h"
  20. #include <sys/resource.h>
  21. #include <sys/types.h>
  22. #include <pwd.h>
  23. #include <unistd.h>
  24. #include <errno.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #define __USE_MISC // for MAP_ANONYMOUS
  28. #include <sys/mman.h>
  29. #include <stdio.h>
  30. static const unsigned long cfgMaxMemoryBytes = 100000000;
  31. int Security_setUser(char* userName, struct Log* logger, struct Except* eh)
  32. {
  33. struct passwd* pw = getpwnam(userName);
  34. if (!pw) {
  35. Except_throw(eh, "Failed to set UID, couldn't find user named [%s].",
  36. strerror(errno));
  37. }
  38. if (setuid(pw->pw_uid)) {
  39. if (errno == EPERM) {
  40. return Security_setUser_PERMISSION;
  41. }
  42. Except_throw(eh, "Failed to set UID [%s]", strerror(errno));
  43. }
  44. if (getuid() != pw->pw_uid) {
  45. Except_throw(eh, "Failed to set UID but seemed to succeed");
  46. }
  47. return 0;
  48. }
  49. static int canOpenFiles()
  50. {
  51. int file = dup(0);
  52. close(file);
  53. return file >= 0;
  54. }
  55. static void noFiles(struct Except* eh)
  56. {
  57. #if !defined(RLIMIT_NOFILE) && defined(RLIMIT_OFILE)
  58. #define RLIMIT_NOFILE RLIMIT_OFILE
  59. #endif
  60. if (!canOpenFiles()) {
  61. Except_throw(eh, "Unable to dupe stdin");
  62. }
  63. if (setrlimit(RLIMIT_NOFILE, &(struct rlimit){ 0, 0 })) {
  64. Except_throw(eh, "Failed to set open file limit to [%s]", strerror(errno));
  65. }
  66. if (canOpenFiles()) {
  67. Except_throw(eh, "Still able to dupe stdin after setting number of files to 0!");
  68. }
  69. }
  70. // RLIMIT_DATA doesn't prevent malloc() on linux.
  71. // see: http://lkml.indiana.edu/hypermail/linux/kernel/0707.1/0675.html
  72. #if !defined(RLIMIT_AS) && defined(RLIMIT_DATA)
  73. #define Security_MEMORY_RLIMIT RLIMIT_DATA
  74. #elif defined(RLIMIT_AS)
  75. #define Security_MEMORY_RLIMIT RLIMIT_AS
  76. #else
  77. #error RLIMIT_AS and RLIMIT_DATA are not defined
  78. #endif
  79. static unsigned long getReportedMaxMemory(struct Except* eh)
  80. {
  81. // Just report the reported maximum allowed memory.
  82. // If no limit, returns 0;
  83. struct rlimit lim = { 0, 0 };
  84. if (getrlimit(Security_MEMORY_RLIMIT, &lim)) {
  85. Except_throw(eh, "Failed to get memory limit [%s]", strerror(errno));
  86. }
  87. #if defined(RLIM_INFINITY)
  88. if (lim.rlim_max == RLIM_INFINITY) {
  89. return 0;
  90. }
  91. #endif // RLIM_INFINITY
  92. if (lim.rlim_max + 1 < lim.rlim_max) { // Systems without RLIM_INFINITY.
  93. return 0;
  94. }
  95. return lim.rlim_max;
  96. }
  97. static unsigned long getMaxMemory(struct Except* eh)
  98. { /* Determine the amount of memory allowed to process. */
  99. unsigned long reportedMemory = getReportedMaxMemory(eh);
  100. // First time around, we try a very small mapping just to make sure it works.
  101. size_t tryMapping = 100;
  102. if (reportedMemory > 0) {
  103. tryMapping = reportedMemory * 2l;
  104. }
  105. // Apple doesn't handle MAP_ANON | MAP_PRIVATE for some (unknown) reason.
  106. // And apple doesn't have MAP_ANONYMOUS, only MAP_ANON.
  107. #ifdef darwin
  108. #define FLAGS MAP_ANON
  109. #elif openbsd
  110. #define FLAGS MAP_PRIVATE | MAP_ANON
  111. #else
  112. #define FLAGS MAP_PRIVATE | MAP_ANONYMOUS
  113. #endif
  114. void* ptr = mmap(NULL, tryMapping, PROT_READ | PROT_WRITE, FLAGS, -1, 0);
  115. if (ptr != MAP_FAILED) {
  116. munmap(ptr, tryMapping);
  117. if (reportedMemory > 0) {
  118. Except_throw(eh, "Memory limit is not enforced, successfully mapped [%zu] bytes, "
  119. "while limit is [%lu] bytes", tryMapping, reportedMemory);
  120. }
  121. } else if (reportedMemory == 0) {
  122. Except_throw(eh, "Testing of memory limit not possible, unable to map memory [%s]",
  123. strerror(errno));
  124. }
  125. return reportedMemory;
  126. }
  127. static void setMaxMemory(unsigned long max, struct Except* eh)
  128. {
  129. unsigned long realMax = getReportedMaxMemory(eh);
  130. if (realMax > 0 && realMax < max) {
  131. Except_throw(eh, "Failed to limit available memory to [%lu] "
  132. "because existing limit is [%lu]", max, realMax);
  133. }
  134. if (setrlimit(Security_MEMORY_RLIMIT, &(struct rlimit){ max, max })) {
  135. Except_throw(eh, "Failed to limit available memory [%s]", strerror(errno));
  136. }
  137. if (!setrlimit(Security_MEMORY_RLIMIT, &(struct rlimit){ max+1, max+1 })) {
  138. Except_throw(eh, "Available memory was modifyable after limiting");
  139. }
  140. realMax = getMaxMemory(eh);
  141. if (realMax != max) {
  142. Except_throw(eh, "Limiting available memory failed");
  143. }
  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->memoryLimitBytes = getMaxMemory(eh);
  153. return out;
  154. }
  155. void Security_dropPermissions(struct Allocator* tempAlloc, struct Log* logger, struct Except* eh)
  156. {
  157. setMaxMemory(cfgMaxMemoryBytes, eh);
  158. noFiles(eh);
  159. Seccomp_dropPermissions(tempAlloc, logger, eh);
  160. }