Security.c 4.6 KB

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