summaryrefslogtreecommitdiff
path: root/usr/src/uts/common/syscall/rlimit.c
blob: 9b7b6ffee6079b73fbb4afa2dca41f6edde56835 (plain)
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
/*	  All Rights Reserved	*/


#include <sys/param.h>
#include <sys/types.h>
#include <sys/inttypes.h>
#include <sys/sysmacros.h>
#include <sys/systm.h>
#include <sys/tuneable.h>
#include <sys/user.h>
#include <sys/errno.h>
#include <sys/vnode.h>
#include <sys/file.h>
#include <sys/proc.h>
#include <sys/resource.h>
#include <sys/ulimit.h>
#include <sys/debug.h>
#include <sys/rctl.h>

#include <vm/as.h>

/*
 * Perhaps ulimit could be moved into a user library, as calls to
 * getrlimit and setrlimit, were it not for binary compatibility
 * restrictions.
 */
long
ulimit(int cmd, long arg)
{
	proc_t *p = curproc;
	long	retval;

	switch (cmd) {

	case UL_GFILLIM: /* Return current file size limit. */
	{
		rlim64_t filesize;

		mutex_enter(&p->p_lock);
		filesize = rctl_enforced_value(rctlproc_legacy[RLIMIT_FSIZE],
		    p->p_rctls, p);
		mutex_exit(&p->p_lock);

		if (get_udatamodel() == DATAMODEL_ILP32) {
			/*
			 * File size is returned in blocks for ulimit.
			 * This function is deprecated and therefore LFS API
			 * didn't define the behaviour of ulimit.
			 * Here we return maximum value of file size possible
			 * so that applications that do not check errors
			 * continue to work.
			 */
			if (filesize > MAXOFF32_T)
				filesize = MAXOFF32_T;
			retval = ((int)filesize >> SCTRSHFT);
		} else
			retval = filesize >> SCTRSHFT;
		break;
	}

	case UL_SFILLIM: /* Set new file size limit. */
	{
		int error = 0;
		rlim64_t lim = (rlim64_t)arg;
		struct rlimit64 rl64;
		rctl_alloc_gp_t *gp = rctl_rlimit_set_prealloc(1);

		if (lim >= (((rlim64_t)MAXOFFSET_T) >> SCTRSHFT))
			lim = (rlim64_t)RLIM64_INFINITY;
		else
			lim <<= SCTRSHFT;

		rl64.rlim_max = rl64.rlim_cur = lim;
		mutex_enter(&p->p_lock);
		if (error = rctl_rlimit_set(rctlproc_legacy[RLIMIT_FSIZE], p,
		    &rl64, gp, RCTL_LOCAL_DENY | RCTL_LOCAL_SIGNAL, SIGXFSZ,
		    CRED())) {
			mutex_exit(&p->p_lock);
			rctl_prealloc_destroy(gp);
			return (set_errno(error));
		}
		mutex_exit(&p->p_lock);
		rctl_prealloc_destroy(gp);
		retval = arg;
		break;
	}

	case UL_GMEMLIM: /* Return maximum possible break value. */
	{
		struct seg *seg;
		struct seg *nextseg;
		struct as *as = p->p_as;
		caddr_t brkend;
		caddr_t brkbase;
		size_t size;
		rlim64_t size_ctl;
		rlim64_t vmem_ctl;

		/*
		 * Find the segment with a virtual address
		 * greater than the end of the current break.
		 */
		nextseg = NULL;
		mutex_enter(&p->p_lock);
		brkbase = (caddr_t)p->p_brkbase;
		brkend = (caddr_t)p->p_brkbase + p->p_brksize;
		mutex_exit(&p->p_lock);

		/*
		 * Since we can't return less than the current break,
		 * initialize the return value to the current break
		 */
		retval = (long)brkend;

		AS_LOCK_ENTER(as, RW_READER);
		for (seg = as_findseg(as, brkend, 0); seg != NULL;
		    seg = AS_SEGNEXT(as, seg)) {
			if (seg->s_base >= brkend) {
				nextseg = seg;
				break;
			}
		}

		mutex_enter(&p->p_lock);
		size_ctl = rctl_enforced_value(rctlproc_legacy[RLIMIT_DATA],
		    p->p_rctls, p);
		vmem_ctl = rctl_enforced_value(rctlproc_legacy[RLIMIT_VMEM],
		    p->p_rctls, p);
		mutex_exit(&p->p_lock);

		/*
		 * First, calculate the maximum break value based on
		 * the user's RLIMIT_DATA, but also taking into account
		 * that this value cannot be greater than as->a_userlimit.
		 * We also take care to make sure that we don't overflow
		 * in the calculation.
		 */
		/*
		 * Since we are casting the RLIMIT_DATA value to a
		 * ulong (a 32-bit value in the 32-bit kernel) we have
		 * to pass this assertion.
		 */
		ASSERT32((size_t)size_ctl <= UINT32_MAX);

		size = (size_t)size_ctl;
		if (as->a_userlimit - brkbase > size)
			retval = MAX((size_t)retval, (size_t)(brkbase + size));
					/* don't return less than current */
		else
			retval = (long)as->a_userlimit;

		/*
		 * The max break cannot extend into the next segment
		 */
		if (nextseg != NULL)
			retval = MIN((uintptr_t)retval,
			    (uintptr_t)nextseg->s_base);

		/*
		 * Handle the case where there is an limit on RLIMIT_VMEM
		 */
		if (vmem_ctl < UINT64_MAX) {
			/* calculate brkend based on the end of page */
			caddr_t brkendpg = (caddr_t)roundup((uintptr_t)brkend,
			    PAGESIZE);
			/*
			 * Large Files: The following assertion has to pass
			 * through to ensure the correctness of the cast.
			 */
			ASSERT32(vmem_ctl <= UINT32_MAX);

			size = (size_t)(vmem_ctl & PAGEMASK);

			if (as->a_size < size)
				size -= as->a_size;
			else
				size = 0;
			/*
			 * Take care to not overflow the calculation
			 */
			if (as->a_userlimit - brkendpg > size)
				retval = MIN((size_t)retval,
				    (size_t)(brkendpg + size));
		}

		AS_LOCK_EXIT(as);

		/* truncate to same boundary as sbrk */

		switch (get_udatamodel()) {
		default:
		case DATAMODEL_ILP32:
			retval = retval & ~(8-1);
			break;
		case DATAMODEL_LP64:
			retval = retval & ~(16-1);
			break;
		}
		break;
	}

	case UL_GDESLIM: /* Return approximate number of open files */
	{
		rlim64_t fdno_ctl;

		mutex_enter(&curproc->p_lock);
		fdno_ctl = rctl_enforced_value(rctlproc_legacy[RLIMIT_NOFILE],
		    curproc->p_rctls, curproc);
		ASSERT(fdno_ctl <= INT_MAX);
		retval = (rlim_t)fdno_ctl;
		mutex_exit(&curproc->p_lock);
		break;
	}

	default:
		return (set_errno(EINVAL));

	}
	return (retval);
}

#ifdef _SYSCALL32_IMPL

int
ulimit32(int cmd, int arg)
{
	return ((int)ulimit(cmd, (long)arg));
}

#endif	/* _SYSCALL32_IMPL */

#if defined(_ILP32) || defined(_SYSCALL32_IMPL)

/*
 * Large Files: getrlimit returns RLIM_SAVED_CUR or RLIM_SAVED_MAX when
 * rlim_cur or rlim_max is not representable in 32-bit rlim_t. These
 * values are just tokens which will be used in setrlimit to set the
 * correct limits. The current limits are saved in the saved_rlimit members
 * in user structures when the token is returned. setrlimit restores
 * the limit values to these saved values when the token is passed.
 * Consider the following common scenario of the apps:
 *
 * 		limit = getrlimit();
 *		savedlimit = limit;
 * 		limit = limit1;
 *		setrlimit(limit)
 *		// execute all processes in the new rlimit state.
 *		setrlimit(savedlimit) // restore the old values.
 *
 * Most apps don't check error returns from getrlimit or setrlimit
 * and this is why we return tokens when the correct value
 * cannot be represented in rlim_t. For more discussion refer to
 * the LFS API document.
 *
 * In the 64-bit kernel, all existing resource limits are treated in this
 * manner.  In the 32-bit kernel, CPU time is treated equivalently to the
 * file size limit above; the VM-related limits are not.  The macro,
 * RLIM_SAVED(x), returns true if the resource limit should be handled in
 * this way on the current kernel.
 */
int
getrlimit32(int resource, struct rlimit32 *rlp)
{
	struct rlimit32 rlim32;
	struct rlimit64 rlim64;
	struct proc *p = curproc;
	struct user *up = PTOU(p);
	int savecur = 0;
	int savemax = 0;

	if (resource < 0 || resource >= RLIM_NLIMITS)
		return (set_errno(EINVAL));

	mutex_enter(&p->p_lock);
	(void) rctl_rlimit_get(rctlproc_legacy[resource], p, &rlim64);
	mutex_exit(&p->p_lock);

	if (rlim64.rlim_max > (rlim64_t)UINT32_MAX) {

		if (rlim64.rlim_max == RLIM64_INFINITY)
			rlim32.rlim_max = RLIM32_INFINITY;
		else {
			savemax = 1;
			rlim32.rlim_max = RLIM32_SAVED_MAX;
			/*CONSTCOND*/
			ASSERT(RLIM_SAVED(resource));
		}

		if (rlim64.rlim_cur == RLIM64_INFINITY)
			rlim32.rlim_cur = RLIM32_INFINITY;
		else if (rlim64.rlim_cur == rlim64.rlim_max) {
			savecur = 1;
			rlim32.rlim_cur = RLIM32_SAVED_MAX;
			/*CONSTCOND*/
			ASSERT(RLIM_SAVED(resource));
		} else if (rlim64.rlim_cur > (rlim64_t)UINT32_MAX) {
			savecur = 1;
			rlim32.rlim_cur = RLIM32_SAVED_CUR;
			/*CONSTCOND*/
			ASSERT(RLIM_SAVED(resource));
		} else
			rlim32.rlim_cur = rlim64.rlim_cur;

		/*
		 * save the current limits in user structure.
		 */
		/*CONSTCOND*/
		if (RLIM_SAVED(resource)) {
			mutex_enter(&p->p_lock);
			if (savemax)
				up->u_saved_rlimit[resource].rlim_max =
				    rlim64.rlim_max;
			if (savecur)
				up->u_saved_rlimit[resource].rlim_cur =
				    rlim64.rlim_cur;
			mutex_exit(&p->p_lock);
		}
	} else {
		ASSERT(rlim64.rlim_cur <= (rlim64_t)UINT32_MAX);
		rlim32.rlim_max = rlim64.rlim_max;
		rlim32.rlim_cur = rlim64.rlim_cur;
	}

	if (copyout(&rlim32, rlp, sizeof (rlim32)))
		return (set_errno(EFAULT));

	return (0);
}

/*
 * See comments above getrlimit32(). When the tokens are passed in the
 * rlimit structure the values are considered equal to the values
 * stored in saved_rlimit members of user structure.
 * When the user passes RLIM_INFINITY to set the resource limit to
 * unlimited internally understand this value as RLIM64_INFINITY and
 * let rlimit() do the job.
 */
int
setrlimit32(int resource, struct rlimit32 *rlp)
{
	struct rlimit32 rlim32;
	struct rlimit64 rlim64;
	struct rlimit64 saved_rlim;
	int	error;
	struct proc *p = ttoproc(curthread);
	struct user *up = PTOU(p);
	rctl_alloc_gp_t *gp;

	if (resource < 0 || resource >= RLIM_NLIMITS)
		return (set_errno(EINVAL));
	if (copyin(rlp, &rlim32, sizeof (rlim32)))
		return (set_errno(EFAULT));

	gp = rctl_rlimit_set_prealloc(1);

	/*
	 * Disallow resource limit tunnelling
	 */
	/*CONSTCOND*/
	if (RLIM_SAVED(resource)) {
		mutex_enter(&p->p_lock);
		saved_rlim = up->u_saved_rlimit[resource];
		mutex_exit(&p->p_lock);
	} else {
		saved_rlim.rlim_max = (rlim64_t)rlim32.rlim_max;
		saved_rlim.rlim_cur = (rlim64_t)rlim32.rlim_cur;
	}

	switch (rlim32.rlim_cur) {
	case RLIM32_INFINITY:
		rlim64.rlim_cur = RLIM64_INFINITY;
		break;
	case RLIM32_SAVED_CUR:
		rlim64.rlim_cur = saved_rlim.rlim_cur;
		break;
	case RLIM32_SAVED_MAX:
		rlim64.rlim_cur = saved_rlim.rlim_max;
		break;
	default:
		rlim64.rlim_cur = (rlim64_t)rlim32.rlim_cur;
		break;
	}

	switch (rlim32.rlim_max) {
	case RLIM32_INFINITY:
		rlim64.rlim_max = RLIM64_INFINITY;
		break;
	case RLIM32_SAVED_MAX:
		rlim64.rlim_max = saved_rlim.rlim_max;
		break;
	case RLIM32_SAVED_CUR:
		rlim64.rlim_max = saved_rlim.rlim_cur;
		break;
	default:
		rlim64.rlim_max = (rlim64_t)rlim32.rlim_max;
		break;
	}

	mutex_enter(&p->p_lock);
	if (error = rctl_rlimit_set(rctlproc_legacy[resource], p, &rlim64, gp,
	    rctlproc_flags[resource], rctlproc_signals[resource], CRED())) {
		mutex_exit(&p->p_lock);
		rctl_prealloc_destroy(gp);
		return (set_errno(error));
	}
	mutex_exit(&p->p_lock);
	rctl_prealloc_destroy(gp);

	return (0);
}

#endif	/* _ILP32 && _SYSCALL32_IMPL */

int
getrlimit64(int resource, struct rlimit64 *rlp)
{
	struct rlimit64 rlim64;
	struct proc *p = ttoproc(curthread);

	if (resource < 0 || resource >= RLIM_NLIMITS)
		return (set_errno(EINVAL));

	mutex_enter(&p->p_lock);
	(void) rctl_rlimit_get(rctlproc_legacy[resource], p, &rlim64);
	mutex_exit(&p->p_lock);

	if (copyout(&rlim64, rlp, sizeof (rlim64)))
		return (set_errno(EFAULT));
	return (0);
}

int
setrlimit64(int resource, struct rlimit64 *rlp)
{
	struct rlimit64 rlim64;
	struct proc *p = ttoproc(curthread);
	int	error;
	rctl_alloc_gp_t *gp;

	if (resource < 0 || resource >= RLIM_NLIMITS)
		return (set_errno(EINVAL));
	if (copyin(rlp, &rlim64, sizeof (rlim64)))
		return (set_errno(EFAULT));

	gp = rctl_rlimit_set_prealloc(1);

	mutex_enter(&p->p_lock);
	if (error = rctl_rlimit_set(rctlproc_legacy[resource], p, &rlim64, gp,
	    rctlproc_flags[resource], rctlproc_signals[resource], CRED())) {
		mutex_exit(&p->p_lock);
		rctl_prealloc_destroy(gp);
		return (set_errno(error));
	}
	mutex_exit(&p->p_lock);
	rctl_prealloc_destroy(gp);
	return (0);

}