/*- * Copyright (c) 2014 EMC Corp. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include #include #include #include #include enum { NL_NPROCS, NL_MAXPROC, NL_MARKER }; static struct { int order; const char *name; } namelist[] = { { NL_NPROCS, "_nprocs" }, { NL_MAXPROC, "_maxproc" }, { NL_MARKER, "" }, }; #define NNAMES (sizeof(namelist) / sizeof(*namelist)) static struct nlist nl[NNAMES]; #define MULTIUSERFUZZ 5 void t2(void) { pid_t p; for (;;) { if ((p = fork()) == 0) { sleep(2); _exit(0); } if (p == -1) break; } } void t1(int priv) { pid_t p; struct passwd *pw; if ((p = fork()) == 0) { if ((pw = getpwnam("nobody")) == NULL) err(1, "no such user: nobody"); if (priv == 0) { if (setgroups(1, &pw->pw_gid) || setegid(pw->pw_gid) || setgid(pw->pw_gid) || seteuid(pw->pw_uid) || setuid(pw->pw_uid)) err(1, "Can't drop privileges to \"nobody\""); } endpwent(); t2(); _exit(0); } waitpid(p, NULL, 0); } int getprocs(void) { kvm_t *kd; char *nlistf; char *memf; char buf[_POSIX2_LINE_MAX]; int i; int nprocs, maxproc; nlistf = memf = NULL; for (i = 0; i < (int)NNAMES; i++) nl[namelist[i].order].n_name = strdup(namelist[i].name); if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf)) == NULL) errx(1, "kvm_openfile(%s, %s): %s", nlistf, memf, buf); if (kvm_nlist(kd, nl) == -1) errx(1, "kvm_nlist: %s", kvm_geterr(kd)); if (kvm_read(kd, nl[NL_NPROCS].n_value, &nprocs, sizeof(nprocs)) != sizeof(nprocs)) errx(1, "kvm_read(): %s", kvm_geterr(kd)); if (kvm_read(kd, nl[NL_MAXPROC].n_value, &maxproc, sizeof(maxproc)) != sizeof(maxproc)) errx(1, "kvm_read(): %s", kvm_geterr(kd)); fprintf(stderr, "%d/%d (%d left)\n", nprocs, maxproc, maxproc - nprocs - 1); kvm_close(kd); return (maxproc - nprocs - 1); } ATF_TC(checkmaxproc); ATF_TC_HEAD(checkmaxproc, tc) { atf_tc_set_md_var(tc, "require.user", "root"); atf_tc_set_md_var(tc, "descr", "Check proc limit for unprivileged users"); } ATF_TC_BODY(checkmaxproc, tc) { int i, n; n = getprocs(); for (i = 0; i < n / 10 * 8; i++) { if (fork() == 0) { sleep(2); _exit(0); } } t1(0); ATF_REQUIRE(getprocs() >= 10 - MULTIUSERFUZZ); } ATF_TP_ADD_TCS(tp) { ATF_TP_ADD_TC(tp, checkmaxproc); return atf_no_error(); }