summaryrefslogtreecommitdiffstats
path: root/src/limits.c
blob: 32b770bb4d4644c7d06772288b53f07fdf4b3ffe (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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
/*
 * SPDX-License-Identifier: ISC
 *
 * Copyright (c) 1999-2021 Todd C. Miller <Todd.Miller@sudo.ws>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

/*
 * This is an open source non-commercial project. Dear PVS-Studio, please check it.
 * PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
 */

#include <config.h>

#include <sys/resource.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#ifdef __linux__
# include <sys/prctl.h>
#endif
#include <ctype.h>
#include <errno.h>
#include <limits.h>

#include "sudo.h"

/*
 * Avoid using RLIM_INFINITY for the nofile soft limit to prevent
 * closefrom_fallback() from closing too many file descriptors.
 */
#if defined(OPEN_MAX) && OPEN_MAX > 256
# define SUDO_OPEN_MAX	OPEN_MAX
#else
# define SUDO_OPEN_MAX	256
#endif

#ifdef __LP64__
# define SUDO_STACK_MIN	(4 * 1024 * 1024)
#else
# define SUDO_STACK_MIN	(2 * 1024 * 1024)
#endif

#ifdef HAVE_SETRLIMIT64
# define getrlimit(a, b) getrlimit64((a), (b))
# define setrlimit(a, b) setrlimit64((a), (b))
# define rlimit rlimit64
# define rlim_t rlim64_t
# undef RLIM_INFINITY
# define RLIM_INFINITY RLIM64_INFINITY
#endif /* HAVE_SETRLIMIT64 */

/* Older BSD systems have RLIMIT_VMEM, not RLIMIT_AS. */
#if !defined(RLIMIT_AS) && defined(RLIMIT_VMEM)
# define RLIMIT_AS RLIMIT_VMEM
#endif

/*
 * macOS doesn't allow nofile soft limit to be infinite or
 * the stack hard limit to be infinite.
 * Linux containers have a problem with an infinite stack soft limit.
 */
static struct rlimit stack_fallback = { SUDO_STACK_MIN, 65532 * 1024 };

static struct saved_limit {
    const char *name;		/* rlimit_foo in lower case */
    int resource;		/* RLIMIT_FOO definition */
    bool override;		/* override limit while sudo executes? */
    bool saved;			/* true if we were able to get the value */
    bool policy;		/* true if policy specified an rlimit */
    bool preserve;		/* true if policy says to preserve user limit */
    rlim_t minlimit;		/* only modify limit if less than this value */
    struct rlimit *fallback;	/* fallback if we fail to set to newlimit */
    struct rlimit newlimit;	/* new limit to use if override is true */
    struct rlimit oldlimit;	/* original limit, valid if saved is true */
    struct rlimit policylimit;	/* limit from policy, valid if policy is true */
} saved_limits[] = {
#ifdef RLIMIT_AS
    {
	"rlimit_as",
	RLIMIT_AS,
	true,					/* override */
	false,					/* saved */
	false,					/* policy */
	false,					/* preserve */
	1 * 1024 * 1024 * 1024,			/* minlimit */
	NULL,					/* fallback */
	{ RLIM_INFINITY, RLIM_INFINITY }	/* newlimit */
    },
#endif
    {
	"rlimit_core",
	RLIMIT_CORE,
	false					/* override */
    },
    {
	"rlimit_cpu",
	RLIMIT_CPU,
	true,					/* override */
	false,					/* saved */
	false,					/* policy */
	false,					/* preserve */
	RLIM_INFINITY,				/* minlimit */
	NULL,
	{ RLIM_INFINITY, RLIM_INFINITY }
    },
    {
	"rlimit_data",
	RLIMIT_DATA,
	true,					/* override */
	false,					/* saved */
	false,					/* policy */
	false,					/* preserve */
	1 * 1024 * 1024 * 1024,			/* minlimit */
	NULL,
	{ RLIM_INFINITY, RLIM_INFINITY }
    },
    {
	"rlimit_fsize",
	RLIMIT_FSIZE,
	true,					/* override */
	false,					/* saved */
	false,					/* policy */
	false,					/* preserve */
	RLIM_INFINITY,				/* minlimit */
	NULL,
	{ RLIM_INFINITY, RLIM_INFINITY }
    },
#ifdef RLIMIT_LOCKS
    {
	"rlimit_locks",
	RLIMIT_LOCKS,
	false					/* override */
    },
#endif
#ifdef RLIMIT_MEMLOCK
    {
	"rlimit_memlock",
	RLIMIT_MEMLOCK,
	false					/* override */
    },
#endif
    {
	"rlimit_nofile",
	RLIMIT_NOFILE,
	true,					/* override */
	false,					/* saved */
	false,					/* policy */
	false,					/* preserve */
	SUDO_OPEN_MAX,				/* minlimit */
	NULL,
	{ SUDO_OPEN_MAX, RLIM_INFINITY }
    },
#ifdef RLIMIT_NPROC
    {
	"rlimit_nproc",
	RLIMIT_NPROC,
	true,					/* override */
	false,					/* saved */
	false,					/* policy */
	false,					/* preserve */
	RLIM_INFINITY,				/* minlimit */
	NULL,
	{ RLIM_INFINITY, RLIM_INFINITY }
    },
#endif
#ifdef RLIMIT_RSS
    {
	"rlimit_rss",
	RLIMIT_RSS,
	true,					/* override */
	false,					/* saved */
	false,					/* policy */
	false,					/* preserve */
	RLIM_INFINITY,				/* minlimit */
	NULL,
	{ RLIM_INFINITY, RLIM_INFINITY }
    },
#endif
    {
	"rlimit_stack",
	RLIMIT_STACK,
	true,					/* override */
	false,					/* saved */
	false,					/* policy */
	false,					/* preserve */
	SUDO_STACK_MIN,				/* minlimit */
	&stack_fallback,
	{ SUDO_STACK_MIN, RLIM_INFINITY }
    }
};

static struct rlimit corelimit;
static bool coredump_disabled;
#ifdef __linux__
static struct rlimit nproclimit;
static int dumpflag;
#endif

/*
 * Disable core dumps to avoid dropping a core with user password in it.
 * Not all operating systems disable core dumps for setuid processes.
 */
void
disable_coredump(void)
{
    struct rlimit rl = { 0, 0 };
    debug_decl(disable_coredump, SUDO_DEBUG_UTIL);

    if (getrlimit(RLIMIT_CORE, &corelimit) == -1)
	sudo_warn("getrlimit(RLIMIT_CORE)");
    sudo_debug_printf(SUDO_DEBUG_INFO, "RLIMIT_CORE [%lld, %lld] -> [0, 0]",
	(long long)corelimit.rlim_cur, (long long)corelimit.rlim_max);
    if (setrlimit(RLIMIT_CORE, &rl) == -1)
	sudo_warn("setrlimit(RLIMIT_CORE)");
#ifdef __linux__
    /* On Linux, also set PR_SET_DUMPABLE to zero (reset by execve). */
    if ((dumpflag = prctl(PR_GET_DUMPABLE, 0, 0, 0, 0)) == -1) {
	sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_ERRNO,
	    "prctl(PR_GET_DUMPABLE, 0, 0, 0, 0)");
	dumpflag = 0;
    }
    if (prctl(PR_SET_DUMPABLE, 0, 0, 0, 0) == -1) {
	sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_ERRNO,
	    "prctl(PR_SET_DUMPABLE, 0, 0, 0, 0)");
    }
#endif /* __linux__ */
    coredump_disabled = true;

    debug_return;
}

/*
 * Restore core resource limit before executing the command.
 */
static void
restore_coredump(void)
{
    debug_decl(restore_coredump, SUDO_DEBUG_UTIL);

    if (coredump_disabled) {
	/*
	 * Linux containers don't allow RLIMIT_CORE to be set back to
	 * RLIM_INFINITY if we set the limit to zero, even for root.
	 */
	if (setrlimit(RLIMIT_CORE, &corelimit) == -1) {
	    sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_ERRNO,
		"setrlimit(RLIMIT_CORE, [%lld, %lld])",
		(long long)corelimit.rlim_cur, (long long)corelimit.rlim_max);
	}
#ifdef __linux__
	if (prctl(PR_SET_DUMPABLE, dumpflag, 0, 0, 0) == -1) {
	    sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_ERRNO,
		"prctl(PR_SET_DUMPABLE, %d, 0, 0, 0)", dumpflag);
	}
#endif /* __linux__ */
    }
    debug_return;
}

/*
 * Unlimit the number of processes since Linux's setuid() will
 * apply resource limits when changing uid and return EAGAIN if
 * nproc would be exceeded by the uid switch.
 *
 * This function is called *after* session setup and before the
 * final setuid() call.
 */
void
unlimit_nproc(void)
{
#ifdef __linux__
    struct rlimit rl = { RLIM_INFINITY, RLIM_INFINITY };
    debug_decl(unlimit_nproc, SUDO_DEBUG_UTIL);

    if (getrlimit(RLIMIT_NPROC, &nproclimit) != 0)
	sudo_warn("getrlimit(RLIMIT_NPROC)");
    sudo_debug_printf(SUDO_DEBUG_INFO, "RLIMIT_NPROC [%lld, %lld] -> [inf, inf]",
	(long long)nproclimit.rlim_cur, (long long)nproclimit.rlim_max);
    if (setrlimit(RLIMIT_NPROC, &rl) == -1) {
	rl.rlim_cur = rl.rlim_max = nproclimit.rlim_max;
	sudo_debug_printf(SUDO_DEBUG_INFO,
	    "RLIMIT_NPROC [%lld, %lld] -> [%lld, %lld]",
	    (long long)nproclimit.rlim_cur, (long long)nproclimit.rlim_max,
	    (long long)rl.rlim_cur, (long long)rl.rlim_max);
	if (setrlimit(RLIMIT_NPROC, &rl) != 0)
	    sudo_warn("setrlimit(RLIMIT_NPROC)");
    }
    debug_return;
#endif /* __linux__ */
}

/*
 * Restore saved value of RLIMIT_NPROC before execve().
 */
void
restore_nproc(void)
{
#ifdef __linux__
    debug_decl(restore_nproc, SUDO_DEBUG_UTIL);

    if (setrlimit(RLIMIT_NPROC, &nproclimit) != 0) {
	sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_ERRNO,
	    "setrlimit(RLIMIT_NPROC, [%lld, %lld])",
	    (long long)nproclimit.rlim_cur, (long long)nproclimit.rlim_max);
    }

    debug_return;
#endif /* __linux__ */
}

/*
 * Unlimit resource limits so sudo is not limited by, e.g.
 * stack, data or file table sizes.
 */
void
unlimit_sudo(void)
{
    unsigned int idx;
    int pass, rc;
    debug_decl(unlimit_sudo, SUDO_DEBUG_UTIL);

    /* Set resource limits to unlimited and stash the old values. */
    for (idx = 0; idx < nitems(saved_limits); idx++) {
	struct saved_limit *lim = &saved_limits[idx];
	if (getrlimit(lim->resource, &lim->oldlimit) == -1)
	    continue;
	sudo_debug_printf(SUDO_DEBUG_INFO,
	    "getrlimit(%s) -> [%lld, %lld]", lim->name,
	    (long long)lim->oldlimit.rlim_cur,
	    (long long)lim->oldlimit.rlim_max);
	lim->saved = true;

	/* Only override the existing limit if it is smaller than minlimit. */
	if (lim->minlimit != RLIM_INFINITY) {
	    if (lim->oldlimit.rlim_cur >= lim->minlimit)
		lim->override = false;
	}
	if (!lim->override)
	    continue;

	for (pass = 0; pass < 2; pass++) {
	    if (lim->newlimit.rlim_cur != RLIM_INFINITY) {
		/* Don't reduce the soft resource limit. */
		if (lim->oldlimit.rlim_cur == RLIM_INFINITY ||
			lim->oldlimit.rlim_cur > lim->newlimit.rlim_cur)
		    lim->newlimit.rlim_cur = lim->oldlimit.rlim_cur;
	    }
	    if (lim->newlimit.rlim_max != RLIM_INFINITY) {
		/* Don't reduce the hard resource limit. */
		if (lim->oldlimit.rlim_max == RLIM_INFINITY ||
			lim->oldlimit.rlim_max > lim->newlimit.rlim_max)
		    lim->newlimit.rlim_max = lim->oldlimit.rlim_max;
	    }
	    if ((rc = setrlimit(lim->resource, &lim->newlimit)) == -1) {
		sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_ERRNO,
		    "setrlimit(%s, [%lld, %lld])", lim->name,
		    (long long)lim->newlimit.rlim_cur,
		    (long long)lim->newlimit.rlim_max);
		if (pass == 0 && lim->fallback != NULL) {
		    /* Try again using fallback values. */
		    lim->newlimit.rlim_cur = lim->fallback->rlim_cur;
		    lim->newlimit.rlim_max = lim->fallback->rlim_max;
		    continue;
		}
	    }
	    break;
	}
	if (rc == -1) {
	    /* Try setting new rlim_cur to old rlim_max. */
	    lim->newlimit.rlim_cur = lim->oldlimit.rlim_max;
	    lim->newlimit.rlim_max = lim->oldlimit.rlim_max;
	    if ((rc = setrlimit(lim->resource, &lim->newlimit)) == -1) {
		sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_ERRNO,
		    "setrlimit(%s, [%lld, %lld])", lim->name,
		    (long long)lim->newlimit.rlim_cur,
		    (long long)lim->newlimit.rlim_max);
	    }
	}
	if (rc == -1)
	    sudo_warn("setrlimit(%s)", lim->name);
    }

    debug_return;
}

/*
 * Restore resource limits modified by unlimit_sudo() and disable_coredump().
 */
void
restore_limits(void)
{
    unsigned int idx;
    debug_decl(restore_limits, SUDO_DEBUG_UTIL);

    /* Restore resource limits to saved values. */
    for (idx = 0; idx < nitems(saved_limits); idx++) {
	struct saved_limit *lim = &saved_limits[idx];
	if (lim->override && lim->saved) {
	    struct rlimit rl = lim->oldlimit;
	    int i, rc;

	    for (i = 0; i < 10; i++) {
		rc = setrlimit(lim->resource, &rl);
		if (rc != -1 || errno != EINVAL)
		    break;

		sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_ERRNO,
		    "setrlimit(%s, [%lld, %lld])", lim->name,
		    (long long)rl.rlim_cur, (long long)rl.rlim_max);

		/*
		 * Soft limit could be lower than current resource usage.
		 * This can be an issue on NetBSD with RLIMIT_STACK and ASLR.
		 */
		if (rl.rlim_cur > LLONG_MAX / 2)
		    break;
		rl.rlim_cur *= 2;
		if (lim->newlimit.rlim_cur != RLIM_INFINITY &&
			rl.rlim_cur > lim->newlimit.rlim_cur) {
		    rl.rlim_cur = lim->newlimit.rlim_cur;
		}
		if (rl.rlim_max != RLIM_INFINITY &&
			rl.rlim_cur > rl.rlim_max) {
		    rl.rlim_max = rl.rlim_cur;
		}
		rc = setrlimit(lim->resource, &rl);
		if (rc != -1 || errno != EINVAL)
		    break;
	    }
	    if (rc == -1)
		sudo_warn("setrlimit(%s)", lim->name);
	}
    }
    restore_coredump();

    debug_return;
}

static bool
store_rlimit(const char *str, rlim_t *val, bool soft)
{
    const size_t inflen = sizeof("infinity") - 1;
    debug_decl(store_rlimit, SUDO_DEBUG_UTIL);

    if (isdigit((unsigned char)*str)) {
	unsigned long long ullval = 0;
	char *ep;

	errno = 0;
#ifdef HAVE_STRTOULL
	ullval = strtoull(str, &ep, 10);
	if (str == ep || (errno == ERANGE && ullval == ULLONG_MAX))
	    debug_return_bool(false);
#else
	ullval = strtoul(str, &ep, 10);
	if (str == ep || (errno == ERANGE && ullval == ULONG_MAX))
	    debug_return_bool(false);
#endif
	if (*ep == '\0' || (soft && *ep == ',')) {
	    *val = ullval;
	    debug_return_bool(true);
	}
	goto done;
    }
    if (strncmp(str, "infinity", inflen) == 0) {
	if (str[inflen] == '\0' || (soft && str[inflen] == ',')) {
	    *val = RLIM_INFINITY;
	    debug_return_bool(true);
	}
    }
done:
    debug_return_bool(false);
}

static bool
set_policy_rlimit(int resource, const char *val)
{
    unsigned int idx;
    debug_decl(set_policy_rlimit, SUDO_DEBUG_UTIL);

    for (idx = 0; idx < nitems(saved_limits); idx++) {
	struct saved_limit *lim = &saved_limits[idx];
	const char *hard, *soft = val;

	if (lim->resource != resource)
	    continue;

	if (strcmp(val, "default") == 0) {
	    /* Use system-assigned limit set by begin_session(). */
	    lim->policy = false;
	    lim->preserve = false;
	    debug_return_bool(true);
	}
	if (strcmp(val, "user") == 0) {
	    /* Preserve invoking user's limit. */
	    lim->policy = false;
	    lim->preserve = true;
	    debug_return_bool(true);
	}

	/*
	 * Expect limit in the form "soft,hard" or "limit" (both soft+hard).
	 */
	hard = strchr(val, ',');
	if (hard != NULL)
	    hard++;
	else
	    hard = soft;

	if (store_rlimit(soft, &lim->policylimit.rlim_cur, true) &&
		store_rlimit(hard, &lim->policylimit.rlim_max, false)) {
	    lim->policy = true;
	    lim->preserve = false;
	    debug_return_bool(true);
	}
	sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
	    "%s: invalid rlimit: %s", lim->name, val);
	debug_return_bool(false);
    }
    sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
	"invalid resource limit: %d", resource);
    debug_return_bool(false);
}

bool
parse_policy_rlimit(const char *str)
{
    bool ret = false;
    debug_decl(parse_policy_rlimit, SUDO_DEBUG_UTIL);

#ifdef RLIMIT_AS
    if (strncmp(str, "as=", sizeof("as=") - 1) == 0) {
	str += sizeof("as=") - 1;
	ret = set_policy_rlimit(RLIMIT_AS, str);
    } else
#endif
#ifdef RLIMIT_CORE
    if (strncmp(str, "core=", sizeof("core=") - 1) == 0) {
	str += sizeof("core=") - 1;
	ret = set_policy_rlimit(RLIMIT_CORE, str);
    } else
#endif
#ifdef RLIMIT_CPU
    if (strncmp(str, "cpu=", sizeof("cpu=") - 1) == 0) {
	str += sizeof("cpu=") - 1;
	ret = set_policy_rlimit(RLIMIT_CPU, str);
    } else
#endif
#ifdef RLIMIT_DATA
    if (strncmp(str, "data=", sizeof("data=") - 1) == 0) {
	str += sizeof("data=") - 1;
	ret = set_policy_rlimit(RLIMIT_DATA, str);
    } else
#endif
#ifdef RLIMIT_FSIZE
    if (strncmp(str, "fsize=", sizeof("fsize=") - 1) == 0) {
	str += sizeof("fsize=") - 1;
	ret = set_policy_rlimit(RLIMIT_FSIZE, str);
    } else
#endif
#ifdef RLIMIT_LOCKS
    if (strncmp(str, "locks=", sizeof("locks=") - 1) == 0) {
	str += sizeof("locks=") - 1;
	ret = set_policy_rlimit(RLIMIT_LOCKS, str);
    } else
#endif
#ifdef RLIMIT_MEMLOCK
    if (strncmp(str, "memlock=", sizeof("memlock=") - 1) == 0) {
	str += sizeof("memlock=") - 1;
	ret = set_policy_rlimit(RLIMIT_MEMLOCK, str);
    } else
#endif
#ifdef RLIMIT_NOFILE
    if (strncmp(str, "nofile=", sizeof("nofile=") - 1) == 0) {
	str += sizeof("nofile=") - 1;
	ret = set_policy_rlimit(RLIMIT_NOFILE, str);
    } else
#endif
#ifdef RLIMIT_NPROC
    if (strncmp(str, "nproc=", sizeof("nproc=") - 1) == 0) {
	str += sizeof("nproc=") - 1;
	ret = set_policy_rlimit(RLIMIT_NPROC, str);
    } else
#endif
#ifdef RLIMIT_RSS
    if (strncmp(str, "rss=", sizeof("rss=") - 1) == 0) {
	str += sizeof("rss=") - 1;
	ret = set_policy_rlimit(RLIMIT_RSS, str);
    } else
#endif
#ifdef RLIMIT_STACK
    if (strncmp(str, "stack=", sizeof("stack=") - 1) == 0) {
	str += sizeof("stack=") - 1;
	ret = set_policy_rlimit(RLIMIT_STACK, str);
    }
#endif
    debug_return_bool(ret);
}

/*
 * Set resource limits as specified by the security policy (if any).
 * This should be run as part of the session setup but after PAM,
 * login.conf, etc.
 */
void
set_policy_rlimits(void)
{
    unsigned int idx;
    debug_decl(set_policy_rlimits, SUDO_DEBUG_UTIL);

    for (idx = 0; idx < nitems(saved_limits); idx++) {
	struct saved_limit *lim = &saved_limits[idx];
	struct rlimit *rl;
	int rc;

	if (!lim->policy && (!lim->preserve || !lim->saved))
	    continue;

	rl = lim->preserve ? &lim->oldlimit : &lim->policylimit;
	if ((rc = setrlimit(lim->resource, rl)) == 0) {
	    sudo_debug_printf(SUDO_DEBUG_INFO|SUDO_DEBUG_LINENO,
		"setrlimit(%s, [%lld, %lld])", lim->name,
		(long long)rl->rlim_cur, (long long)rl->rlim_max);
	    continue;
	}

	sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO|SUDO_DEBUG_ERRNO,
	    "setrlimit(%s, [%lld, %lld])", lim->name,
	    (long long)rl->rlim_cur, (long long)rl->rlim_max);

	if (rl->rlim_cur > lim->oldlimit.rlim_max || rl->rlim_max > lim->oldlimit.rlim_max) {
	    /* Try setting policy rlim_cur to old rlim_max. */
	    if (rl->rlim_cur > lim->oldlimit.rlim_max)
		rl->rlim_cur = lim->oldlimit.rlim_max;
	    if (rl->rlim_max > lim->oldlimit.rlim_max)
		rl->rlim_max = lim->oldlimit.rlim_max;
	    if ((rc = setrlimit(lim->resource, rl)) == -1) {
		sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_ERRNO,
		    "setrlimit(%s, [%lld, %lld])", lim->name,
		    (long long)rl->rlim_cur, (long long)rl->rlim_max);
	    }
	}
	if (rc == -1)
	    sudo_warn("setrlimit(%s)", lim->name);
    }

    debug_return;
}

int
serialize_rlimits(char **info, size_t info_max)
{
    char *str;
    unsigned int idx, nstored = 0;
    debug_decl(serialize_rlimits, SUDO_DEBUG_UTIL);

    for (idx = 0; idx < nitems(saved_limits); idx++) {
	const struct saved_limit *lim = &saved_limits[idx];
	const struct rlimit *rl = &lim->oldlimit;
	char curlim[(((sizeof(long long) * 8) + 2) / 3) + 2];
	char maxlim[(((sizeof(long long) * 8) + 2) / 3) + 2];

	if (!lim->saved)
	    continue;

	if (nstored == info_max)
	    goto oom;

	if (rl->rlim_cur == RLIM_INFINITY) {
	    strlcpy(curlim, "infinity", sizeof(curlim));
	} else {
	    snprintf(curlim, sizeof(curlim), "%llu",
		(unsigned long long)rl->rlim_cur);
	}
	if (rl->rlim_max == RLIM_INFINITY) {
	    strlcpy(maxlim, "infinity", sizeof(maxlim));
	} else {
	    snprintf(maxlim, sizeof(maxlim), "%llu",
		(unsigned long long)rl->rlim_max);
	}
	if (asprintf(&str, "%s=%s,%s", lim->name, curlim, maxlim) == -1)
	    goto oom;
	info[nstored++] = str;
    }
    debug_return_int(nstored);
oom:
    while (nstored--)
	free(info[nstored]);
    debug_return_int(-1);
}