shim_getrlimit.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. create_lock(&rlimit_lock);
  57. return 0;
  58. }
  59. uint64_t get_rlimit_cur(int resource) {
  60. assert(resource >= 0 && RLIM_NLIMITS > resource);
  61. lock(&rlimit_lock);
  62. uint64_t rlim = __rlim[resource].rlim_cur;
  63. unlock(&rlimit_lock);
  64. return rlim;
  65. }
  66. void set_rlimit_cur(int resource, uint64_t rlim) {
  67. assert(resource >= 0 && RLIM_NLIMITS > resource);
  68. lock(&rlimit_lock);
  69. __rlim[resource].rlim_cur = rlim;
  70. unlock(&rlimit_lock);
  71. }
  72. int shim_do_getrlimit(int resource, struct __kernel_rlimit* rlim) {
  73. if (resource < 0 || RLIM_NLIMITS <= resource)
  74. return -EINVAL;
  75. if (!rlim || test_user_memory(rlim, sizeof(*rlim), true))
  76. return -EFAULT;
  77. lock(&rlimit_lock);
  78. rlim->rlim_cur = __rlim[resource].rlim_cur;
  79. rlim->rlim_max = __rlim[resource].rlim_max;
  80. unlock(&rlimit_lock);
  81. return 0;
  82. }
  83. int shim_do_setrlimit(int resource, struct __kernel_rlimit* rlim) {
  84. struct shim_thread* cur_thread = get_cur_thread();
  85. assert(cur_thread);
  86. if (resource < 0 || RLIM_NLIMITS <= resource)
  87. return -EINVAL;
  88. if (!rlim || test_user_memory(rlim, sizeof(*rlim), false))
  89. return -EFAULT;
  90. if (rlim->rlim_cur > rlim->rlim_max)
  91. return -EINVAL;
  92. if (rlim->rlim_max > __rlim[resource].rlim_max && cur_thread->euid)
  93. return -EPERM;
  94. lock(&rlimit_lock);
  95. __rlim[resource].rlim_cur = rlim->rlim_cur;
  96. __rlim[resource].rlim_max = rlim->rlim_max;
  97. unlock(&rlimit_lock);
  98. return 0;
  99. }
  100. int shim_do_prlimit64(pid_t pid, int resource, const struct __kernel_rlimit64* new_rlim,
  101. struct __kernel_rlimit64* old_rlim) {
  102. struct shim_thread* cur_thread = get_cur_thread();
  103. assert(cur_thread);
  104. // XXX: Do not support setting/getting the rlimit of other processes yet.
  105. if (pid && pid != (pid_t)cur_thread->tgid)
  106. return -ENOSYS;
  107. if (resource < 0 || RLIM_NLIMITS <= resource)
  108. return -EINVAL;
  109. if (old_rlim) {
  110. if (test_user_memory(old_rlim, sizeof(*old_rlim), true))
  111. return -EFAULT;
  112. }
  113. if (new_rlim) {
  114. if (test_user_memory((void*)new_rlim, sizeof(*new_rlim), false))
  115. return -EFAULT;
  116. if (new_rlim->rlim_cur > new_rlim->rlim_max)
  117. return -EINVAL;
  118. if (new_rlim->rlim_max > __rlim[resource].rlim_max && cur_thread->euid)
  119. return -EPERM;
  120. }
  121. lock(&rlimit_lock);
  122. if (old_rlim)
  123. *old_rlim = __rlim[resource];
  124. if (new_rlim)
  125. __rlim[resource] = *new_rlim;
  126. unlock(&rlimit_lock);
  127. return 0;
  128. }