shim_getrlimit.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /* Copyright (C) 2014 Stony Brook University
  2. This file is part of Graphene Library OS.
  3. Graphene Library OS is free software: you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public License
  5. as published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. Graphene Library OS 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 Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. /*
  14. * shim_getrlimit.c
  15. *
  16. * Implementation of system call "getrlimit" and "setrlimit".
  17. */
  18. #include <asm/resource.h>
  19. #include <shim_checkpoint.h>
  20. #include <shim_internal.h>
  21. #include <shim_table.h>
  22. #include <shim_utils.h>
  23. #include <shim_vma.h>
  24. /*
  25. * TODO: implement actual limitation on each resource.
  26. *
  27. * The current behavor(i.e. sys_stack_size, brk_max_size) may be subject
  28. * to be fixed.
  29. */
  30. #define MAX_THREADS (0x3fffffff / 2)
  31. #define DEFAULT_MAX_FDS (1024)
  32. #define MAX_MAX_FDS (65536) /* 4096: Linux initial value */
  33. #define MLOCK_LIMIT (64 * 1024)
  34. #define MQ_BYTES_MAX 819200
  35. static struct __kernel_rlimit64 __rlim[RLIM_NLIMITS] __attribute_migratable = {
  36. [RLIMIT_CPU] = {RLIM_INFINITY, RLIM_INFINITY},
  37. [RLIMIT_FSIZE] = {RLIM_INFINITY, RLIM_INFINITY},
  38. [RLIMIT_DATA] = {RLIM_INFINITY, RLIM_INFINITY},
  39. [RLIMIT_STACK] = {DEFAULT_SYS_STACK_SIZE, RLIM_INFINITY},
  40. [RLIMIT_CORE] = {0, RLIM_INFINITY},
  41. [RLIMIT_RSS] = {RLIM_INFINITY, RLIM_INFINITY},
  42. [RLIMIT_NPROC] = {MAX_THREADS, MAX_THREADS},
  43. [RLIMIT_NOFILE] = {DEFAULT_MAX_FDS, MAX_MAX_FDS},
  44. [RLIMIT_MEMLOCK] = {MLOCK_LIMIT, MLOCK_LIMIT},
  45. [RLIMIT_AS] = {RLIM_INFINITY, RLIM_INFINITY},
  46. [RLIMIT_LOCKS] = {RLIM_INFINITY, RLIM_INFINITY},
  47. /* [RLIMIT_SIGPENDING] = [RLIMIT_NPROC] for initial value */
  48. [RLIMIT_SIGPENDING] = {MAX_THREADS, MAX_THREADS},
  49. [RLIMIT_MSGQUEUE] = {MQ_BYTES_MAX, MQ_BYTES_MAX},
  50. [RLIMIT_NICE] = {0, 0},
  51. [RLIMIT_RTPRIO] = {0, 0},
  52. [RLIMIT_RTTIME] = {RLIM_INFINITY, RLIM_INFINITY},
  53. };
  54. static struct shim_lock rlimit_lock;
  55. int init_rlimit(void) {
  56. if (!create_lock(&rlimit_lock)) {
  57. return -ENOMEM;
  58. }
  59. return 0;
  60. }
  61. uint64_t get_rlimit_cur(int resource) {
  62. assert(resource >= 0 && RLIM_NLIMITS > resource);
  63. lock(&rlimit_lock);
  64. uint64_t rlim = __rlim[resource].rlim_cur;
  65. unlock(&rlimit_lock);
  66. return rlim;
  67. }
  68. void set_rlimit_cur(int resource, uint64_t rlim) {
  69. assert(resource >= 0 && RLIM_NLIMITS > resource);
  70. lock(&rlimit_lock);
  71. __rlim[resource].rlim_cur = rlim;
  72. unlock(&rlimit_lock);
  73. }
  74. int shim_do_getrlimit(int resource, struct __kernel_rlimit* rlim) {
  75. if (resource < 0 || RLIM_NLIMITS <= resource)
  76. return -EINVAL;
  77. if (!rlim || test_user_memory(rlim, sizeof(*rlim), true))
  78. return -EFAULT;
  79. lock(&rlimit_lock);
  80. rlim->rlim_cur = __rlim[resource].rlim_cur;
  81. rlim->rlim_max = __rlim[resource].rlim_max;
  82. unlock(&rlimit_lock);
  83. return 0;
  84. }
  85. int shim_do_setrlimit(int resource, struct __kernel_rlimit* rlim) {
  86. struct shim_thread* cur_thread = get_cur_thread();
  87. assert(cur_thread);
  88. if (resource < 0 || RLIM_NLIMITS <= resource)
  89. return -EINVAL;
  90. if (!rlim || test_user_memory(rlim, sizeof(*rlim), false))
  91. return -EFAULT;
  92. if (rlim->rlim_cur > rlim->rlim_max)
  93. return -EINVAL;
  94. lock(&rlimit_lock);
  95. if (rlim->rlim_max > __rlim[resource].rlim_max && cur_thread->euid) {
  96. unlock(&rlimit_lock);
  97. return -EPERM;
  98. }
  99. __rlim[resource].rlim_cur = rlim->rlim_cur;
  100. __rlim[resource].rlim_max = rlim->rlim_max;
  101. unlock(&rlimit_lock);
  102. return 0;
  103. }
  104. int shim_do_prlimit64(pid_t pid, int resource, const struct __kernel_rlimit64* new_rlim,
  105. struct __kernel_rlimit64* old_rlim) {
  106. struct shim_thread* cur_thread = get_cur_thread();
  107. assert(cur_thread);
  108. int ret = 0;
  109. // XXX: Do not support setting/getting the rlimit of other processes yet.
  110. if (pid && pid != (pid_t)cur_thread->tgid)
  111. return -ENOSYS;
  112. if (resource < 0 || RLIM_NLIMITS <= resource)
  113. return -EINVAL;
  114. if (old_rlim) {
  115. if (test_user_memory(old_rlim, sizeof(*old_rlim), true))
  116. return -EFAULT;
  117. }
  118. if (new_rlim) {
  119. if (test_user_memory((void*)new_rlim, sizeof(*new_rlim), false)) {
  120. ret = -EFAULT;
  121. goto out;
  122. }
  123. if (new_rlim->rlim_cur > new_rlim->rlim_max) {
  124. ret = -EINVAL;
  125. goto out;
  126. }
  127. }
  128. lock(&rlimit_lock);
  129. if (new_rlim) {
  130. if (new_rlim->rlim_max > __rlim[resource].rlim_max && cur_thread->euid) {
  131. ret = -EPERM;
  132. goto out;
  133. }
  134. }
  135. if (old_rlim)
  136. *old_rlim = __rlim[resource];
  137. if (new_rlim)
  138. __rlim[resource] = *new_rlim;
  139. out:
  140. unlock(&rlimit_lock);
  141. return ret;
  142. }