-
Notifications
You must be signed in to change notification settings - Fork 202
/
libos_getrlimit.c
179 lines (153 loc) · 5.63 KB
/
libos_getrlimit.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/* SPDX-License-Identifier: LGPL-3.0-or-later */
/* Copyright (C) 2014 Stony Brook University */
/*
* Implementation of system calls "getrlimit", "setrlimit" and "sysinfo".
*/
#include "libos_checkpoint.h"
#include "libos_internal.h"
#include "libos_lock.h"
#include "libos_process.h"
#include "libos_table.h"
#include "libos_thread.h"
#include "libos_vma.h"
#include "linux_abi/limits.h"
#include "linux_abi/sysinfo.h"
/*
* TODO: implement actual limitation on each resource.
*
* The current behavor(i.e. sys_stack_size, brk_max_size) may be subject
* to be fixed.
*/
#define MAX_THREADS (0x3fffffff / 2)
#define DEFAULT_MAX_FDS 900 /* We have to keep this lower than the standard 1024, otherwise we'll
hit the limit on the host sooner than the app would reach this
value (because Gramine-internal fds in the PAL also counts towards
the host limit). Ideally, we should have a PAL API which tells
LibOS how many PAL handles it can use simultaneously. */
#define MAX_MAX_FDS 65536
#undef MLOCK_LIMIT /* Clashes with a macro from linux/resource.h. TODO: Remove after cleaning up
* our headers. */
#define MLOCK_LIMIT (64 * 1024)
#define MQ_BYTES_MAX 819200
static struct __kernel_rlimit64 __rlim[RLIM_NLIMITS] __attribute_migratable = {
[RLIMIT_CPU] = {RLIM_INFINITY, RLIM_INFINITY},
[RLIMIT_FSIZE] = {RLIM_INFINITY, RLIM_INFINITY},
[RLIMIT_DATA] = {RLIM_INFINITY, RLIM_INFINITY},
[RLIMIT_STACK] = {DEFAULT_SYS_STACK_SIZE, RLIM_INFINITY},
[RLIMIT_CORE] = {0, RLIM_INFINITY},
[RLIMIT_RSS] = {RLIM_INFINITY, RLIM_INFINITY},
[RLIMIT_NPROC] = {MAX_THREADS, MAX_THREADS},
[RLIMIT_NOFILE] = {DEFAULT_MAX_FDS, MAX_MAX_FDS},
[RLIMIT_MEMLOCK] = {MLOCK_LIMIT, MLOCK_LIMIT},
[RLIMIT_AS] = {RLIM_INFINITY, RLIM_INFINITY},
[RLIMIT_LOCKS] = {RLIM_INFINITY, RLIM_INFINITY},
/* [RLIMIT_SIGPENDING] = [RLIMIT_NPROC] for initial value */
[RLIMIT_SIGPENDING] = {MAX_THREADS, MAX_THREADS},
[RLIMIT_MSGQUEUE] = {MQ_BYTES_MAX, MQ_BYTES_MAX},
[RLIMIT_NICE] = {0, 0},
[RLIMIT_RTPRIO] = {0, 0},
[RLIMIT_RTTIME] = {RLIM_INFINITY, RLIM_INFINITY},
};
static struct libos_lock rlimit_lock;
int init_rlimit(void) {
if (!create_lock(&rlimit_lock)) {
return -ENOMEM;
}
return 0;
}
uint64_t get_rlimit_cur(int resource) {
assert(resource >= 0 && RLIM_NLIMITS > resource);
lock(&rlimit_lock);
uint64_t rlim = __rlim[resource].rlim_cur;
unlock(&rlimit_lock);
return rlim;
}
void set_rlimit_cur(int resource, uint64_t rlim) {
assert(resource >= 0 && RLIM_NLIMITS > resource);
lock(&rlimit_lock);
__rlim[resource].rlim_cur = rlim;
unlock(&rlimit_lock);
}
long libos_syscall_getrlimit(int resource, struct __kernel_rlimit* rlim) {
if (resource < 0 || RLIM_NLIMITS <= resource)
return -EINVAL;
if (!is_user_memory_writable(rlim, sizeof(*rlim)))
return -EFAULT;
lock(&rlimit_lock);
rlim->rlim_cur = __rlim[resource].rlim_cur;
rlim->rlim_max = __rlim[resource].rlim_max;
unlock(&rlimit_lock);
return 0;
}
long libos_syscall_setrlimit(int resource, struct __kernel_rlimit* rlim) {
struct libos_thread* cur_thread = get_cur_thread();
assert(cur_thread);
if (resource < 0 || RLIM_NLIMITS <= resource)
return -EINVAL;
if (!is_user_memory_readable(rlim, sizeof(*rlim)))
return -EFAULT;
if (rlim->rlim_cur > rlim->rlim_max)
return -EINVAL;
lock(&rlimit_lock);
if (rlim->rlim_max > __rlim[resource].rlim_max && cur_thread->euid) {
unlock(&rlimit_lock);
return -EPERM;
}
__rlim[resource].rlim_cur = rlim->rlim_cur;
__rlim[resource].rlim_max = rlim->rlim_max;
unlock(&rlimit_lock);
return 0;
}
long libos_syscall_prlimit64(pid_t pid, int resource, const struct __kernel_rlimit64* new_rlim,
struct __kernel_rlimit64* old_rlim) {
struct libos_thread* cur_thread = get_cur_thread();
assert(cur_thread);
int ret = 0;
// XXX: Do not support setting/getting the rlimit of other processes yet.
if (pid && (IDTYPE)pid != g_process.pid)
return -ENOSYS;
if (resource < 0 || RLIM_NLIMITS <= resource)
return -EINVAL;
if (old_rlim) {
if (!is_user_memory_writable(old_rlim, sizeof(*old_rlim)))
return -EFAULT;
}
if (new_rlim) {
if (!is_user_memory_readable((void*)new_rlim, sizeof(*new_rlim))) {
ret = -EFAULT;
goto out;
}
if (new_rlim->rlim_cur > new_rlim->rlim_max) {
ret = -EINVAL;
goto out;
}
}
lock(&rlimit_lock);
if (new_rlim) {
if (new_rlim->rlim_max > __rlim[resource].rlim_max && cur_thread->euid) {
ret = -EPERM;
goto out;
}
}
if (old_rlim)
*old_rlim = __rlim[resource];
if (new_rlim)
__rlim[resource] = *new_rlim;
out:
unlock(&rlimit_lock);
return ret;
}
/* minimal implementation; tested apps only care about total/free RAM */
long libos_syscall_sysinfo(struct sysinfo* info) {
if (!is_user_memory_writable(info, sizeof(*info)))
return -EFAULT;
size_t free_mem = g_pal_public_state->mem_total - get_total_memory_usage();
memset(info, 0, sizeof(*info));
info->totalram = g_pal_public_state->mem_total;
info->totalhigh = g_pal_public_state->mem_total;
info->freeram = free_mem;
info->freehigh = free_mem;
info->mem_unit = 1;
info->procs = 1; /* report only this Gramine process */
return 0;
}