1*a325d9c4SApple OSS Distributions /*
2*a325d9c4SApple OSS Distributions * cd $XNU/tests
3*a325d9c4SApple OSS Distributions * xcrun -sdk macosx.internal/iphoneos.internal make proc_rlimit LDFLAGS="-ldarwintest"
4*a325d9c4SApple OSS Distributions */
5*a325d9c4SApple OSS Distributions #include <stdio.h>
6*a325d9c4SApple OSS Distributions #include <unistd.h>
7*a325d9c4SApple OSS Distributions #include <sys/resource.h>
8*a325d9c4SApple OSS Distributions #include <errno.h>
9*a325d9c4SApple OSS Distributions #include <sys/sysctl.h>
10*a325d9c4SApple OSS Distributions #include <darwintest.h>
11*a325d9c4SApple OSS Distributions
12*a325d9c4SApple OSS Distributions /* Defined in <sys/resource.h> but not visible to user space */
13*a325d9c4SApple OSS Distributions #define RLIMIT_NLIMITS 9
14*a325d9c4SApple OSS Distributions
15*a325d9c4SApple OSS Distributions /* Defined in <sys/resource.h> and visible to user space */
16*a325d9c4SApple OSS Distributions static const char *RESOURCE_STRING[] = {
17*a325d9c4SApple OSS Distributions "RLIMIT_CPU", /* #define RLIMIT_CPU 0 */
18*a325d9c4SApple OSS Distributions "RLIMIT_FSIZE", /* #define RLIMIT_FSIZE 1 */
19*a325d9c4SApple OSS Distributions "RLIMIT_DATA", /* #define RLIMIT_DATA 2 */
20*a325d9c4SApple OSS Distributions "RLIMIT_STACK", /* #define RLIMIT_STACK 3 */
21*a325d9c4SApple OSS Distributions "RLIMIT_CORE", /* #define RLIMIT_CORE 4 */
22*a325d9c4SApple OSS Distributions "RLIMIT_AS/RSS", /* #define RLIMIT_AS 5 */
23*a325d9c4SApple OSS Distributions /* #define RLIMIT_RSS RLIMIT_AS */
24*a325d9c4SApple OSS Distributions "RLIMIT_MEMLOCK", /* #define RLIMIT_MEMLOCK 6 */
25*a325d9c4SApple OSS Distributions "RLIMIT_NPROC", /* #define RLIMIT_NPROC 7 */
26*a325d9c4SApple OSS Distributions "RLIMIT_NOFILE" /* #define RLIMIT_NOFILE 8 */
27*a325d9c4SApple OSS Distributions };
28*a325d9c4SApple OSS Distributions
29*a325d9c4SApple OSS Distributions /* Change limit values by this arbitrary amount */
30*a325d9c4SApple OSS Distributions #define LIMIT_DIFF 64
31*a325d9c4SApple OSS Distributions
32*a325d9c4SApple OSS Distributions /* Limit type */
33*a325d9c4SApple OSS Distributions #define SOFT_LIMIT 0
34*a325d9c4SApple OSS Distributions #define HARD_LIMIT 1
35*a325d9c4SApple OSS Distributions
36*a325d9c4SApple OSS Distributions /* Action on changing limit values */
37*a325d9c4SApple OSS Distributions #define LOWER 0
38*a325d9c4SApple OSS Distributions #define RAISE 1
39*a325d9c4SApple OSS Distributions
40*a325d9c4SApple OSS Distributions static struct rlimit orig_rlimit[RLIMIT_NLIMITS];
41*a325d9c4SApple OSS Distributions
42*a325d9c4SApple OSS Distributions /* Maximum number of open files allowed by normal user */
43*a325d9c4SApple OSS Distributions static rlim_t maxfilesperproc;
44*a325d9c4SApple OSS Distributions static size_t maxfilesperproc_size = sizeof(maxfilesperproc);
45*a325d9c4SApple OSS Distributions
46*a325d9c4SApple OSS Distributions /* Maximum number of open files allowed by super user */
47*a325d9c4SApple OSS Distributions static rlim_t maxfiles;
48*a325d9c4SApple OSS Distributions static size_t maxfiles_size = sizeof(maxfiles);
49*a325d9c4SApple OSS Distributions
50*a325d9c4SApple OSS Distributions /* Maximum number of simultaneous processes allowed by normal user */
51*a325d9c4SApple OSS Distributions static rlim_t maxprocperuid;
52*a325d9c4SApple OSS Distributions static size_t maxprocperuid_size = sizeof(maxprocperuid);
53*a325d9c4SApple OSS Distributions
54*a325d9c4SApple OSS Distributions /* Maximum number of simultaneous processes allowed by super user */
55*a325d9c4SApple OSS Distributions static rlim_t maxproc;
56*a325d9c4SApple OSS Distributions static size_t maxproc_size = sizeof(maxproc);
57*a325d9c4SApple OSS Distributions
58*a325d9c4SApple OSS Distributions static bool superuser = FALSE;
59*a325d9c4SApple OSS Distributions
60*a325d9c4SApple OSS Distributions static int
get_initial_rlimits(void)61*a325d9c4SApple OSS Distributions get_initial_rlimits(void)
62*a325d9c4SApple OSS Distributions {
63*a325d9c4SApple OSS Distributions int err = -1;
64*a325d9c4SApple OSS Distributions int i;
65*a325d9c4SApple OSS Distributions
66*a325d9c4SApple OSS Distributions for (i = 0; i < RLIMIT_NLIMITS; i++) {
67*a325d9c4SApple OSS Distributions err = getrlimit(i, &orig_rlimit[i]);
68*a325d9c4SApple OSS Distributions T_QUIET; T_EXPECT_EQ(0, err, "getrlimit(%15s, soft: 0x%16llx, hard 0x%16llx) %s", RESOURCE_STRING[i], orig_rlimit[i].rlim_cur, orig_rlimit[i].rlim_max, err == 0 ? "" : strerror(errno));
69*a325d9c4SApple OSS Distributions }
70*a325d9c4SApple OSS Distributions return err;
71*a325d9c4SApple OSS Distributions }
72*a325d9c4SApple OSS Distributions
73*a325d9c4SApple OSS Distributions static void
print_rlimits(bool initial_limits)74*a325d9c4SApple OSS Distributions print_rlimits(bool initial_limits)
75*a325d9c4SApple OSS Distributions {
76*a325d9c4SApple OSS Distributions int err;
77*a325d9c4SApple OSS Distributions int i;
78*a325d9c4SApple OSS Distributions
79*a325d9c4SApple OSS Distributions for (i = 0; i < RLIMIT_NLIMITS; i++) {
80*a325d9c4SApple OSS Distributions struct rlimit lim;
81*a325d9c4SApple OSS Distributions
82*a325d9c4SApple OSS Distributions if (initial_limits) {
83*a325d9c4SApple OSS Distributions lim = orig_rlimit[i];
84*a325d9c4SApple OSS Distributions } else {
85*a325d9c4SApple OSS Distributions err = getrlimit(i, &lim);
86*a325d9c4SApple OSS Distributions T_QUIET; T_EXPECT_EQ(0, err, "getrlimit(%15s, soft: 0x%16llx, hard 0x%16llx) %s", RESOURCE_STRING[i], lim.rlim_cur, lim.rlim_max, err == 0 ? "" : strerror(errno));
87*a325d9c4SApple OSS Distributions }
88*a325d9c4SApple OSS Distributions T_LOG("%35s soft: 0x%16llx hard 0x%16llx", RESOURCE_STRING[i], lim.rlim_cur, lim.rlim_max);
89*a325d9c4SApple OSS Distributions }
90*a325d9c4SApple OSS Distributions }
91*a325d9c4SApple OSS Distributions
92*a325d9c4SApple OSS Distributions /*
93*a325d9c4SApple OSS Distributions * Change "limit_type" of all of the process's "rlimit" by amount
94*a325d9c4SApple OSS Distributions *
95*a325d9c4SApple OSS Distributions * limit_type: SOFT_LIMIT/HARD_LIMIT
96*a325d9c4SApple OSS Distributions * amount: rlim_t
97*a325d9c4SApple OSS Distributions * action: RAISE/LOWER
98*a325d9c4SApple OSS Distributions */
99*a325d9c4SApple OSS Distributions static void
change_rlimits(int limit_type,rlim_t amount,int action)100*a325d9c4SApple OSS Distributions change_rlimits(int limit_type, rlim_t amount, int action)
101*a325d9c4SApple OSS Distributions {
102*a325d9c4SApple OSS Distributions int err = -1;
103*a325d9c4SApple OSS Distributions int i;
104*a325d9c4SApple OSS Distributions
105*a325d9c4SApple OSS Distributions for (i = 0; i < RLIMIT_NLIMITS; i++) {
106*a325d9c4SApple OSS Distributions struct rlimit newlim; // for setrlimit
107*a325d9c4SApple OSS Distributions struct rlimit verifylim; // for getrlimit
108*a325d9c4SApple OSS Distributions bool expect_failure = FALSE;
109*a325d9c4SApple OSS Distributions int expect_errno = 0;
110*a325d9c4SApple OSS Distributions
111*a325d9c4SApple OSS Distributions /* Get the current limit values */
112*a325d9c4SApple OSS Distributions err = getrlimit(i, &newlim);
113*a325d9c4SApple OSS Distributions T_EXPECT_EQ(0, err, "getrlimit(%15s, soft: 0x%16llx, hard 0x%16llx) %s", RESOURCE_STRING[i], newlim.rlim_cur, newlim.rlim_max, err == 0 ? "" : strerror(errno));
114*a325d9c4SApple OSS Distributions
115*a325d9c4SApple OSS Distributions /* Changing soft limit */
116*a325d9c4SApple OSS Distributions if (limit_type == SOFT_LIMIT) {
117*a325d9c4SApple OSS Distributions if (action == RAISE) {
118*a325d9c4SApple OSS Distributions /* Raising soft limits to exceed hard limits is not allowed and we expect to see failure on setrlimit call later */
119*a325d9c4SApple OSS Distributions if (newlim.rlim_cur + amount > newlim.rlim_max) {
120*a325d9c4SApple OSS Distributions expect_failure = TRUE;
121*a325d9c4SApple OSS Distributions expect_errno = EINVAL;
122*a325d9c4SApple OSS Distributions }
123*a325d9c4SApple OSS Distributions newlim.rlim_cur += amount;
124*a325d9c4SApple OSS Distributions } else if (action == LOWER) {
125*a325d9c4SApple OSS Distributions if (newlim.rlim_cur == 0) {
126*a325d9c4SApple OSS Distributions /* Soft limit might be 0 already, if so skip lowering it */
127*a325d9c4SApple OSS Distributions } else {
128*a325d9c4SApple OSS Distributions newlim.rlim_cur -= amount;
129*a325d9c4SApple OSS Distributions }
130*a325d9c4SApple OSS Distributions } else {
131*a325d9c4SApple OSS Distributions T_FAIL("Unknown action on soft limit: %d", action);
132*a325d9c4SApple OSS Distributions }
133*a325d9c4SApple OSS Distributions }
134*a325d9c4SApple OSS Distributions /* Changing hard limit */
135*a325d9c4SApple OSS Distributions else if (limit_type == HARD_LIMIT) {
136*a325d9c4SApple OSS Distributions if (action == RAISE) {
137*a325d9c4SApple OSS Distributions newlim.rlim_max += amount;
138*a325d9c4SApple OSS Distributions
139*a325d9c4SApple OSS Distributions /* Raising hard limits is not allowed for normal user and we expect to see failure on setrlimit call later */
140*a325d9c4SApple OSS Distributions expect_failure = TRUE;
141*a325d9c4SApple OSS Distributions expect_errno = EPERM;
142*a325d9c4SApple OSS Distributions } else if (action == LOWER) {
143*a325d9c4SApple OSS Distributions if (newlim.rlim_max == 0) {
144*a325d9c4SApple OSS Distributions /* Hard limit might be 0 already, if so skip lowering it (e.g., RLIMIT_CORE on iOS) */
145*a325d9c4SApple OSS Distributions } else {
146*a325d9c4SApple OSS Distributions newlim.rlim_max -= amount;
147*a325d9c4SApple OSS Distributions }
148*a325d9c4SApple OSS Distributions /* Soft limit might need to be changed as well since soft cannot be greater than hard */
149*a325d9c4SApple OSS Distributions if (newlim.rlim_cur > newlim.rlim_max) {
150*a325d9c4SApple OSS Distributions newlim.rlim_cur = newlim.rlim_max;
151*a325d9c4SApple OSS Distributions }
152*a325d9c4SApple OSS Distributions } else {
153*a325d9c4SApple OSS Distributions T_FAIL("Unknown action on hard limit: %d", action);
154*a325d9c4SApple OSS Distributions }
155*a325d9c4SApple OSS Distributions }
156*a325d9c4SApple OSS Distributions /* Changing unknown limit type */
157*a325d9c4SApple OSS Distributions else {
158*a325d9c4SApple OSS Distributions T_FAIL("Unknown limit type: %d", limit_type);
159*a325d9c4SApple OSS Distributions }
160*a325d9c4SApple OSS Distributions
161*a325d9c4SApple OSS Distributions /* Request the kernel to change limit values */
162*a325d9c4SApple OSS Distributions err = setrlimit(i, &newlim);
163*a325d9c4SApple OSS Distributions
164*a325d9c4SApple OSS Distributions if (expect_failure) {
165*a325d9c4SApple OSS Distributions /* We expect the setrlimit call to fail */
166*a325d9c4SApple OSS Distributions T_EXPECT_EQ(-1, err, "setrlimit(%15s, soft: 0x%16llx, hard 0x%16llx) failed as expected: %s", RESOURCE_STRING[i], newlim.rlim_cur, newlim.rlim_max, strerror(errno));
167*a325d9c4SApple OSS Distributions T_EXPECT_EQ(expect_errno, errno, "Expect errno %d, errno returned %d", expect_errno, errno);
168*a325d9c4SApple OSS Distributions continue;
169*a325d9c4SApple OSS Distributions } else {
170*a325d9c4SApple OSS Distributions T_EXPECT_EQ(0, err, "setrlimit(%15s, soft: 0x%16llx, hard 0x%16llx) %s", RESOURCE_STRING[i], newlim.rlim_cur, newlim.rlim_max, err == 0 ? "" : strerror(errno));
171*a325d9c4SApple OSS Distributions }
172*a325d9c4SApple OSS Distributions
173*a325d9c4SApple OSS Distributions /* Verify the kernel correctly changed the limit values */
174*a325d9c4SApple OSS Distributions err = getrlimit(i, &verifylim);
175*a325d9c4SApple OSS Distributions T_EXPECT_EQ(0, err, "getrlimit(%15s, soft: 0x%16llx, hard 0x%16llx) %s", RESOURCE_STRING[i], verifylim.rlim_cur, verifylim.rlim_max, err == 0 ? "" : strerror(errno));
176*a325d9c4SApple OSS Distributions
177*a325d9c4SApple OSS Distributions /* The kernel forces the hard limit of RLIMIT_NOFILE to be at most maxfileperproc for normal user when changing the hard limit with setrlimit */
178*a325d9c4SApple OSS Distributions if (i == RLIMIT_NOFILE && limit_type == HARD_LIMIT && newlim.rlim_max > maxfilesperproc) {
179*a325d9c4SApple OSS Distributions if (newlim.rlim_cur != verifylim.rlim_cur ||
180*a325d9c4SApple OSS Distributions maxfilesperproc != verifylim.rlim_max) {
181*a325d9c4SApple OSS Distributions T_FAIL("Mismatch limit values %s despite a successful setrlimit call (setrlimit'd soft 0x%16llx hard 0x%16llx but getrlimit'd soft 0x%16llx hard 0x%16llx)",
182*a325d9c4SApple OSS Distributions RESOURCE_STRING[i], newlim.rlim_cur, newlim.rlim_max, verifylim.rlim_cur, verifylim.rlim_max);
183*a325d9c4SApple OSS Distributions }
184*a325d9c4SApple OSS Distributions }
185*a325d9c4SApple OSS Distributions /* The kernel forces the hard limit of RLIMIT_NPROC to be at most maxproc for normal user when changing either soft/hard limit with setrlimit */
186*a325d9c4SApple OSS Distributions else if (i == RLIMIT_NPROC && newlim.rlim_max > maxprocperuid) {
187*a325d9c4SApple OSS Distributions if (newlim.rlim_cur != verifylim.rlim_cur ||
188*a325d9c4SApple OSS Distributions maxprocperuid != verifylim.rlim_max) {
189*a325d9c4SApple OSS Distributions T_FAIL("Mismatch limit values %s despite a successful setrlimit call (setrlimit'd soft 0x%16llx hard 0x%16llx but getrlimit'd soft 0x%16llx hard 0x%16llx)",
190*a325d9c4SApple OSS Distributions RESOURCE_STRING[i], newlim.rlim_cur, newlim.rlim_max, verifylim.rlim_cur, verifylim.rlim_max);
191*a325d9c4SApple OSS Distributions }
192*a325d9c4SApple OSS Distributions } else {
193*a325d9c4SApple OSS Distributions if (newlim.rlim_cur != verifylim.rlim_cur ||
194*a325d9c4SApple OSS Distributions newlim.rlim_max != verifylim.rlim_max) {
195*a325d9c4SApple OSS Distributions T_FAIL("Mismatch limit values %s despite a successful setrlimit call (setrlimit'd soft 0x%16llx hard 0x%16llx but getrlimit'd soft 0x%16llx hard 0x%16llx)",
196*a325d9c4SApple OSS Distributions RESOURCE_STRING[i], newlim.rlim_cur, newlim.rlim_max, verifylim.rlim_cur, verifylim.rlim_max);
197*a325d9c4SApple OSS Distributions }
198*a325d9c4SApple OSS Distributions }
199*a325d9c4SApple OSS Distributions }
200*a325d9c4SApple OSS Distributions }
201*a325d9c4SApple OSS Distributions
202*a325d9c4SApple OSS Distributions T_DECL(proc_rlimit,
203*a325d9c4SApple OSS Distributions "Test basic functionalities of the getrlimit and setrlimit")
204*a325d9c4SApple OSS Distributions {
205*a325d9c4SApple OSS Distributions int err;
206*a325d9c4SApple OSS Distributions struct rlimit lim;
207*a325d9c4SApple OSS Distributions
208*a325d9c4SApple OSS Distributions T_SETUPBEGIN;
209*a325d9c4SApple OSS Distributions
210*a325d9c4SApple OSS Distributions if (geteuid() == 0) {
211*a325d9c4SApple OSS Distributions superuser = TRUE;
212*a325d9c4SApple OSS Distributions T_SKIP("This test should not be run as super user.");
213*a325d9c4SApple OSS Distributions }
214*a325d9c4SApple OSS Distributions
215*a325d9c4SApple OSS Distributions /* Use sysctl to query the real limits of RLIMIT_NOFILE/RLIMIT_NPROC for normal user on Apple's systems */
216*a325d9c4SApple OSS Distributions err = sysctlbyname("kern.maxfilesperproc", &maxfilesperproc, &maxfilesperproc_size, NULL, 0);
217*a325d9c4SApple OSS Distributions T_EXPECT_EQ_INT(0, err, "maxfilesperproc: %llu", maxfilesperproc);
218*a325d9c4SApple OSS Distributions
219*a325d9c4SApple OSS Distributions err = sysctlbyname("kern.maxprocperuid", &maxprocperuid, &maxprocperuid_size, NULL, 0);
220*a325d9c4SApple OSS Distributions T_EXPECT_EQ_INT(0, err, "maxprocperuid: %llu", maxprocperuid);
221*a325d9c4SApple OSS Distributions
222*a325d9c4SApple OSS Distributions /* Use sysctl to query the real limits of RLIMIT_NOFILE/RLIMIT_NPROC for super user on Apple's systems (placeholder for adding super user tests) */
223*a325d9c4SApple OSS Distributions err = sysctlbyname("kern.maxfiles", &maxfiles, &maxfiles_size, NULL, 0);
224*a325d9c4SApple OSS Distributions T_EXPECT_EQ_INT(0, err, "maxfiles: %llu", maxfiles);
225*a325d9c4SApple OSS Distributions
226*a325d9c4SApple OSS Distributions err = sysctlbyname("kern.maxproc", &maxproc, &maxproc_size, NULL, 0);
227*a325d9c4SApple OSS Distributions T_EXPECT_EQ_INT(0, err, "maxproc: %llu", maxproc);
228*a325d9c4SApple OSS Distributions
229*a325d9c4SApple OSS Distributions /* Issue getrlimit syscall to retrieve the initial resource limit values before calling setrlimit */
230*a325d9c4SApple OSS Distributions err = get_initial_rlimits();
231*a325d9c4SApple OSS Distributions T_EXPECT_EQ(0, err, "Obtained initial resource values.");
232*a325d9c4SApple OSS Distributions
233*a325d9c4SApple OSS Distributions /* Print out resource limit values to stdout for less-painful triage in case needed */
234*a325d9c4SApple OSS Distributions T_LOG("Resource limits before the test:");
235*a325d9c4SApple OSS Distributions print_rlimits(TRUE);
236*a325d9c4SApple OSS Distributions
237*a325d9c4SApple OSS Distributions T_SETUPEND;
238*a325d9c4SApple OSS Distributions
239*a325d9c4SApple OSS Distributions /* Lower soft limits by arbitrary amount */
240*a325d9c4SApple OSS Distributions T_LOG("---------Lowering soft limits by 0x%x---------:\n", LIMIT_DIFF);
241*a325d9c4SApple OSS Distributions change_rlimits(SOFT_LIMIT, LIMIT_DIFF, LOWER);
242*a325d9c4SApple OSS Distributions
243*a325d9c4SApple OSS Distributions /* Raise soft limits back to the orginal values */
244*a325d9c4SApple OSS Distributions T_LOG("---------Raising soft limits by 0x%x---------:\n", LIMIT_DIFF);
245*a325d9c4SApple OSS Distributions change_rlimits(SOFT_LIMIT, LIMIT_DIFF, RAISE);
246*a325d9c4SApple OSS Distributions
247*a325d9c4SApple OSS Distributions /* Lower hard limits */
248*a325d9c4SApple OSS Distributions T_LOG("---------Lowering hard limits by 0x%x---------:", LIMIT_DIFF);
249*a325d9c4SApple OSS Distributions change_rlimits(HARD_LIMIT, LIMIT_DIFF, LOWER);
250*a325d9c4SApple OSS Distributions
251*a325d9c4SApple OSS Distributions /* Raise soft limits to exceed hard limits (setrlimit should fail, but the darwintest should pass) */
252*a325d9c4SApple OSS Distributions T_LOG("---------Attempting to raised soft limits by 0x%x to exceed hard limits---------:", LIMIT_DIFF);
253*a325d9c4SApple OSS Distributions change_rlimits(SOFT_LIMIT, LIMIT_DIFF, RAISE);
254*a325d9c4SApple OSS Distributions
255*a325d9c4SApple OSS Distributions /* Raise hard limits (setrlimit should fail, but the darwintest should pass) */
256*a325d9c4SApple OSS Distributions T_LOG("---------Attempting to raise hard limits by 0x%x---------:", LIMIT_DIFF);
257*a325d9c4SApple OSS Distributions change_rlimits(HARD_LIMIT, LIMIT_DIFF, RAISE);
258*a325d9c4SApple OSS Distributions
259*a325d9c4SApple OSS Distributions /* Get and set a non-existing resource limit */
260*a325d9c4SApple OSS Distributions T_LOG("---------Accessing a non-existing resource---------:");
261*a325d9c4SApple OSS Distributions err = getrlimit(RLIMIT_NLIMITS + 1, &lim);
262*a325d9c4SApple OSS Distributions T_EXPECT_EQ(-1, err, "Expect getrlimit to fail when accessing a non-existing resource: %s\n", strerror(errno));
263*a325d9c4SApple OSS Distributions T_EXPECT_EQ(EINVAL, errno, "Expect errno %d, errno returned %d", EINVAL, errno);
264*a325d9c4SApple OSS Distributions
265*a325d9c4SApple OSS Distributions err = setrlimit(RLIMIT_NLIMITS + 1, &lim);
266*a325d9c4SApple OSS Distributions T_EXPECT_EQ(-1, err, "Expect setrlimit to fail when accessing a non-existing resource: %s\n", strerror(errno));
267*a325d9c4SApple OSS Distributions T_EXPECT_EQ(EINVAL, errno, "Expect errno %d, errno returned %d", EINVAL, errno);
268*a325d9c4SApple OSS Distributions
269*a325d9c4SApple OSS Distributions T_LOG("Resource limits after the test:");
270*a325d9c4SApple OSS Distributions print_rlimits(FALSE);
271*a325d9c4SApple OSS Distributions }
272