1*57718be8SEnji Cooper /* $NetBSD: t_clone.c,v 1.3 2011/12/12 20:55:44 joerg Exp $ */ 2*57718be8SEnji Cooper 3*57718be8SEnji Cooper /*- 4*57718be8SEnji Cooper * Copyright (c) 2008 The NetBSD Foundation, Inc. 5*57718be8SEnji Cooper * All rights reserved. 6*57718be8SEnji Cooper * 7*57718be8SEnji Cooper * This code is derived from software contributed to The NetBSD Foundation 8*57718be8SEnji Cooper * by Jason R. Thorpe. 9*57718be8SEnji Cooper * 10*57718be8SEnji Cooper * Redistribution and use in source and binary forms, with or without 11*57718be8SEnji Cooper * modification, are permitted provided that the following conditions 12*57718be8SEnji Cooper * are met: 13*57718be8SEnji Cooper * 1. Redistributions of source code must retain the above copyright 14*57718be8SEnji Cooper * notice, this list of conditions and the following disclaimer. 15*57718be8SEnji Cooper * 2. Redistributions in binary form must reproduce the above copyright 16*57718be8SEnji Cooper * notice, this list of conditions and the following disclaimer in the 17*57718be8SEnji Cooper * documentation and/or other materials provided with the distribution. 18*57718be8SEnji Cooper * 19*57718be8SEnji Cooper * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20*57718be8SEnji Cooper * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21*57718be8SEnji Cooper * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22*57718be8SEnji Cooper * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23*57718be8SEnji Cooper * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24*57718be8SEnji Cooper * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25*57718be8SEnji Cooper * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26*57718be8SEnji Cooper * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27*57718be8SEnji Cooper * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28*57718be8SEnji Cooper * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29*57718be8SEnji Cooper * POSSIBILITY OF SUCH DAMAGE. 30*57718be8SEnji Cooper */ 31*57718be8SEnji Cooper 32*57718be8SEnji Cooper #include <sys/cdefs.h> 33*57718be8SEnji Cooper __COPYRIGHT("@(#) Copyright (c) 2008\ 34*57718be8SEnji Cooper The NetBSD Foundation, inc. All rights reserved."); 35*57718be8SEnji Cooper __RCSID("$NetBSD: t_clone.c,v 1.3 2011/12/12 20:55:44 joerg Exp $"); 36*57718be8SEnji Cooper 37*57718be8SEnji Cooper #include <sys/mman.h> 38*57718be8SEnji Cooper #include <sys/resource.h> 39*57718be8SEnji Cooper #include <sys/types.h> 40*57718be8SEnji Cooper #include <sys/wait.h> 41*57718be8SEnji Cooper 42*57718be8SEnji Cooper #include <errno.h> 43*57718be8SEnji Cooper #include <sched.h> 44*57718be8SEnji Cooper #include <signal.h> 45*57718be8SEnji Cooper #include <stdio.h> 46*57718be8SEnji Cooper #include <stdlib.h> 47*57718be8SEnji Cooper #include <string.h> 48*57718be8SEnji Cooper #include <unistd.h> 49*57718be8SEnji Cooper 50*57718be8SEnji Cooper #include <atf-c.h> 51*57718be8SEnji Cooper 52*57718be8SEnji Cooper #define STACKSIZE (8 * 1024) 53*57718be8SEnji Cooper #define FROBVAL 41973 54*57718be8SEnji Cooper #define CHILDEXIT 0xa5 55*57718be8SEnji Cooper 56*57718be8SEnji Cooper static int 57*57718be8SEnji Cooper dummy(void *arg) 58*57718be8SEnji Cooper { 59*57718be8SEnji Cooper 60*57718be8SEnji Cooper return 0; 61*57718be8SEnji Cooper } 62*57718be8SEnji Cooper 63*57718be8SEnji Cooper static int 64*57718be8SEnji Cooper clone_func(void *arg) 65*57718be8SEnji Cooper { 66*57718be8SEnji Cooper long *frobp = arg, diff; 67*57718be8SEnji Cooper 68*57718be8SEnji Cooper printf("child: stack ~= %p, frobme = %p\n", &frobp, frobp); 69*57718be8SEnji Cooper fflush(stdout); 70*57718be8SEnji Cooper 71*57718be8SEnji Cooper if (frobp[0] != getppid()) 72*57718be8SEnji Cooper return 1; 73*57718be8SEnji Cooper 74*57718be8SEnji Cooper if (frobp[0] == getpid()) 75*57718be8SEnji Cooper return 2; 76*57718be8SEnji Cooper 77*57718be8SEnji Cooper diff = labs(frobp[1] - (long) &frobp); 78*57718be8SEnji Cooper 79*57718be8SEnji Cooper if (diff > 1024) 80*57718be8SEnji Cooper return 3; 81*57718be8SEnji Cooper 82*57718be8SEnji Cooper frobp[1] = FROBVAL; 83*57718be8SEnji Cooper 84*57718be8SEnji Cooper return (CHILDEXIT); 85*57718be8SEnji Cooper } 86*57718be8SEnji Cooper 87*57718be8SEnji Cooper ATF_TC(clone_basic); 88*57718be8SEnji Cooper ATF_TC_HEAD(clone_basic, tc) 89*57718be8SEnji Cooper { 90*57718be8SEnji Cooper 91*57718be8SEnji Cooper atf_tc_set_md_var(tc, "descr", "Checks clone(2)"); 92*57718be8SEnji Cooper } 93*57718be8SEnji Cooper 94*57718be8SEnji Cooper ATF_TC_BODY(clone_basic, tc) 95*57718be8SEnji Cooper { 96*57718be8SEnji Cooper sigset_t mask; 97*57718be8SEnji Cooper void *allocstack, *stack; 98*57718be8SEnji Cooper pid_t pid; 99*57718be8SEnji Cooper volatile long frobme[2]; 100*57718be8SEnji Cooper int stat; 101*57718be8SEnji Cooper 102*57718be8SEnji Cooper allocstack = mmap(NULL, STACKSIZE, PROT_READ|PROT_WRITE|PROT_EXEC, 103*57718be8SEnji Cooper MAP_PRIVATE|MAP_ANON, -1, (off_t) 0); 104*57718be8SEnji Cooper 105*57718be8SEnji Cooper ATF_REQUIRE_ERRNO(errno, allocstack != MAP_FAILED); 106*57718be8SEnji Cooper 107*57718be8SEnji Cooper stack = allocstack; 108*57718be8SEnji Cooper #ifndef __MACHINE_STACK_GROWS_UP 109*57718be8SEnji Cooper stack = (char *)stack + STACKSIZE; 110*57718be8SEnji Cooper #endif 111*57718be8SEnji Cooper 112*57718be8SEnji Cooper printf("parent: stack = %p, frobme = %p\n", stack, frobme); 113*57718be8SEnji Cooper fflush(stdout); 114*57718be8SEnji Cooper 115*57718be8SEnji Cooper frobme[0] = (long)getpid(); 116*57718be8SEnji Cooper frobme[1] = (long)stack; 117*57718be8SEnji Cooper 118*57718be8SEnji Cooper sigemptyset(&mask); 119*57718be8SEnji Cooper sigaddset(&mask, SIGUSR1); 120*57718be8SEnji Cooper 121*57718be8SEnji Cooper ATF_REQUIRE_ERRNO(errno, sigprocmask(SIG_BLOCK, &mask, NULL) != -1); 122*57718be8SEnji Cooper 123*57718be8SEnji Cooper switch (pid = __clone(clone_func, stack, 124*57718be8SEnji Cooper CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|SIGUSR1, 125*57718be8SEnji Cooper __UNVOLATILE(frobme))) { 126*57718be8SEnji Cooper case 0: 127*57718be8SEnji Cooper atf_tc_fail("clone() returned 0"); 128*57718be8SEnji Cooper /*NOTREACHED*/ 129*57718be8SEnji Cooper case -1: 130*57718be8SEnji Cooper atf_tc_fail("clone() failed: %s", strerror(errno)); 131*57718be8SEnji Cooper /*NOTREACHED*/ 132*57718be8SEnji Cooper default: 133*57718be8SEnji Cooper while (waitpid(pid, &stat, __WCLONE) != pid) 134*57718be8SEnji Cooper continue; 135*57718be8SEnji Cooper } 136*57718be8SEnji Cooper 137*57718be8SEnji Cooper ATF_REQUIRE_MSG(WIFEXITED(stat) != 0, "child didn't exit"); 138*57718be8SEnji Cooper 139*57718be8SEnji Cooper printf("parent: childexit = 0x%x, frobme = %ld\n", 140*57718be8SEnji Cooper WEXITSTATUS(stat), frobme[1]); 141*57718be8SEnji Cooper 142*57718be8SEnji Cooper switch (WEXITSTATUS(stat)) { 143*57718be8SEnji Cooper case CHILDEXIT: 144*57718be8SEnji Cooper ATF_REQUIRE_EQ(frobme[1], FROBVAL); 145*57718be8SEnji Cooper break; 146*57718be8SEnji Cooper case 1: 147*57718be8SEnji Cooper atf_tc_fail("child: argument does not contain parent's pid"); 148*57718be8SEnji Cooper /*NOTREACHED*/ 149*57718be8SEnji Cooper case 2: 150*57718be8SEnji Cooper atf_tc_fail("child: called in parent's pid"); 151*57718be8SEnji Cooper /*NOTREACHED*/ 152*57718be8SEnji Cooper case 3: 153*57718be8SEnji Cooper atf_tc_fail("child: called with bad stack"); 154*57718be8SEnji Cooper /*NOTREACHED*/ 155*57718be8SEnji Cooper default: 156*57718be8SEnji Cooper atf_tc_fail("child returned unknown code: %d", 157*57718be8SEnji Cooper WEXITSTATUS(stat)); 158*57718be8SEnji Cooper /*NOTREACHED*/ 159*57718be8SEnji Cooper } 160*57718be8SEnji Cooper 161*57718be8SEnji Cooper ATF_REQUIRE_ERRNO(errno, munmap(allocstack, STACKSIZE) != -1); 162*57718be8SEnji Cooper } 163*57718be8SEnji Cooper 164*57718be8SEnji Cooper ATF_TC(clone_null_stack); 165*57718be8SEnji Cooper ATF_TC_HEAD(clone_null_stack, tc) 166*57718be8SEnji Cooper { 167*57718be8SEnji Cooper 168*57718be8SEnji Cooper atf_tc_set_md_var(tc, "descr", 169*57718be8SEnji Cooper "Checks that clone(2) fails when stack pointer is NULL"); 170*57718be8SEnji Cooper } 171*57718be8SEnji Cooper 172*57718be8SEnji Cooper ATF_TC_BODY(clone_null_stack, tc) 173*57718be8SEnji Cooper { 174*57718be8SEnji Cooper int rv; 175*57718be8SEnji Cooper 176*57718be8SEnji Cooper rv = __clone(dummy, NULL, 177*57718be8SEnji Cooper CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|SIGCHLD, NULL); 178*57718be8SEnji Cooper 179*57718be8SEnji Cooper ATF_REQUIRE_EQ(rv, -1); 180*57718be8SEnji Cooper ATF_REQUIRE_EQ(errno, EINVAL); 181*57718be8SEnji Cooper } 182*57718be8SEnji Cooper 183*57718be8SEnji Cooper ATF_TC(clone_null_func); 184*57718be8SEnji Cooper ATF_TC_HEAD(clone_null_func, tc) 185*57718be8SEnji Cooper { 186*57718be8SEnji Cooper 187*57718be8SEnji Cooper atf_tc_set_md_var(tc, "descr", 188*57718be8SEnji Cooper "Checks that clone(2) fails when function pointer is NULL"); 189*57718be8SEnji Cooper } 190*57718be8SEnji Cooper 191*57718be8SEnji Cooper ATF_TC_BODY(clone_null_func, tc) 192*57718be8SEnji Cooper { 193*57718be8SEnji Cooper void *allocstack, *stack; 194*57718be8SEnji Cooper int rv; 195*57718be8SEnji Cooper 196*57718be8SEnji Cooper allocstack = mmap(NULL, STACKSIZE, PROT_READ|PROT_WRITE|PROT_EXEC, 197*57718be8SEnji Cooper MAP_PRIVATE|MAP_ANON, -1, (off_t) 0); 198*57718be8SEnji Cooper ATF_REQUIRE_ERRNO(errno, allocstack != MAP_FAILED); 199*57718be8SEnji Cooper stack = allocstack; 200*57718be8SEnji Cooper #ifndef __MACHINE_STACK_GROWS_UP 201*57718be8SEnji Cooper stack = (char *)stack + STACKSIZE; 202*57718be8SEnji Cooper #endif 203*57718be8SEnji Cooper 204*57718be8SEnji Cooper errno = 0; 205*57718be8SEnji Cooper rv = __clone(0, stack, 206*57718be8SEnji Cooper CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|SIGCHLD, NULL); 207*57718be8SEnji Cooper 208*57718be8SEnji Cooper ATF_REQUIRE_EQ(rv, -1); 209*57718be8SEnji Cooper ATF_REQUIRE_EQ(errno, EINVAL); 210*57718be8SEnji Cooper 211*57718be8SEnji Cooper ATF_REQUIRE_ERRNO(errno, munmap(allocstack, STACKSIZE) != -1); 212*57718be8SEnji Cooper } 213*57718be8SEnji Cooper 214*57718be8SEnji Cooper ATF_TC(clone_out_of_proc); 215*57718be8SEnji Cooper ATF_TC_HEAD(clone_out_of_proc, tc) 216*57718be8SEnji Cooper { 217*57718be8SEnji Cooper 218*57718be8SEnji Cooper atf_tc_set_md_var(tc, "descr", 219*57718be8SEnji Cooper "Checks that clone(2) fails when running out of processes"); 220*57718be8SEnji Cooper atf_tc_set_md_var(tc, "require.user", "unprivileged"); 221*57718be8SEnji Cooper } 222*57718be8SEnji Cooper 223*57718be8SEnji Cooper ATF_TC_BODY(clone_out_of_proc, tc) 224*57718be8SEnji Cooper { 225*57718be8SEnji Cooper struct rlimit rl; 226*57718be8SEnji Cooper int rv; 227*57718be8SEnji Cooper 228*57718be8SEnji Cooper ATF_REQUIRE_ERRNO(errno, getrlimit(RLIMIT_NPROC, &rl) != -1); 229*57718be8SEnji Cooper 230*57718be8SEnji Cooper rl.rlim_cur = 0; 231*57718be8SEnji Cooper rl.rlim_max = 0; 232*57718be8SEnji Cooper 233*57718be8SEnji Cooper ATF_REQUIRE_ERRNO(errno, setrlimit(RLIMIT_NPROC, &rl) != -1); 234*57718be8SEnji Cooper 235*57718be8SEnji Cooper errno = 0; 236*57718be8SEnji Cooper rv = __clone(dummy, malloc(10240), 237*57718be8SEnji Cooper CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|SIGCHLD, (void *)&rl); 238*57718be8SEnji Cooper 239*57718be8SEnji Cooper ATF_REQUIRE_EQ(rv, -1); 240*57718be8SEnji Cooper ATF_REQUIRE_EQ(errno, EAGAIN); 241*57718be8SEnji Cooper } 242*57718be8SEnji Cooper 243*57718be8SEnji Cooper ATF_TP_ADD_TCS(tp) 244*57718be8SEnji Cooper { 245*57718be8SEnji Cooper 246*57718be8SEnji Cooper ATF_TP_ADD_TC(tp, clone_basic); 247*57718be8SEnji Cooper ATF_TP_ADD_TC(tp, clone_null_stack); 248*57718be8SEnji Cooper ATF_TP_ADD_TC(tp, clone_null_func); 249*57718be8SEnji Cooper ATF_TP_ADD_TC(tp, clone_out_of_proc); 250*57718be8SEnji Cooper 251*57718be8SEnji Cooper return atf_no_error(); 252*57718be8SEnji Cooper } 253