Home | History | Annotate | Line # | Download | only in sys
      1 /*	$NetBSD: t_ptrace_exec_wait.h,v 1.1 2020/05/05 00:23:12 kamil Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2016, 2017, 2018, 2019, 2020 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  * POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 static void
     30 traceme_vfork_exec(bool masked, bool ignored)
     31 {
     32 	const int sigval = SIGTRAP;
     33 	pid_t child, wpid;
     34 #if defined(TWAIT_HAVE_STATUS)
     35 	int status;
     36 #endif
     37 	struct sigaction sa;
     38 	struct ptrace_siginfo info;
     39 	sigset_t intmask;
     40 	struct kinfo_proc2 kp;
     41 	size_t len = sizeof(kp);
     42 
     43 	int name[6];
     44 	const size_t namelen = __arraycount(name);
     45 	ki_sigset_t kp_sigmask;
     46 	ki_sigset_t kp_sigignore;
     47 
     48 	memset(&info, 0, sizeof(info));
     49 
     50 	DPRINTF("Before forking process PID=%d\n", getpid());
     51 	SYSCALL_REQUIRE((child = vfork()) != -1);
     52 	if (child == 0) {
     53 		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
     54 		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
     55 
     56 		if (masked) {
     57 			sigemptyset(&intmask);
     58 			sigaddset(&intmask, sigval);
     59 			sigprocmask(SIG_BLOCK, &intmask, NULL);
     60 		}
     61 
     62 		if (ignored) {
     63 			memset(&sa, 0, sizeof(sa));
     64 			sa.sa_handler = SIG_IGN;
     65 			sigemptyset(&sa.sa_mask);
     66 			FORKEE_ASSERT(sigaction(sigval, &sa, NULL) != -1);
     67 		}
     68 
     69 		DPRINTF("Before calling execve(2) from child\n");
     70 		execlp("/bin/echo", "/bin/echo", NULL);
     71 
     72 		/* NOTREACHED */
     73 		FORKEE_ASSERTX(0 && "Not reached");
     74 	}
     75 	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
     76 
     77 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
     78 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
     79 
     80 	validate_status_stopped(status, sigval);
     81 
     82 	name[0] = CTL_KERN,
     83 	name[1] = KERN_PROC2,
     84 	name[2] = KERN_PROC_PID;
     85 	name[3] = getpid();
     86 	name[4] = sizeof(kp);
     87 	name[5] = 1;
     88 
     89 	ATF_REQUIRE_EQ(sysctl(name, namelen, &kp, &len, NULL, 0), 0);
     90 
     91 	if (masked)
     92 		kp_sigmask = kp.p_sigmask;
     93 
     94 	if (ignored)
     95 		kp_sigignore = kp.p_sigignore;
     96 
     97 	name[3] = getpid();
     98 
     99 	ATF_REQUIRE_EQ(sysctl(name, namelen, &kp, &len, NULL, 0), 0);
    100 
    101 	if (masked) {
    102 		DPRINTF("kp_sigmask="
    103 		    "%#02" PRIx32 "%02" PRIx32 "%02" PRIx32 "%02" PRIx32"\n",
    104 		    kp_sigmask.__bits[0], kp_sigmask.__bits[1],
    105 		    kp_sigmask.__bits[2], kp_sigmask.__bits[3]);
    106 
    107 	        DPRINTF("kp.p_sigmask="
    108 	            "%#02" PRIx32 "%02" PRIx32 "%02" PRIx32 "%02" PRIx32"\n",
    109 	            kp.p_sigmask.__bits[0], kp.p_sigmask.__bits[1],
    110 	            kp.p_sigmask.__bits[2], kp.p_sigmask.__bits[3]);
    111 
    112 		ATF_REQUIRE(!memcmp(&kp_sigmask, &kp.p_sigmask,
    113 		    sizeof(kp_sigmask)));
    114 	}
    115 
    116 	if (ignored) {
    117 		DPRINTF("kp_sigignore="
    118 		    "%#02" PRIx32 "%02" PRIx32 "%02" PRIx32 "%02" PRIx32"\n",
    119 		    kp_sigignore.__bits[0], kp_sigignore.__bits[1],
    120 		    kp_sigignore.__bits[2], kp_sigignore.__bits[3]);
    121 
    122 	        DPRINTF("kp.p_sigignore="
    123 	            "%#02" PRIx32 "%02" PRIx32 "%02" PRIx32 "%02" PRIx32"\n",
    124 	            kp.p_sigignore.__bits[0], kp.p_sigignore.__bits[1],
    125 	            kp.p_sigignore.__bits[2], kp.p_sigignore.__bits[3]);
    126 
    127 		ATF_REQUIRE(!memcmp(&kp_sigignore, &kp.p_sigignore,
    128 		    sizeof(kp_sigignore)));
    129 	}
    130 
    131 	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
    132 	SYSCALL_REQUIRE(
    133 	    ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
    134 
    135 	DPRINTF("Signal traced to lwpid=%d\n", info.psi_lwpid);
    136 	DPRINTF("Signal properties: si_signo=%#x si_code=%#x si_errno=%#x\n",
    137 	    info.psi_siginfo.si_signo, info.psi_siginfo.si_code,
    138 	    info.psi_siginfo.si_errno);
    139 
    140 	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, sigval);
    141 	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, TRAP_EXEC);
    142 
    143 	DPRINTF("Before resuming the child process where it left off and "
    144 	    "without signal to be sent\n");
    145 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
    146 
    147 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
    148 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
    149 
    150 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
    151 	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
    152 }
    153 
    154 #define TRACEME_VFORK_EXEC(test, masked, ignored)			\
    155 ATF_TC(test);								\
    156 ATF_TC_HEAD(test, tc)							\
    157 {									\
    158 	atf_tc_set_md_var(tc, "descr",					\
    159 	    "Verify PT_TRACE_ME followed by exec(3) in a vfork(2)ed "	\
    160 	    "child%s%s", masked ? " with masked signal" : "",		\
    161 	    masked ? " with ignored signal" : "");			\
    162 }									\
    163 									\
    164 ATF_TC_BODY(test, tc)							\
    165 {									\
    166 									\
    167 	traceme_vfork_exec(masked, ignored);				\
    168 }
    169 
    170 TRACEME_VFORK_EXEC(traceme_vfork_exec, false, false)
    171 TRACEME_VFORK_EXEC(traceme_vfork_signalmasked_exec, true, false)
    172 TRACEME_VFORK_EXEC(traceme_vfork_signalignored_exec, false, true)
    173 
    174 /// ----------------------------------------------------------------------------
    175 
    176 static void
    177 traceme_exec(bool masked, bool ignored)
    178 {
    179 	const int sigval = SIGTRAP;
    180 	pid_t child, wpid;
    181 #if defined(TWAIT_HAVE_STATUS)
    182 	int status;
    183 #endif
    184 	struct sigaction sa;
    185 	struct ptrace_siginfo info;
    186 	sigset_t intmask;
    187 	struct kinfo_proc2 kp;
    188 	size_t len = sizeof(kp);
    189 
    190 	int name[6];
    191 	const size_t namelen = __arraycount(name);
    192 	ki_sigset_t kp_sigmask;
    193 	ki_sigset_t kp_sigignore;
    194 
    195 	memset(&info, 0, sizeof(info));
    196 
    197 	DPRINTF("Before forking process PID=%d\n", getpid());
    198 	SYSCALL_REQUIRE((child = fork()) != -1);
    199 	if (child == 0) {
    200 		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
    201 		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
    202 
    203 		if (masked) {
    204 			sigemptyset(&intmask);
    205 			sigaddset(&intmask, sigval);
    206 			sigprocmask(SIG_BLOCK, &intmask, NULL);
    207 		}
    208 
    209 		if (ignored) {
    210 			memset(&sa, 0, sizeof(sa));
    211 			sa.sa_handler = SIG_IGN;
    212 			sigemptyset(&sa.sa_mask);
    213 			FORKEE_ASSERT(sigaction(sigval, &sa, NULL) != -1);
    214 		}
    215 
    216 		DPRINTF("Before calling execve(2) from child\n");
    217 		execlp("/bin/echo", "/bin/echo", NULL);
    218 
    219 		FORKEE_ASSERT(0 && "Not reached");
    220 	}
    221 	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
    222 
    223 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
    224 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
    225 
    226 	validate_status_stopped(status, sigval);
    227 
    228 	name[0] = CTL_KERN,
    229 	name[1] = KERN_PROC2,
    230 	name[2] = KERN_PROC_PID;
    231 	name[3] = getpid();
    232 	name[4] = sizeof(kp);
    233 	name[5] = 1;
    234 
    235 	ATF_REQUIRE_EQ(sysctl(name, namelen, &kp, &len, NULL, 0), 0);
    236 
    237 	if (masked)
    238 		kp_sigmask = kp.p_sigmask;
    239 
    240 	if (ignored)
    241 		kp_sigignore = kp.p_sigignore;
    242 
    243 	name[3] = getpid();
    244 
    245 	ATF_REQUIRE_EQ(sysctl(name, namelen, &kp, &len, NULL, 0), 0);
    246 
    247 	if (masked) {
    248 		DPRINTF("kp_sigmask="
    249 		    "%#02" PRIx32 "%02" PRIx32 "%02" PRIx32 "%02" PRIx32"\n",
    250 		    kp_sigmask.__bits[0], kp_sigmask.__bits[1],
    251 		    kp_sigmask.__bits[2], kp_sigmask.__bits[3]);
    252 
    253 		DPRINTF("kp.p_sigmask="
    254 		    "%#02" PRIx32 "%02" PRIx32 "%02" PRIx32 "%02" PRIx32"\n",
    255 		    kp.p_sigmask.__bits[0], kp.p_sigmask.__bits[1],
    256 		    kp.p_sigmask.__bits[2], kp.p_sigmask.__bits[3]);
    257 
    258 		ATF_REQUIRE(!memcmp(&kp_sigmask, &kp.p_sigmask,
    259 		    sizeof(kp_sigmask)));
    260 	}
    261 
    262 	if (ignored) {
    263 		DPRINTF("kp_sigignore="
    264 		    "%#02" PRIx32 "%02" PRIx32 "%02" PRIx32 "%02" PRIx32"\n",
    265 		    kp_sigignore.__bits[0], kp_sigignore.__bits[1],
    266 		    kp_sigignore.__bits[2], kp_sigignore.__bits[3]);
    267 
    268 		DPRINTF("kp.p_sigignore="
    269 		    "%#02" PRIx32 "%02" PRIx32 "%02" PRIx32 "%02" PRIx32"\n",
    270 		    kp.p_sigignore.__bits[0], kp.p_sigignore.__bits[1],
    271 		    kp.p_sigignore.__bits[2], kp.p_sigignore.__bits[3]);
    272 
    273 		ATF_REQUIRE(!memcmp(&kp_sigignore, &kp.p_sigignore,
    274 		    sizeof(kp_sigignore)));
    275 	}
    276 
    277 	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
    278 	SYSCALL_REQUIRE(
    279 	    ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
    280 
    281 	DPRINTF("Signal traced to lwpid=%d\n", info.psi_lwpid);
    282 	DPRINTF("Signal properties: si_signo=%#x si_code=%#x si_errno=%#x\n",
    283 	    info.psi_siginfo.si_signo, info.psi_siginfo.si_code,
    284 	    info.psi_siginfo.si_errno);
    285 
    286 	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, sigval);
    287 	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, TRAP_EXEC);
    288 
    289 	DPRINTF("Before resuming the child process where it left off and "
    290 	    "without signal to be sent\n");
    291 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
    292 
    293 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
    294 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
    295 
    296 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
    297 	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
    298 }
    299 
    300 #define TRACEME_EXEC(test, masked, ignored)				\
    301 ATF_TC(test);								\
    302 ATF_TC_HEAD(test, tc)							\
    303 {									\
    304        atf_tc_set_md_var(tc, "descr",					\
    305            "Detect SIGTRAP TRAP_EXEC from "				\
    306            "child%s%s", masked ? " with masked signal" : "",		\
    307            masked ? " with ignored signal" : "");			\
    308 }									\
    309 									\
    310 ATF_TC_BODY(test, tc)							\
    311 {									\
    312 									\
    313        traceme_exec(masked, ignored);					\
    314 }
    315 
    316 TRACEME_EXEC(traceme_exec, false, false)
    317 TRACEME_EXEC(traceme_signalmasked_exec, true, false)
    318 TRACEME_EXEC(traceme_signalignored_exec, false, true)
    319 
    320 #define ATF_TP_ADD_TCS_PTRACE_WAIT_EXEC() \
    321 	ATF_TP_ADD_TC(tp, traceme_vfork_exec); \
    322 	ATF_TP_ADD_TC(tp, traceme_vfork_signalmasked_exec); \
    323 	ATF_TP_ADD_TC(tp, traceme_vfork_signalignored_exec); \
    324 	ATF_TP_ADD_TC(tp, traceme_exec); \
    325 	ATF_TP_ADD_TC(tp, traceme_signalmasked_exec); \
    326 	ATF_TP_ADD_TC(tp, traceme_signalignored_exec);
    327