Security.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 <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. int Security_setUser(char* userName, struct Log* logger, struct Except* eh)
  27. {
  28. struct passwd* pw = getpwnam(userName);
  29. if (!pw) {
  30. Except_throw(eh, "Failed to set UID, couldn't find user named [%s].", strerror(errno));
  31. }
  32. if (setuid(pw->pw_uid)) {
  33. if (errno == EPERM) {
  34. return Security_setUser_PERMISSION;
  35. }
  36. Except_throw(eh, "Failed to set UID [%s]", strerror(errno));
  37. }
  38. if (getuid() != pw->pw_uid) {
  39. Except_throw(eh, "Failed to set UID but seemed to succeed");
  40. }
  41. return 0;
  42. }
  43. static void noFiles(struct Except* eh)
  44. {
  45. #if !defined(RLIMIT_NOFILE) && defined(RLIMIT_OFILE)
  46. #define RLIMIT_NOFILE RLIMIT_OFILE
  47. #endif
  48. int file = dup(0);
  49. if (file < 0) {
  50. Except_throw(eh, "Unable to dupe stdin");
  51. }
  52. close(file);
  53. if (setrlimit(RLIMIT_NOFILE, &(struct rlimit){ 0, 0 })) {
  54. Except_throw(eh, "Failed to set open file limit to [%s]", strerror(errno));
  55. }
  56. file = dup(0);
  57. close(file);
  58. if (file >= 0) {
  59. Except_throw(eh, "Still able to dupe stdin after setting number of files to 0!");
  60. }
  61. }
  62. static void noForks(struct Except* eh)
  63. {
  64. /* TODO: understand why we are still able to fork after setting NPROC to 0
  65. int pid = fork();
  66. if (pid == 0) { exit(0); }
  67. if (pid < 0) {
  68. Except_throw(eh, "Unable to fork prior to setting noForks");
  69. }
  70. if (setrlimit(RLIMIT_NPROC, &(struct rlimit){ 0, 0 })) {
  71. Except_throw(eh, "Failed to set fork limit to [%s]", strerror(errno));
  72. }
  73. pid = fork();
  74. if (pid == 0) { exit(0); }
  75. if (pid > 0) {
  76. Except_throw(eh, "Still able to fork after calling noForks!");
  77. }
  78. */
  79. }
  80. static void maxMemory(unsigned long max, struct Except* eh)
  81. {
  82. // RLIMIT_DATA doesn't prevent malloc() on linux.
  83. // see: http://lkml.indiana.edu/hypermail/linux/kernel/0707.1/0675.html
  84. #if !defined(RLIMIT_AS) && defined(RLIMIT_DATA)
  85. #define RLIMIT_AS RLIMIT_DATA
  86. #endif
  87. if (setrlimit(RLIMIT_AS, &(struct rlimit){ max, max })) {
  88. Except_throw(eh, "Failed to limit available memory [%s]", strerror(errno));
  89. }
  90. }
  91. void Security_dropPermissions(struct Except* eh)
  92. {
  93. maxMemory(100000000, eh);
  94. noFiles(eh);
  95. return;
  96. noForks(eh);
  97. }