shim_getrlimit.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 <shim_internal.h>
  19. #include <shim_checkpoint.h>
  20. #include <shim_table.h>
  21. #include <shim_utils.h>
  22. #include <shim_vma.h>
  23. #include <asm/resource.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. {
  74. if (resource < 0 || RLIM_NLIMITS <= resource)
  75. return -EINVAL;
  76. if (!rlim || test_user_memory(rlim, sizeof(*rlim), true))
  77. return -EFAULT;
  78. lock(&rlimit_lock);
  79. rlim->rlim_cur = __rlim[resource].rlim_cur;
  80. rlim->rlim_max = __rlim[resource].rlim_max;
  81. unlock(&rlimit_lock);
  82. return 0;
  83. }
  84. int shim_do_setrlimit (int resource, struct __kernel_rlimit * rlim)
  85. {
  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. if (rlim->rlim_max > __rlim[resource].rlim_max && cur_thread->euid)
  95. return -EPERM;
  96. lock(&rlimit_lock);
  97. __rlim[resource].rlim_cur = rlim->rlim_cur;
  98. __rlim[resource].rlim_max = rlim->rlim_max;
  99. unlock(&rlimit_lock);
  100. return 0;
  101. }
  102. int shim_do_prlimit64(pid_t pid, int resource, const struct __kernel_rlimit64* new_rlim,
  103. struct __kernel_rlimit64* old_rlim) {
  104. struct shim_thread* cur_thread = get_cur_thread();
  105. assert(cur_thread);
  106. // XXX: Do not support setting/getting the rlimit of other processes yet.
  107. if (pid && pid != (pid_t)cur_thread->tgid)
  108. return -ENOSYS;
  109. if (resource < 0 || RLIM_NLIMITS <= resource)
  110. return -EINVAL;
  111. if (old_rlim) {
  112. if (test_user_memory(old_rlim, sizeof(*old_rlim), true))
  113. return -EFAULT;
  114. }
  115. if (new_rlim) {
  116. if (test_user_memory((void*)new_rlim, sizeof(*new_rlim), false))
  117. return -EFAULT;
  118. if (new_rlim->rlim_cur > new_rlim->rlim_max)
  119. return -EINVAL;
  120. if (new_rlim->rlim_max > __rlim[resource].rlim_max && cur_thread->euid)
  121. return -EPERM;
  122. }
  123. lock(&rlimit_lock);
  124. if (old_rlim)
  125. *old_rlim = __rlim[resource];
  126. if (new_rlim)
  127. __rlim[resource] = *new_rlim;
  128. unlock(&rlimit_lock);
  129. return 0;
  130. }